]>
Commit | Line | Data |
---|---|---|
1087485c JT |
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 | ||
91e2f81c VY |
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" | |
91e2f81c | 42 | #include "irc_dictionary.h" |
1087485c JT |
43 | #include "numeric.h" |
44 | ||
45 | struct cachefile *user_motd = NULL; | |
46 | struct cachefile *oper_motd = NULL; | |
47 | struct cacheline *emptyline = NULL; | |
48 | rb_dlink_list links_cache_list; | |
49 | char user_motd_changed[MAX_DATE_STRING]; | |
50 | ||
85550587 | 51 | struct Dictionary *help_dict_oper = NULL; |
1087485c JT |
52 | struct Dictionary *help_dict_user = NULL; |
53 | ||
54 | /* init_cache() | |
55 | * | |
56 | * inputs - | |
57 | * outputs - | |
58 | * side effects - inits the file/line cache blockheaps, loads motds | |
59 | */ | |
60 | void | |
61 | init_cache(void) | |
62 | { | |
63 | /* allocate the emptyline */ | |
64 | emptyline = rb_malloc(sizeof(struct cacheline)); | |
65 | emptyline->data[0] = ' '; | |
66 | emptyline->data[1] = '\0'; | |
67 | user_motd_changed[0] = '\0'; | |
68 | ||
69 | user_motd = cache_file(MPATH, "ircd.motd", 0); | |
70 | oper_motd = cache_file(OPATH, "opers.motd", 0); | |
71 | memset(&links_cache_list, 0, sizeof(links_cache_list)); | |
72 | ||
85550587 | 73 | help_dict_oper = irc_dictionary_create(strcasecmp); |
1087485c JT |
74 | help_dict_user = irc_dictionary_create(strcasecmp); |
75 | } | |
76 | ||
22b98b1e VY |
77 | /* |
78 | * removes tabs from src, replaces with 8 spaces, and returns the length | |
79 | * of the new string. if the new string would be greater than destlen, | |
80 | * it is truncated to destlen - 1 | |
81 | */ | |
82 | static size_t | |
83 | untabify(char *dest, const char *src, size_t destlen) | |
84 | { | |
85 | size_t x = 0, i; | |
86 | const char *s = src; | |
87 | char *d = dest; | |
88 | ||
89 | while(*s != '\0' && x < destlen - 1) | |
90 | { | |
91 | if(*s == '\t') | |
92 | { | |
93 | for(i = 0; i < 8 && x < destlen - 1; i++, x++, d++) | |
94 | *d = ' '; | |
95 | s++; | |
96 | } else | |
97 | { | |
98 | *d++ = *s++; | |
99 | x++; | |
100 | } | |
101 | } | |
102 | *d = '\0'; | |
103 | return x; | |
104 | } | |
105 | ||
1087485c JT |
106 | /* cache_file() |
107 | * | |
108 | * inputs - file to cache, files "shortname", flags to set | |
109 | * outputs - pointer to file cached, else NULL | |
110 | * side effects - | |
111 | */ | |
112 | struct cachefile * | |
113 | cache_file(const char *filename, const char *shortname, int flags) | |
114 | { | |
115 | FILE *in; | |
116 | struct cachefile *cacheptr; | |
117 | struct cacheline *lineptr; | |
118 | char line[BUFSIZE]; | |
119 | char *p; | |
120 | ||
121 | if((in = fopen(filename, "r")) == NULL) | |
122 | return NULL; | |
123 | ||
124 | ||
125 | cacheptr = rb_malloc(sizeof(struct cachefile)); | |
126 | ||
127 | rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name)); | |
128 | cacheptr->flags = flags; | |
129 | ||
130 | /* cache the file... */ | |
131 | while(fgets(line, sizeof(line), in) != NULL) | |
132 | { | |
133 | if((p = strpbrk(line, "\r\n")) != NULL) | |
134 | *p = '\0'; | |
135 | ||
136 | if(!EmptyString(line)) | |
137 | { | |
138 | lineptr = rb_malloc(sizeof(struct cacheline)); | |
4231cedc | 139 | untabify(lineptr->data, line, sizeof(lineptr->data)); |
1087485c JT |
140 | rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents); |
141 | } | |
142 | else | |
143 | rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents); | |
144 | } | |
145 | ||
d06f3da9 SB |
146 | if (0 == rb_dlink_list_length(&cacheptr->contents)) |
147 | { | |
148 | /* No contents. Don't cache it after all. */ | |
149 | rb_free(cacheptr); | |
150 | cacheptr = NULL; | |
151 | } | |
152 | ||
1087485c JT |
153 | fclose(in); |
154 | return cacheptr; | |
155 | } | |
156 | ||
157 | void | |
158 | cache_links(void *unused) | |
159 | { | |
160 | struct Client *target_p; | |
161 | rb_dlink_node *ptr; | |
162 | rb_dlink_node *next_ptr; | |
163 | char *links_line; | |
164 | ||
165 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head) | |
166 | { | |
167 | rb_free(ptr->data); | |
168 | rb_free_rb_dlink_node(ptr); | |
169 | } | |
170 | ||
171 | links_cache_list.head = links_cache_list.tail = NULL; | |
172 | links_cache_list.length = 0; | |
173 | ||
174 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
175 | { | |
176 | target_p = ptr->data; | |
177 | ||
178 | /* skip ourselves (done in /links) and hidden servers */ | |
179 | if(IsMe(target_p) || | |
180 | (IsHidden(target_p) && !ConfigServerHide.disable_hidden)) | |
181 | continue; | |
182 | ||
183 | /* if the below is ever modified, change LINKSLINELEN */ | |
184 | links_line = rb_malloc(LINKSLINELEN); | |
185 | rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s", | |
186 | target_p->name, me.name, | |
187 | target_p->info[0] ? target_p->info : | |
188 | "(Unknown Location)"); | |
189 | ||
190 | rb_dlinkAddTailAlloc(links_line, &links_cache_list); | |
191 | } | |
192 | } | |
193 | ||
194 | /* free_cachefile() | |
195 | * | |
196 | * inputs - cachefile to free | |
197 | * outputs - | |
198 | * side effects - cachefile and its data is free'd | |
199 | */ | |
200 | void | |
201 | free_cachefile(struct cachefile *cacheptr) | |
202 | { | |
203 | rb_dlink_node *ptr; | |
204 | rb_dlink_node *next_ptr; | |
205 | ||
206 | if(cacheptr == NULL) | |
207 | return; | |
208 | ||
209 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head) | |
210 | { | |
211 | if(ptr->data != emptyline) | |
212 | rb_free(ptr->data); | |
213 | } | |
214 | ||
215 | rb_free(cacheptr); | |
216 | } | |
217 | ||
212380e3 AC |
218 | /* load_help() |
219 | * | |
220 | * inputs - | |
221 | * outputs - | |
e8149a2c JT |
222 | * side effects - old help cache deleted |
223 | * - contents of help directories are loaded. | |
212380e3 AC |
224 | */ |
225 | void | |
226 | load_help(void) | |
227 | { | |
228 | DIR *helpfile_dir = NULL; | |
229 | struct dirent *ldirent= NULL; | |
d74fa5b5 | 230 | char filename[PATH_MAX]; |
212380e3 | 231 | struct cachefile *cacheptr; |
e8149a2c | 232 | struct DictionaryIter iter; |
212380e3 | 233 | |
4231cedc VY |
234 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) |
235 | struct stat sb; | |
236 | #endif | |
237 | ||
85550587 | 238 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper) |
e8149a2c | 239 | { |
85550587 JT |
240 | irc_dictionary_delete(help_dict_oper, cacheptr->name); |
241 | free_cachefile(cacheptr); | |
242 | } | |
243 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user) | |
244 | { | |
245 | irc_dictionary_delete(help_dict_user, cacheptr->name); | |
e8149a2c JT |
246 | free_cachefile(cacheptr); |
247 | } | |
248 | ||
212380e3 AC |
249 | helpfile_dir = opendir(HPATH); |
250 | ||
251 | if(helpfile_dir == NULL) | |
252 | return; | |
253 | ||
254 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
255 | { | |
cd5d9abf JT |
256 | if(ldirent->d_name[0] == '.') |
257 | continue; | |
b2f0da88 | 258 | rb_snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name); |
212380e3 | 259 | cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER); |
85550587 | 260 | irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr); |
212380e3 AC |
261 | } |
262 | ||
263 | closedir(helpfile_dir); | |
264 | helpfile_dir = opendir(UHPATH); | |
265 | ||
266 | if(helpfile_dir == NULL) | |
267 | return; | |
268 | ||
269 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
270 | { | |
cd5d9abf JT |
271 | if(ldirent->d_name[0] == '.') |
272 | continue; | |
b2f0da88 | 273 | rb_snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name); |
212380e3 | 274 | |
4231cedc VY |
275 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) |
276 | if(lstat(filename, &sb) < 0) | |
277 | continue; | |
278 | ||
279 | /* ok, if its a symlink, we work on the presumption if an | |
280 | * oper help exists of that name, its a symlink to that --fl | |
281 | */ | |
282 | if(S_ISLNK(sb.st_mode)) | |
283 | { | |
284 | cacheptr = irc_dictionary_retrieve(help_dict_oper, ldirent->d_name); | |
285 | ||
286 | if(cacheptr != NULL) | |
287 | { | |
288 | cacheptr->flags |= HELP_USER; | |
289 | continue; | |
290 | } | |
291 | } | |
292 | #endif | |
293 | ||
212380e3 | 294 | cacheptr = cache_file(filename, ldirent->d_name, HELP_USER); |
85550587 | 295 | irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr); |
212380e3 AC |
296 | } |
297 | ||
298 | closedir(helpfile_dir); | |
1087485c JT |
299 | } |
300 | ||
301 | /* send_user_motd() | |
302 | * | |
303 | * inputs - client to send motd to | |
304 | * outputs - client is sent motd if exists, else ERR_NOMOTD | |
305 | * side effects - | |
306 | */ | |
307 | void | |
308 | send_user_motd(struct Client *source_p) | |
309 | { | |
310 | struct cacheline *lineptr; | |
311 | rb_dlink_node *ptr; | |
312 | const char *myname = get_id(&me, source_p); | |
313 | const char *nick = get_id(source_p, source_p); | |
314 | if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0) | |
315 | { | |
316 | sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick); | |
317 | return; | |
318 | } | |
319 | ||
320 | sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name); | |
321 | ||
322 | RB_DLINK_FOREACH(ptr, user_motd->contents.head) | |
323 | { | |
324 | lineptr = ptr->data; | |
325 | sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data); | |
326 | } | |
327 | ||
328 | sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick); | |
329 | } | |
330 | ||
331 | void | |
332 | cache_user_motd(void) | |
333 | { | |
334 | struct stat sb; | |
335 | struct tm *local_tm; | |
336 | ||
337 | if(stat(MPATH, &sb) == 0) | |
338 | { | |
339 | local_tm = localtime(&sb.st_mtime); | |
340 | ||
341 | if(local_tm != NULL) | |
342 | { | |
343 | rb_snprintf(user_motd_changed, sizeof(user_motd_changed), | |
344 | "%d/%d/%d %d:%d", | |
345 | local_tm->tm_mday, local_tm->tm_mon + 1, | |
346 | 1900 + local_tm->tm_year, local_tm->tm_hour, | |
347 | local_tm->tm_min); | |
348 | } | |
349 | } | |
350 | free_cachefile(user_motd); | |
351 | user_motd = cache_file(MPATH, "ircd.motd", 0); | |
352 | } | |
fe18f4bc JT |
353 | |
354 | ||
355 | /* send_oper_motd() | |
356 | * | |
357 | * inputs - client to send motd to | |
358 | * outputs - client is sent oper motd if exists | |
359 | * side effects - | |
360 | */ | |
361 | void | |
362 | send_oper_motd(struct Client *source_p) | |
363 | { | |
364 | struct cacheline *lineptr; | |
365 | rb_dlink_node *ptr; | |
366 | ||
367 | if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0) | |
368 | return; | |
369 | ||
370 | sendto_one(source_p, form_str(RPL_OMOTDSTART), | |
371 | me.name, source_p->name); | |
372 | ||
373 | RB_DLINK_FOREACH(ptr, oper_motd->contents.head) | |
374 | { | |
375 | lineptr = ptr->data; | |
376 | sendto_one(source_p, form_str(RPL_OMOTD), | |
377 | me.name, source_p->name, lineptr->data); | |
378 | } | |
379 | ||
380 | sendto_one(source_p, form_str(RPL_ENDOFOMOTD), | |
381 | me.name, source_p->name); | |
382 | } |