X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=ircd%2Fmotd.c;h=bdad75eeb5bb5c20d7d3679b0dcc36461b2c4341;hb=refs%2Fheads%2Fupstream;hp=2dc5daaafc497eb11a756ab6b72e3a09b5bfc112;hpb=3bad42600acc8707336aa73cd3e7bcb9a89fbe74;p=ircu2.10.12-pk.git diff --git a/ircd/motd.c b/ircd/motd.c index 2dc5daa..bdad75e 100644 --- a/ircd/motd.c +++ b/ircd/motd.c @@ -20,9 +20,13 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * $Id$ */ +/** @file + * @brief Message-of-the-day manipulation implementation. + * @version $Id$ + */ +#include "config.h" + #include "motd.h" #include "class.h" #include "client.h" @@ -30,6 +34,7 @@ #include "ircd.h" #include "ircd_alloc.h" #include "ircd_features.h" +#include "ircd_log.h" #include "ircd_reply.h" #include "ircd_string.h" #include "match.h" @@ -39,60 +44,58 @@ #include "s_conf.h" #include "s_debug.h" #include "s_user.h" +#include "s_stats.h" #include "send.h" -#include +/* #include -- Now using assert in ircd_log.h */ #include #include #include #include +/** Global list of messages of the day. */ static struct { - struct Motd* local; - struct Motd* remote; - struct Motd* other; - struct Motd* freelist; -} MotdList; - -/* Create a struct Motd and initialize it */ + struct Motd* local; /**< Local MOTD. */ + struct Motd* remote; /**< Remote MOTD. */ + struct Motd* other; /**< MOTDs specified in configuration file. */ + struct Motd* freelist; /**< Currently unused Motd structs. */ + struct MotdCache* cachelist; /**< List of MotdCache entries. */ +} MotdList = { 0, 0, 0, 0, 0 }; + +/** Create a struct Motd and initialize it. + * @param[in] hostmask Hostmask (or connection class name) to filter on. + * @param[in] path Path to MOTD file. + * @param[in] maxcount Maximum number of lines permitted for MOTD. + */ static struct Motd * motd_create(const char *hostmask, const char *path, int maxcount) { 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; - } - } - /* 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; + if (hostmask == NULL) + tmp->type = MOTD_UNIVERSAL; + else if (find_class(hostmask)) + tmp->type = MOTD_CLASS; + else if (ipmask_parse(hostmask, &tmp->address, &tmp->addrbits)) + tmp->type = MOTD_IPMASK; + else + tmp->type = MOTD_HOSTMASK; - case MOTD_CLASS: - tmp->id.class = atoi(hostmask); - break; - } + if (hostmask != NULL) + DupString(tmp->hostmask, hostmask); + else + tmp->hostmask = NULL; DupString(tmp->path, path); tmp->maxcount = maxcount; @@ -101,7 +104,12 @@ motd_create(const char *hostmask, const char *path, int maxcount) return tmp; } -/* This function reads a motd out of a file (if needed) and caches it */ +/** This function reads a motd out of a file (if needed) and caches it. + * If a matching cache entry already exists, reuse it. Otherwise, + * allocate and populate a new MotdCache for it. + * @param[in] motd Specification for MOTD file. + * @return Matching MotdCache entry. + */ static struct MotdCache * motd_cache(struct Motd *motd) { @@ -118,6 +126,16 @@ motd_cache(struct Motd *motd) 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, @@ -135,10 +153,14 @@ motd_cache(struct Motd *motd) 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'; @@ -152,13 +174,26 @@ motd_cache(struct Motd *motd) 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; } +/** Clear and dereference the Motd::cache element of \a motd. + * If the MotdCache::ref count goes to zero, free it. + * @param[in] motd MOTD to uncache. + */ static void motd_decache(struct Motd *motd) { @@ -171,18 +206,28 @@ motd_decache(struct Motd *motd) 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 */ +/** Deallocate a MOTD structure. + * If it has cached content, uncache it. + * @param[in] motd MOTD to destroy. + */ static void motd_destroy(struct Motd *motd) { 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); @@ -190,35 +235,50 @@ motd_destroy(struct Motd *motd) MotdList.freelist = motd; } -/* We use this routine to look up the struct Motd to send to any given - * user. +/** Find the first matching MOTD block for a user. + * If the user is remote, always use remote MOTD. + * Otherwise, if there is a hostmask- or class-based MOTD that matches + * the user, use it. + * Otherwise, use the local MOTD. + * @param[in] cptr Client to find MOTD for. + * @return Pointer to first matching MOTD for the client. */ static struct Motd * motd_lookup(struct Client *cptr) { struct Motd *ptr; - int class = -1; + char *c_class = NULL; assert(0 != cptr); if (!MyUser(cptr)) /* not my user, always return remote motd */ return MotdList.remote; - class = get_client_class(cptr); + c_class = get_client_class(cptr); + assert(c_class != NULL); - /* 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, c_class)) + return ptr; + else if (ptr->type == MOTD_HOSTMASK + && !match(ptr->hostmask, cli_sockhost(cptr))) return ptr; - else if (ptr->type == MOTD_HOSTMASK && - !match(ptr->id.hostmask, cli_sockhost(cptr))) + else if (ptr->type == MOTD_IPMASK + && ipmask_check(&cli_ip(cptr), &ptr->address, ptr->addrbits)) return ptr; } return MotdList.local; /* Ok, return the default motd */ } -/* Here is a routine that takes a MotdCache and sends it to a user */ +/** Send the content of a MotdCache to a user. + * If \a cache is NULL, simply send ERR_NOMOTD to the client. + * @param[in] cptr Client to send MOTD to. + * @param[in] cache MOTD body to send to client. + */ static int motd_forward(struct Client *cptr, struct MotdCache *cache) { @@ -242,7 +302,9 @@ motd_forward(struct Client *cptr, struct MotdCache *cache) return send_reply(cptr, RPL_ENDOFMOTD); /* end */ } -/* This routine is used to send the MOTD off to a user. */ +/** Find the MOTD for a client and send it. + * @param[in] cptr Client being greeted. + */ int motd_send(struct Client* cptr) { @@ -251,18 +313,26 @@ motd_send(struct Client* cptr) return motd_forward(cptr, motd_cache(motd_lookup(cptr))); } -/* This routine sends the MOTD or something to newly-registered users. */ +/** Send the signon MOTD to a user. + * If FEAT_NODEFAULTMOTD is true and a matching MOTD exists for the + * user, direct the client to type /MOTD to read it. Otherwise, call + * motd_forward() for the user. + * @param[in] cptr Client that has just connected. + */ void motd_signon(struct Client* cptr) { struct MotdCache *cache; + const char *banner = NULL; cache = motd_cache(motd_lookup(cptr)); - if (!feature_bool(FEAT_NODEFAULTMOTD)) + 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 " @@ -273,7 +343,9 @@ motd_signon(struct Client* cptr) } } -/* motd_recache causes all the MOTD caches to be cleared */ +/** Clear all cached MOTD bodies. + * The local and remote MOTDs are re-cached immediately. + */ void motd_recache(void) { @@ -290,22 +362,29 @@ motd_recache(void) motd_cache(MotdList.remote); } -/* motd_init initializes the MOTD routines, including reading the - * ircd.motd and remote.motd files into cache +/** Re-cache the local and remote MOTDs. + * If they already exist, they are deallocated first. */ void motd_init(void) { + if (MotdList.local) /* destroy old local... */ + motd_destroy(MotdList.local); + MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES); motd_cache(MotdList.local); /* init local and cache it */ + 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 */ - - MotdList.other = 0; /* no T-lines processed yet */ } -/* This routine adds a MOTD */ +/** Add a new MOTD. + * @param[in] hostmask Hostmask (or connection class name) to send this to. + * @param[in] path Pathname of file to send. + */ void motd_add(const char *hostmask, const char *path) { @@ -317,7 +396,11 @@ motd_add(const char *hostmask, const char *path) MotdList.other = tmp; } -/* This routine clears the list of MOTDs */ +/** Clear out all MOTDs. + * Compared to motd_recache(), this destroys all hostmask- or + * class-based MOTDs rather than simply uncaching them. + * Re-cache the local and remote MOTDs. + */ void motd_clear(void) { @@ -327,7 +410,8 @@ motd_clear(void) 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); } @@ -339,17 +423,66 @@ motd_clear(void) motd_cache(MotdList.remote); } -/* This is called to report T-lines */ +/** Report list of non-default MOTDs. + * @param[in] to Client requesting statistics. + * @param[in] sd Stats descriptor for request (ignored). + * @param[in] param Extra parameter from user (ignored). + */ void -motd_report(struct Client *to) +motd_report(struct Client *to, const struct StatDesc *sd, char *param) { 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); +} + +/** Report MOTD memory usage to a client. + * @param[in] cptr Client requesting memory usage. + */ +void +motd_memory_count(struct Client *cptr) +{ + struct Motd *ptr; + struct MotdCache *cache; + unsigned int mt = 0, /* motd count */ + mtc = 0, /* motd cache count */ + mtf = 0; /* motd free list count */ + size_t mtm = 0, /* memory consumed by motd */ + mtcm = 0; /* memory consumed by motd cache */ + if (MotdList.local) + { + mt++; + mtm += sizeof(struct Motd); + mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0; } + + if (MotdList.remote) + { + mt++; + mtm += sizeof(struct Motd); + mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0; + } + + for (ptr = MotdList.other; ptr; ptr = ptr->next) + { + mt++; + mtm += sizeof(struct Motd); + mtm += ptr->path ? (strlen(ptr->path) + 1) : 0; + } + + for (cache = MotdList.cachelist; cache; cache = cache->next) + { + mtc++; + mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1)); + } + + if (MotdList.freelist) + for (ptr = MotdList.freelist; ptr; ptr = ptr->next) + mtf++; + + send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, + ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)", + mt, mtm, mtc, mtcm, mtf, (mtf * sizeof(struct Motd))); }