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