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