]>
Commit | Line | Data |
---|---|---|
acc2595c VY |
1 | /*\r |
2 | * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).\r | |
3 | * cache.c - code for caching files\r | |
4 | *\r | |
5 | * Copyright (C) 2003 Lee Hardy <lee@leeh.co.uk>\r | |
6 | * Copyright (C) 2003-2005 ircd-ratbox development team\r | |
7 | *\r | |
8 | * Redistribution and use in source and binary forms, with or without\r | |
9 | * modification, are permitted provided that the following conditions are\r | |
10 | * met:\r | |
11 | *\r | |
12 | * 1.Redistributions of source code must retain the above copyright notice,\r | |
13 | * this list of conditions and the following disclaimer.\r | |
14 | * 2.Redistributions in binary form must reproduce the above copyright\r | |
15 | * notice, this list of conditions and the following disclaimer in the\r | |
16 | * documentation and/or other materials provided with the distribution.\r | |
17 | * 3.The name of the author may not be used to endorse or promote products\r | |
18 | * derived from this software without specific prior written permission.\r | |
19 | *\r | |
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r | |
21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r | |
22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r | |
23 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,\r | |
24 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r | |
25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r | |
26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r | |
28 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r | |
29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r | |
30 | * POSSIBILITY OF SUCH DAMAGE.\r | |
31 | *\r | |
32 | * $Id: cache.c 25119 2008-03-13 16:57:05Z androsyn $\r | |
33 | */\r | |
34 | \r | |
a83914b3 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" | |
42 | #include "sprintf_irc.h" | |
43 | #include "irc_dictionary.h" | |
acc2595c | 44 | #include "numeric.h"\r |
acc2595c VY |
45 | \r |
46 | struct cachefile *user_motd = NULL;\r | |
47 | struct cachefile *oper_motd = NULL;\r | |
48 | struct cacheline *emptyline = NULL;\r | |
49 | rb_dlink_list links_cache_list;\r | |
50 | char user_motd_changed[MAX_DATE_STRING];\r | |
51 | \r | |
5a34b193 | 52 | struct Dictionary *help_dict_oper = NULL; |
acc2595c VY |
53 | struct Dictionary *help_dict_user = NULL;\r |
54 | \r | |
55 | /* init_cache()\r | |
56 | *\r | |
57 | * inputs -\r | |
58 | * outputs -\r | |
59 | * side effects - inits the file/line cache blockheaps, loads motds\r | |
60 | */\r | |
61 | void\r | |
62 | init_cache(void)\r | |
63 | {\r | |
64 | /* allocate the emptyline */\r | |
65 | emptyline = rb_malloc(sizeof(struct cacheline));\r | |
66 | emptyline->data[0] = ' ';\r | |
67 | emptyline->data[1] = '\0';\r | |
68 | user_motd_changed[0] = '\0';\r | |
69 | \r | |
70 | user_motd = cache_file(MPATH, "ircd.motd", 0);\r | |
71 | oper_motd = cache_file(OPATH, "opers.motd", 0);\r | |
72 | memset(&links_cache_list, 0, sizeof(links_cache_list));\r | |
73 | \r | |
5a34b193 | 74 | help_dict_oper = irc_dictionary_create(strcasecmp); |
acc2595c VY |
75 | help_dict_user = irc_dictionary_create(strcasecmp);\r |
76 | }\r | |
77 | \r | |
78 | /* cache_file()\r | |
79 | *\r | |
80 | * inputs - file to cache, files "shortname", flags to set\r | |
81 | * outputs - pointer to file cached, else NULL\r | |
82 | * side effects -\r | |
83 | */\r | |
84 | struct cachefile *\r | |
85 | cache_file(const char *filename, const char *shortname, int flags)\r | |
86 | {\r | |
87 | FILE *in;\r | |
88 | struct cachefile *cacheptr;\r | |
89 | struct cacheline *lineptr;\r | |
90 | char line[BUFSIZE];\r | |
91 | char *p;\r | |
92 | \r | |
93 | if((in = fopen(filename, "r")) == NULL)\r | |
94 | return NULL;\r | |
95 | \r | |
96 | \r | |
97 | cacheptr = rb_malloc(sizeof(struct cachefile));\r | |
98 | \r | |
99 | rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));\r | |
100 | cacheptr->flags = flags;\r | |
101 | \r | |
102 | /* cache the file... */\r | |
103 | while(fgets(line, sizeof(line), in) != NULL)\r | |
104 | {\r | |
105 | if((p = strpbrk(line, "\r\n")) != NULL)\r | |
106 | *p = '\0';\r | |
107 | \r | |
108 | if(!EmptyString(line))\r | |
109 | {\r | |
110 | lineptr = rb_malloc(sizeof(struct cacheline));\r | |
111 | rb_strlcpy(lineptr->data, line, sizeof(lineptr->data));\r | |
112 | rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);\r | |
113 | }\r | |
114 | else\r | |
115 | rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents);\r | |
116 | }\r | |
117 | \r | |
118 | fclose(in);\r | |
119 | return cacheptr;\r | |
120 | }\r | |
121 | \r | |
122 | void\r | |
123 | cache_links(void *unused)\r | |
124 | {\r | |
125 | struct Client *target_p;\r | |
126 | rb_dlink_node *ptr;\r | |
127 | rb_dlink_node *next_ptr;\r | |
128 | char *links_line;\r | |
129 | \r | |
130 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head)\r | |
131 | {\r | |
132 | rb_free(ptr->data);\r | |
133 | rb_free_rb_dlink_node(ptr);\r | |
134 | }\r | |
135 | \r | |
136 | links_cache_list.head = links_cache_list.tail = NULL;\r | |
137 | links_cache_list.length = 0;\r | |
138 | \r | |
139 | RB_DLINK_FOREACH(ptr, global_serv_list.head)\r | |
140 | {\r | |
141 | target_p = ptr->data;\r | |
142 | \r | |
143 | /* skip ourselves (done in /links) and hidden servers */\r | |
144 | if(IsMe(target_p) ||\r | |
145 | (IsHidden(target_p) && !ConfigServerHide.disable_hidden))\r | |
146 | continue;\r | |
147 | \r | |
148 | /* if the below is ever modified, change LINKSLINELEN */\r | |
149 | links_line = rb_malloc(LINKSLINELEN);\r | |
150 | rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s",\r | |
151 | target_p->name, me.name, \r | |
152 | target_p->info[0] ? target_p->info : \r | |
153 | "(Unknown Location)");\r | |
154 | \r | |
155 | rb_dlinkAddTailAlloc(links_line, &links_cache_list);\r | |
156 | }\r | |
157 | }\r | |
158 | \r | |
159 | /* free_cachefile()\r | |
160 | *\r | |
161 | * inputs - cachefile to free\r | |
162 | * outputs -\r | |
163 | * side effects - cachefile and its data is free'd\r | |
164 | */\r | |
165 | void\r | |
166 | free_cachefile(struct cachefile *cacheptr)\r | |
167 | {\r | |
168 | rb_dlink_node *ptr;\r | |
169 | rb_dlink_node *next_ptr;\r | |
170 | \r | |
171 | if(cacheptr == NULL)\r | |
172 | return;\r | |
173 | \r | |
174 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)\r | |
175 | {\r | |
176 | if(ptr->data != emptyline)\r | |
177 | rb_free(ptr->data);\r | |
178 | }\r | |
179 | \r | |
180 | rb_free(cacheptr);\r | |
181 | }\r | |
182 | \r | |
212380e3 | 183 | /* load_help() |
184 | * | |
185 | * inputs - | |
186 | * outputs - | |
e8149a2c JT |
187 | * side effects - old help cache deleted |
188 | * - contents of help directories are loaded. | |
212380e3 | 189 | */ |
190 | void | |
191 | load_help(void) | |
192 | { | |
193 | DIR *helpfile_dir = NULL; | |
194 | struct dirent *ldirent= NULL; | |
195 | char filename[MAXPATHLEN]; | |
196 | struct cachefile *cacheptr; | |
e8149a2c | 197 | struct DictionaryIter iter; |
212380e3 | 198 | |
5a34b193 | 199 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper) |
e8149a2c | 200 | { |
5a34b193 JT |
201 | irc_dictionary_delete(help_dict_oper, cacheptr->name); |
202 | free_cachefile(cacheptr); | |
203 | } | |
204 | DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user) | |
205 | { | |
206 | irc_dictionary_delete(help_dict_user, cacheptr->name); | |
e8149a2c JT |
207 | free_cachefile(cacheptr); |
208 | } | |
209 | ||
212380e3 | 210 | helpfile_dir = opendir(HPATH); |
211 | ||
212 | if(helpfile_dir == NULL) | |
213 | return; | |
214 | ||
215 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
216 | { | |
38e6acdd | 217 | rb_snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name); |
212380e3 | 218 | cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER); |
5a34b193 | 219 | irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr); |
212380e3 | 220 | } |
221 | ||
222 | closedir(helpfile_dir); | |
223 | helpfile_dir = opendir(UHPATH); | |
224 | ||
225 | if(helpfile_dir == NULL) | |
226 | return; | |
227 | ||
228 | while((ldirent = readdir(helpfile_dir)) != NULL) | |
229 | { | |
38e6acdd | 230 | rb_snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name); |
212380e3 | 231 | |
212380e3 | 232 | cacheptr = cache_file(filename, ldirent->d_name, HELP_USER); |
5a34b193 | 233 | irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr); |
212380e3 | 234 | } |
235 | ||
236 | closedir(helpfile_dir); | |
acc2595c VY |
237 | }\r |
238 | \r | |
239 | /* send_user_motd()\r | |
240 | *\r | |
241 | * inputs - client to send motd to\r | |
242 | * outputs - client is sent motd if exists, else ERR_NOMOTD\r | |
243 | * side effects -\r | |
244 | */\r | |
245 | void\r | |
246 | send_user_motd(struct Client *source_p)\r | |
247 | {\r | |
248 | struct cacheline *lineptr;\r | |
249 | rb_dlink_node *ptr;\r | |
250 | const char *myname = get_id(&me, source_p);\r | |
251 | const char *nick = get_id(source_p, source_p);\r | |
252 | if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0)\r | |
253 | {\r | |
254 | sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);\r | |
255 | return;\r | |
256 | }\r | |
a83914b3 | 257 | \r |
acc2595c VY |
258 | sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);\r |
259 | \r | |
260 | RB_DLINK_FOREACH(ptr, user_motd->contents.head)\r | |
261 | {\r | |
262 | lineptr = ptr->data;\r | |
263 | sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);\r | |
264 | }\r | |
a83914b3 | 265 | \r |
acc2595c VY |
266 | sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);\r |
267 | }\r | |
268 | \r | |
269 | void\r | |
270 | cache_user_motd(void)\r | |
271 | {\r | |
272 | struct stat sb;\r | |
273 | struct tm *local_tm;\r | |
274 | \r | |
275 | if(stat(MPATH, &sb) == 0) \r | |
276 | {\r | |
277 | local_tm = localtime(&sb.st_mtime);\r | |
278 | \r | |
279 | if(local_tm != NULL) \r | |
280 | {\r | |
281 | rb_snprintf(user_motd_changed, sizeof(user_motd_changed),\r | |
282 | "%d/%d/%d %d:%d",\r | |
283 | local_tm->tm_mday, local_tm->tm_mon + 1,\r | |
284 | 1900 + local_tm->tm_year, local_tm->tm_hour,\r | |
285 | local_tm->tm_min);\r | |
286 | }\r | |
287 | } \r | |
288 | free_cachefile(user_motd);\r | |
289 | user_motd = cache_file(MPATH, "ircd.motd", 0);\r | |
290 | }\r |