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