]> jfr.im git - irc/rqf/shadowircd.git/blob - src/cache.c
Restore and update send_oper_motd().
[irc/rqf/shadowircd.git] / src / cache.c
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
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"
44 #include "numeric.h"
45
46 struct cachefile *user_motd = NULL;
47 struct cachefile *oper_motd = NULL;
48 struct cacheline *emptyline = NULL;
49 rb_dlink_list links_cache_list;
50 char user_motd_changed[MAX_DATE_STRING];
51
52 struct Dictionary *help_dict_oper = NULL;
53 struct Dictionary *help_dict_user = NULL;
54
55 /* init_cache()
56 *
57 * inputs -
58 * outputs -
59 * side effects - inits the file/line cache blockheaps, loads motds
60 */
61 void
62 init_cache(void)
63 {
64 /* allocate the emptyline */
65 emptyline = rb_malloc(sizeof(struct cacheline));
66 emptyline->data[0] = ' ';
67 emptyline->data[1] = '\0';
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 help_dict_oper = irc_dictionary_create(strcasecmp);
75 help_dict_user = irc_dictionary_create(strcasecmp);
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
97 cacheptr = rb_malloc(sizeof(struct cachefile));
98
99 rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));
100 cacheptr->flags = flags;
101
102 /* cache the file... */
103 while(fgets(line, sizeof(line), in) != NULL)
104 {
105 if((p = strpbrk(line, "\r\n")) != NULL)
106 *p = '\0';
107
108 if(!EmptyString(line))
109 {
110 lineptr = rb_malloc(sizeof(struct cacheline));
111 rb_strlcpy(lineptr->data, line, sizeof(lineptr->data));
112 rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
113 }
114 else
115 rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents);
116 }
117
118 fclose(in);
119 return cacheptr;
120 }
121
122 void
123 cache_links(void *unused)
124 {
125 struct Client *target_p;
126 rb_dlink_node *ptr;
127 rb_dlink_node *next_ptr;
128 char *links_line;
129
130 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head)
131 {
132 rb_free(ptr->data);
133 rb_free_rb_dlink_node(ptr);
134 }
135
136 links_cache_list.head = links_cache_list.tail = NULL;
137 links_cache_list.length = 0;
138
139 RB_DLINK_FOREACH(ptr, global_serv_list.head)
140 {
141 target_p = ptr->data;
142
143 /* skip ourselves (done in /links) and hidden servers */
144 if(IsMe(target_p) ||
145 (IsHidden(target_p) && !ConfigServerHide.disable_hidden))
146 continue;
147
148 /* if the below is ever modified, change LINKSLINELEN */
149 links_line = rb_malloc(LINKSLINELEN);
150 rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s",
151 target_p->name, me.name,
152 target_p->info[0] ? target_p->info :
153 "(Unknown Location)");
154
155 rb_dlinkAddTailAlloc(links_line, &links_cache_list);
156 }
157 }
158
159 /* free_cachefile()
160 *
161 * inputs - cachefile to free
162 * outputs -
163 * side effects - cachefile and its data is free'd
164 */
165 void
166 free_cachefile(struct cachefile *cacheptr)
167 {
168 rb_dlink_node *ptr;
169 rb_dlink_node *next_ptr;
170
171 if(cacheptr == NULL)
172 return;
173
174 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
175 {
176 if(ptr->data != emptyline)
177 rb_free(ptr->data);
178 }
179
180 rb_free(cacheptr);
181 }
182
183 /* load_help()
184 *
185 * inputs -
186 * outputs -
187 * side effects - old help cache deleted
188 * - contents of help directories are loaded.
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;
197 struct DictionaryIter iter;
198
199 DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper)
200 {
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);
207 free_cachefile(cacheptr);
208 }
209
210 helpfile_dir = opendir(HPATH);
211
212 if(helpfile_dir == NULL)
213 return;
214
215 while((ldirent = readdir(helpfile_dir)) != NULL)
216 {
217 rb_snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name);
218 cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER);
219 irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr);
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 {
230 rb_snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name);
231
232 cacheptr = cache_file(filename, ldirent->d_name, HELP_USER);
233 irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr);
234 }
235
236 closedir(helpfile_dir);
237 }
238
239 /* send_user_motd()
240 *
241 * inputs - client to send motd to
242 * outputs - client is sent motd if exists, else ERR_NOMOTD
243 * side effects -
244 */
245 void
246 send_user_motd(struct Client *source_p)
247 {
248 struct cacheline *lineptr;
249 rb_dlink_node *ptr;
250 const char *myname = get_id(&me, source_p);
251 const char *nick = get_id(source_p, source_p);
252 if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0)
253 {
254 sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
255 return;
256 }
257
258 sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
259
260 RB_DLINK_FOREACH(ptr, user_motd->contents.head)
261 {
262 lineptr = ptr->data;
263 sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);
264 }
265
266 sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
267 }
268
269 void
270 cache_user_motd(void)
271 {
272 struct stat sb;
273 struct tm *local_tm;
274
275 if(stat(MPATH, &sb) == 0)
276 {
277 local_tm = localtime(&sb.st_mtime);
278
279 if(local_tm != NULL)
280 {
281 rb_snprintf(user_motd_changed, sizeof(user_motd_changed),
282 "%d/%d/%d %d:%d",
283 local_tm->tm_mday, local_tm->tm_mon + 1,
284 1900 + local_tm->tm_year, local_tm->tm_hour,
285 local_tm->tm_min);
286 }
287 }
288 free_cachefile(user_motd);
289 user_motd = cache_file(MPATH, "ircd.motd", 0);
290 }
291
292
293 /* send_oper_motd()
294 *
295 * inputs - client to send motd to
296 * outputs - client is sent oper motd if exists
297 * side effects -
298 */
299 void
300 send_oper_motd(struct Client *source_p)
301 {
302 struct cacheline *lineptr;
303 rb_dlink_node *ptr;
304
305 if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0)
306 return;
307
308 sendto_one(source_p, form_str(RPL_OMOTDSTART),
309 me.name, source_p->name);
310
311 RB_DLINK_FOREACH(ptr, oper_motd->contents.head)
312 {
313 lineptr = ptr->data;
314 sendto_one(source_p, form_str(RPL_OMOTD),
315 me.name, source_p->name, lineptr->data);
316 }
317
318 sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
319 me.name, source_p->name);
320 }