]> jfr.im git - irc/rqf/shadowircd.git/blame - src/cache.c
Make this compile, but lots of warnings.
[irc/rqf/shadowircd.git] / src / cache.c
CommitLineData
212380e3 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 *
1d3e262e 32 * $Id: cache.c 3436 2007-05-02 19:56:40Z jilles $
212380e3 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"
9802490c 47#include "irc_dictionary.h"
212380e3 48
49static BlockHeap *cachefile_heap = NULL;
50static BlockHeap *cacheline_heap = NULL;
51
52struct cachefile *user_motd = NULL;
53struct cachefile *oper_motd = NULL;
212380e3 54char user_motd_changed[MAX_DATE_STRING];
55
5a34b193
JT
56struct Dictionary *help_dict_oper = NULL;
57struct Dictionary *help_dict_user = NULL;
9802490c 58
212380e3 59/* init_cache()
60 *
61 * inputs -
62 * outputs -
63 * side effects - inits the file/line cache blockheaps, loads motds
64 */
65void
66init_cache(void)
67{
68 cachefile_heap = BlockHeapCreate(sizeof(struct cachefile), CACHEFILE_HEAP_SIZE);
69 cacheline_heap = BlockHeapCreate(sizeof(struct cacheline), CACHELINE_HEAP_SIZE);
70
212380e3 71 user_motd_changed[0] = '\0';
72
73 user_motd = cache_file(MPATH, "ircd.motd", 0);
74 oper_motd = cache_file(OPATH, "opers.motd", 0);
9802490c 75
5a34b193
JT
76 help_dict_oper = irc_dictionary_create(strcasecmp);
77 help_dict_user = irc_dictionary_create(strcasecmp);
212380e3 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 */
86struct cachefile *
87cache_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 if(strcmp(shortname, "ircd.motd") == 0)
99 {
100 struct stat sb;
101 struct tm *local_tm;
102
103 if(fstat(fileno(in), &sb) < 0)
104 return NULL;
105
106 local_tm = localtime(&sb.st_mtime);
107
108 if(local_tm != NULL)
109 ircsnprintf(user_motd_changed, sizeof(user_motd_changed),
110 "%d/%d/%d %d:%d",
111 local_tm->tm_mday, local_tm->tm_mon + 1,
112 1900 + local_tm->tm_year, local_tm->tm_hour,
113 local_tm->tm_min);
114 }
115
116 cacheptr = BlockHeapAlloc(cachefile_heap);
117
118 strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));
119 cacheptr->flags = flags;
120
121 /* cache the file... */
122 while(fgets(line, sizeof(line), in) != NULL)
123 {
124 if((p = strchr(line, '\n')) != NULL)
125 *p = '\0';
126
1d3e262e 127 lineptr = BlockHeapAlloc(cacheline_heap);
128 if(EmptyString(line))
129 strlcpy(lineptr->data, " ", sizeof(lineptr->data));
212380e3 130 else
1d3e262e 131 strlcpy(lineptr->data, line, sizeof(lineptr->data));
132 dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
212380e3 133 }
134
135 fclose(in);
136 return cacheptr;
137}
138
212380e3 139/* free_cachefile()
140 *
141 * inputs - cachefile to free
142 * outputs -
143 * side effects - cachefile and its data is free'd
144 */
145void
146free_cachefile(struct cachefile *cacheptr)
147{
148 dlink_node *ptr;
149 dlink_node *next_ptr;
150
151 if(cacheptr == NULL)
152 return;
153
154 DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
155 {
1d3e262e 156 BlockHeapFree(cacheline_heap, ptr->data);
212380e3 157 }
158
159 BlockHeapFree(cachefile_heap, cacheptr);
160}
161
162/* load_help()
163 *
164 * inputs -
165 * outputs -
e8149a2c
JT
166 * side effects - old help cache deleted
167 * - contents of help directories are loaded.
212380e3 168 */
169void
170load_help(void)
171{
172 DIR *helpfile_dir = NULL;
173 struct dirent *ldirent= NULL;
174 char filename[MAXPATHLEN];
175 struct cachefile *cacheptr;
e8149a2c 176 struct DictionaryIter iter;
212380e3 177
5a34b193 178 DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper)
e8149a2c 179 {
5a34b193
JT
180 irc_dictionary_delete(help_dict_oper, cacheptr->name);
181 free_cachefile(cacheptr);
182 }
183 DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user)
184 {
185 irc_dictionary_delete(help_dict_user, cacheptr->name);
e8149a2c
JT
186 free_cachefile(cacheptr);
187 }
188
212380e3 189 helpfile_dir = opendir(HPATH);
190
191 if(helpfile_dir == NULL)
192 return;
193
194 while((ldirent = readdir(helpfile_dir)) != NULL)
195 {
196 ircsnprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name);
197 cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER);
5a34b193 198 irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr);
212380e3 199 }
200
201 closedir(helpfile_dir);
202 helpfile_dir = opendir(UHPATH);
203
204 if(helpfile_dir == NULL)
205 return;
206
207 while((ldirent = readdir(helpfile_dir)) != NULL)
208 {
209 ircsnprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name);
210
212380e3 211 cacheptr = cache_file(filename, ldirent->d_name, HELP_USER);
5a34b193 212 irc_dictionary_add(help_dict_user, cacheptr->name, cacheptr);
212380e3 213 }
214
215 closedir(helpfile_dir);
216}
217
218/* send_user_motd()
219 *
220 * inputs - client to send motd to
221 * outputs - client is sent motd if exists, else ERR_NOMOTD
222 * side effects -
223 */
224void
225send_user_motd(struct Client *source_p)
226{
227 struct cacheline *lineptr;
228 dlink_node *ptr;
229 const char *myname = get_id(&me, source_p);
230 const char *nick = get_id(source_p, source_p);
231
232 if(user_motd == NULL || dlink_list_length(&user_motd->contents) == 0)
233 {
234 sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
235 return;
236 }
237
238 sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
239
240 DLINK_FOREACH(ptr, user_motd->contents.head)
241 {
242 lineptr = ptr->data;
243 sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);
244 }
245
246 sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
247}
248
249/* send_oper_motd()
250 *
251 * inputs - client to send motd to
252 * outputs - client is sent oper motd if exists
253 * side effects -
254 */
255void
256send_oper_motd(struct Client *source_p)
257{
258 struct cacheline *lineptr;
259 dlink_node *ptr;
260
261 if(oper_motd == NULL || dlink_list_length(&oper_motd->contents) == 0)
262 return;
263
264 sendto_one(source_p, form_str(RPL_OMOTDSTART),
265 me.name, source_p->name);
266
267 DLINK_FOREACH(ptr, oper_motd->contents.head)
268 {
269 lineptr = ptr->data;
270 sendto_one(source_p, form_str(RPL_OMOTD),
271 me.name, source_p->name, lineptr->data);
272 }
273
274 sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
275 me.name, source_p->name);
276}
277