2008-04-02 20:50:20 +02:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
|
|
|
|
* cache.c - code for caching files
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Lee Hardy <lee@leeh.co.uk>
|
|
|
|
* Copyright (C) 2003-2005 ircd-ratbox development team
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* 1.Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2.Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3.The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2008-04-02 02:19:12 +02:00
|
|
|
#include "stdinc.h"
|
|
|
|
#include "ircd_defs.h"
|
|
|
|
#include "s_conf.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "cache.h"
|
2016-03-06 21:17:19 +01:00
|
|
|
#include "rb_dictionary.h"
|
2008-04-02 20:50:20 +02:00
|
|
|
#include "numeric.h"
|
2013-09-10 07:35:56 +02:00
|
|
|
#include "send.h"
|
2008-04-02 20:50:20 +02:00
|
|
|
|
|
|
|
struct cachefile *user_motd = NULL;
|
|
|
|
struct cachefile *oper_motd = NULL;
|
|
|
|
struct cacheline *emptyline = NULL;
|
|
|
|
rb_dlink_list links_cache_list;
|
|
|
|
char user_motd_changed[MAX_DATE_STRING];
|
|
|
|
|
2016-03-23 14:09:58 +01:00
|
|
|
rb_dictionary *help_dict_oper = NULL;
|
|
|
|
rb_dictionary *help_dict_user = NULL;
|
2008-04-02 20:50:20 +02:00
|
|
|
|
|
|
|
/* init_cache()
|
|
|
|
*
|
|
|
|
* inputs -
|
|
|
|
* outputs -
|
|
|
|
* side effects - inits the file/line cache blockheaps, loads motds
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_cache(void)
|
|
|
|
{
|
|
|
|
/* allocate the emptyline */
|
|
|
|
emptyline = rb_malloc(sizeof(struct cacheline));
|
2013-01-15 23:24:33 +01:00
|
|
|
emptyline->data = rb_strdup(" ");
|
|
|
|
|
2008-04-02 20:50:20 +02:00
|
|
|
user_motd_changed[0] = '\0';
|
|
|
|
|
2016-03-25 00:45:28 +01:00
|
|
|
user_motd = cache_file(ircd_paths[IRCD_PATH_IRCD_MOTD], "ircd.motd", 0);
|
|
|
|
oper_motd = cache_file(ircd_paths[IRCD_PATH_IRCD_OMOTD], "opers.motd", 0);
|
2008-04-02 20:50:20 +02:00
|
|
|
memset(&links_cache_list, 0, sizeof(links_cache_list));
|
|
|
|
|
2016-04-05 12:39:59 +02:00
|
|
|
help_dict_oper = rb_dictionary_create("oper help", rb_strcasecmp);
|
|
|
|
help_dict_user = rb_dictionary_create("user help", rb_strcasecmp);
|
2008-04-02 20:50:20 +02:00
|
|
|
}
|
|
|
|
|
2014-03-03 05:25:47 +01:00
|
|
|
/*
|
2008-06-28 09:22:43 +02:00
|
|
|
* removes tabs from src, replaces with 8 spaces, and returns the length
|
|
|
|
* of the new string. if the new string would be greater than destlen,
|
|
|
|
* it is truncated to destlen - 1
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
untabify(char *dest, const char *src, size_t destlen)
|
|
|
|
{
|
|
|
|
size_t x = 0, i;
|
|
|
|
const char *s = src;
|
|
|
|
char *d = dest;
|
|
|
|
|
|
|
|
while(*s != '\0' && x < destlen - 1)
|
|
|
|
{
|
|
|
|
if(*s == '\t')
|
|
|
|
{
|
|
|
|
for(i = 0; i < 8 && x < destlen - 1; i++, x++, d++)
|
|
|
|
*d = ' ';
|
|
|
|
s++;
|
2014-03-03 05:25:47 +01:00
|
|
|
} else
|
2008-06-28 09:22:43 +02:00
|
|
|
{
|
|
|
|
*d++ = *s++;
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2008-04-02 20:50:20 +02:00
|
|
|
/* cache_file()
|
|
|
|
*
|
|
|
|
* inputs - file to cache, files "shortname", flags to set
|
|
|
|
* outputs - pointer to file cached, else NULL
|
|
|
|
* side effects -
|
|
|
|
*/
|
|
|
|
struct cachefile *
|
|
|
|
cache_file(const char *filename, const char *shortname, int flags)
|
|
|
|
{
|
|
|
|
FILE *in;
|
|
|
|
struct cachefile *cacheptr;
|
|
|
|
struct cacheline *lineptr;
|
|
|
|
char line[BUFSIZE];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if((in = fopen(filename, "r")) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
cacheptr = rb_malloc(sizeof(struct cachefile));
|
|
|
|
|
|
|
|
rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));
|
|
|
|
cacheptr->flags = flags;
|
|
|
|
|
|
|
|
/* cache the file... */
|
|
|
|
while(fgets(line, sizeof(line), in) != NULL)
|
|
|
|
{
|
|
|
|
if((p = strpbrk(line, "\r\n")) != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if(!EmptyString(line))
|
|
|
|
{
|
2013-01-15 23:24:33 +01:00
|
|
|
char untabline[BUFSIZE];
|
|
|
|
|
2008-04-02 20:50:20 +02:00
|
|
|
lineptr = rb_malloc(sizeof(struct cacheline));
|
2013-01-15 23:24:33 +01:00
|
|
|
|
|
|
|
untabify(untabline, line, sizeof(untabline));
|
|
|
|
lineptr->data = rb_strdup(untabline);
|
|
|
|
|
2008-04-02 20:50:20 +02:00
|
|
|
rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents);
|
|
|
|
}
|
|
|
|
|
2010-01-21 17:27:34 +01:00
|
|
|
if (0 == rb_dlink_list_length(&cacheptr->contents))
|
|
|
|
{
|
|
|
|
/* No contents. Don't cache it after all. */
|
|
|
|
rb_free(cacheptr);
|
|
|
|
cacheptr = NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-02 20:50:20 +02:00
|
|
|
fclose(in);
|
|
|
|
return cacheptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cache_links(void *unused)
|
|
|
|
{
|
|
|
|
struct Client *target_p;
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
rb_dlink_node *next_ptr;
|
|
|
|
char *links_line;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head)
|
|
|
|
{
|
|
|
|
rb_free(ptr->data);
|
|
|
|
rb_free_rb_dlink_node(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
links_cache_list.head = links_cache_list.tail = NULL;
|
|
|
|
links_cache_list.length = 0;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, global_serv_list.head)
|
|
|
|
{
|
|
|
|
target_p = ptr->data;
|
|
|
|
|
|
|
|
/* skip ourselves (done in /links) and hidden servers */
|
|
|
|
if(IsMe(target_p) ||
|
|
|
|
(IsHidden(target_p) && !ConfigServerHide.disable_hidden))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* if the below is ever modified, change LINKSLINELEN */
|
|
|
|
links_line = rb_malloc(LINKSLINELEN);
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(links_line, LINKSLINELEN, "%s %s :1 %s",
|
2014-03-03 05:25:47 +01:00
|
|
|
target_p->name, me.name,
|
|
|
|
target_p->info[0] ? target_p->info :
|
2008-04-02 20:50:20 +02:00
|
|
|
"(Unknown Location)");
|
|
|
|
|
|
|
|
rb_dlinkAddTailAlloc(links_line, &links_cache_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free_cachefile()
|
|
|
|
*
|
|
|
|
* inputs - cachefile to free
|
|
|
|
* outputs -
|
|
|
|
* side effects - cachefile and its data is free'd
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
free_cachefile(struct cachefile *cacheptr)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
rb_dlink_node *next_ptr;
|
|
|
|
|
|
|
|
if(cacheptr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
|
|
|
|
{
|
|
|
|
if(ptr->data != emptyline)
|
2013-01-15 23:24:33 +01:00
|
|
|
{
|
|
|
|
struct cacheline *line = ptr->data;
|
|
|
|
rb_free(line->data);
|
|
|
|
rb_free(line);
|
|
|
|
}
|
2008-04-02 20:50:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_free(cacheptr);
|
|
|
|
}
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
/* load_help()
|
|
|
|
*
|
|
|
|
* inputs -
|
|
|
|
* outputs -
|
2007-12-12 22:26:17 +01:00
|
|
|
* side effects - old help cache deleted
|
|
|
|
* - contents of help directories are loaded.
|
2007-01-25 07:40:21 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
load_help(void)
|
|
|
|
{
|
|
|
|
DIR *helpfile_dir = NULL;
|
|
|
|
struct dirent *ldirent= NULL;
|
2011-10-28 16:45:18 +02:00
|
|
|
char filename[PATH_MAX];
|
2007-01-25 07:40:21 +01:00
|
|
|
struct cachefile *cacheptr;
|
2016-03-23 14:09:58 +01:00
|
|
|
rb_dictionary_iter iter;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-06-28 09:48:30 +02:00
|
|
|
#if defined(S_ISLNK) && defined(HAVE_LSTAT)
|
|
|
|
struct stat sb;
|
|
|
|
#endif
|
|
|
|
|
2016-03-23 13:45:44 +01:00
|
|
|
RB_DICTIONARY_FOREACH(cacheptr, &iter, help_dict_oper)
|
2007-12-12 22:26:17 +01:00
|
|
|
{
|
2016-03-06 21:17:19 +01:00
|
|
|
rb_dictionary_delete(help_dict_oper, cacheptr->name);
|
2008-03-29 23:38:22 +01:00
|
|
|
free_cachefile(cacheptr);
|
|
|
|
}
|
2016-03-23 13:45:44 +01:00
|
|
|
RB_DICTIONARY_FOREACH(cacheptr, &iter, help_dict_user)
|
2008-03-29 23:38:22 +01:00
|
|
|
{
|
2016-03-06 21:17:19 +01:00
|
|
|
rb_dictionary_delete(help_dict_user, cacheptr->name);
|
2007-12-12 22:26:17 +01:00
|
|
|
free_cachefile(cacheptr);
|
|
|
|
}
|
|
|
|
|
2016-03-25 00:45:28 +01:00
|
|
|
helpfile_dir = opendir(ircd_paths[IRCD_PATH_OPERHELP]);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
if(helpfile_dir == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while((ldirent = readdir(helpfile_dir)) != NULL)
|
|
|
|
{
|
2010-01-22 02:02:45 +01:00
|
|
|
if(ldirent->d_name[0] == '.')
|
|
|
|
continue;
|
2016-03-25 00:45:28 +01:00
|
|
|
snprintf(filename, sizeof(filename), "%s%c%s", ircd_paths[IRCD_PATH_OPERHELP], RB_PATH_SEPARATOR, ldirent->d_name);
|
2007-01-25 07:40:21 +01:00
|
|
|
cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER);
|
2016-03-06 21:17:19 +01:00
|
|
|
rb_dictionary_add(help_dict_oper, cacheptr->name, cacheptr);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(helpfile_dir);
|
2016-03-25 00:50:13 +01:00
|
|
|
helpfile_dir = opendir(ircd_paths[IRCD_PATH_USERHELP]);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
if(helpfile_dir == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while((ldirent = readdir(helpfile_dir)) != NULL)
|
|
|
|
{
|
2010-01-22 02:02:45 +01:00
|
|
|
if(ldirent->d_name[0] == '.')
|
|
|
|
continue;
|
2016-03-25 00:45:28 +01:00
|
|
|
snprintf(filename, sizeof(filename), "%s%c%s", ircd_paths[IRCD_PATH_USERHELP], RB_PATH_SEPARATOR, ldirent->d_name);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-06-28 09:48:30 +02:00
|
|
|
#if defined(S_ISLNK) && defined(HAVE_LSTAT)
|
|
|
|
if(lstat(filename, &sb) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ok, if its a symlink, we work on the presumption if an
|
|
|
|
* oper help exists of that name, its a symlink to that --fl
|
|
|
|
*/
|
|
|
|
if(S_ISLNK(sb.st_mode))
|
|
|
|
{
|
2016-03-06 21:17:19 +01:00
|
|
|
cacheptr = rb_dictionary_retrieve(help_dict_oper, ldirent->d_name);
|
2008-06-28 09:48:30 +02:00
|
|
|
|
|
|
|
if(cacheptr != NULL)
|
|
|
|
{
|
|
|
|
cacheptr->flags |= HELP_USER;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
cacheptr = cache_file(filename, ldirent->d_name, HELP_USER);
|
2016-03-06 21:17:19 +01:00
|
|
|
rb_dictionary_add(help_dict_user, cacheptr->name, cacheptr);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(helpfile_dir);
|
2008-04-02 20:50:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send_user_motd()
|
|
|
|
*
|
|
|
|
* inputs - client to send motd to
|
|
|
|
* outputs - client is sent motd if exists, else ERR_NOMOTD
|
|
|
|
* side effects -
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
send_user_motd(struct Client *source_p)
|
|
|
|
{
|
|
|
|
struct cacheline *lineptr;
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
const char *myname = get_id(&me, source_p);
|
|
|
|
const char *nick = get_id(source_p, source_p);
|
|
|
|
if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0)
|
|
|
|
{
|
|
|
|
sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, user_motd->contents.head)
|
|
|
|
{
|
|
|
|
lineptr = ptr->data;
|
|
|
|
sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cache_user_motd(void)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
struct tm *local_tm;
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2016-03-25 00:45:28 +01:00
|
|
|
if(stat(ircd_paths[IRCD_PATH_IRCD_MOTD], &sb) == 0)
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
|
|
|
local_tm = localtime(&sb.st_mtime);
|
|
|
|
|
2014-03-03 05:25:47 +01:00
|
|
|
if(local_tm != NULL)
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(user_motd_changed, sizeof(user_motd_changed),
|
2008-04-02 20:50:20 +02:00
|
|
|
"%d/%d/%d %d:%d",
|
|
|
|
local_tm->tm_mday, local_tm->tm_mon + 1,
|
|
|
|
1900 + local_tm->tm_year, local_tm->tm_hour,
|
|
|
|
local_tm->tm_min);
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
}
|
2008-04-02 20:50:20 +02:00
|
|
|
free_cachefile(user_motd);
|
2016-03-25 00:45:28 +01:00
|
|
|
user_motd = cache_file(ircd_paths[IRCD_PATH_IRCD_MOTD], "ircd.motd", 0);
|
2008-04-02 20:50:20 +02:00
|
|
|
}
|
2008-04-03 01:16:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* send_oper_motd()
|
|
|
|
*
|
|
|
|
* inputs - client to send motd to
|
|
|
|
* outputs - client is sent oper motd if exists
|
|
|
|
* side effects -
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
send_oper_motd(struct Client *source_p)
|
|
|
|
{
|
|
|
|
struct cacheline *lineptr;
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
|
|
|
if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0)
|
|
|
|
return;
|
|
|
|
|
2014-03-03 05:25:47 +01:00
|
|
|
sendto_one(source_p, form_str(RPL_OMOTDSTART),
|
2008-04-03 01:16:49 +02:00
|
|
|
me.name, source_p->name);
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, oper_motd->contents.head)
|
|
|
|
{
|
|
|
|
lineptr = ptr->data;
|
|
|
|
sendto_one(source_p, form_str(RPL_OMOTD),
|
|
|
|
me.name, source_p->name, lineptr->data);
|
|
|
|
}
|
|
|
|
|
2014-03-03 05:25:47 +01:00
|
|
|
sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
|
2008-04-03 01:16:49 +02:00
|
|
|
me.name, source_p->name);
|
|
|
|
}
|