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