]> jfr.im git - irc/rqf/shadowircd.git/blob - src/cache.c
fixing cache.c compilation - code stolen from ratbox3, with irc dictionary addition
[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 "ratbox_lib.h"
37 #include "struct.h"
38 #include "s_conf.h"
39 #include "client.h"
40 #include "hash.h"
41 #include "cache.h"
42 #include "match.h"
43 #include "ircd.h"
44 #include "numeric.h"
45 #include "send.h"
46
47 struct cachefile *user_motd = NULL;
48 struct cachefile *oper_motd = NULL;
49 struct cacheline *emptyline = NULL;
50 rb_dlink_list links_cache_list;
51 char user_motd_changed[MAX_DATE_STRING];
52
53 struct Dictionary *help_dict_oper = NULL;
54 struct Dictionary *help_dict_user = NULL;
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 /* allocate the emptyline */
66 emptyline = rb_malloc(sizeof(struct cacheline));
67 emptyline->data[0] = ' ';
68 emptyline->data[1] = '\0';
69 user_motd_changed[0] = '\0';
70
71 user_motd = cache_file(MPATH, "ircd.motd", 0);
72 oper_motd = cache_file(OPATH, "opers.motd", 0);
73 memset(&links_cache_list, 0, sizeof(links_cache_list));
74
75 help_dict_oper = irc_dictionary_create(strcasecmp);
76 help_dict_user = irc_dictionary_create(strcasecmp);
77 }
78
79 /* cache_file()
80 *
81 * inputs - file to cache, files "shortname", flags to set
82 * outputs - pointer to file cached, else NULL
83 * side effects -
84 */
85 struct cachefile *
86 cache_file(const char *filename, const char *shortname, int flags)
87 {
88 FILE *in;
89 struct cachefile *cacheptr;
90 struct cacheline *lineptr;
91 char line[BUFSIZE];
92 char *p;
93
94 if((in = fopen(filename, "r")) == NULL)
95 return NULL;
96
97
98 cacheptr = rb_malloc(sizeof(struct cachefile));
99
100 rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));
101 cacheptr->flags = flags;
102
103 /* cache the file... */
104 while(fgets(line, sizeof(line), in) != NULL)
105 {
106 if((p = strpbrk(line, "\r\n")) != NULL)
107 *p = '\0';
108
109 if(!EmptyString(line))
110 {
111 lineptr = rb_malloc(sizeof(struct cacheline));
112 rb_strlcpy(lineptr->data, line, sizeof(lineptr->data));
113 rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
114 }
115 else
116 rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents);
117 }
118
119 fclose(in);
120 return cacheptr;
121 }
122
123 void
124 cache_links(void *unused)
125 {
126 struct Client *target_p;
127 rb_dlink_node *ptr;
128 rb_dlink_node *next_ptr;
129 char *links_line;
130
131 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head)
132 {
133 rb_free(ptr->data);
134 rb_free_rb_dlink_node(ptr);
135 }
136
137 links_cache_list.head = links_cache_list.tail = NULL;
138 links_cache_list.length = 0;
139
140 RB_DLINK_FOREACH(ptr, global_serv_list.head)
141 {
142 target_p = ptr->data;
143
144 /* skip ourselves (done in /links) and hidden servers */
145 if(IsMe(target_p) ||
146 (IsHidden(target_p) && !ConfigServerHide.disable_hidden))
147 continue;
148
149 /* if the below is ever modified, change LINKSLINELEN */
150 links_line = rb_malloc(LINKSLINELEN);
151 rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s",
152 target_p->name, me.name,
153 target_p->info[0] ? target_p->info :
154 "(Unknown Location)");
155
156 rb_dlinkAddTailAlloc(links_line, &links_cache_list);
157 }
158 }
159
160 /* free_cachefile()
161 *
162 * inputs - cachefile to free
163 * outputs -
164 * side effects - cachefile and its data is free'd
165 */
166 void
167 free_cachefile(struct cachefile *cacheptr)
168 {
169 rb_dlink_node *ptr;
170 rb_dlink_node *next_ptr;
171
172 if(cacheptr == NULL)
173 return;
174
175 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
176 {
177 if(ptr->data != emptyline)
178 rb_free(ptr->data);
179 }
180
181 rb_free(cacheptr);
182 }
183
184 /* load_help()
185 *
186 * inputs -
187 * outputs -
188 * side effects - old help cache deleted
189 * - contents of help directories are loaded.
190 */
191 void
192 load_help(void)
193 {
194 DIR *helpfile_dir = NULL;
195 struct dirent *ldirent= NULL;
196 char filename[MAXPATHLEN];
197 struct cachefile *cacheptr;
198 struct DictionaryIter iter;
199
200 DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper)
201 {
202 irc_dictionary_delete(help_dict_oper, cacheptr->name);
203 free_cachefile(cacheptr);
204 }
205 DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user)
206 {
207 irc_dictionary_delete(help_dict_user, cacheptr->name);
208 free_cachefile(cacheptr);
209 }
210
211 helpfile_dir = opendir(HPATH);
212
213 if(helpfile_dir == NULL)
214 return;
215
216 while((ldirent = readdir(helpfile_dir)) != NULL)
217 {
218 rb_snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name);
219 cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER);
220 irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr);
221 }
222
223 closedir(helpfile_dir);
224 helpfile_dir = opendir(UHPATH);
225
226 if(helpfile_dir == NULL)
227 return;
228
229 while((ldirent = readdir(helpfile_dir)) != NULL)
230 {
231 rb_snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name);
232
233 cacheptr = cache_file(filename, ldirent->d_name, HELP_USER);
234 irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr);
235 }
236
237 closedir(helpfile_dir);
238 }
239
240 /* send_user_motd()
241 *
242 * inputs - client to send motd to
243 * outputs - client is sent motd if exists, else ERR_NOMOTD
244 * side effects -
245 */
246 void
247 send_user_motd(struct Client *source_p)
248 {
249 struct cacheline *lineptr;
250 rb_dlink_node *ptr;
251 const char *myname = get_id(&me, source_p);
252 const char *nick = get_id(source_p, source_p);
253 if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0)
254 {
255 sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
256 return;
257 }
258 SetCork(source_p);
259 sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
260
261 RB_DLINK_FOREACH(ptr, user_motd->contents.head)
262 {
263 lineptr = ptr->data;
264 sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);
265 }
266 ClearCork(source_p);
267 sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
268 }
269
270 void
271 cache_user_motd(void)
272 {
273 struct stat sb;
274 struct tm *local_tm;
275
276 if(stat(MPATH, &sb) == 0)
277 {
278 local_tm = localtime(&sb.st_mtime);
279
280 if(local_tm != NULL)
281 {
282 rb_snprintf(user_motd_changed, sizeof(user_motd_changed),
283 "%d/%d/%d %d:%d",
284 local_tm->tm_mday, local_tm->tm_mon + 1,
285 1900 + local_tm->tm_year, local_tm->tm_hour,
286 local_tm->tm_min);
287 }
288 }
289 free_cachefile(user_motd);
290 user_motd = cache_file(MPATH, "ircd.motd", 0);
291 }