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