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)
73 int type = MOTD_UNIVERSAL;
77 if (hostmask != NULL && find_class(hostmask))
81 /* allocate memory and initialize the structure */
82 if (MotdList.freelist)
84 tmp = MotdList.freelist;
85 MotdList.freelist = tmp->next;
87 tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
93 DupString(tmp->hostmask, hostmask);
95 DupString(tmp->path, path);
96 tmp->maxcount = maxcount;
102 /** This function reads a motd out of a file (if needed) and caches it.
103 * If a matching cache entry already exists, reuse it. Otherwise,
104 * allocate and populate a new MotdCache for it.
105 * @param[in] motd Specification for MOTD file.
106 * @return Matching MotdCache entry.
108 static struct MotdCache *
109 motd_cache(struct Motd *motd)
112 struct MotdCache* cache;
114 char line[MOTD_LINESIZE + 2]; /* \r\n */
119 assert(0 != motd->path);
124 /* try to find it in the list of cached files... */
125 for (cache = MotdList.cachelist; cache; cache = cache->next) {
126 if (!strcmp(cache->path, motd->path) &&
127 cache->maxcount == motd->maxcount) { /* found one... */
128 cache->ref++; /* increase reference count... */
129 motd->cache = cache; /* remember cache... */
130 return motd->cache; /* return it */
134 /* gotta read in the file, now */
135 if (!(file = fbopen(motd->path, "r"))) {
136 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
141 /* need the file's modification time */
142 if (-1 == fbstat(&sb, file)) {
147 /* Ok, allocate a structure; we'll realloc later to trim memory */
148 cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
149 (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
152 DupString(cache->path, motd->path);
153 cache->maxcount = motd->maxcount;
155 cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
158 while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
159 /* copy over line, stopping when we overflow or hit line end */
160 for (tmp = line, i = 0;
161 i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
163 cache->motd[cache->count][i] = *tmp;
164 cache->motd[cache->count][i] = '\0';
169 fbclose(file); /* close the file */
171 /* trim memory usage a little */
172 motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
173 (MOTD_LINESIZE * (cache->count - 1)));
174 memcpy(motd->cache, cache, sizeof(struct MotdCache) +
175 (MOTD_LINESIZE * (cache->count - 1)));
178 /* now link it in... */
179 motd->cache->next = MotdList.cachelist;
180 motd->cache->prev_p = &MotdList.cachelist;
181 if (MotdList.cachelist)
182 MotdList.cachelist->prev_p = &motd->cache->next;
183 MotdList.cachelist = motd->cache;
188 /** Clear and dereference the Motd::cache element of \a motd.
189 * If the MotdCache::ref count goes to zero, free it.
190 * @param[in] motd MOTD to uncache.
193 motd_decache(struct Motd *motd)
195 struct MotdCache* cache;
199 if (!(cache = motd->cache)) /* we can be called for records with no cache */
202 motd->cache = 0; /* zero the cache */
204 if (!--cache->ref) { /* reduce reference count... */
205 if (cache->next) /* ref is 0, delink from list and free */
206 cache->next->prev_p = cache->prev_p;
207 *cache->prev_p = cache->next;
209 MyFree(cache->path); /* free path info... */
211 MyFree(cache); /* very simple for a reason... */
215 /** Deallocate a MOTD structure.
216 * If it has cached content, uncache it.
217 * @param[in] motd MOTD to destroy.
220 motd_destroy(struct Motd *motd)
224 MyFree(motd->path); /* we always must have a path */
225 MyFree(motd->hostmask);
226 if (motd->cache) /* drop the cache */
229 motd->next = MotdList.freelist;
230 MotdList.freelist = motd;
233 /** Find the first matching MOTD block for a user.
234 * If the user is remote, always use remote MOTD.
235 * Otherwise, if there is a hostmask- or class-based MOTD that matches
237 * Otherwise, use the local MOTD.
238 * @param[in] cptr Client to find MOTD for.
239 * @return Pointer to first matching MOTD for the client.
242 motd_lookup(struct Client *cptr)
245 char *c_class = NULL;
249 if (!MyUser(cptr)) /* not my user, always return remote motd */
250 return MotdList.remote;
252 c_class = get_client_class(cptr);
254 /* check the motd blocks first */
255 for (ptr = MotdList.other; ptr; ptr = ptr->next)
257 if (ptr->type == MOTD_CLASS &&
258 !match(ptr->hostmask, c_class))
260 else if (ptr->type == MOTD_HOSTMASK && c_class != NULL &&
261 !match(ptr->hostmask, cli_sockhost(cptr)))
265 return MotdList.local; /* Ok, return the default motd */
268 /** Send the content of a MotdCache to a user.
269 * If \a cache is NULL, simply send ERR_NOMOTD to the client.
270 * @param[in] cptr Client to send MOTD to.
271 * @param[in] cache MOTD body to send to client.
274 motd_forward(struct Client *cptr, struct MotdCache *cache)
280 if (!cache) /* no motd to send */
281 return send_reply(cptr, ERR_NOMOTD);
284 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
285 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
286 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
287 cache->modtime.tm_mday, cache->modtime.tm_hour,
288 cache->modtime.tm_min);
290 for (i = 0; i < cache->count; i++)
291 send_reply(cptr, RPL_MOTD, cache->motd[i]);
293 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
296 /** Find the MOTD for a client and send it.
297 * @param[in] cptr Client being greeted.
300 motd_send(struct Client* cptr)
304 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
307 /** Send the signon MOTD to a user.
308 * If FEAT_NODEFAULTMOTD is true and a matching MOTD exists for the
309 * user, direct the client to type /MOTD to read it. Otherwise, call
310 * motd_forward() for the user.
311 * @param[in] cptr Client that has just connected.
314 motd_signon(struct Client* cptr)
316 struct MotdCache *cache;
317 const char *banner = NULL;
319 cache = motd_cache(motd_lookup(cptr));
321 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
322 motd_forward(cptr, cache);
324 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
325 if ((banner = feature_str(FEAT_MOTD_BANNER)))
326 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
327 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
328 "AUP before continuing using this service.\002");
329 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
330 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
331 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
332 cache->modtime.tm_hour, cache->modtime.tm_min);
333 send_reply(cptr, RPL_ENDOFMOTD);
337 /** Clear all cached MOTD bodies.
338 * The local and remote MOTDs are re-cached immediately.
345 motd_decache(MotdList.local); /* decache local and remote MOTDs */
346 motd_decache(MotdList.remote);
348 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
351 /* now recache local and remote MOTDs */
352 motd_cache(MotdList.local);
353 motd_cache(MotdList.remote);
356 /** Re-cache the local and remote MOTDs.
357 * If they already exist, they are deallocated first.
362 if (MotdList.local) /* destroy old local... */
363 motd_destroy(MotdList.local);
365 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
366 motd_cache(MotdList.local); /* init local and cache it */
368 if (MotdList.remote) /* destroy old remote... */
369 motd_destroy(MotdList.remote);
371 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
372 motd_cache(MotdList.remote); /* init remote and cache it */
376 * @param[in] hostmask Hostmask (or connection class name) to send this to.
377 * @param[in] path Pathname of file to send.
380 motd_add(const char *hostmask, const char *path)
384 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
386 tmp->next = MotdList.other; /* link it into the list */
387 MotdList.other = tmp;
390 /** Clear out all MOTDs.
391 * Compared to motd_recache(), this destroys all hostmask- or
392 * class-based MOTDs rather than simply uncaching them.
393 * Re-cache the local and remote MOTDs.
398 struct Motd *ptr, *next;
400 motd_decache(MotdList.local); /* decache local and remote MOTDs */
401 motd_decache(MotdList.remote);
403 if (MotdList.other) /* destroy other MOTDs */
404 for (ptr = MotdList.other; ptr; ptr = next)
412 /* now recache local and remote MOTDs */
413 motd_cache(MotdList.local);
414 motd_cache(MotdList.remote);
417 /** Report list of non-default MOTDs.
418 * @param[in] to Client requesting statistics.
419 * @param[in] sd Stats descriptor for request (ignored).
420 * @param[in] param Extra parameter from user (ignored).
423 motd_report(struct Client *to, const struct StatDesc *sd, char *param)
427 for (ptr = MotdList.other; ptr; ptr = ptr->next)
428 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
429 ptr->hostmask, ptr->path);
432 /** Report MOTD memory usage to a client.
433 * @param[in] cptr Client requesting memory usage.
436 motd_memory_count(struct Client *cptr)
439 struct MotdCache *cache;
440 unsigned int mt = 0, /* motd count */
441 mtm = 0, /* memory consumed by motd */
442 mtc = 0, /* motd cache count */
443 mtcm = 0, /* memory consumed by motd cache */
444 mtf = 0; /* motd free list count */
448 mtm += sizeof(struct Motd);
449 mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
455 mtm += sizeof(struct Motd);
456 mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
459 for (ptr = MotdList.other; ptr; ptr = ptr->next)
462 mtm += sizeof(struct Motd);
463 mtm += ptr->path ? (strlen(ptr->path) + 1) : 0;
466 for (cache = MotdList.cachelist; cache; cache = cache->next)
469 mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
472 if (MotdList.freelist)
473 for (ptr = MotdList.freelist; ptr; ptr = ptr->next)
476 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
477 ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)",
478 mt, mtm, mtc, mtcm, mtf, (mtf * sizeof(struct Motd)));