1 /********************************************************************\
2 * BitlBee -- An IRC to other IM-networks gateway *
4 * Copyright 2002-2010 Wilmer van der Gaast and others *
5 \********************************************************************/
7 /* Help file control */
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.
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.
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., 59 Temple Place,
23 Suite 330, Boston, MA 02111-1307 USA
34 help_t
*help_init( help_t
**help
, const char *helpfile
)
42 *help
= h
= g_new0 ( help_t
, 1 );
44 h
->fd
= open( helpfile
, O_RDONLY
53 return( *help
= NULL
);
56 if( fstat( h
->fd
, stat
) != 0 )
59 return( *help
= NULL
);
61 mtime
= stat
->st_mtime
;
63 s
= g_new (char, BUFSIZE
+ 1 );
66 while( ( ( i
= read( h
->fd
, s
+ buflen
, BUFSIZE
- buflen
) ) > 0 ) ||
67 ( i
== 0 && strstr( s
, "\n%\n" ) ) )
70 memset( s
+ buflen
, 0, BUFSIZE
- buflen
);
71 if( !( t
= strstr( s
, "\n%\n" ) ) || s
[0] != '?' )
78 i
= strchr( s
, '\n' ) - s
;
82 h
= h
->next
= g_new0( help_t
, 1 );
84 h
->title
= g_new ( char, i
);
86 strncpy( h
->title
, s
+ 1, i
- 1 );
89 h
->offset
.file_offset
= lseek( h
->fd
, 0, SEEK_CUR
) - buflen
+ i
+ 1;
90 h
->length
= t
- s
- i
- 1;
93 buflen
-= ( t
+ 3 - s
);
94 t
= g_strdup( t
+ 3 );
96 s
= g_renew( char, t
, BUFSIZE
+ 1 );
105 void help_free( help_t
**help
)
108 int last_fd
= -1; /* Weak de-dupe */
110 if( help
== NULL
|| *help
== NULL
)
116 if( h
->fd
!= last_fd
)
129 char *help_get( help_t
**help
, char *title
)
135 for( h
= *help
; h
; h
= h
->next
)
137 if( h
->title
!= NULL
&& g_strcasecmp( h
->title
, title
) == 0 )
140 if( h
&& h
->length
> 0 )
142 char *s
= g_new( char, h
->length
+ 1 );
147 if( fstat( h
->fd
, stat
) != 0 )
152 mtime
= stat
->st_mtime
;
154 if( mtime
> h
->mtime
)
160 if( lseek( h
->fd
, h
->offset
.file_offset
, SEEK_SET
) == -1 ||
161 read( h
->fd
, s
, h
->length
) != h
->length
)
166 strncpy( s
, h
->offset
.mem_offset
, h
->length
);
174 int help_add_mem( help_t
**help
, const char *title
, const char *content
)
176 help_t
*h
, *l
= NULL
;
178 for( h
= *help
; h
; h
= h
->next
)
180 if( g_strcasecmp( h
->title
, title
) == 0 )
187 h
= l
->next
= g_new0( struct help
, 1 );
189 *help
= h
= g_new0( struct help
, 1 );
191 h
->title
= g_strdup( title
);
192 h
->length
= strlen( content
);
193 h
->offset
.mem_offset
= g_strdup( content
);
198 char *help_get_whatsnew( help_t
**help
, int old
)
204 for( h
= *help
; h
; h
= h
->next
)
205 if( h
->title
!= NULL
&& strncmp( h
->title
, "whatsnew", 8 ) == 0 &&
206 sscanf( h
->title
+ 8, "%x", &v
) == 1 && v
> old
)
208 char *s
= help_get( &h
, h
->title
);
210 ret
= g_string_new( s
);
212 g_string_append_printf( ret
, "\n\n%s", s
);
216 return ret
? g_string_free( ret
, FALSE
) : NULL
;