]>
Commit | Line | Data |
---|---|---|
ae4091d2 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 | * | |
ae4091d2 JT |
32 | */ |
33 | ||
a83914b3 VY |
34 | #include "stdinc.h" |
35 | #include "ircd_defs.h" | |
36 | #include "common.h" | |
37 | #include "s_conf.h" | |
38 | #include "client.h" | |
39 | #include "hash.h" | |
40 | #include "cache.h" | |
a83914b3 | 41 | #include "irc_dictionary.h" |
ae4091d2 JT |
42 | #include "numeric.h" |
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 | ||
5a34b193 | 50 | struct Dictionary *help_dict_oper = NULL; |
ae4091d2 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)); | |
64 | emptyline->data[0] = ' '; | |
65 | emptyline->data[1] = '\0'; | |
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 | ||
5a34b193 | 72 | help_dict_oper = irc_dictionary_create(strcasecmp); |
ae4091d2 JT |
73 | help_dict_user = irc_dictionary_create(strcasecmp); |
74 | } | |
75 | ||
7dd98666 VY |
76 | /* |
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++; | |
95 | } else | |
96 | { | |
97 | *d++ = *s++; | |
98 | x++; | |
99 | } | |
100 | } | |
101 | *d = '\0'; | |
102 | return x; | |
103 | } | |
104 | ||
ae4091d2 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 | { | |
137 | lineptr = rb_malloc(sizeof(struct cacheline)); | |
c735f930 | 138 | untabify(lineptr->data, line, sizeof(lineptr->data)); |
ae4091d2 JT |
139 | rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents); |
140 | } | |
141 | else | |
142 | rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents); | |
143 | } | |
144 | ||
b4c26701 SB |
145 | if (0 == rb_dlink_list_length(&cacheptr->contents)) |
146 | { | |
147 | /* No contents. Don't cache it after all. */ | |
148 | rb_free(cacheptr); | |
149 | cacheptr = NULL; | |
150 | } | |
151 | ||
ae4091d2 JT |
152 | fclose(in); |
153 | return cacheptr; | |
154 | } | |
155 | ||
156 | void | |
157 | cache_links(void *unused) | |
158 | { | |
159 | struct Client *target_p; | |
160 | rb_dlink_node *ptr; | |
161 | rb_dlink_node *next_ptr; | |
162 | char *links_line; | |
163 | ||
164 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head) | |
165 | { | |
166 | rb_free(ptr->data); | |
167 | rb_free_rb_dlink_node(ptr); | |
168 | } | |
169 | ||
170 | links_cache_list.head = links_cache_list.tail = NULL; | |
171 | links_cache_list.length = 0; | |
172 | ||
173 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
174 | { | |
175 | target_p = ptr->data; | |
176 | ||
177 | /* skip ourselves (done in /links) and hidden servers */ | |
178 | if(IsMe(target_p) || | |
179 | (IsHidden(target_p) && !ConfigServerHide.disable_hidden)) | |
180 | continue; | |
181 | ||
182 | /* if the below is ever modified, change LINKSLINELEN */ | |
183 | links_line = rb_malloc(LINKSLINELEN); | |
184 | rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s", | |
185 | target_p->name, me.name, | |
186 | target_p->info[0] ? target_p->info : | |
187 | "(Unknown Location)"); | |
188 | ||
189 | rb_dlinkAddTailAlloc(links_line, &links_cache_list); | |
190 | } | |
191 | } | |
192 | ||
193 | /* free_cachefile() | |
194 | * | |
195 | * inputs - cachefile to free | |
196 | * outputs - | |
197 | * side effects - cachefile and its data is free'd | |
198 | */ | |
199 | void | |
200 | free_cachefile(struct cachefile *cacheptr) | |
201 | { | |
202 | rb_dlink_node *ptr; | |
203 | rb_dlink_node *next_ptr; | |
204 | ||
205 | if(cacheptr == NULL) | |
206 | return; | |
207 | ||
208 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head) | |
209 | { | |
210 | if(ptr->data != emptyline) | |
211 | rb_free(ptr->data); | |
212 | } | |
213 | ||
214 | rb_free(cacheptr); | |
215 | } | |
216 | ||
212380e3 | 217 | /* load_help() |
218 | * | |
219 | * inputs - | |
220 | * outputs - | |
e8149a2c JT |
221 | * side effects - old help cache deleted |
222 | * - contents of help directories are loaded. | |
212380e3 | 223 | */ |
224 | void | |
225 | load_help(void) | |
226 | { | |
227 | DIR *helpfile_dir = NULL; | |
228 | struct dirent *ldirent= NULL; | |
229 | char filename[MAXPATHLEN]; | |
230 | struct cachefile *cacheptr; | |
e8149a2c | 231 | struct DictionaryIter iter; |
212380e3 | 232 | |
c735f930 VY |
233 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) |
234 | struct stat sb; | |
235 | #endif | |
236 | ||
5a34b193 | 237 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper) |
e8149a2c | 238 | { |
5a34b193 JT |
239 | irc_dictionary_delete(help_dict_oper, cacheptr->name); |
240 | free_cachefile(cacheptr); | |
241 | } | |
242 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user) | |
243 | { | |
244 | irc_dictionary_delete(help_dict_user, cacheptr->name); | |
e8149a2c JT |
245 | free_cachefile(cacheptr); |
246 | } | |
247 | ||
212380e3 | 248 | helpfile_dir = opendir(HPATH); |
249 | ||
250 | if(helpfile_dir == NULL) | |
251 | return; | |
252 | ||
253 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
254 | { | |
7748deb5 JT |
255 | if(ldirent->d_name[0] == '.') |
256 | continue; | |
38e6acdd | 257 | rb_snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name); |
212380e3 | 258 | cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER); |
5a34b193 | 259 | irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr); |
212380e3 | 260 | } |
261 | ||
262 | closedir(helpfile_dir); | |
263 | helpfile_dir = opendir(UHPATH); | |
264 | ||
265 | if(helpfile_dir == NULL) | |
266 | return; | |
267 | ||
268 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
269 | { | |
7748deb5 JT |
270 | if(ldirent->d_name[0] == '.') |
271 | continue; | |
38e6acdd | 272 | rb_snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name); |
212380e3 | 273 | |
c735f930 VY |
274 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) |
275 | if(lstat(filename, &sb) < 0) | |
276 | continue; | |
277 | ||
278 | /* ok, if its a symlink, we work on the presumption if an | |
279 | * oper help exists of that name, its a symlink to that --fl | |
280 | */ | |
281 | if(S_ISLNK(sb.st_mode)) | |
282 | { | |
283 | cacheptr = irc_dictionary_retrieve(help_dict_oper, ldirent->d_name); | |
284 | ||
285 | if(cacheptr != NULL) | |
286 | { | |
287 | cacheptr->flags |= HELP_USER; | |
288 | continue; | |
289 | } | |
290 | } | |
291 | #endif | |
292 | ||
212380e3 | 293 | cacheptr = cache_file(filename, ldirent->d_name, HELP_USER); |
5a34b193 | 294 | irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr); |
212380e3 | 295 | } |
296 | ||
297 | closedir(helpfile_dir); | |
ae4091d2 JT |
298 | } |
299 | ||
300 | /* send_user_motd() | |
301 | * | |
302 | * inputs - client to send motd to | |
303 | * outputs - client is sent motd if exists, else ERR_NOMOTD | |
304 | * side effects - | |
305 | */ | |
306 | void | |
307 | send_user_motd(struct Client *source_p) | |
308 | { | |
309 | struct cacheline *lineptr; | |
310 | rb_dlink_node *ptr; | |
311 | const char *myname = get_id(&me, source_p); | |
312 | const char *nick = get_id(source_p, source_p); | |
313 | if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0) | |
314 | { | |
315 | sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick); | |
316 | return; | |
317 | } | |
318 | ||
319 | sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name); | |
320 | ||
321 | RB_DLINK_FOREACH(ptr, user_motd->contents.head) | |
322 | { | |
323 | lineptr = ptr->data; | |
324 | sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data); | |
325 | } | |
326 | ||
327 | sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick); | |
328 | } | |
329 | ||
330 | void | |
331 | cache_user_motd(void) | |
332 | { | |
333 | struct stat sb; | |
334 | struct tm *local_tm; | |
335 | ||
336 | if(stat(MPATH, &sb) == 0) | |
337 | { | |
338 | local_tm = localtime(&sb.st_mtime); | |
339 | ||
340 | if(local_tm != NULL) | |
341 | { | |
342 | rb_snprintf(user_motd_changed, sizeof(user_motd_changed), | |
343 | "%d/%d/%d %d:%d", | |
344 | local_tm->tm_mday, local_tm->tm_mon + 1, | |
345 | 1900 + local_tm->tm_year, local_tm->tm_hour, | |
346 | local_tm->tm_min); | |
347 | } | |
348 | } | |
349 | free_cachefile(user_motd); | |
350 | user_motd = cache_file(MPATH, "ircd.motd", 0); | |
351 | } | |
de0ae50d JT |
352 | |
353 | ||
354 | /* send_oper_motd() | |
355 | * | |
356 | * inputs - client to send motd to | |
357 | * outputs - client is sent oper motd if exists | |
358 | * side effects - | |
359 | */ | |
360 | void | |
361 | send_oper_motd(struct Client *source_p) | |
362 | { | |
363 | struct cacheline *lineptr; | |
364 | rb_dlink_node *ptr; | |
365 | ||
366 | if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0) | |
367 | return; | |
368 | ||
369 | sendto_one(source_p, form_str(RPL_OMOTDSTART), | |
370 | me.name, source_p->name); | |
371 | ||
372 | RB_DLINK_FOREACH(ptr, oper_motd->contents.head) | |
373 | { | |
374 | lineptr = ptr->data; | |
375 | sendto_one(source_p, form_str(RPL_OMOTD), | |
376 | me.name, source_p->name, lineptr->data); | |
377 | } | |
378 | ||
379 | sendto_one(source_p, form_str(RPL_ENDOFOMOTD), | |
380 | me.name, source_p->name); | |
381 | } |