[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[674a01d] | 4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 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., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
[674a01d] | 28 | #include "help.h" |
---|
[b7d3cc34] | 29 | #undef read |
---|
| 30 | #undef write |
---|
| 31 | |
---|
| 32 | #define BUFSIZE 1100 |
---|
| 33 | |
---|
[c227706] | 34 | help_t *help_init( help_t **help, const char *helpfile ) |
---|
[b7d3cc34] | 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 | |
---|
[c227706] | 44 | h->fd = open( helpfile, O_RDONLY |
---|
[b7d3cc34] | 45 | #ifdef _WIN32 |
---|
| 46 | | O_BINARY |
---|
| 47 | #endif |
---|
| 48 | ); |
---|
| 49 | |
---|
| 50 | if( h->fd == -1 ) |
---|
| 51 | { |
---|
| 52 | g_free( h ); |
---|
| 53 | return( *help = NULL ); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | if( fstat( h->fd, stat ) != 0 ) |
---|
| 57 | { |
---|
| 58 | g_free( h ); |
---|
| 59 | return( *help = NULL ); |
---|
| 60 | } |
---|
| 61 | mtime = stat->st_mtime; |
---|
| 62 | |
---|
| 63 | s = g_new (char, BUFSIZE + 1 ); |
---|
| 64 | s[BUFSIZE] = 0; |
---|
| 65 | |
---|
| 66 | while( ( ( i = read( h->fd, s + buflen, BUFSIZE - buflen ) ) > 0 ) || |
---|
| 67 | ( i == 0 && strstr( s, "\n%\n" ) ) ) |
---|
| 68 | { |
---|
| 69 | buflen += i; |
---|
| 70 | memset( s + buflen, 0, BUFSIZE - buflen ); |
---|
| 71 | if( !( t = strstr( s, "\n%\n" ) ) || s[0] != '?' ) |
---|
| 72 | { |
---|
| 73 | /* FIXME: Clean up */ |
---|
[0fbda193] | 74 | help_free( help ); |
---|
[b7d3cc34] | 75 | g_free( s ); |
---|
[0fbda193] | 76 | return NULL; |
---|
[b7d3cc34] | 77 | } |
---|
| 78 | i = strchr( s, '\n' ) - s; |
---|
| 79 | |
---|
[0fbda193] | 80 | if( h->title ) |
---|
[b7d3cc34] | 81 | { |
---|
| 82 | h = h->next = g_new0( help_t, 1 ); |
---|
| 83 | } |
---|
[0fbda193] | 84 | h->title = g_new ( char, i ); |
---|
[b7d3cc34] | 85 | |
---|
[0fbda193] | 86 | strncpy( h->title, s + 1, i - 1 ); |
---|
| 87 | h->title[i-1] = 0; |
---|
[b7d3cc34] | 88 | h->fd = (*help)->fd; |
---|
| 89 | h->offset.file_offset = lseek( h->fd, 0, SEEK_CUR ) - buflen + i + 1; |
---|
| 90 | h->length = t - s - i - 1; |
---|
| 91 | h->mtime = mtime; |
---|
| 92 | |
---|
| 93 | buflen -= ( t + 3 - s ); |
---|
| 94 | t = g_strdup( t + 3 ); |
---|
| 95 | g_free( s ); |
---|
| 96 | s = g_renew( char, t, BUFSIZE + 1 ); |
---|
| 97 | s[BUFSIZE] = 0; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | g_free( s ); |
---|
| 101 | |
---|
| 102 | return( *help ); |
---|
| 103 | } |
---|
| 104 | |
---|
[0fbda193] | 105 | void help_free( help_t **help ) |
---|
| 106 | { |
---|
| 107 | help_t *h, *oh; |
---|
| 108 | int last_fd = -1; /* Weak de-dupe */ |
---|
| 109 | |
---|
| 110 | if( help == NULL || *help == NULL ) |
---|
| 111 | return; |
---|
| 112 | |
---|
| 113 | h = *help; |
---|
| 114 | while( h ) |
---|
| 115 | { |
---|
| 116 | if( h->fd != last_fd ) |
---|
| 117 | { |
---|
| 118 | close( h->fd ); |
---|
| 119 | last_fd = h->fd; |
---|
| 120 | } |
---|
| 121 | g_free( h->title ); |
---|
| 122 | h = (oh=h)->next; |
---|
| 123 | g_free( oh ); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | *help = NULL; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | char *help_get( help_t **help, char *title ) |
---|
[b7d3cc34] | 130 | { |
---|
| 131 | time_t mtime; |
---|
| 132 | struct stat stat[1]; |
---|
| 133 | help_t *h; |
---|
| 134 | |
---|
[c227706] | 135 | for( h = *help; h; h = h->next ) |
---|
[b7d3cc34] | 136 | { |
---|
[0fbda193] | 137 | if( h->title != NULL && g_strcasecmp( h->title, title ) == 0 ) |
---|
[c227706] | 138 | break; |
---|
[b7d3cc34] | 139 | } |
---|
[022e77f] | 140 | if( h && h->length > 0 ) |
---|
[b7d3cc34] | 141 | { |
---|
| 142 | char *s = g_new( char, h->length + 1 ); |
---|
| 143 | |
---|
| 144 | s[h->length] = 0; |
---|
| 145 | if( h->fd >= 0 ) |
---|
| 146 | { |
---|
[0fbda193] | 147 | if( fstat( h->fd, stat ) != 0 ) |
---|
| 148 | { |
---|
| 149 | g_free( s ); |
---|
| 150 | return NULL; |
---|
| 151 | } |
---|
| 152 | mtime = stat->st_mtime; |
---|
| 153 | |
---|
| 154 | if( mtime > h->mtime ) |
---|
| 155 | { |
---|
| 156 | g_free( s ); |
---|
| 157 | return NULL; |
---|
| 158 | } |
---|
| 159 | |
---|
[fef7813] | 160 | if( lseek( h->fd, h->offset.file_offset, SEEK_SET ) == -1 || |
---|
| 161 | read( h->fd, s, h->length ) != h->length ) |
---|
| 162 | return NULL; |
---|
[b7d3cc34] | 163 | } |
---|
| 164 | else |
---|
| 165 | { |
---|
| 166 | strncpy( s, h->offset.mem_offset, h->length ); |
---|
| 167 | } |
---|
[022e77f] | 168 | return s; |
---|
[b7d3cc34] | 169 | } |
---|
| 170 | |
---|
[022e77f] | 171 | return NULL; |
---|
[b7d3cc34] | 172 | } |
---|
[e5d8d21] | 173 | |
---|
| 174 | int help_add_mem( help_t **help, const char *title, const char *content ) |
---|
| 175 | { |
---|
| 176 | help_t *h, *l = NULL; |
---|
| 177 | |
---|
| 178 | for( h = *help; h; h = h->next ) |
---|
| 179 | { |
---|
| 180 | if( g_strcasecmp( h->title, title ) == 0 ) |
---|
| 181 | return 0; |
---|
| 182 | |
---|
| 183 | l = h; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | if( l ) |
---|
| 187 | h = l->next = g_new0( struct help, 1 ); |
---|
| 188 | else |
---|
| 189 | *help = h = g_new0( struct help, 1 ); |
---|
| 190 | h->fd = -1; |
---|
| 191 | h->title = g_strdup( title ); |
---|
| 192 | h->length = strlen( content ); |
---|
| 193 | h->offset.mem_offset = g_strdup( content ); |
---|
| 194 | |
---|
| 195 | return 1; |
---|
| 196 | } |
---|
[674a01d] | 197 | |
---|
| 198 | char *help_get_whatsnew( help_t **help, int old ) |
---|
| 199 | { |
---|
| 200 | GString *ret = NULL; |
---|
| 201 | help_t *h; |
---|
| 202 | int v; |
---|
| 203 | |
---|
| 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 ) |
---|
| 207 | { |
---|
| 208 | char *s = help_get( &h, h->title ); |
---|
| 209 | if( ret == NULL ) |
---|
| 210 | ret = g_string_new( s ); |
---|
| 211 | else |
---|
| 212 | g_string_append_printf( ret, "\n\n%s", s ); |
---|
| 213 | g_free( s ); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | return ret ? g_string_free( ret, FALSE ) : NULL; |
---|
| 217 | } |
---|