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 25119 2008-03-13 16:57:05Z androsyn $
36 #include "ircd_defs.h"
42 #include "irc_dictionary.h"
45 struct cachefile
*user_motd
= NULL
;
46 struct cachefile
*oper_motd
= NULL
;
47 struct cacheline
*emptyline
= NULL
;
48 rb_dlink_list links_cache_list
;
49 char user_motd_changed
[MAX_DATE_STRING
];
51 struct Dictionary
*help_dict_oper
= NULL
;
52 struct Dictionary
*help_dict_user
= NULL
;
58 * side effects - inits the file/line cache blockheaps, loads motds
63 /* allocate the emptyline */
64 emptyline
= rb_malloc(sizeof(struct cacheline
));
65 emptyline
->data
= rb_strdup(" ");
67 user_motd_changed
[0] = '\0';
69 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
70 oper_motd
= cache_file(OPATH
, "opers.motd", 0);
71 memset(&links_cache_list
, 0, sizeof(links_cache_list
));
73 help_dict_oper
= irc_dictionary_create(strcasecmp
);
74 help_dict_user
= irc_dictionary_create(strcasecmp
);
78 * removes tabs from src, replaces with 8 spaces, and returns the length
79 * of the new string. if the new string would be greater than destlen,
80 * it is truncated to destlen - 1
83 untabify(char *dest
, const char *src
, size_t destlen
)
89 while(*s
!= '\0' && x
< destlen
- 1)
93 for(i
= 0; i
< 8 && x
< destlen
- 1; i
++, x
++, d
++)
108 * inputs - file to cache, files "shortname", flags to set
109 * outputs - pointer to file cached, else NULL
113 cache_file(const char *filename
, const char *shortname
, int flags
)
116 struct cachefile
*cacheptr
;
117 struct cacheline
*lineptr
;
121 if((in
= fopen(filename
, "r")) == NULL
)
125 cacheptr
= rb_malloc(sizeof(struct cachefile
));
127 rb_strlcpy(cacheptr
->name
, shortname
, sizeof(cacheptr
->name
));
128 cacheptr
->flags
= flags
;
130 /* cache the file... */
131 while(fgets(line
, sizeof(line
), in
) != NULL
)
133 if((p
= strpbrk(line
, "\r\n")) != NULL
)
136 if(!EmptyString(line
))
138 char untabline
[BUFSIZE
];
140 lineptr
= rb_malloc(sizeof(struct cacheline
));
142 untabify(untabline
, line
, sizeof(untabline
));
143 lineptr
->data
= rb_strdup(untabline
);
145 rb_dlinkAddTail(lineptr
, &lineptr
->linenode
, &cacheptr
->contents
);
148 rb_dlinkAddTailAlloc(emptyline
, &cacheptr
->contents
);
151 if (0 == rb_dlink_list_length(&cacheptr
->contents
))
153 /* No contents. Don't cache it after all. */
163 cache_links(void *unused
)
165 struct Client
*target_p
;
167 rb_dlink_node
*next_ptr
;
170 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, links_cache_list
.head
)
173 rb_free_rb_dlink_node(ptr
);
176 links_cache_list
.head
= links_cache_list
.tail
= NULL
;
177 links_cache_list
.length
= 0;
179 RB_DLINK_FOREACH(ptr
, global_serv_list
.head
)
181 target_p
= ptr
->data
;
183 /* skip ourselves (done in /links) and hidden servers */
185 (IsHidden(target_p
) && !ConfigServerHide
.disable_hidden
))
188 /* if the below is ever modified, change LINKSLINELEN */
189 links_line
= rb_malloc(LINKSLINELEN
);
190 rb_snprintf(links_line
, LINKSLINELEN
, "%s %s :1 %s",
191 target_p
->name
, me
.name
,
192 target_p
->info
[0] ? target_p
->info
:
193 "(Unknown Location)");
195 rb_dlinkAddTailAlloc(links_line
, &links_cache_list
);
201 * inputs - cachefile to free
203 * side effects - cachefile and its data is free'd
206 free_cachefile(struct cachefile
*cacheptr
)
209 rb_dlink_node
*next_ptr
;
214 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, cacheptr
->contents
.head
)
216 if(ptr
->data
!= emptyline
)
218 struct cacheline
*line
= ptr
->data
;
231 * side effects - old help cache deleted
232 * - contents of help directories are loaded.
237 DIR *helpfile_dir
= NULL
;
238 struct dirent
*ldirent
= NULL
;
239 char filename
[PATH_MAX
];
240 struct cachefile
*cacheptr
;
241 struct DictionaryIter iter
;
243 #if defined(S_ISLNK) && defined(HAVE_LSTAT)
247 DICTIONARY_FOREACH(cacheptr
, &iter
, help_dict_oper
)
249 irc_dictionary_delete(help_dict_oper
, cacheptr
->name
);
250 free_cachefile(cacheptr
);
252 DICTIONARY_FOREACH(cacheptr
, &iter
, help_dict_user
)
254 irc_dictionary_delete(help_dict_user
, cacheptr
->name
);
255 free_cachefile(cacheptr
);
258 helpfile_dir
= opendir(HPATH
);
260 if(helpfile_dir
== NULL
)
263 while((ldirent
= readdir(helpfile_dir
)) != NULL
)
265 if(ldirent
->d_name
[0] == '.')
267 rb_snprintf(filename
, sizeof(filename
), "%s/%s", HPATH
, ldirent
->d_name
);
268 cacheptr
= cache_file(filename
, ldirent
->d_name
, HELP_OPER
);
269 irc_dictionary_add(help_dict_oper
, cacheptr
->name
, cacheptr
);
272 closedir(helpfile_dir
);
273 helpfile_dir
= opendir(UHPATH
);
275 if(helpfile_dir
== NULL
)
278 while((ldirent
= readdir(helpfile_dir
)) != NULL
)
280 if(ldirent
->d_name
[0] == '.')
282 rb_snprintf(filename
, sizeof(filename
), "%s/%s", UHPATH
, ldirent
->d_name
);
284 #if defined(S_ISLNK) && defined(HAVE_LSTAT)
285 if(lstat(filename
, &sb
) < 0)
288 /* ok, if its a symlink, we work on the presumption if an
289 * oper help exists of that name, its a symlink to that --fl
291 if(S_ISLNK(sb
.st_mode
))
293 cacheptr
= irc_dictionary_retrieve(help_dict_oper
, ldirent
->d_name
);
297 cacheptr
->flags
|= HELP_USER
;
303 cacheptr
= cache_file(filename
, ldirent
->d_name
, HELP_USER
);
304 irc_dictionary_add(help_dict_user
, cacheptr
->name
, cacheptr
);
307 closedir(helpfile_dir
);
312 * inputs - client to send motd to
313 * outputs - client is sent motd if exists, else ERR_NOMOTD
317 send_user_motd(struct Client
*source_p
)
319 struct cacheline
*lineptr
;
321 const char *myname
= get_id(&me
, source_p
);
322 const char *nick
= get_id(source_p
, source_p
);
323 if(user_motd
== NULL
|| rb_dlink_list_length(&user_motd
->contents
) == 0)
325 sendto_one(source_p
, form_str(ERR_NOMOTD
), myname
, nick
);
329 sendto_one(source_p
, form_str(RPL_MOTDSTART
), myname
, nick
, me
.name
);
331 RB_DLINK_FOREACH(ptr
, user_motd
->contents
.head
)
334 sendto_one(source_p
, form_str(RPL_MOTD
), myname
, nick
, lineptr
->data
);
337 sendto_one(source_p
, form_str(RPL_ENDOFMOTD
), myname
, nick
);
341 cache_user_motd(void)
346 if(stat(MPATH
, &sb
) == 0)
348 local_tm
= localtime(&sb
.st_mtime
);
352 rb_snprintf(user_motd_changed
, sizeof(user_motd_changed
),
354 local_tm
->tm_mday
, local_tm
->tm_mon
+ 1,
355 1900 + local_tm
->tm_year
, local_tm
->tm_hour
,
359 free_cachefile(user_motd
);
360 user_motd
= cache_file(MPATH
, "ircd.motd", 0);
366 * inputs - client to send motd to
367 * outputs - client is sent oper motd if exists
371 send_oper_motd(struct Client
*source_p
)
373 struct cacheline
*lineptr
;
376 if(oper_motd
== NULL
|| rb_dlink_list_length(&oper_motd
->contents
) == 0)
379 sendto_one(source_p
, form_str(RPL_OMOTDSTART
),
380 me
.name
, source_p
->name
);
382 RB_DLINK_FOREACH(ptr
, oper_motd
->contents
.head
)
385 sendto_one(source_p
, form_str(RPL_OMOTD
),
386 me
.name
, source_p
->name
, lineptr
->data
);
389 sendto_one(source_p
, form_str(RPL_ENDOFOMOTD
),
390 me
.name
, source_p
->name
);