Add support for using bitlbee-like help files.
[jelmer/ctrlproxy.git] / src / help.c
1 /*
2         ctrlproxy: A modular IRC proxy
3         help: module for access the help system
4
5         (c) 2006 Jelmer Vernooij <jelmer@nl.linux.org>
6         (c) 2004 Wilmer van der Gaast <wilmer@gaast.net>
7
8         This program is free software; you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation; either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program; if not, write to the Free Software
20         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "internals.h"
24 #include "help.h"
25 #include "log.h"
26
27 #define BUFSIZE 1100
28
29 void help_free(help_t *h)
30 {
31         g_mapped_file_free(h->file);
32         g_hash_table_destroy(h->entries);
33         g_free(h);
34 }
35
36 help_t *help_init( const char *helpfile )
37 {
38         help_t *h;
39         char *data;
40         GError *error = NULL;
41         gsize len, i, k;
42         char *p;
43         
44         h = g_new0 (help_t, 1);
45         
46         h->file = g_mapped_file_new(helpfile, TRUE, &error);
47         if (h->file != NULL) {
48                 len = g_mapped_file_get_length(h->file);
49                 data = g_mapped_file_get_contents(h->file);
50         }
51         
52         if( h->file == NULL ) {
53                 log_global(LOG_WARNING, "Unable to open help file `%s': %s", helpfile, 
54                                   error->message);
55                 g_free( h );
56                 return NULL;
57         }
58
59         h->entries = g_hash_table_new(g_str_hash, g_str_equal);
60         i = 0;
61         while (i < len) {
62                 if (data[i] == '?') {
63                         /* Key starts here */
64                         k = i+1;
65                         p = g_strstr_len(data+k, len-k, "\n%\n");
66                         if (p == NULL) {
67                                 log_global(LOG_WARNING, "Error parsing help file");
68                                 goto error;
69                         }
70                         p[1] = 0;
71                         i+=strlen(data+i+3);
72                         p = g_strstr_len(data+k, len-k, "\n");
73                         p[0] = 0;
74                         g_hash_table_insert(h->entries, data+k, p+1);
75                 } else {
76                         log_global(LOG_WARNING, "Unknown character '%c' in help file", 
77                                            data[i]);
78                         i++;
79                 }
80         }
81
82         return h;
83
84 error:
85         g_mapped_file_free(h->file);
86         g_hash_table_destroy(h->entries);
87         g_free(h);
88         return NULL;
89 }
90
91 const char *help_get(help_t *h, const char *string)
92 {
93         return g_hash_table_lookup(h->entries, string);
94 }