Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / cache.c
blob8e94a1603bcf4bbb140c74259ccefb73e1b3d751
1 /*
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
10 * met:
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.
33 #include "stdinc.h"
34 #include "ircd_defs.h"
35 #include "common.h"
36 #include "s_conf.h"
37 #include "tools.h"
38 #include "client.h"
39 #include "memory.h"
40 #include "balloc.h"
41 #include "event.h"
42 #include "hash.h"
43 #include "cache.h"
44 #include "sprintf_irc.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 struct cacheline *emptyline = NULL;
52 dlink_list links_cache_list;
53 char user_motd_changed[MAX_DATE_STRING];
55 /* init_cache()
57 * inputs -
58 * outputs -
59 * side effects - inits the file/line cache blockheaps, loads motds
61 void
62 init_cache(void)
64 cachefile_heap = BlockHeapCreate(sizeof(struct cachefile), CACHEFILE_HEAP_SIZE);
65 cacheline_heap = BlockHeapCreate(sizeof(struct cacheline), CACHELINE_HEAP_SIZE);
67 /* allocate the emptyline */
68 emptyline = BlockHeapAlloc(cacheline_heap);
69 emptyline->data[0] = ' ';
70 emptyline->data[1] = '\0';
71 user_motd_changed[0] = '\0';
73 user_motd = cache_file(MPATH, "ircd.motd", 0);
74 oper_motd = cache_file(OPATH, "opers.motd", 0);
75 memset(&links_cache_list, 0, sizeof(links_cache_list));
78 /* cache_file()
80 * inputs - file to cache, files "shortname", flags to set
81 * outputs - pointer to file cached, else NULL
82 * side effects -
84 struct cachefile *
85 cache_file(const char *filename, const char *shortname, int flags)
87 FILE *in;
88 struct cachefile *cacheptr;
89 struct cacheline *lineptr;
90 char line[BUFSIZE];
91 char *p;
93 if((in = fopen(filename, "r")) == NULL)
94 return NULL;
96 if(strcmp(shortname, "ircd.motd") == 0)
98 struct stat sb;
99 struct tm *local_tm;
101 if(fstat(fileno(in), &sb) < 0)
102 return NULL;
104 local_tm = localtime(&sb.st_mtime);
106 if(local_tm != NULL)
107 ircsnprintf(user_motd_changed, sizeof(user_motd_changed),
108 "%d/%d/%d %d:%d",
109 local_tm->tm_mday, local_tm->tm_mon + 1,
110 1900 + local_tm->tm_year, local_tm->tm_hour,
111 local_tm->tm_min);
114 cacheptr = BlockHeapAlloc(cachefile_heap);
116 strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));
117 cacheptr->flags = flags;
119 /* cache the file... */
120 while(fgets(line, sizeof(line), in) != NULL)
122 if((p = strchr(line, '\n')) != NULL)
123 *p = '\0';
125 if(!EmptyString(line))
127 lineptr = BlockHeapAlloc(cacheline_heap);
128 strlcpy(lineptr->data, line, sizeof(lineptr->data));
129 dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
131 else
132 dlinkAddTailAlloc(emptyline, &cacheptr->contents);
135 fclose(in);
136 return cacheptr;
139 void
140 cache_links(void *unused)
142 struct Client *target_p;
143 dlink_node *ptr;
144 dlink_node *next_ptr;
145 char *links_line;
147 DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head)
149 MyFree(ptr->data);
150 free_dlink_node(ptr);
153 links_cache_list.head = links_cache_list.tail = NULL;
154 links_cache_list.length = 0;
156 DLINK_FOREACH(ptr, global_serv_list.head)
158 target_p = ptr->data;
160 /* skip ourselves (done in /links) and hidden servers */
161 if(IsMe(target_p) ||
162 (IsHidden(target_p) && !ConfigServerHide.disable_hidden))
163 continue;
165 /* if the below is ever modified, change LINKSLINELEN */
166 links_line = MyMalloc(LINKSLINELEN);
167 ircsnprintf(links_line, LINKSLINELEN, "%s %s :1 %s",
168 target_p->name, me.name,
169 target_p->info[0] ? target_p->info :
170 "(Unknown Location)");
172 dlinkAddTailAlloc(links_line, &links_cache_list);
176 /* free_cachefile()
178 * inputs - cachefile to free
179 * outputs -
180 * side effects - cachefile and its data is free'd
182 void
183 free_cachefile(struct cachefile *cacheptr)
185 dlink_node *ptr;
186 dlink_node *next_ptr;
188 if(cacheptr == NULL)
189 return;
191 DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
193 if(ptr->data != emptyline)
194 BlockHeapFree(cacheline_heap, ptr->data);
197 BlockHeapFree(cachefile_heap, cacheptr);
200 /* load_help()
202 * inputs -
203 * outputs -
204 * side effects - contents of help directories are loaded.
206 void
207 load_help(void)
209 DIR *helpfile_dir = NULL;
210 struct dirent *ldirent= NULL;
211 char filename[MAXPATHLEN];
212 struct cachefile *cacheptr;
214 #if defined(S_ISLNK) && defined(HAVE_LSTAT)
215 struct stat sb;
216 #endif
218 /* opers must be done first */
219 helpfile_dir = opendir(HPATH);
221 if(helpfile_dir == NULL)
222 return;
224 while((ldirent = readdir(helpfile_dir)) != NULL)
226 ircsnprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name);
227 cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER);
228 add_to_help_hash(cacheptr->name, cacheptr);
231 closedir(helpfile_dir);
232 helpfile_dir = opendir(UHPATH);
234 if(helpfile_dir == NULL)
235 return;
237 while((ldirent = readdir(helpfile_dir)) != NULL)
239 ircsnprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name);
241 #if defined(S_ISLNK) && defined(HAVE_LSTAT)
242 if(lstat(filename, &sb) < 0)
243 continue;
245 /* ok, if its a symlink, we work on the presumption if an
246 * oper help exists of that name, its a symlink to that --fl
248 if(S_ISLNK(sb.st_mode))
250 cacheptr = hash_find_help(ldirent->d_name, HELP_OPER);
252 if(cacheptr != NULL)
254 cacheptr->flags |= HELP_USER;
255 continue;
258 #endif
260 cacheptr = cache_file(filename, ldirent->d_name, HELP_USER);
261 add_to_help_hash(cacheptr->name, cacheptr);
264 closedir(helpfile_dir);
267 /* send_user_motd()
269 * inputs - client to send motd to
270 * outputs - client is sent motd if exists, else ERR_NOMOTD
271 * side effects -
273 void
274 send_user_motd(struct Client *source_p)
276 struct cacheline *lineptr;
277 dlink_node *ptr;
278 const char *myname = get_id(&me, source_p);
279 const char *nick = get_id(source_p, source_p);
281 if(user_motd == NULL || dlink_list_length(&user_motd->contents) == 0)
283 sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
284 return;
287 sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
289 DLINK_FOREACH(ptr, user_motd->contents.head)
291 lineptr = ptr->data;
292 sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);
295 sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
298 /* send_oper_motd()
300 * inputs - client to send motd to
301 * outputs - client is sent oper motd if exists
302 * side effects -
304 void
305 send_oper_motd(struct Client *source_p)
307 struct cacheline *lineptr;
308 dlink_node *ptr;
310 if(oper_motd == NULL || dlink_list_length(&oper_motd->contents) == 0)
311 return;
313 sendto_one(source_p, form_str(RPL_OMOTDSTART),
314 me.name, source_p->name);
316 DLINK_FOREACH(ptr, oper_motd->contents.head)
318 lineptr = ptr->data;
319 sendto_one(source_p, form_str(RPL_OMOTD),
320 me.name, source_p->name, lineptr->data);
323 sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
324 me.name, source_p->name);