]>
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 | ||
77 | /* cache_file() | |
78 | * | |
79 | * inputs - file to cache, files "shortname", flags to set | |
80 | * outputs - pointer to file cached, else NULL | |
81 | * side effects - | |
82 | */ | |
83 | struct cachefile * | |
84 | cache_file(const char *filename, const char *shortname, int flags) | |
85 | { | |
86 | FILE *in; | |
87 | struct cachefile *cacheptr; | |
88 | struct cacheline *lineptr; | |
89 | char line[BUFSIZE]; | |
90 | char *p; | |
91 | ||
92 | if((in = fopen(filename, "r")) == NULL) | |
93 | return NULL; | |
94 | ||
95 | ||
96 | cacheptr = rb_malloc(sizeof(struct cachefile)); | |
97 | ||
98 | rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name)); | |
99 | cacheptr->flags = flags; | |
100 | ||
101 | /* cache the file... */ | |
102 | while(fgets(line, sizeof(line), in) != NULL) | |
103 | { | |
104 | if((p = strpbrk(line, "\r\n")) != NULL) | |
105 | *p = '\0'; | |
106 | ||
107 | if(!EmptyString(line)) | |
108 | { | |
109 | lineptr = rb_malloc(sizeof(struct cacheline)); | |
110 | rb_strlcpy(lineptr->data, line, sizeof(lineptr->data)); | |
111 | rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents); | |
112 | } | |
113 | else | |
114 | rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents); | |
115 | } | |
116 | ||
117 | fclose(in); | |
118 | return cacheptr; | |
119 | } | |
120 | ||
121 | void | |
122 | cache_links(void *unused) | |
123 | { | |
124 | struct Client *target_p; | |
125 | rb_dlink_node *ptr; | |
126 | rb_dlink_node *next_ptr; | |
127 | char *links_line; | |
128 | ||
129 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head) | |
130 | { | |
131 | rb_free(ptr->data); | |
132 | rb_free_rb_dlink_node(ptr); | |
133 | } | |
134 | ||
135 | links_cache_list.head = links_cache_list.tail = NULL; | |
136 | links_cache_list.length = 0; | |
137 | ||
138 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
139 | { | |
140 | target_p = ptr->data; | |
141 | ||
142 | /* skip ourselves (done in /links) and hidden servers */ | |
143 | if(IsMe(target_p) || | |
144 | (IsHidden(target_p) && !ConfigServerHide.disable_hidden)) | |
145 | continue; | |
146 | ||
147 | /* if the below is ever modified, change LINKSLINELEN */ | |
148 | links_line = rb_malloc(LINKSLINELEN); | |
149 | rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s", | |
150 | target_p->name, me.name, | |
151 | target_p->info[0] ? target_p->info : | |
152 | "(Unknown Location)"); | |
153 | ||
154 | rb_dlinkAddTailAlloc(links_line, &links_cache_list); | |
155 | } | |
156 | } | |
157 | ||
158 | /* free_cachefile() | |
159 | * | |
160 | * inputs - cachefile to free | |
161 | * outputs - | |
162 | * side effects - cachefile and its data is free'd | |
163 | */ | |
164 | void | |
165 | free_cachefile(struct cachefile *cacheptr) | |
166 | { | |
167 | rb_dlink_node *ptr; | |
168 | rb_dlink_node *next_ptr; | |
169 | ||
170 | if(cacheptr == NULL) | |
171 | return; | |
172 | ||
173 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head) | |
174 | { | |
175 | if(ptr->data != emptyline) | |
176 | rb_free(ptr->data); | |
177 | } | |
178 | ||
179 | rb_free(cacheptr); | |
180 | } | |
181 | ||
212380e3 AC |
182 | /* load_help() |
183 | * | |
184 | * inputs - | |
185 | * outputs - | |
e8149a2c JT |
186 | * side effects - old help cache deleted |
187 | * - contents of help directories are loaded. | |
212380e3 AC |
188 | */ |
189 | void | |
190 | load_help(void) | |
191 | { | |
192 | DIR *helpfile_dir = NULL; | |
193 | struct dirent *ldirent= NULL; | |
194 | char filename[MAXPATHLEN]; | |
195 | struct cachefile *cacheptr; | |
e8149a2c | 196 | struct DictionaryIter iter; |
212380e3 | 197 | |
85550587 | 198 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper) |
e8149a2c | 199 | { |
85550587 JT |
200 | irc_dictionary_delete(help_dict_oper, cacheptr->name); |
201 | free_cachefile(cacheptr); | |
202 | } | |
203 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user) | |
204 | { | |
205 | irc_dictionary_delete(help_dict_user, cacheptr->name); | |
e8149a2c JT |
206 | free_cachefile(cacheptr); |
207 | } | |
208 | ||
212380e3 AC |
209 | helpfile_dir = opendir(HPATH); |
210 | ||
211 | if(helpfile_dir == NULL) | |
212 | return; | |
213 | ||
214 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
215 | { | |
b2f0da88 | 216 | rb_snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name); |
212380e3 | 217 | cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER); |
85550587 | 218 | irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr); |
212380e3 AC |
219 | } |
220 | ||
221 | closedir(helpfile_dir); | |
222 | helpfile_dir = opendir(UHPATH); | |
223 | ||
224 | if(helpfile_dir == NULL) | |
225 | return; | |
226 | ||
227 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
228 | { | |
b2f0da88 | 229 | rb_snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name); |
212380e3 | 230 | |
212380e3 | 231 | cacheptr = cache_file(filename, ldirent->d_name, HELP_USER); |
85550587 | 232 | irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr); |
212380e3 AC |
233 | } |
234 | ||
235 | closedir(helpfile_dir); | |
1087485c JT |
236 | } |
237 | ||
238 | /* send_user_motd() | |
239 | * | |
240 | * inputs - client to send motd to | |
241 | * outputs - client is sent motd if exists, else ERR_NOMOTD | |
242 | * side effects - | |
243 | */ | |
244 | void | |
245 | send_user_motd(struct Client *source_p) | |
246 | { | |
247 | struct cacheline *lineptr; | |
248 | rb_dlink_node *ptr; | |
249 | const char *myname = get_id(&me, source_p); | |
250 | const char *nick = get_id(source_p, source_p); | |
251 | if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0) | |
252 | { | |
253 | sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick); | |
254 | return; | |
255 | } | |
256 | ||
257 | sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name); | |
258 | ||
259 | RB_DLINK_FOREACH(ptr, user_motd->contents.head) | |
260 | { | |
261 | lineptr = ptr->data; | |
262 | sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data); | |
263 | } | |
264 | ||
265 | sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick); | |
266 | } | |
267 | ||
268 | void | |
269 | cache_user_motd(void) | |
270 | { | |
271 | struct stat sb; | |
272 | struct tm *local_tm; | |
273 | ||
274 | if(stat(MPATH, &sb) == 0) | |
275 | { | |
276 | local_tm = localtime(&sb.st_mtime); | |
277 | ||
278 | if(local_tm != NULL) | |
279 | { | |
280 | rb_snprintf(user_motd_changed, sizeof(user_motd_changed), | |
281 | "%d/%d/%d %d:%d", | |
282 | local_tm->tm_mday, local_tm->tm_mon + 1, | |
283 | 1900 + local_tm->tm_year, local_tm->tm_hour, | |
284 | local_tm->tm_min); | |
285 | } | |
286 | } | |
287 | free_cachefile(user_motd); | |
288 | user_motd = cache_file(MPATH, "ircd.motd", 0); | |
289 | } | |
fe18f4bc JT |
290 | |
291 | ||
292 | /* send_oper_motd() | |
293 | * | |
294 | * inputs - client to send motd to | |
295 | * outputs - client is sent oper motd if exists | |
296 | * side effects - | |
297 | */ | |
298 | void | |
299 | send_oper_motd(struct Client *source_p) | |
300 | { | |
301 | struct cacheline *lineptr; | |
302 | rb_dlink_node *ptr; | |
303 | ||
304 | if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0) | |
305 | return; | |
306 | ||
307 | sendto_one(source_p, form_str(RPL_OMOTDSTART), | |
308 | me.name, source_p->name); | |
309 | ||
310 | RB_DLINK_FOREACH(ptr, oper_motd->contents.head) | |
311 | { | |
312 | lineptr = ptr->data; | |
313 | sendto_one(source_p, form_str(RPL_OMOTD), | |
314 | me.name, source_p->name, lineptr->data); | |
315 | } | |
316 | ||
317 | sendto_one(source_p, form_str(RPL_ENDOFOMOTD), | |
318 | me.name, source_p->name); | |
319 | } |