]>
Commit | Line | Data |
---|---|---|
212380e3 | 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 254 2005-09-21 23:35:12Z nenolod $ | |
33 | */ | |
34 | ||
35 | #include "stdinc.h" | |
36 | #include "ircd_defs.h" | |
37 | #include "common.h" | |
38 | #include "s_conf.h" | |
39 | #include "tools.h" | |
40 | #include "client.h" | |
41 | #include "memory.h" | |
42 | #include "balloc.h" | |
43 | #include "event.h" | |
44 | #include "hash.h" | |
45 | #include "cache.h" | |
46 | #include "sprintf_irc.h" | |
47 | ||
48 | static BlockHeap *cachefile_heap = NULL; | |
49 | static BlockHeap *cacheline_heap = NULL; | |
50 | ||
51 | struct cachefile *user_motd = NULL; | |
52 | struct cachefile *oper_motd = NULL; | |
53 | struct cacheline *emptyline = NULL; | |
54 | dlink_list links_cache_list; | |
55 | char user_motd_changed[MAX_DATE_STRING]; | |
56 | ||
57 | /* init_cache() | |
58 | * | |
59 | * inputs - | |
60 | * outputs - | |
61 | * side effects - inits the file/line cache blockheaps, loads motds | |
62 | */ | |
63 | void | |
64 | init_cache(void) | |
65 | { | |
66 | cachefile_heap = BlockHeapCreate(sizeof(struct cachefile), CACHEFILE_HEAP_SIZE); | |
67 | cacheline_heap = BlockHeapCreate(sizeof(struct cacheline), CACHELINE_HEAP_SIZE); | |
68 | ||
69 | /* allocate the emptyline */ | |
70 | emptyline = BlockHeapAlloc(cacheline_heap); | |
71 | emptyline->data[0] = ' '; | |
72 | emptyline->data[1] = '\0'; | |
73 | user_motd_changed[0] = '\0'; | |
74 | ||
75 | user_motd = cache_file(MPATH, "ircd.motd", 0); | |
76 | oper_motd = cache_file(OPATH, "opers.motd", 0); | |
77 | memset(&links_cache_list, 0, sizeof(links_cache_list)); | |
78 | } | |
79 | ||
80 | /* cache_file() | |
81 | * | |
82 | * inputs - file to cache, files "shortname", flags to set | |
83 | * outputs - pointer to file cached, else NULL | |
84 | * side effects - | |
85 | */ | |
86 | struct cachefile * | |
87 | cache_file(const char *filename, const char *shortname, int flags) | |
88 | { | |
89 | FILE *in; | |
90 | struct cachefile *cacheptr; | |
91 | struct cacheline *lineptr; | |
92 | char line[BUFSIZE]; | |
93 | char *p; | |
94 | ||
95 | if((in = fopen(filename, "r")) == NULL) | |
96 | return NULL; | |
97 | ||
98 | if(strcmp(shortname, "ircd.motd") == 0) | |
99 | { | |
100 | struct stat sb; | |
101 | struct tm *local_tm; | |
102 | ||
103 | if(fstat(fileno(in), &sb) < 0) | |
104 | return NULL; | |
105 | ||
106 | local_tm = localtime(&sb.st_mtime); | |
107 | ||
108 | if(local_tm != NULL) | |
109 | ircsnprintf(user_motd_changed, sizeof(user_motd_changed), | |
110 | "%d/%d/%d %d:%d", | |
111 | local_tm->tm_mday, local_tm->tm_mon + 1, | |
112 | 1900 + local_tm->tm_year, local_tm->tm_hour, | |
113 | local_tm->tm_min); | |
114 | } | |
115 | ||
116 | cacheptr = BlockHeapAlloc(cachefile_heap); | |
117 | ||
118 | strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name)); | |
119 | cacheptr->flags = flags; | |
120 | ||
121 | /* cache the file... */ | |
122 | while(fgets(line, sizeof(line), in) != NULL) | |
123 | { | |
124 | if((p = strchr(line, '\n')) != NULL) | |
125 | *p = '\0'; | |
126 | ||
127 | if(!EmptyString(line)) | |
128 | { | |
129 | lineptr = BlockHeapAlloc(cacheline_heap); | |
130 | strlcpy(lineptr->data, line, sizeof(lineptr->data)); | |
131 | dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents); | |
132 | } | |
133 | else | |
134 | dlinkAddTailAlloc(emptyline, &cacheptr->contents); | |
135 | } | |
136 | ||
137 | fclose(in); | |
138 | return cacheptr; | |
139 | } | |
140 | ||
141 | void | |
142 | cache_links(void *unused) | |
143 | { | |
144 | struct Client *target_p; | |
145 | dlink_node *ptr; | |
146 | dlink_node *next_ptr; | |
147 | char *links_line; | |
148 | ||
149 | DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head) | |
150 | { | |
151 | MyFree(ptr->data); | |
152 | free_dlink_node(ptr); | |
153 | } | |
154 | ||
155 | links_cache_list.head = links_cache_list.tail = NULL; | |
156 | links_cache_list.length = 0; | |
157 | ||
158 | DLINK_FOREACH(ptr, global_serv_list.head) | |
159 | { | |
160 | target_p = ptr->data; | |
161 | ||
162 | /* skip ourselves (done in /links) and hidden servers */ | |
163 | if(IsMe(target_p) || | |
164 | (IsHidden(target_p) && !ConfigServerHide.disable_hidden)) | |
165 | continue; | |
166 | ||
167 | /* if the below is ever modified, change LINKSLINELEN */ | |
168 | links_line = MyMalloc(LINKSLINELEN); | |
169 | ircsnprintf(links_line, LINKSLINELEN, "%s %s :1 %s", | |
170 | target_p->name, me.name, | |
171 | target_p->info[0] ? target_p->info : | |
172 | "(Unknown Location)"); | |
173 | ||
174 | dlinkAddTailAlloc(links_line, &links_cache_list); | |
175 | } | |
176 | } | |
177 | ||
178 | /* free_cachefile() | |
179 | * | |
180 | * inputs - cachefile to free | |
181 | * outputs - | |
182 | * side effects - cachefile and its data is free'd | |
183 | */ | |
184 | void | |
185 | free_cachefile(struct cachefile *cacheptr) | |
186 | { | |
187 | dlink_node *ptr; | |
188 | dlink_node *next_ptr; | |
189 | ||
190 | if(cacheptr == NULL) | |
191 | return; | |
192 | ||
193 | DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head) | |
194 | { | |
195 | if(ptr->data != emptyline) | |
196 | BlockHeapFree(cacheline_heap, ptr->data); | |
197 | } | |
198 | ||
199 | BlockHeapFree(cachefile_heap, cacheptr); | |
200 | } | |
201 | ||
202 | /* load_help() | |
203 | * | |
204 | * inputs - | |
205 | * outputs - | |
206 | * side effects - contents of help directories are loaded. | |
207 | */ | |
208 | void | |
209 | load_help(void) | |
210 | { | |
211 | DIR *helpfile_dir = NULL; | |
212 | struct dirent *ldirent= NULL; | |
213 | char filename[MAXPATHLEN]; | |
214 | struct cachefile *cacheptr; | |
215 | ||
216 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) | |
217 | struct stat sb; | |
218 | #endif | |
219 | ||
220 | /* opers must be done first */ | |
221 | helpfile_dir = opendir(HPATH); | |
222 | ||
223 | if(helpfile_dir == NULL) | |
224 | return; | |
225 | ||
226 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
227 | { | |
228 | ircsnprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name); | |
229 | cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER); | |
230 | add_to_help_hash(cacheptr->name, cacheptr); | |
231 | } | |
232 | ||
233 | closedir(helpfile_dir); | |
234 | helpfile_dir = opendir(UHPATH); | |
235 | ||
236 | if(helpfile_dir == NULL) | |
237 | return; | |
238 | ||
239 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
240 | { | |
241 | ircsnprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name); | |
242 | ||
243 | #if defined(S_ISLNK) && defined(HAVE_LSTAT) | |
244 | if(lstat(filename, &sb) < 0) | |
245 | continue; | |
246 | ||
247 | /* ok, if its a symlink, we work on the presumption if an | |
248 | * oper help exists of that name, its a symlink to that --fl | |
249 | */ | |
250 | if(S_ISLNK(sb.st_mode)) | |
251 | { | |
252 | cacheptr = hash_find_help(ldirent->d_name, HELP_OPER); | |
253 | ||
254 | if(cacheptr != NULL) | |
255 | { | |
256 | cacheptr->flags |= HELP_USER; | |
257 | continue; | |
258 | } | |
259 | } | |
260 | #endif | |
261 | ||
262 | cacheptr = cache_file(filename, ldirent->d_name, HELP_USER); | |
263 | add_to_help_hash(cacheptr->name, cacheptr); | |
264 | } | |
265 | ||
266 | closedir(helpfile_dir); | |
267 | } | |
268 | ||
269 | /* send_user_motd() | |
270 | * | |
271 | * inputs - client to send motd to | |
272 | * outputs - client is sent motd if exists, else ERR_NOMOTD | |
273 | * side effects - | |
274 | */ | |
275 | void | |
276 | send_user_motd(struct Client *source_p) | |
277 | { | |
278 | struct cacheline *lineptr; | |
279 | dlink_node *ptr; | |
280 | const char *myname = get_id(&me, source_p); | |
281 | const char *nick = get_id(source_p, source_p); | |
282 | ||
283 | if(user_motd == NULL || dlink_list_length(&user_motd->contents) == 0) | |
284 | { | |
285 | sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick); | |
286 | return; | |
287 | } | |
288 | ||
289 | sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name); | |
290 | ||
291 | DLINK_FOREACH(ptr, user_motd->contents.head) | |
292 | { | |
293 | lineptr = ptr->data; | |
294 | sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data); | |
295 | } | |
296 | ||
297 | sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick); | |
298 | } | |
299 | ||
300 | /* send_oper_motd() | |
301 | * | |
302 | * inputs - client to send motd to | |
303 | * outputs - client is sent oper motd if exists | |
304 | * side effects - | |
305 | */ | |
306 | void | |
307 | send_oper_motd(struct Client *source_p) | |
308 | { | |
309 | struct cacheline *lineptr; | |
310 | dlink_node *ptr; | |
311 | ||
312 | if(oper_motd == NULL || dlink_list_length(&oper_motd->contents) == 0) | |
313 | return; | |
314 | ||
315 | sendto_one(source_p, form_str(RPL_OMOTDSTART), | |
316 | me.name, source_p->name); | |
317 | ||
318 | DLINK_FOREACH(ptr, oper_motd->contents.head) | |
319 | { | |
320 | lineptr = ptr->data; | |
321 | sendto_one(source_p, form_str(RPL_OMOTD), | |
322 | me.name, source_p->name, lineptr->data); | |
323 | } | |
324 | ||
325 | sendto_one(source_p, form_str(RPL_ENDOFOMOTD), | |
326 | me.name, source_p->name); | |
327 | } | |
328 |