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"
37 #include "ircd_reply.h"
38 #include "ircd_string.h"
55 /** Global list of messages of the day. */
57 struct Motd* local; /**< Local MOTD. */
58 struct Motd* remote; /**< Remote MOTD. */
59 struct Motd* other; /**< MOTDs specified in configuration file. */
60 struct Motd* freelist; /**< Currently unused Motd structs. */
61 struct MotdCache* cachelist; /**< List of MotdCache entries. */
62 } MotdList = { 0, 0, 0, 0, 0 };
64 /** Create a struct Motd and initialize it.
65 * @param[in] hostmask Hostmask (or connection class name) to filter on.
66 * @param[in] path Path to MOTD file.
67 * @param[in] maxcount Maximum number of lines permitted for MOTD.
70 motd_create(const char *hostmask, const char *path, int maxcount)
76 /* allocate memory and initialize the structure */
77 if (MotdList.freelist)
79 tmp = MotdList.freelist;
80 MotdList.freelist = tmp->next;
82 tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
86 tmp->type = MOTD_UNIVERSAL;
87 else if (find_class(hostmask))
88 tmp->type = MOTD_CLASS;
89 else if (ipmask_parse(hostmask, &tmp->address, &tmp->addrbits))
90 tmp->type = MOTD_IPMASK;
92 tmp->type = MOTD_HOSTMASK;
95 DupString(tmp->hostmask, hostmask);
97 DupString(tmp->path, path);
98 tmp->maxcount = maxcount;
104 /** This function reads a motd out of a file (if needed) and caches it.
105 * If a matching cache entry already exists, reuse it. Otherwise,
106 * allocate and populate a new MotdCache for it.
107 * @param[in] motd Specification for MOTD file.
108 * @return Matching MotdCache entry.
110 static struct MotdCache *
111 motd_cache(struct Motd *motd)
114 struct MotdCache* cache;
116 char line[MOTD_LINESIZE + 2]; /* \r\n */
121 assert(0 != motd->path);
126 /* try to find it in the list of cached files... */
127 for (cache = MotdList.cachelist; cache; cache = cache->next) {
128 if (!strcmp(cache->path, motd->path) &&
129 cache->maxcount == motd->maxcount) { /* found one... */
130 cache->ref++; /* increase reference count... */
131 motd->cache = cache; /* remember cache... */
132 return motd->cache; /* return it */
136 /* gotta read in the file, now */
137 if (!(file = fbopen(motd->path, "r"))) {
138 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
143 /* need the file's modification time */
144 if (-1 == fbstat(&sb, file)) {
149 /* Ok, allocate a structure; we'll realloc later to trim memory */
150 cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
151 (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
154 DupString(cache->path, motd->path);
155 cache->maxcount = motd->maxcount;
157 cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
160 while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
161 /* copy over line, stopping when we overflow or hit line end */
162 for (tmp = line, i = 0;
163 i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
165 cache->motd[cache->count][i] = *tmp;
166 cache->motd[cache->count][i] = '\0';
171 fbclose(file); /* close the file */
173 /* trim memory usage a little */
174 motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
175 (MOTD_LINESIZE * (cache->count - 1)));
176 memcpy(motd->cache, cache, sizeof(struct MotdCache) +
177 (MOTD_LINESIZE * (cache->count - 1)));
180 /* now link it in... */
181 motd->cache->next = MotdList.cachelist;
182 motd->cache->prev_p = &MotdList.cachelist;
183 if (MotdList.cachelist)
184 MotdList.cachelist->prev_p = &motd->cache->next;
185 MotdList.cachelist = motd->cache;
190 /** Clear and dereference the Motd::cache element of \a motd.
191 * If the MotdCache::ref count goes to zero, free it.
192 * @param[in] motd MOTD to uncache.
195 motd_decache(struct Motd *motd)
197 struct MotdCache* cache;
201 if (!(cache = motd->cache)) /* we can be called for records with no cache */
204 motd->cache = 0; /* zero the cache */
206 if (!--cache->ref) { /* reduce reference count... */
207 if (cache->next) /* ref is 0, delink from list and free */
208 cache->next->prev_p = cache->prev_p;
209 *cache->prev_p = cache->next;
211 MyFree(cache->path); /* free path info... */
213 MyFree(cache); /* very simple for a reason... */
217 /** Deallocate a MOTD structure.
218 * If it has cached content, uncache it.
219 * @param[in] motd MOTD to destroy.
222 motd_destroy(struct Motd *motd)
226 MyFree(motd->path); /* we always must have a path */
227 MyFree(motd->hostmask);
228 if (motd->cache) /* drop the cache */
231 motd->next = MotdList.freelist;
232 MotdList.freelist = motd;
235 /** Find the first matching MOTD block for a user.
236 * If the user is remote, always use remote MOTD.
237 * Otherwise, if there is a hostmask- or class-based MOTD that matches
239 * Otherwise, use the local MOTD.
240 * @param[in] cptr Client to find MOTD for.
241 * @return Pointer to first matching MOTD for the client.
244 motd_lookup(struct Client *cptr)
247 char *c_class = NULL;
251 if (!MyUser(cptr)) /* not my user, always return remote motd */
252 return MotdList.remote;
254 c_class = get_client_class(cptr);
255 assert(c_class != NULL);
257 /* check the motd blocks first */
258 for (ptr = MotdList.other; ptr; ptr = ptr->next)
260 if (ptr->type == MOTD_CLASS
261 && !match(ptr->hostmask, c_class))
263 else if (ptr->type == MOTD_HOSTMASK
264 && !match(ptr->hostmask, cli_sockhost(cptr)))
266 else if (ptr->type == MOTD_IPMASK
267 && ipmask_check(&cli_ip(cptr), &ptr->address, ptr->addrbits))
271 return MotdList.local; /* Ok, return the default motd */
274 /** Send the content of a MotdCache to a user.
275 * If \a cache is NULL, simply send ERR_NOMOTD to the client.
276 * @param[in] cptr Client to send MOTD to.
277 * @param[in] cache MOTD body to send to client.
280 motd_forward(struct Client *cptr, struct MotdCache *cache)
286 if (!cache) /* no motd to send */
287 return send_reply(cptr, ERR_NOMOTD);
290 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
291 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
292 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
293 cache->modtime.tm_mday, cache->modtime.tm_hour,
294 cache->modtime.tm_min);
296 for (i = 0; i < cache->count; i++)
297 send_reply(cptr, RPL_MOTD, cache->motd[i]);
299 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
302 /** Find the MOTD for a client and send it.
303 * @param[in] cptr Client being greeted.
306 motd_send(struct Client* cptr)
310 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
313 /** Send the signon MOTD to a user.
314 * If FEAT_NODEFAULTMOTD is true and a matching MOTD exists for the
315 * user, direct the client to type /MOTD to read it. Otherwise, call
316 * motd_forward() for the user.
317 * @param[in] cptr Client that has just connected.
320 motd_signon(struct Client* cptr)
322 struct MotdCache *cache;
323 const char *banner = NULL;
325 cache = motd_cache(motd_lookup(cptr));
327 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
328 motd_forward(cptr, cache);
330 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
331 if ((banner = feature_str(FEAT_MOTD_BANNER)))
332 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
333 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
334 "AUP before continuing using this service.\002");
335 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
336 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
337 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
338 cache->modtime.tm_hour, cache->modtime.tm_min);
339 send_reply(cptr, RPL_ENDOFMOTD);
343 /** Clear all cached MOTD bodies.
344 * The local and remote MOTDs are re-cached immediately.
351 motd_decache(MotdList.local); /* decache local and remote MOTDs */
352 motd_decache(MotdList.remote);
354 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
357 /* now recache local and remote MOTDs */
358 motd_cache(MotdList.local);
359 motd_cache(MotdList.remote);
362 /** Re-cache the local and remote MOTDs.
363 * If they already exist, they are deallocated first.
368 if (MotdList.local) /* destroy old local... */
369 motd_destroy(MotdList.local);
371 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
372 motd_cache(MotdList.local); /* init local and cache it */
374 if (MotdList.remote) /* destroy old remote... */
375 motd_destroy(MotdList.remote);
377 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
378 motd_cache(MotdList.remote); /* init remote and cache it */
382 * @param[in] hostmask Hostmask (or connection class name) to send this to.
383 * @param[in] path Pathname of file to send.
386 motd_add(const char *hostmask, const char *path)
390 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
392 tmp->next = MotdList.other; /* link it into the list */
393 MotdList.other = tmp;
396 /** Clear out all MOTDs.
397 * Compared to motd_recache(), this destroys all hostmask- or
398 * class-based MOTDs rather than simply uncaching them.
399 * Re-cache the local and remote MOTDs.
404 struct Motd *ptr, *next;
406 motd_decache(MotdList.local); /* decache local and remote MOTDs */
407 motd_decache(MotdList.remote);
409 if (MotdList.other) /* destroy other MOTDs */
410 for (ptr = MotdList.other; ptr; ptr = next)
418 /* now recache local and remote MOTDs */
419 motd_cache(MotdList.local);
420 motd_cache(MotdList.remote);
423 /** Report list of non-default MOTDs.
424 * @param[in] to Client requesting statistics.
425 * @param[in] sd Stats descriptor for request (ignored).
426 * @param[in] param Extra parameter from user (ignored).
429 motd_report(struct Client *to, const struct StatDesc *sd, char *param)
433 for (ptr = MotdList.other; ptr; ptr = ptr->next)
434 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
435 ptr->hostmask, ptr->path);
438 /** Report MOTD memory usage to a client.
439 * @param[in] cptr Client requesting memory usage.
442 motd_memory_count(struct Client *cptr)
445 struct MotdCache *cache;
446 unsigned int mt = 0, /* motd count */
447 mtm = 0, /* memory consumed by motd */
448 mtc = 0, /* motd cache count */
449 mtcm = 0, /* memory consumed by motd cache */
450 mtf = 0; /* motd free list count */
454 mtm += sizeof(struct Motd);
455 mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
461 mtm += sizeof(struct Motd);
462 mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
465 for (ptr = MotdList.other; ptr; ptr = ptr->next)
468 mtm += sizeof(struct Motd);
469 mtm += ptr->path ? (strlen(ptr->path) + 1) : 0;
472 for (cache = MotdList.cachelist; cache; cache = cache->next)
475 mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
478 if (MotdList.freelist)
479 for (ptr = MotdList.freelist; ptr; ptr = ptr->next)
482 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
483 ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)",
484 mt, mtm, mtc, mtcm, mtf, (mtf * sizeof(struct Motd)));