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