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.
33 #include "ircd_alloc.h"
34 #include "ircd_features.h"
35 #include "ircd_reply.h"
36 #include "ircd_string.h"
57 struct Motd* freelist;
58 struct MotdCache* cachelist;
59 } MotdList = { 0, 0, 0, 0, 0 };
61 /* Create a struct Motd and initialize it */
63 motd_create(const char *hostmask, const char *path, int maxcount)
66 int type = MOTD_UNIVERSAL;
70 if (hostmask != NULL && find_class(hostmask))
74 /* allocate memory and initialize the structure */
75 if (MotdList.freelist)
77 tmp = MotdList.freelist;
78 MotdList.freelist = tmp->next;
80 tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
86 DupString(tmp->hostmask, hostmask);
88 DupString(tmp->path, path);
89 tmp->maxcount = maxcount;
95 /* This function reads a motd out of a file (if needed) and caches it */
96 static struct MotdCache *
97 motd_cache(struct Motd *motd)
100 struct MotdCache* cache;
102 char line[MOTD_LINESIZE + 2]; /* \r\n */
107 assert(0 != motd->path);
112 /* try to find it in the list of cached files... */
113 for (cache = MotdList.cachelist; cache; cache = cache->next) {
114 if (!strcmp(cache->path, motd->path) &&
115 cache->maxcount == motd->maxcount) { /* found one... */
116 cache->ref++; /* increase reference count... */
117 motd->cache = cache; /* remember cache... */
118 return motd->cache; /* return it */
122 /* gotta read in the file, now */
123 if (!(file = fbopen(motd->path, "r"))) {
124 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
129 /* need the file's modification time */
130 if (-1 == fbstat(&sb, file)) {
135 /* Ok, allocate a structure; we'll realloc later to trim memory */
136 cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
137 (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
140 DupString(cache->path, motd->path);
141 cache->maxcount = motd->maxcount;
143 cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
146 while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
147 /* copy over line, stopping when we overflow or hit line end */
148 for (tmp = line, i = 0;
149 i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
151 cache->motd[cache->count][i] = *tmp;
152 cache->motd[cache->count][i] = '\0';
157 fbclose(file); /* close the file */
159 /* trim memory usage a little */
160 motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
161 (MOTD_LINESIZE * (cache->count - 1)));
162 memcpy(motd->cache, cache, sizeof(struct MotdCache) +
163 (MOTD_LINESIZE * (cache->count - 1)));
166 /* now link it in... */
167 motd->cache->next = MotdList.cachelist;
168 motd->cache->prev_p = &MotdList.cachelist;
169 if (MotdList.cachelist)
170 MotdList.cachelist->prev_p = &motd->cache->next;
171 MotdList.cachelist = motd->cache;
177 motd_decache(struct Motd *motd)
179 struct MotdCache* cache;
183 if (!(cache = motd->cache)) /* we can be called for records with no cache */
186 motd->cache = 0; /* zero the cache */
188 if (!--cache->ref) { /* reduce reference count... */
189 if (cache->next) /* ref is 0, delink from list and free */
190 cache->next->prev_p = cache->prev_p;
191 *cache->prev_p = cache->next;
193 MyFree(cache->path); /* free path info... */
195 MyFree(cache); /* very simple for a reason... */
199 /* This function destroys a struct Motd, destroying the cache if needed */
201 motd_destroy(struct Motd *motd)
205 MyFree(motd->path); /* we always must have a path */
206 MyFree(motd->hostmask);
207 if (motd->cache) /* drop the cache */
210 motd->next = MotdList.freelist;
211 MotdList.freelist = motd;
214 /* We use this routine to look up the struct Motd to send to any given
218 motd_lookup(struct Client *cptr)
221 char *c_class = NULL;
225 if (!MyUser(cptr)) /* not my user, always return remote motd */
226 return MotdList.remote;
228 c_class = get_client_class(cptr);
230 /* check the motd blocks first */
231 for (ptr = MotdList.other; ptr; ptr = ptr->next)
233 if (ptr->type == MOTD_CLASS &&
234 !match(ptr->hostmask, c_class))
236 else if (ptr->type == MOTD_HOSTMASK && c_class != NULL &&
237 !match(ptr->hostmask, cli_sockhost(cptr)))
241 return MotdList.local; /* Ok, return the default motd */
244 /* Here is a routine that takes a MotdCache and sends it to a user */
246 motd_forward(struct Client *cptr, struct MotdCache *cache)
252 if (!cache) /* no motd to send */
253 return send_reply(cptr, ERR_NOMOTD);
256 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
257 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
258 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
259 cache->modtime.tm_mday, cache->modtime.tm_hour,
260 cache->modtime.tm_min);
262 for (i = 0; i < cache->count; i++)
263 send_reply(cptr, RPL_MOTD, cache->motd[i]);
265 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
268 /* This routine is used to send the MOTD off to a user. */
270 motd_send(struct Client* cptr)
274 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
277 /* This routine sends the MOTD or something to newly-registered users. */
279 motd_signon(struct Client* cptr)
281 struct MotdCache *cache;
282 const char *banner = NULL;
284 cache = motd_cache(motd_lookup(cptr));
286 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
287 motd_forward(cptr, cache);
289 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
290 if ((banner = feature_str(FEAT_MOTD_BANNER)))
291 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
292 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
293 "AUP before continuing using this service.\002");
294 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
295 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
296 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
297 cache->modtime.tm_hour, cache->modtime.tm_min);
298 send_reply(cptr, RPL_ENDOFMOTD);
302 /* motd_recache causes all the MOTD caches to be cleared */
308 motd_decache(MotdList.local); /* decache local and remote MOTDs */
309 motd_decache(MotdList.remote);
311 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
314 /* now recache local and remote MOTDs */
315 motd_cache(MotdList.local);
316 motd_cache(MotdList.remote);
319 /* motd_init initializes the MOTD routines, including reading the
320 * ircd.motd and remote.motd files into cache
325 if (MotdList.local) /* destroy old local... */
326 motd_destroy(MotdList.local);
328 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
329 motd_cache(MotdList.local); /* init local and cache it */
331 if (MotdList.remote) /* destroy old remote... */
332 motd_destroy(MotdList.remote);
334 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
335 motd_cache(MotdList.remote); /* init remote and cache it */
338 /* This routine adds a MOTD */
340 motd_add(const char *hostmask, const char *path)
344 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
346 tmp->next = MotdList.other; /* link it into the list */
347 MotdList.other = tmp;
350 /* This routine clears the list of MOTDs */
354 struct Motd *ptr, *next;
356 motd_decache(MotdList.local); /* decache local and remote MOTDs */
357 motd_decache(MotdList.remote);
359 if (MotdList.other) /* destroy other MOTDs */
360 for (ptr = MotdList.other; ptr; ptr = next)
368 /* now recache local and remote MOTDs */
369 motd_cache(MotdList.local);
370 motd_cache(MotdList.remote);
373 /* This is called to report T-lines */
375 motd_report(struct Client *to, struct StatDesc *sd, int stat, char *param)
379 for (ptr = MotdList.other; ptr; ptr = ptr->next)
380 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
381 ptr->hostmask, ptr->path);
385 motd_memory_count(struct Client *cptr)
388 struct MotdCache *cache;
389 unsigned int mt = 0, /* motd count */
390 mtm = 0, /* memory consumed by motd */
391 mtc = 0, /* motd cache count */
392 mtcm = 0, /* memory consumed by motd cache */
393 mtf = 0; /* motd free list count */
397 mtm += sizeof(struct Motd);
398 mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
404 mtm += sizeof(struct Motd);
405 mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
408 for (ptr = MotdList.other; ptr; ptr = ptr->next)
411 mtm += sizeof(struct Motd);
412 mtm += ptr->path ? (strlen(ptr->path) + 1) : 0;
415 for (cache = MotdList.cachelist; cache; cache = cache->next)
418 mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
421 if (MotdList.freelist)
422 for (ptr = MotdList.freelist; ptr; ptr = ptr->next)
425 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
426 ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)",
427 mt, mtm, mtc, mtcm, mtf, (mtf * sizeof(struct Motd)));