*
* $Id$
*/
+#include "config.h"
+
#include "motd.h"
#include "class.h"
#include "client.h"
#include "fileio.h"
#include "ircd.h"
#include "ircd_alloc.h"
+#include "ircd_features.h"
#include "ircd_reply.h"
#include "ircd_string.h"
#include "match.h"
#include <sys/stat.h>
static struct {
- struct Motd* local;
- struct Motd* remote;
- struct Motd* other;
- struct Motd* freelist;
-} MotdList;
+ struct Motd* local;
+ struct Motd* remote;
+ struct Motd* other;
+ struct Motd* freelist;
+ struct MotdCache* cachelist;
+} MotdList = { 0, 0, 0, 0, 0 };
/* Create a struct Motd and initialize it */
static struct Motd *
{
struct Motd* tmp;
int type = MOTD_UNIVERSAL;
- const char* s;
-
+
assert(0 != path);
-
- if (hostmask) { /* figure out if it's a class or hostmask */
- type = MOTD_CLASS; /* all digits, convert to class */
-
- for (s = hostmask; *s; s++)
- if (!IsDigit(*s)) { /* not a digit, not a class... */
- type = MOTD_HOSTMASK;
- break;
- }
- }
-
+
+ if (hostmask != NULL && find_class(hostmask))
+ type = MOTD_CLASS;
+ else
+ type = MOTD_HOSTMASK;
/* allocate memory and initialize the structure */
- if (MotdList.freelist) {
+ if (MotdList.freelist)
+ {
tmp = MotdList.freelist;
MotdList.freelist = tmp->next;
} else
tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
-
+
tmp->next = 0;
tmp->type = type;
-
- switch (type) {
- case MOTD_HOSTMASK:
- DupString(tmp->id.hostmask, hostmask);
- break;
-
- case MOTD_CLASS:
- tmp->id.class = atoi(hostmask);
- break;
- }
-
+
+ if (hostmask != NULL)
+ DupString(tmp->hostmask, hostmask);
+
DupString(tmp->path, path);
tmp->maxcount = maxcount;
tmp->cache = 0;
if (motd->cache)
return motd->cache;
+ /* try to find it in the list of cached files... */
+ for (cache = MotdList.cachelist; cache; cache = cache->next) {
+ if (!strcmp(cache->path, motd->path) &&
+ cache->maxcount == motd->maxcount) { /* found one... */
+ cache->ref++; /* increase reference count... */
+ motd->cache = cache; /* remember cache... */
+ return motd->cache; /* return it */
+ }
+ }
+
/* gotta read in the file, now */
if (!(file = fbopen(motd->path, "r"))) {
Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
(MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
+ cache->ref = 1;
+ DupString(cache->path, motd->path);
+ cache->maxcount = motd->maxcount;
+
cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
cache->count = 0;
- while (cache->count < motd->maxcount && fbgets(line, sizeof(line), file)) {
+ while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
/* copy over line, stopping when we overflow or hit line end */
for (tmp = line, i = 0;
i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
fbclose(file); /* close the file */
/* trim memory usage a little */
- motd->cache = (struct MotdCache *)MyRealloc(cache, sizeof(struct MotdCache) +
- (MOTD_LINESIZE *
- (cache->count - 1)));
+ motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
+ (MOTD_LINESIZE * (cache->count - 1)));
+ memcpy(motd->cache, cache, sizeof(struct MotdCache) +
+ (MOTD_LINESIZE * (cache->count - 1)));
+ MyFree(cache);
+
+ /* now link it in... */
+ motd->cache->next = MotdList.cachelist;
+ motd->cache->prev_p = &MotdList.cachelist;
+ if (MotdList.cachelist)
+ MotdList.cachelist->prev_p = &motd->cache->next;
+ MotdList.cachelist = motd->cache;
return motd->cache;
}
motd->cache = 0; /* zero the cache */
- MyFree(cache); /* very simple for a reason... */
+ if (!--cache->ref) { /* reduce reference count... */
+ if (cache->next) /* ref is 0, delink from list and free */
+ cache->next->prev_p = cache->prev_p;
+ *cache->prev_p = cache->next;
+
+ MyFree(cache->path); /* free path info... */
+
+ MyFree(cache); /* very simple for a reason... */
+ }
}
/* This function destroys a struct Motd, destroying the cache if needed */
assert(0 != motd);
MyFree(motd->path); /* we always must have a path */
- if (motd->type == MOTD_HOSTMASK) /* free a host mask if any */
- MyFree(motd->id.hostmask);
+ MyFree(motd->hostmask);
if (motd->cache) /* drop the cache */
motd_decache(motd);
motd_lookup(struct Client *cptr)
{
struct Motd *ptr;
- int class = -1;
+ char *class = NULL;
assert(0 != cptr);
class = get_client_class(cptr);
- /* check the T-lines first */
- for (ptr = MotdList.other; ptr; ptr = ptr->next) {
- if (ptr->type == MOTD_CLASS && ptr->id.class == class)
+ /* check the motd blocks first */
+ for (ptr = MotdList.other; ptr; ptr = ptr->next)
+ {
+ if (ptr->type == MOTD_CLASS &&
+ !match(ptr->hostmask, class))
return ptr;
- else if (ptr->type == MOTD_HOSTMASK &&
- !match(ptr->id.hostmask, cli_sockhost(cptr)))
+ else if (ptr->type == MOTD_HOSTMASK && class != NULL &&
+ !match(ptr->hostmask, cli_sockhost(cptr)))
return ptr;
}
motd_signon(struct Client* cptr)
{
struct MotdCache *cache;
+ const char *banner = NULL;
cache = motd_cache(motd_lookup(cptr));
-#ifdef NODEFAULTMOTD
- send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
- send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the AUP "
- "before continuing using this service.\002");
- send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was last "
- "changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
- cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
- cache->modtime.tm_hour, cache->modtime.tm_min);
- send_reply(cptr, RPL_ENDOFMOTD);
-#else
- motd_forward(cptr, cache);
-#endif
+ if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
+ motd_forward(cptr, cache);
+ else {
+ send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
+ if ((banner = feature_str(FEAT_MOTD_BANNER)))
+ send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
+ send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
+ "AUP before continuing using this service.\002");
+ send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
+ "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
+ cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
+ cache->modtime.tm_hour, cache->modtime.tm_min);
+ send_reply(cptr, RPL_ENDOFMOTD);
+ }
}
/* motd_recache causes all the MOTD caches to be cleared */
void
motd_init(void)
{
- MotdList.local = motd_create(0, MPATH, MOTD_MAXLINES); /* init local */
- motd_cache(MotdList.local); /* and cache it */
+ if (MotdList.local) /* destroy old local... */
+ motd_destroy(MotdList.local);
- MotdList.remote = motd_create(0, RPATH, MOTD_MAXREMOTE); /* init remote */
- motd_cache(MotdList.remote); /* and cache it */
+ MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
+ motd_cache(MotdList.local); /* init local and cache it */
- MotdList.other = 0; /* no T-lines processed yet */
+ if (MotdList.remote) /* destroy old remote... */
+ motd_destroy(MotdList.remote);
+
+ MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
+ motd_cache(MotdList.remote); /* init remote and cache it */
}
/* This routine adds a MOTD */
motd_decache(MotdList.remote);
if (MotdList.other) /* destroy other MOTDs */
- for (ptr = MotdList.other; ptr; ptr = next) {
+ for (ptr = MotdList.other; ptr; ptr = next)
+ {
next = ptr->next;
motd_destroy(ptr);
}
{
struct Motd *ptr;
- for (ptr = MotdList.other; ptr; ptr = ptr->next) {
- if (ptr->type == MOTD_CLASS) /* class requires special handling */
- send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %d %s", ptr->id.class,
- ptr->path);
- else if (ptr->type == MOTD_HOSTMASK)
- send_reply(to, RPL_STATSTLINE, 'T', ptr->id.hostmask, ptr->path);
- }
+ for (ptr = MotdList.other; ptr; ptr = ptr->next)
+ send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
+ ptr->hostmask, ptr->path);
}