2 * IRC - Internet Relay Chat, ircd/motd.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
5 * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
7 * See file AUTHORS in IRC package for additional names of
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 1, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * @brief Message-of-the-day manipulation implementation.
35 #include "ircd_alloc.h"
36 #include "ircd_features.h"
38 #include "ircd_reply.h"
39 #include "ircd_string.h"
50 /* #include <assert.h> -- Now using assert in ircd_log.h */
56 /** Global list of messages of the day. */
58 struct Motd* local; /**< Local MOTD. */
59 struct Motd* remote; /**< Remote MOTD. */
60 struct Motd* other; /**< MOTDs specified in configuration file. */
61 struct Motd* freelist; /**< Currently unused Motd structs. */
62 struct MotdCache* cachelist; /**< List of MotdCache entries. */
63 } MotdList = { 0, 0, 0, 0, 0 };
65 /** Create a struct Motd and initialize it.
66 * @param[in] hostmask Hostmask (or connection class name) to filter on.
67 * @param[in] path Path to MOTD file.
68 * @param[in] maxcount Maximum number of lines permitted for MOTD.
71 motd_create(const char *hostmask, const char *path, int maxcount)
77 /* allocate memory and initialize the structure */
78 if (MotdList.freelist)
80 tmp = MotdList.freelist;
81 MotdList.freelist = tmp->next;
83 tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
87 tmp->type = MOTD_UNIVERSAL;
88 else if (find_class(hostmask))
89 tmp->type = MOTD_CLASS;
90 else if (ipmask_parse(hostmask, &tmp->address, &tmp->addrbits))
91 tmp->type = MOTD_IPMASK;
93 tmp->type = MOTD_HOSTMASK;
96 DupString(tmp->hostmask, hostmask);
98 DupString(tmp->path, path);
99 tmp->maxcount = maxcount;
105 /** This function reads a motd out of a file (if needed) and caches it.
106 * If a matching cache entry already exists, reuse it. Otherwise,
107 * allocate and populate a new MotdCache for it.
108 * @param[in] motd Specification for MOTD file.
109 * @return Matching MotdCache entry.
111 static struct MotdCache *
112 motd_cache(struct Motd *motd)
115 struct MotdCache* cache;
117 char line[MOTD_LINESIZE + 2]; /* \r\n */
122 assert(0 != motd->path);
127 /* try to find it in the list of cached files... */
128 for (cache = MotdList.cachelist; cache; cache = cache->next) {
129 if (!strcmp(cache->path, motd->path) &&
130 cache->maxcount == motd->maxcount) { /* found one... */
131 cache->ref++; /* increase reference count... */
132 motd->cache = cache; /* remember cache... */
133 return motd->cache; /* return it */
137 /* gotta read in the file, now */
138 if (!(file = fbopen(motd->path, "r"))) {
139 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
144 /* need the file's modification time */
145 if (-1 == fbstat(&sb, file)) {
150 /* Ok, allocate a structure; we'll realloc later to trim memory */
151 cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
152 (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
155 DupString(cache->path, motd->path);
156 cache->maxcount = motd->maxcount;
158 cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
161 while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
162 /* copy over line, stopping when we overflow or hit line end */
163 for (tmp = line, i = 0;
164 i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
166 cache->motd[cache->count][i] = *tmp;
167 cache->motd[cache->count][i] = '\0';
172 fbclose(file); /* close the file */
174 /* trim memory usage a little */
175 motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
176 (MOTD_LINESIZE * (cache->count - 1)));
177 memcpy(motd->cache, cache, sizeof(struct MotdCache) +
178 (MOTD_LINESIZE * (cache->count - 1)));
181 /* now link it in... */
182 motd->cache->next = MotdList.cachelist;
183 motd->cache->prev_p = &MotdList.cachelist;
184 if (MotdList.cachelist)
185 MotdList.cachelist->prev_p = &motd->cache->next;
186 MotdList.cachelist = motd->cache;
191 /** Clear and dereference the Motd::cache element of \a motd.
192 * If the MotdCache::ref count goes to zero, free it.
193 * @param[in] motd MOTD to uncache.
196 motd_decache(struct Motd *motd)
198 struct MotdCache* cache;
202 if (!(cache = motd->cache)) /* we can be called for records with no cache */
205 motd->cache = 0; /* zero the cache */
207 if (!--cache->ref) { /* reduce reference count... */
208 if (cache->next) /* ref is 0, delink from list and free */
209 cache->next->prev_p = cache->prev_p;
210 *cache->prev_p = cache->next;
212 MyFree(cache->path); /* free path info... */
214 MyFree(cache); /* very simple for a reason... */
218 /** Deallocate a MOTD structure.
219 * If it has cached content, uncache it.
220 * @param[in] motd MOTD to destroy.
223 motd_destroy(struct Motd *motd)
227 MyFree(motd->path); /* we always must have a path */
228 MyFree(motd->hostmask);
229 if (motd->cache) /* drop the cache */
232 motd->next = MotdList.freelist;
233 MotdList.freelist = motd;
236 /** Find the first matching MOTD block for a user.
237 * If the user is remote, always use remote MOTD.
238 * Otherwise, if there is a hostmask- or class-based MOTD that matches
240 * Otherwise, use the local MOTD.
241 * @param[in] cptr Client to find MOTD for.
242 * @return Pointer to first matching MOTD for the client.
245 motd_lookup(struct Client *cptr)
248 char *c_class = NULL;
252 if (!MyUser(cptr)) /* not my user, always return remote motd */
253 return MotdList.remote;
255 c_class = get_client_class(cptr);
256 assert(c_class != NULL);
258 /* check the motd blocks first */
259 for (ptr = MotdList.other; ptr; ptr = ptr->next)
261 if (ptr->type == MOTD_CLASS
262 && !match(ptr->hostmask, c_class))
264 else if (ptr->type == MOTD_HOSTMASK
265 && !match(ptr->hostmask, cli_sockhost(cptr)))
267 else if (ptr->type == MOTD_IPMASK
268 && ipmask_check(&cli_ip(cptr), &ptr->address, ptr->addrbits))
272 return MotdList.local; /* Ok, return the default motd */
275 /** Send the content of a MotdCache to a user.
276 * If \a cache is NULL, simply send ERR_NOMOTD to the client.
277 * @param[in] cptr Client to send MOTD to.
278 * @param[in] cache MOTD body to send to client.
281 motd_forward(struct Client *cptr, struct MotdCache *cache)
287 if (!cache) /* no motd to send */
288 return send_reply(cptr, ERR_NOMOTD);
291 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
292 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
293 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
294 cache->modtime.tm_mday, cache->modtime.tm_hour,
295 cache->modtime.tm_min);
297 for (i = 0; i < cache->count; i++)
298 send_reply(cptr, RPL_MOTD, cache->motd[i]);
300 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
303 /** Find the MOTD for a client and send it.
304 * @param[in] cptr Client being greeted.
307 motd_send(struct Client* cptr)
311 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
314 /** Send the signon MOTD to a user.
315 * If FEAT_NODEFAULTMOTD is true and a matching MOTD exists for the
316 * user, direct the client to type /MOTD to read it. Otherwise, call
317 * motd_forward() for the user.
318 * @param[in] cptr Client that has just connected.
321 motd_signon(struct Client* cptr)
323 struct MotdCache *cache;
324 const char *banner = NULL;
326 cache = motd_cache(motd_lookup(cptr));
328 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
329 motd_forward(cptr, cache);
331 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
332 if ((banner = feature_str(FEAT_MOTD_BANNER)))
333 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
334 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
335 "AUP before continuing using this service.\002");
336 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
337 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
338 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
339 cache->modtime.tm_hour, cache->modtime.tm_min);
340 send_reply(cptr, RPL_ENDOFMOTD);
344 /** Clear all cached MOTD bodies.
345 * The local and remote MOTDs are re-cached immediately.
352 motd_decache(MotdList.local); /* decache local and remote MOTDs */
353 motd_decache(MotdList.remote);
355 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
358 /* now recache local and remote MOTDs */
359 motd_cache(MotdList.local);
360 motd_cache(MotdList.remote);
363 /** Re-cache the local and remote MOTDs.
364 * If they already exist, they are deallocated first.
369 if (MotdList.local) /* destroy old local... */
370 motd_destroy(MotdList.local);
372 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
373 motd_cache(MotdList.local); /* init local and cache it */
375 if (MotdList.remote) /* destroy old remote... */
376 motd_destroy(MotdList.remote);
378 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
379 motd_cache(MotdList.remote); /* init remote and cache it */
383 * @param[in] hostmask Hostmask (or connection class name) to send this to.
384 * @param[in] path Pathname of file to send.
387 motd_add(const char *hostmask, const char *path)
391 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
393 tmp->next = MotdList.other; /* link it into the list */
394 MotdList.other = tmp;
397 /** Clear out all MOTDs.
398 * Compared to motd_recache(), this destroys all hostmask- or
399 * class-based MOTDs rather than simply uncaching them.
400 * Re-cache the local and remote MOTDs.
405 struct Motd *ptr, *next;
407 motd_decache(MotdList.local); /* decache local and remote MOTDs */
408 motd_decache(MotdList.remote);
410 if (MotdList.other) /* destroy other MOTDs */
411 for (ptr = MotdList.other; ptr; ptr = next)
419 /* now recache local and remote MOTDs */
420 motd_cache(MotdList.local);
421 motd_cache(MotdList.remote);
424 /** Report list of non-default MOTDs.
425 * @param[in] to Client requesting statistics.
426 * @param[in] sd Stats descriptor for request (ignored).
427 * @param[in] param Extra parameter from user (ignored).
430 motd_report(struct Client *to, const struct StatDesc *sd, char *param)
434 for (ptr = MotdList.other; ptr; ptr = ptr->next)
435 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
436 ptr->hostmask, ptr->path);
439 /** Report MOTD memory usage to a client.
440 * @param[in] cptr Client requesting memory usage.
443 motd_memory_count(struct Client *cptr)
446 struct MotdCache *cache;
447 unsigned int mt = 0, /* motd count */
448 mtm = 0, /* memory consumed by motd */
449 mtc = 0, /* motd cache count */
450 mtcm = 0, /* memory consumed by motd cache */
451 mtf = 0; /* motd free list count */
455 mtm += sizeof(struct Motd);
456 mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
462 mtm += sizeof(struct Motd);
463 mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
466 for (ptr = MotdList.other; ptr; ptr = ptr->next)
469 mtm += sizeof(struct Motd);
470 mtm += ptr->path ? (strlen(ptr->path) + 1) : 0;
473 for (cache = MotdList.cachelist; cache; cache = cache->next)
476 mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
479 if (MotdList.freelist)
480 for (ptr = MotdList.freelist; ptr; ptr = ptr->next)
483 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
484 ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)",
485 mt, mtm, mtc, mtcm, mtf, (mtf * sizeof(struct Motd)));