]>
jfr.im git - solanum.git/blob - src/cache.c
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * cache.c - code for caching files
5 * Copyright (C) 2003 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2003-2005 ircd-ratbox development team
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
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.
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.
32 * $Id: cache.c 3436 2007-05-02 19:56:40Z jilles $
36 #include "ircd_defs.h"
42 #include "sprintf_irc.h"
43 #include "irc_dictionary.h"
46 static BlockHeap
*cachefile_heap
= NULL
;
47 static BlockHeap
*cacheline_heap
= NULL
;
49 struct cachefile
*user_motd
= NULL
;
50 struct cachefile
*oper_motd
= NULL
;
51 char user_motd_changed
[MAX_DATE_STRING
];
53 struct Dictionary
*help_dict_oper
= NULL
;
54 struct Dictionary
*help_dict_user
= NULL
;
60 * side effects - inits the file/line cache blockheaps, loads motds
65 cachefile_heap
= BlockHeapCreate(sizeof(struct cachefile
), CACHEFILE_HEAP_SIZE
);
66 cacheline_heap
= BlockHeapCreate(sizeof(struct cacheline
), CACHELINE_HEAP_SIZE
);
68 user_motd_changed
[0] = '\0';
70 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
71 oper_motd
= cache_file(OPATH
, "opers.motd", 0);
73 help_dict_oper
= irc_dictionary_create(strcasecmp
);
74 help_dict_user
= irc_dictionary_create(strcasecmp
);
79 * inputs - file to cache, files "shortname", flags to set
80 * outputs - pointer to file cached, else NULL
84 cache_file(const char *filename
, const char *shortname
, int flags
)
87 struct cachefile
*cacheptr
;
88 struct cacheline
*lineptr
;
92 if((in
= fopen(filename
, "r")) == NULL
)
95 if(strcmp(shortname
, "ircd.motd") == 0)
100 if(fstat(fileno(in
), &sb
) < 0)
103 local_tm
= localtime(&sb
.st_mtime
);
106 rb_snprintf(user_motd_changed
, sizeof(user_motd_changed
),
108 local_tm
->tm_mday
, local_tm
->tm_mon
+ 1,
109 1900 + local_tm
->tm_year
, local_tm
->tm_hour
,
113 cacheptr
= BlockHeapAlloc(cachefile_heap
);
115 strlcpy(cacheptr
->name
, shortname
, sizeof(cacheptr
->name
));
116 cacheptr
->flags
= flags
;
118 /* cache the file... */
119 while(fgets(line
, sizeof(line
), in
) != NULL
)
121 if((p
= strchr(line
, '\n')) != NULL
)
124 lineptr
= BlockHeapAlloc(cacheline_heap
);
125 if(EmptyString(line
))
126 strlcpy(lineptr
->data
, " ", sizeof(lineptr
->data
));
128 strlcpy(lineptr
->data
, line
, sizeof(lineptr
->data
));
129 rb_dlinkAddTail(lineptr
, &lineptr
->linenode
, &cacheptr
->contents
);
138 * inputs - cachefile to free
140 * side effects - cachefile and its data is free'd
143 free_cachefile(struct cachefile
*cacheptr
)
146 rb_dlink_node
*next_ptr
;
151 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, cacheptr
->contents
.head
)
153 BlockHeapFree(cacheline_heap
, ptr
->data
);
156 BlockHeapFree(cachefile_heap
, cacheptr
);
163 * side effects - old help cache deleted
164 * - contents of help directories are loaded.
169 DIR *helpfile_dir
= NULL
;
170 struct dirent
*ldirent
= NULL
;
171 char filename
[MAXPATHLEN
];
172 struct cachefile
*cacheptr
;
173 struct DictionaryIter iter
;
175 DICTIONARY_FOREACH(cacheptr
, &iter
, help_dict_oper
)
177 irc_dictionary_delete(help_dict_oper
, cacheptr
->name
);
178 free_cachefile(cacheptr
);
180 DICTIONARY_FOREACH(cacheptr
, &iter
, help_dict_user
)
182 irc_dictionary_delete(help_dict_user
, cacheptr
->name
);
183 free_cachefile(cacheptr
);
186 helpfile_dir
= opendir(HPATH
);
188 if(helpfile_dir
== NULL
)
191 while((ldirent
= readdir(helpfile_dir
)) != NULL
)
193 rb_snprintf(filename
, sizeof(filename
), "%s/%s", HPATH
, ldirent
->d_name
);
194 cacheptr
= cache_file(filename
, ldirent
->d_name
, HELP_OPER
);
195 irc_dictionary_add(help_dict_oper
, cacheptr
->name
, cacheptr
);
198 closedir(helpfile_dir
);
199 helpfile_dir
= opendir(UHPATH
);
201 if(helpfile_dir
== NULL
)
204 while((ldirent
= readdir(helpfile_dir
)) != NULL
)
206 rb_snprintf(filename
, sizeof(filename
), "%s/%s", UHPATH
, ldirent
->d_name
);
208 cacheptr
= cache_file(filename
, ldirent
->d_name
, HELP_USER
);
209 irc_dictionary_add(help_dict_user
, cacheptr
->name
, cacheptr
);
212 closedir(helpfile_dir
);
217 * inputs - client to send motd to
218 * outputs - client is sent motd if exists, else ERR_NOMOTD
222 send_user_motd(struct Client
*source_p
)
224 struct cacheline
*lineptr
;
226 const char *myname
= get_id(&me
, source_p
);
227 const char *nick
= get_id(source_p
, source_p
);
229 if(user_motd
== NULL
|| rb_dlink_list_length(&user_motd
->contents
) == 0)
231 sendto_one(source_p
, form_str(ERR_NOMOTD
), myname
, nick
);
235 sendto_one(source_p
, form_str(RPL_MOTDSTART
), myname
, nick
, me
.name
);
237 RB_DLINK_FOREACH(ptr
, user_motd
->contents
.head
)
240 sendto_one(source_p
, form_str(RPL_MOTD
), myname
, nick
, lineptr
->data
);
243 sendto_one(source_p
, form_str(RPL_ENDOFMOTD
), myname
, nick
);
248 * inputs - client to send motd to
249 * outputs - client is sent oper motd if exists
253 send_oper_motd(struct Client
*source_p
)
255 struct cacheline
*lineptr
;
258 if(oper_motd
== NULL
|| rb_dlink_list_length(&oper_motd
->contents
) == 0)
261 sendto_one(source_p
, form_str(RPL_OMOTDSTART
),
262 me
.name
, source_p
->name
);
264 RB_DLINK_FOREACH(ptr
, oper_motd
->contents
.head
)
267 sendto_one(source_p
, form_str(RPL_OMOTD
),
268 me
.name
, source_p
->name
, lineptr
->data
);
271 sendto_one(source_p
, form_str(RPL_ENDOFOMOTD
),
272 me
.name
, source_p
->name
);