]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd). | |
3 | * cache.c - code for caching files | |
4 | * | |
5 | * Copyright (C) 2003 Lee Hardy <lee@leeh.co.uk> | |
6 | * Copyright (C) 2003-2005 ircd-ratbox development team | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions are | |
10 | * met: | |
11 | * | |
12 | * 1.Redistributions of source code must retain the above copyright notice, | |
13 | * this list of conditions and the following disclaimer. | |
14 | * 2.Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
17 | * 3.The name of the author may not be used to endorse or promote products | |
18 | * derived from this software without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
23 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
24 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
28 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
30 | * POSSIBILITY OF SUCH DAMAGE. | |
31 | * | |
32 | * $Id: cache.c 25119 2008-03-13 16:57:05Z androsyn $ | |
33 | */ | |
34 | ||
35 | #include "stdinc.h" | |
36 | #include "ircd_defs.h" | |
37 | #include "common.h" | |
38 | #include "s_conf.h" | |
39 | #include "client.h" | |
40 | #include "hash.h" | |
41 | #include "cache.h" | |
42 | #include "irc_dictionary.h" | |
43 | #include "numeric.h" | |
44 | #include "send.h" | |
45 | ||
46 | struct cachefile *user_motd = NULL; | |
47 | struct cachefile *oper_motd = NULL; | |
48 | struct cacheline *emptyline = NULL; | |
49 | rb_dlink_list links_cache_list; | |
50 | char user_motd_changed[MAX_DATE_STRING]; | |
51 | ||
52 | struct Dictionary *help_dict_oper = NULL; | |
53 | struct Dictionary *help_dict_user = NULL; | |
54 | ||
55 | /* init_cache() | |
56 | * | |
57 | * inputs - | |
58 | * outputs - | |
59 | * side effects - inits the file/line cache blockheaps, loads motds | |
60 | */ | |
61 | void | |
62 | init_cache(void) | |
63 | { | |
64 | /* allocate the emptyline */ | |
65 | emptyline = rb_malloc(sizeof(struct cacheline)); | |
66 | emptyline->data = rb_strdup(" "); | |
67 | ||
68 | user_motd_changed[0] = '\0'; | |
69 | ||
70 | user_motd = cache_file(MPATH, "ircd.motd", 0); | |
71 | oper_motd = cache_file(OPATH, "opers.motd", 0); | |
72 | memset(&links_cache_list, 0, sizeof(links_cache_list)); | |
73 | ||
74 | help_dict_oper = irc_dictionary_create("oper help", strcasecmp); | |
75 | help_dict_user = irc_dictionary_create("user help", strcasecmp); | |
76 | } | |
77 | ||
78 | /* | |
79 | * removes tabs from src, replaces with 8 spaces, and returns the length | |
80 | * of the new string. if the new string would be greater than destlen, | |
81 | * it is truncated to destlen - 1 | |
82 | */ | |
83 | static size_t | |
84 | untabify(char *dest, const char *src, size_t destlen) | |
85 | { | |
86 | size_t x = 0, i; | |
87 | const char *s = src; | |
88 | char *d = dest; | |
89 | ||
90 | while(*s != '\0' && x < destlen - 1) | |
91 | { | |
92 | if(*s == '\t') | |
93 | { | |
94 | for(i = 0; i < 8 && x < destlen - 1; i++, x++, d++) | |
95 | *d = ' '; | |
96 | s++; | |
97 | } else | |
98 | { | |
99 | *d++ = *s++; | |
100 | x++; | |
101 | } | |
102 | } | |
103 | *d = '\0'; | |
104 | return x; | |
105 | } | |
106 | ||
107 | /* cache_file() | |
108 | * | |
109 | * inputs - file to cache, files "shortname", flags to set | |
110 | * outputs - pointer to file cached, else NULL | |
111 | * side effects - | |
112 | */ | |
113 | struct cachefile * | |
114 | cache_file(const char *filename, const char *shortname, int flags) | |
115 | { | |
116 | FILE *in; | |
117 | struct cachefile *cacheptr; | |
118 | struct cacheline *lineptr; | |
119 | char line[BUFSIZE]; | |
120 | char *p; | |
121 | ||
122 | if((in = fopen(filename, "r")) == NULL) | |
123 | return NULL; | |
124 | ||
125 | ||
126 | cacheptr = rb_malloc(sizeof(struct cachefile)); | |
127 | ||
128 | rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name)); | |
129 | cacheptr->flags = flags; | |
130 | ||
131 | /* cache the file... */ | |
132 | while(fgets(line, sizeof(line), in) != NULL) | |
133 | { | |
134 | if((p = strpbrk(line, "\r\n")) != NULL) | |
135 | *p = '\0'; | |
136 | ||
137 | if(!EmptyString(line)) | |
138 | { | |
139 | char untabline[BUFSIZE]; | |
140 | ||
141 | lineptr = rb_malloc(sizeof(struct cacheline)); | |
142 | ||
143 | untabify(untabline, line, sizeof(untabline)); | |
144 | lineptr->data = rb_strdup(untabline); | |
145 | ||
146 | rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents); | |
147 | } | |
148 | else | |
149 | rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents); | |
150 | } | |
151 | ||
152 | if (0 == rb_dlink_list_length(&cacheptr->contents)) | |
153 | { | |
154 | /* No contents. Don't cache it after all. */ | |
155 | rb_free(cacheptr); | |
156 | cacheptr = NULL; | |
157 | } | |
158 | ||
159 | fclose(in); | |
160 | return cacheptr; | |
161 | } | |
162 | ||
163 | void | |
164 | cache_links(void *unused) | |
165 | { | |
166 | struct Client *target_p; | |
167 | rb_dlink_node *ptr; | |
168 | rb_dlink_node *next_ptr; | |
169 | char *links_line; | |
170 | ||
171 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head) | |
172 | { | |
173 | rb_free(ptr->data); | |
174 | rb_free_rb_dlink_node(ptr); | |
175 | } | |
176 | ||
177 | links_cache_list.head = links_cache_list.tail = NULL; | |
178 | links_cache_list.length = 0; | |
179 | ||
180 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
181 | { | |
182 | target_p = ptr->data; | |
183 | ||
184 | /* skip ourselves (done in /links) and hidden servers */ | |
185 | if(IsMe(target_p) || | |
186 | (IsHidden(target_p) && !ConfigServerHide.disable_hidden)) | |
187 | continue; | |
188 | ||
189 | /* if the below is ever modified, change LINKSLINELEN */ | |
190 | links_line = rb_malloc(LINKSLINELEN); | |
191 | snprintf(links_line, LINKSLINELEN, "%s %s :1 %s", | |
192 | target_p->name, me.name, | |
193 | target_p->info[0] ? target_p->info : | |
194 | "(Unknown Location)"); | |
195 | ||
196 | rb_dlinkAddTailAlloc(links_line, &links_cache_list); | |
197 | } | |
198 | } | |
199 | ||
200 | /* free_cachefile() | |
201 | * | |
202 | * inputs - cachefile to free | |
203 | * outputs - | |
204 | * side effects - cachefile and its data is free'd | |
205 | */ | |
206 | void | |
207 | free_cachefile(struct cachefile *cacheptr) | |
208 | { | |
209 | rb_dlink_node *ptr; | |
210 | rb_dlink_node *next_ptr; | |
211 | ||
212 | if(cacheptr == NULL) | |
213 | return; | |
214 | ||
215 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head) | |
216 | { | |
217 | if(ptr->data != emptyline) | |
218 | { | |
219 | struct cacheline *line = ptr->data; | |
220 | rb_free(line->data); | |
221 | rb_free(line); | |
222 | } | |
223 | } | |
224 | ||
225 | rb_free(cacheptr); | |
226 | } | |
227 | ||
228 | /* load_help() | |
229 | * | |
230 | * inputs - | |
231 | * outputs - | |
232 | * side effects - old help cache deleted | |
233 | * - contents of help directories are loaded. | |
234 | */ | |
235 | void | |
236 | load_help(void) | |
237 | { | |
238 | DIR *helpfile_dir = NULL; | |
239 | struct dirent *ldirent= NULL; | |
240 | char filename[PATH_MAX]; | |
241 | struct cachefile *cacheptr; | |
242 | struct DictionaryIter iter; | |
243 | ||
244 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) | |
245 | struct stat sb; | |
246 | #endif | |
247 | ||
248 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper) | |
249 | { | |
250 | irc_dictionary_delete(help_dict_oper, cacheptr->name); | |
251 | free_cachefile(cacheptr); | |
252 | } | |
253 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user) | |
254 | { | |
255 | irc_dictionary_delete(help_dict_user, cacheptr->name); | |
256 | free_cachefile(cacheptr); | |
257 | } | |
258 | ||
259 | helpfile_dir = opendir(HPATH); | |
260 | ||
261 | if(helpfile_dir == NULL) | |
262 | return; | |
263 | ||
264 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
265 | { | |
266 | if(ldirent->d_name[0] == '.') | |
267 | continue; | |
268 | snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name); | |
269 | cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER); | |
270 | irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr); | |
271 | } | |
272 | ||
273 | closedir(helpfile_dir); | |
274 | helpfile_dir = opendir(UHPATH); | |
275 | ||
276 | if(helpfile_dir == NULL) | |
277 | return; | |
278 | ||
279 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
280 | { | |
281 | if(ldirent->d_name[0] == '.') | |
282 | continue; | |
283 | snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name); | |
284 | ||
285 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) | |
286 | if(lstat(filename, &sb) < 0) | |
287 | continue; | |
288 | ||
289 | /* ok, if its a symlink, we work on the presumption if an | |
290 | * oper help exists of that name, its a symlink to that --fl | |
291 | */ | |
292 | if(S_ISLNK(sb.st_mode)) | |
293 | { | |
294 | cacheptr = irc_dictionary_retrieve(help_dict_oper, ldirent->d_name); | |
295 | ||
296 | if(cacheptr != NULL) | |
297 | { | |
298 | cacheptr->flags |= HELP_USER; | |
299 | continue; | |
300 | } | |
301 | } | |
302 | #endif | |
303 | ||
304 | cacheptr = cache_file(filename, ldirent->d_name, HELP_USER); | |
305 | irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr); | |
306 | } | |
307 | ||
308 | closedir(helpfile_dir); | |
309 | } | |
310 | ||
311 | /* send_user_motd() | |
312 | * | |
313 | * inputs - client to send motd to | |
314 | * outputs - client is sent motd if exists, else ERR_NOMOTD | |
315 | * side effects - | |
316 | */ | |
317 | void | |
318 | send_user_motd(struct Client *source_p) | |
319 | { | |
320 | struct cacheline *lineptr; | |
321 | rb_dlink_node *ptr; | |
322 | const char *myname = get_id(&me, source_p); | |
323 | const char *nick = get_id(source_p, source_p); | |
324 | if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0) | |
325 | { | |
326 | sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick); | |
327 | return; | |
328 | } | |
329 | ||
330 | sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name); | |
331 | ||
332 | RB_DLINK_FOREACH(ptr, user_motd->contents.head) | |
333 | { | |
334 | lineptr = ptr->data; | |
335 | sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data); | |
336 | } | |
337 | ||
338 | sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick); | |
339 | } | |
340 | ||
341 | void | |
342 | cache_user_motd(void) | |
343 | { | |
344 | struct stat sb; | |
345 | struct tm *local_tm; | |
346 | ||
347 | if(stat(MPATH, &sb) == 0) | |
348 | { | |
349 | local_tm = localtime(&sb.st_mtime); | |
350 | ||
351 | if(local_tm != NULL) | |
352 | { | |
353 | snprintf(user_motd_changed, sizeof(user_motd_changed), | |
354 | "%d/%d/%d %d:%d", | |
355 | local_tm->tm_mday, local_tm->tm_mon + 1, | |
356 | 1900 + local_tm->tm_year, local_tm->tm_hour, | |
357 | local_tm->tm_min); | |
358 | } | |
359 | } | |
360 | free_cachefile(user_motd); | |
361 | user_motd = cache_file(MPATH, "ircd.motd", 0); | |
362 | } | |
363 | ||
364 | ||
365 | /* send_oper_motd() | |
366 | * | |
367 | * inputs - client to send motd to | |
368 | * outputs - client is sent oper motd if exists | |
369 | * side effects - | |
370 | */ | |
371 | void | |
372 | send_oper_motd(struct Client *source_p) | |
373 | { | |
374 | struct cacheline *lineptr; | |
375 | rb_dlink_node *ptr; | |
376 | ||
377 | if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0) | |
378 | return; | |
379 | ||
380 | sendto_one(source_p, form_str(RPL_OMOTDSTART), | |
381 | me.name, source_p->name); | |
382 | ||
383 | RB_DLINK_FOREACH(ptr, oper_motd->contents.head) | |
384 | { | |
385 | lineptr = ptr->data; | |
386 | sendto_one(source_p, form_str(RPL_OMOTD), | |
387 | me.name, source_p->name, lineptr->data); | |
388 | } | |
389 | ||
390 | sendto_one(source_p, form_str(RPL_ENDOFOMOTD), | |
391 | me.name, source_p->name); | |
392 | } |