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