0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 18:22:50 +01:00
construct/ircd/cache.cc

284 lines
6.9 KiB
C++
Raw Normal View History

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.
*/
#include <ircd/stdinc.h>
#include <ircd/s_conf.h>
#include <ircd/client.h>
#include <ircd/hash.h>
#include <ircd/cache.h>
#include <ircd/numeric.h>
#include <ircd/send.h>
2008-04-02 20:50:20 +02:00
#include <map>
2016-07-31 08:03:05 +02:00
#include <vector>
2016-07-31 08:03:05 +02:00
struct cachefile user_motd {};
struct cachefile oper_motd {};
2008-04-02 20:50:20 +02:00
char user_motd_changed[MAX_DATE_STRING];
2016-07-31 08:03:05 +02:00
std::map<std::string, std::shared_ptr<cachefile>, case_insensitive_less> help_dict_oper;
std::map<std::string, std::shared_ptr<cachefile>, case_insensitive_less> help_dict_user;
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)
{
user_motd_changed[0] = '\0';
2016-07-31 08:03:05 +02:00
user_motd.cache(fs::paths[IRCD_PATH_IRCD_MOTD], "ircd.motd", 0);
oper_motd.cache(fs::paths[IRCD_PATH_IRCD_OMOTD], "opers.motd", 0);
2008-04-02 20:50:20 +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++;
} else
{
*d++ = *s++;
x++;
}
}
*d = '\0';
return x;
}
2016-07-31 08:03:05 +02:00
/* cachefile::cache()
2008-04-02 20:50:20 +02:00
*
* inputs - file to cache, files "shortname", flags to set
2016-07-31 08:03:05 +02:00
* outputs - none
* side effects - cachefile.contents is populated with the lines from the file
2008-04-02 20:50:20 +02:00
*/
2016-07-31 08:03:05 +02:00
void
cachefile::cache(const char *filename, const char *shortname, int flags_)
2008-04-02 20:50:20 +02:00
{
FILE *in;
char line[BUFSIZE];
char *p;
if((in = fopen(filename, "r")) == NULL)
2016-07-31 08:03:05 +02:00
return;
2008-04-02 20:50:20 +02:00
2016-07-31 08:03:05 +02:00
contents.clear();
2008-04-02 20:50:20 +02:00
2016-07-31 08:03:05 +02:00
name = shortname;
flags = flags_;
2008-04-02 20:50:20 +02:00
/* cache the file... */
while(fgets(line, sizeof(line), in) != NULL)
{
2016-07-31 08:03:05 +02:00
char untabline[BUFSIZE];
2008-04-02 20:50:20 +02:00
if((p = strpbrk(line, "\r\n")) != NULL)
*p = '\0';
2016-07-31 08:03:05 +02:00
untabify(untabline, line, sizeof(untabline));
contents.push_back(untabline);
}
2008-04-02 20:50:20 +02:00
fclose(in);
}
/* load_help()
*
* inputs -
* outputs -
2007-12-12 22:26:17 +01:00
* side effects - old help cache deleted
* - contents of help directories are loaded.
*/
void
load_help(void)
{
DIR *helpfile_dir = NULL;
struct dirent *ldirent= NULL;
char filename[PATH_MAX];
2008-06-28 09:48:30 +02:00
#if defined(S_ISLNK) && defined(HAVE_LSTAT)
struct stat sb;
#endif
void *elem;
2016-07-31 08:03:05 +02:00
help_dict_oper.clear();
help_dict_user.clear();
2007-12-12 22:26:17 +01:00
helpfile_dir = opendir(fs::paths[IRCD_PATH_OPERHELP]);
if(helpfile_dir == NULL)
return;
while((ldirent = readdir(helpfile_dir)) != NULL)
{
if(ldirent->d_name[0] == '.')
continue;
snprintf(filename, sizeof(filename), "%s%c%s", fs::paths[IRCD_PATH_OPERHELP], RB_PATH_SEPARATOR, ldirent->d_name);
2016-07-31 08:03:05 +02:00
help_dict_oper[ldirent->d_name] = std::make_shared<cachefile>(filename, ldirent->d_name, HELP_OPER);
}
closedir(helpfile_dir);
helpfile_dir = opendir(fs::paths[IRCD_PATH_USERHELP]);
if(helpfile_dir == NULL)
return;
while((ldirent = readdir(helpfile_dir)) != NULL)
{
if(ldirent->d_name[0] == '.')
continue;
snprintf(filename, sizeof(filename), "%s%c%s", fs::paths[IRCD_PATH_USERHELP], RB_PATH_SEPARATOR, ldirent->d_name);
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-07-31 08:21:10 +02:00
std::shared_ptr<cachefile> cacheptr = help_dict_oper[ldirent->d_name];
2008-06-28 09:48:30 +02:00
if(cacheptr != NULL)
{
cacheptr->flags |= HELP_USER;
continue;
}
}
#endif
2016-07-31 08:03:05 +02:00
help_dict_user[ldirent->d_name] = std::make_shared<cachefile>(filename, ldirent->d_name, HELP_USER);
}
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)
{
rb_dlink_node *ptr;
const char *myname = get_id(&me, source_p);
const char *nick = get_id(source_p, source_p);
2016-07-31 08:03:05 +02:00
if (user_motd.contents.size() == 0)
2008-04-02 20:50:20 +02:00
{
sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
return;
}
sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
2016-07-31 08:03:05 +02:00
for (auto it = user_motd.contents.begin(); it != user_motd.contents.end(); it++)
2008-04-02 20:50:20 +02:00
{
2016-07-31 08:03:05 +02:00
sendto_one(source_p, form_str(RPL_MOTD), myname, nick, it->c_str());
2008-04-02 20:50:20 +02:00
}
sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
}
void
cache_user_motd(void)
{
struct stat sb;
struct tm *local_tm;
if(stat(fs::paths[IRCD_PATH_IRCD_MOTD], &sb) == 0)
2008-04-02 20:50:20 +02:00
{
local_tm = localtime(&sb.st_mtime);
if(local_tm != NULL)
2008-04-02 20:50:20 +02: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);
}
}
2016-07-31 08:03:05 +02:00
user_motd.cache(fs::paths[IRCD_PATH_IRCD_MOTD], "ircd.motd", 0);
2008-04-02 20:50:20 +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;
2016-07-31 08:03:05 +02:00
if (oper_motd.contents.size() == 0)
return;
sendto_one(source_p, form_str(RPL_OMOTDSTART),
me.name, source_p->name);
2016-07-31 08:03:05 +02:00
for (auto it = oper_motd.contents.begin(); it != oper_motd.contents.end(); it++)
{
sendto_one(source_p, form_str(RPL_OMOTD),
2016-07-31 08:03:05 +02:00
me.name, source_p->name, it->c_str());
}
sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
me.name, source_p->name);
}