source: help.c @ be1efa3

Last change on this file since be1efa3 was 6f10697, checked in by dequis <dx@…>, at 2015-01-16T19:50:23Z

Fix incorrect Free Software Foundation address

  • Property mode set to 100644
File size: 4.5 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Help file control                                                    */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "help.h"
29#undef read
30#undef write
31
32#define BUFSIZE 1100
33
34help_t *help_init( help_t **help, const char *helpfile )
35{
36        int i, buflen = 0;
37        help_t *h;
38        char *s, *t;
39        time_t mtime;
40        struct stat stat[1];
41       
42        *help = h = g_new0 ( help_t, 1 );
43       
44        h->fd = open( helpfile, O_RDONLY );
45       
46        if( h->fd == -1 )
47        {
48                g_free( h );
49                return( *help = NULL );
50        }
51       
52        if( fstat( h->fd, stat ) != 0 )
53        {
54                g_free( h );
55                return( *help = NULL );
56        }
57        mtime = stat->st_mtime;
58       
59        s = g_new (char, BUFSIZE + 1 );
60        s[BUFSIZE] = 0;
61       
62        while( ( ( i = read( h->fd, s + buflen, BUFSIZE - buflen ) ) > 0 ) ||
63               ( i == 0 && strstr( s, "\n%\n" ) ) )
64        {
65                buflen += i;
66                memset( s + buflen, 0, BUFSIZE - buflen );
67                if( !( t = strstr( s, "\n%\n" ) ) || s[0] != '?' )
68                {
69                        /* FIXME: Clean up */
70                        help_free( help );
71                        g_free( s );
72                        return NULL;
73                }
74                i = strchr( s, '\n' ) - s;
75               
76                if( h->title )
77                {
78                        h = h->next = g_new0( help_t, 1 );
79                }
80                h->title = g_new ( char, i );
81               
82                strncpy( h->title, s + 1, i - 1 );
83                h->title[i-1] = 0;
84                h->fd = (*help)->fd;
85                h->offset.file_offset = lseek( h->fd, 0, SEEK_CUR ) - buflen + i + 1;
86                h->length = t - s - i - 1;
87                h->mtime = mtime;
88               
89                buflen -= ( t + 3 - s );
90                t = g_strdup( t + 3 );
91                g_free( s );
92                s = g_renew( char, t, BUFSIZE + 1 );
93                s[BUFSIZE] = 0;
94        }
95       
96        g_free( s );
97       
98        return( *help );
99}
100
101void help_free( help_t **help )
102{
103        help_t *h, *oh;
104        int last_fd = -1; /* Weak de-dupe */
105       
106        if( help == NULL || *help == NULL )
107                return;
108       
109        h = *help;
110        while( h )
111        {
112                if( h->fd != last_fd )
113                {
114                        close( h->fd );
115                        last_fd = h->fd;
116                }
117                g_free( h->title );
118                h = (oh=h)->next;
119                g_free( oh );
120        }
121       
122        *help = NULL;
123}
124
125char *help_get( help_t **help, char *title )
126{
127        time_t mtime;
128        struct stat stat[1];
129        help_t *h;
130
131        for( h = *help; h; h = h->next )
132        {
133                if( h->title != NULL && g_strcasecmp( h->title, title ) == 0 )
134                        break;
135        }
136        if( h && h->length > 0 )
137        {
138                char *s = g_new( char, h->length + 1 );
139               
140                s[h->length] = 0;
141                if( h->fd >= 0 )
142                {
143                        if( fstat( h->fd, stat ) != 0 )
144                        {
145                                g_free( s );
146                                return NULL;
147                        }
148                        mtime = stat->st_mtime;
149               
150                        if( mtime > h->mtime )
151                        {
152                                g_free( s );
153                                return NULL;
154                        }
155                       
156                        if( lseek( h->fd, h->offset.file_offset, SEEK_SET ) == -1 ||
157                            read( h->fd, s, h->length ) != h->length )
158                                return NULL;
159                }
160                else
161                {
162                        strncpy( s, h->offset.mem_offset, h->length );
163                }
164                return s;
165        }
166       
167        return NULL;
168}
169
170int help_add_mem( help_t **help, const char *title, const char *content )
171{
172        help_t *h, *l = NULL;
173       
174        for( h = *help; h; h = h->next )
175        {
176                if( g_strcasecmp( h->title, title ) == 0 )
177                        return 0;
178               
179                l = h;
180        }
181       
182        if( l )
183                h = l->next = g_new0( struct help, 1 );
184        else
185                *help = h = g_new0( struct help, 1 );
186        h->fd = -1;
187        h->title = g_strdup( title );
188        h->length = strlen( content );
189        h->offset.mem_offset = g_strdup( content );
190       
191        return 1;
192}
193
194char *help_get_whatsnew( help_t **help, int old )
195{
196        GString *ret = NULL;
197        help_t *h;
198        int v;
199       
200        for( h = *help; h; h = h->next )
201                if( h->title != NULL && strncmp( h->title, "whatsnew", 8 ) == 0 &&
202                    sscanf( h->title + 8, "%x", &v ) == 1 && v > old )
203                {
204                        char *s = help_get( &h, h->title );
205                        if( ret == NULL )
206                                ret = g_string_new( s );
207                        else
208                                g_string_append_printf( ret, "\n\n%s", s );
209                        g_free( s );
210                }
211       
212        return ret ? g_string_free( ret, FALSE ) : NULL;
213}
Note: See TracBrowser for help on using the repository browser.