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
|
2016-08-14 01:42:12 +02:00
|
|
|
* Copyright (C) 2016 Charybdis development team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
2008-04-02 20:50:20 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
namespace cache = ircd::cache;
|
|
|
|
using namespace cache;
|
2008-04-02 20:50:20 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
static size_t untabify(char *dest, const char *src, size_t destlen);
|
2008-04-02 20:50:20 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
struct cache::file
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
std::string filename;
|
|
|
|
std::string shortname;
|
|
|
|
std::ifstream stream;
|
|
|
|
int flags;
|
|
|
|
std::vector<std::string> contents;
|
2008-04-02 20:50:20 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
file(const char *const &filename, const char *const &shortname, const int &flags);
|
|
|
|
file() = default;
|
2008-04-02 20:50:20 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
file &operator=(file &&) noexcept;
|
|
|
|
};
|
2008-06-28 09:22:43 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
file motd::user;
|
|
|
|
file motd::oper;
|
|
|
|
|
|
|
|
dict help::oper;
|
|
|
|
dict help::user;
|
|
|
|
|
|
|
|
char motd::user_motd_changed[MAX_DATE_STRING];
|
2008-06-28 09:22:43 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
|
|
|
|
/* init_cache()
|
2008-04-02 20:50:20 +02:00
|
|
|
*
|
2016-08-14 01:42:12 +02:00
|
|
|
* inputs -
|
|
|
|
* outputs -
|
|
|
|
* side effects - inits the file/line cache blockheaps, loads motds
|
2008-04-02 20:50:20 +02:00
|
|
|
*/
|
2016-07-31 08:03:05 +02:00
|
|
|
void
|
2016-08-14 01:42:12 +02:00
|
|
|
cache::init()
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
2016-08-31 08:25:25 +02:00
|
|
|
using namespace path;
|
2016-08-14 05:35:06 +02:00
|
|
|
|
|
|
|
motd::user = file(get(IRCD_MOTD), "ircd.motd", 0);
|
|
|
|
motd::oper = file(get(IRCD_OMOTD), "opers.motd", 0);
|
2008-04-02 20:50:20 +02:00
|
|
|
}
|
|
|
|
|
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
|
2016-08-14 01:42:12 +02:00
|
|
|
cache::help::load()
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
oper.clear();
|
|
|
|
user.clear();
|
2007-12-12 22:26:17 +01:00
|
|
|
|
2016-08-14 05:35:06 +02:00
|
|
|
DIR *dir(opendir(path::get(path::OPERHELP)));
|
2016-08-14 01:42:12 +02:00
|
|
|
if (!dir)
|
2007-01-25 07:40:21 +01:00
|
|
|
return;
|
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
struct dirent *ldirent(nullptr);
|
|
|
|
while ((ldirent = readdir(dir)) != nullptr)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
const auto &d_name(ldirent->d_name);
|
|
|
|
if (d_name[0] == '.')
|
2010-01-22 02:02:45 +01:00
|
|
|
continue;
|
2016-08-14 01:42:12 +02:00
|
|
|
|
|
|
|
char filename[PATH_MAX];
|
2016-08-14 05:35:06 +02:00
|
|
|
const auto &ophelp_path(path::get(path::OPERHELP));
|
|
|
|
snprintf(filename, sizeof(filename), "%s%c%s", ophelp_path, RB_PATH_SEPARATOR, d_name);
|
2016-08-14 01:42:12 +02:00
|
|
|
oper.emplace(d_name, std::make_shared<file>(filename, d_name, OPER));
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
closedir(dir);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-14 05:35:06 +02:00
|
|
|
dir = opendir(path::get(path::USERHELP));
|
2016-08-14 01:42:12 +02:00
|
|
|
if (dir == nullptr)
|
2007-01-25 07:40:21 +01:00
|
|
|
return;
|
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
while ((ldirent = readdir(dir)) != nullptr)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
const auto &d_name(ldirent->d_name);
|
|
|
|
if (d_name[0] == '.')
|
2010-01-22 02:02:45 +01:00
|
|
|
continue;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
char filename[PATH_MAX];
|
2016-08-14 05:35:06 +02:00
|
|
|
const auto &userhelp_path(path::get(path::USERHELP));
|
|
|
|
snprintf(filename, sizeof(filename), "%s%c%s", userhelp_path, RB_PATH_SEPARATOR, d_name);
|
2016-08-14 01:42:12 +02:00
|
|
|
|
|
|
|
#if defined(S_ISLNK) && defined(HAVE_LSTAT)
|
|
|
|
struct stat sb;
|
|
|
|
if (lstat(filename, &sb) < 0)
|
2008-06-28 09:48:30 +02:00
|
|
|
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
|
|
|
|
*/
|
2016-08-14 01:42:12 +02:00
|
|
|
if (S_ISLNK(sb.st_mode))
|
2008-06-28 09:48:30 +02:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
const auto it(oper.find(d_name));
|
|
|
|
if (it != end(oper))
|
2008-06-28 09:48:30 +02:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
auto &file(it->second);
|
|
|
|
file->flags |= USER;
|
2008-06-28 09:48:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 01:42:12 +02:00
|
|
|
#endif
|
2008-06-28 09:48:30 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
user.emplace(d_name, std::make_shared<file>(filename, d_name, USER));
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cache::motd::cache_user(void)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
2016-08-31 08:25:25 +02:00
|
|
|
const auto &path(path::get(path::IRCD_MOTD));
|
2016-08-14 05:35:06 +02:00
|
|
|
if (stat(path, &sb) == 0)
|
2016-08-14 01:42:12 +02:00
|
|
|
{
|
|
|
|
struct tm *const local_tm(localtime(&sb.st_mtime));
|
|
|
|
if (local_tm != nullptr)
|
|
|
|
{
|
|
|
|
snprintf(user_motd_changed, sizeof(user_motd_changed),
|
|
|
|
"%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-08-14 05:35:06 +02:00
|
|
|
user = file(path, "ircd.motd", 0);
|
2016-08-14 01:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cache::motd::cache_oper(void)
|
|
|
|
{
|
2016-08-31 08:25:25 +02:00
|
|
|
oper = cache::file(path::get(path::IRCD_OMOTD), "opers.motd", 0);
|
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
|
2016-08-25 13:57:23 +02:00
|
|
|
cache::motd::send_user(client &source)
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
2016-08-25 13:57:23 +02:00
|
|
|
const char *const myname(get_id(me, source));
|
|
|
|
const char *const nick(get_id(source, source));
|
2016-07-31 08:03:05 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
if (user.contents.empty())
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
2016-08-25 13:57:23 +02:00
|
|
|
sendto_one(&source, form_str(ERR_NOMOTD), myname, nick);
|
2008-04-02 20:50:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-25 13:57:23 +02:00
|
|
|
sendto_one(&source, form_str(RPL_MOTDSTART), myname, nick, me.name);
|
2008-04-02 20:50:20 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
for (const auto &it : user.contents)
|
2016-08-25 13:57:23 +02:00
|
|
|
sendto_one(&source, form_str(RPL_MOTD), myname, nick, it.c_str());
|
2008-04-02 20:50:20 +02:00
|
|
|
|
2016-08-25 13:57:23 +02:00
|
|
|
sendto_one(&source, form_str(RPL_ENDOFMOTD), myname, nick);
|
2008-04-02 20:50:20 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
/* send_oper_motd()
|
|
|
|
*
|
|
|
|
* inputs - client to send motd to
|
|
|
|
* outputs - client is sent oper motd if exists
|
|
|
|
* side effects -
|
|
|
|
*/
|
2008-04-02 20:50:20 +02:00
|
|
|
void
|
2016-08-25 13:57:23 +02:00
|
|
|
cache::motd::send_oper(client &source)
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
if (oper.contents.empty())
|
|
|
|
return;
|
|
|
|
|
2016-08-25 13:57:23 +02:00
|
|
|
sendto_one(&source, form_str(RPL_OMOTDSTART), me.name, source.name);
|
2016-08-14 01:42:12 +02:00
|
|
|
|
|
|
|
for (const auto &it : motd::oper.contents)
|
2016-08-25 13:57:23 +02:00
|
|
|
sendto_one(&source, form_str(RPL_OMOTD), me.name, source.name, it.c_str());
|
2016-08-14 01:42:12 +02:00
|
|
|
|
2016-08-25 13:57:23 +02:00
|
|
|
sendto_one(&source, form_str(RPL_ENDOFOMOTD), me.name, source.name);
|
2016-08-14 01:42:12 +02:00
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
/* ircd::cache::file::file()
|
|
|
|
*
|
|
|
|
* inputs - file to cache, files "shortname", flags to set
|
|
|
|
* outputs - none
|
|
|
|
* side effects - cachefile.contents is populated with the lines from the file
|
|
|
|
*/
|
|
|
|
file::file(const char *const &filename,
|
|
|
|
const char *const &shortname,
|
|
|
|
const int &flags)
|
|
|
|
:filename(filename)
|
|
|
|
,shortname(shortname)
|
|
|
|
,stream(filename)
|
|
|
|
,flags(flags)
|
|
|
|
,contents([this]
|
|
|
|
{
|
|
|
|
std::vector<std::string> contents;
|
|
|
|
while (stream.good())
|
2008-04-02 20:50:20 +02:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
char line[BUFSIZE], *p;
|
|
|
|
stream.getline(line, sizeof(line));
|
|
|
|
if ((p = strpbrk(line, "\r\n")) != nullptr)
|
|
|
|
*p = '\0';
|
2008-04-02 20:50:20 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
char untabline[BUFSIZE];
|
|
|
|
untabify(untabline, line, sizeof(untabline));
|
|
|
|
contents.emplace_back(untabline);
|
2014-03-03 05:25:47 +01:00
|
|
|
}
|
2016-07-31 08:03:05 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
stream.close();
|
|
|
|
return contents;
|
|
|
|
}())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
file &file::operator=(file &&other)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
filename = std::move(other.filename);
|
|
|
|
shortname = std::move(other.shortname);
|
|
|
|
flags = std::move(other.flags);
|
|
|
|
contents = std::move(other.contents);
|
|
|
|
return *this;
|
2008-04-02 20:50:20 +02:00
|
|
|
}
|
2008-04-03 01:16:49 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
uint
|
|
|
|
cache::flags(const file &file)
|
|
|
|
{
|
|
|
|
return file.flags;
|
|
|
|
}
|
2008-04-03 01:16:49 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
const std::string &
|
|
|
|
cache::name(const file &file)
|
2008-04-03 01:16:49 +02:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
return file.shortname;
|
|
|
|
}
|
2008-04-03 01:16:49 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
const std::vector<std::string> &
|
|
|
|
cache::contents(const file &file)
|
|
|
|
{
|
|
|
|
return file.contents;
|
|
|
|
}
|
2008-04-03 01:16:49 +02:00
|
|
|
|
2016-08-14 01:42:12 +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;
|
2008-04-03 01:16:49 +02:00
|
|
|
|
2016-08-14 01:42:12 +02:00
|
|
|
while(*s != '\0' && x < destlen - 1)
|
2008-04-03 01:16:49 +02:00
|
|
|
{
|
2016-08-14 01:42:12 +02:00
|
|
|
if(*s == '\t')
|
|
|
|
{
|
|
|
|
for(i = 0; i < 8 && x < destlen - 1; i++, x++, d++)
|
|
|
|
*d = ' ';
|
|
|
|
s++;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
*d++ = *s++;
|
|
|
|
x++;
|
|
|
|
}
|
2008-04-03 01:16:49 +02:00
|
|
|
}
|
2016-08-14 01:42:12 +02:00
|
|
|
*d = '\0';
|
|
|
|
return x;
|
2008-04-03 01:16:49 +02:00
|
|
|
}
|