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"
56 struct Motd* freelist;
57 struct MotdCache* cachelist;
58 } MotdList = { 0, 0, 0, 0, 0 };
60 /* Create a struct Motd and initialize it */
62 motd_create(const char *hostmask, const char *path, int maxcount)
65 int type = MOTD_UNIVERSAL;
69 if (hostmask != NULL && find_class(hostmask))
73 /* allocate memory and initialize the structure */
74 if (MotdList.freelist)
76 tmp = MotdList.freelist;
77 MotdList.freelist = tmp->next;
79 tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
85 DupString(tmp->hostmask, hostmask);
87 DupString(tmp->path, path);
88 tmp->maxcount = maxcount;
94 /* This function reads a motd out of a file (if needed) and caches it */
95 static struct MotdCache *
96 motd_cache(struct Motd *motd)
99 struct MotdCache* cache;
101 char line[MOTD_LINESIZE + 2]; /* \r\n */
106 assert(0 != motd->path);
111 /* try to find it in the list of cached files... */
112 for (cache = MotdList.cachelist; cache; cache = cache->next) {
113 if (!strcmp(cache->path, motd->path) &&
114 cache->maxcount == motd->maxcount) { /* found one... */
115 cache->ref++; /* increase reference count... */
116 motd->cache = cache; /* remember cache... */
117 return motd->cache; /* return it */
121 /* gotta read in the file, now */
122 if (!(file = fbopen(motd->path, "r"))) {
123 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
128 /* need the file's modification time */
129 if (-1 == fbstat(&sb, file)) {
134 /* Ok, allocate a structure; we'll realloc later to trim memory */
135 cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
136 (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
139 DupString(cache->path, motd->path);
140 cache->maxcount = motd->maxcount;
142 cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
145 while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
146 /* copy over line, stopping when we overflow or hit line end */
147 for (tmp = line, i = 0;
148 i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
150 cache->motd[cache->count][i] = *tmp;
151 cache->motd[cache->count][i] = '\0';
156 fbclose(file); /* close the file */
158 /* trim memory usage a little */
159 motd->cache = (struct MotdCache *)MyRealloc(cache, sizeof(struct MotdCache) +
161 (cache->count - 1)));
163 /* now link it in... */
164 motd->cache->next = MotdList.cachelist;
165 motd->cache->prev_p = &MotdList.cachelist;
166 if (MotdList.cachelist)
167 MotdList.cachelist->prev_p = &motd->cache->next;
168 MotdList.cachelist = motd->cache;
174 motd_decache(struct Motd *motd)
176 struct MotdCache* cache;
180 if (!(cache = motd->cache)) /* we can be called for records with no cache */
183 motd->cache = 0; /* zero the cache */
185 if (!--cache->ref) { /* reduce reference count... */
186 if (cache->next) /* ref is 0, delink from list and free */
187 cache->next->prev_p = cache->prev_p;
188 *cache->prev_p = cache->next;
190 MyFree(cache->path); /* free path info... */
192 MyFree(cache); /* very simple for a reason... */
196 /* This function destroys a struct Motd, destroying the cache if needed */
198 motd_destroy(struct Motd *motd)
202 MyFree(motd->path); /* we always must have a path */
203 MyFree(motd->hostmask);
204 if (motd->cache) /* drop the cache */
207 motd->next = MotdList.freelist;
208 MotdList.freelist = motd;
211 /* We use this routine to look up the struct Motd to send to any given
215 motd_lookup(struct Client *cptr)
222 if (!MyUser(cptr)) /* not my user, always return remote motd */
223 return MotdList.remote;
225 class = get_client_class(cptr);
227 /* check the motd blocks first */
228 for (ptr = MotdList.other; ptr; ptr = ptr->next)
230 if (ptr->type == MOTD_CLASS &&
231 !match(ptr->hostmask, class))
233 else if (ptr->type == MOTD_HOSTMASK && class != NULL &&
234 !match(ptr->hostmask, cli_sockhost(cptr)))
238 return MotdList.local; /* Ok, return the default motd */
241 /* Here is a routine that takes a MotdCache and sends it to a user */
243 motd_forward(struct Client *cptr, struct MotdCache *cache)
249 if (!cache) /* no motd to send */
250 return send_reply(cptr, ERR_NOMOTD);
253 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
254 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
255 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
256 cache->modtime.tm_mday, cache->modtime.tm_hour,
257 cache->modtime.tm_min);
259 for (i = 0; i < cache->count; i++)
260 send_reply(cptr, RPL_MOTD, cache->motd[i]);
262 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
265 /* This routine is used to send the MOTD off to a user. */
267 motd_send(struct Client* cptr)
271 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
274 /* This routine sends the MOTD or something to newly-registered users. */
276 motd_signon(struct Client* cptr)
278 struct MotdCache *cache;
279 const char *banner = NULL;
281 cache = motd_cache(motd_lookup(cptr));
283 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
284 motd_forward(cptr, cache);
286 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
287 if ((banner = feature_str(FEAT_MOTD_BANNER)))
288 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
289 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
290 "AUP before continuing using this service.\002");
291 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
292 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
293 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
294 cache->modtime.tm_hour, cache->modtime.tm_min);
295 send_reply(cptr, RPL_ENDOFMOTD);
299 /* motd_recache causes all the MOTD caches to be cleared */
305 motd_decache(MotdList.local); /* decache local and remote MOTDs */
306 motd_decache(MotdList.remote);
308 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
311 /* now recache local and remote MOTDs */
312 motd_cache(MotdList.local);
313 motd_cache(MotdList.remote);
316 /* motd_init initializes the MOTD routines, including reading the
317 * ircd.motd and remote.motd files into cache
322 if (MotdList.local) /* destroy old local... */
323 motd_destroy(MotdList.local);
325 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
326 motd_cache(MotdList.local); /* init local and cache it */
328 if (MotdList.remote) /* destroy old remote... */
329 motd_destroy(MotdList.remote);
331 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
332 motd_cache(MotdList.remote); /* init remote and cache it */
335 /* This routine adds a MOTD */
337 motd_add(const char *hostmask, const char *path)
341 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
343 tmp->next = MotdList.other; /* link it into the list */
344 MotdList.other = tmp;
347 /* This routine clears the list of MOTDs */
351 struct Motd *ptr, *next;
353 motd_decache(MotdList.local); /* decache local and remote MOTDs */
354 motd_decache(MotdList.remote);
356 if (MotdList.other) /* destroy other MOTDs */
357 for (ptr = MotdList.other; ptr; ptr = next)
365 /* now recache local and remote MOTDs */
366 motd_cache(MotdList.local);
367 motd_cache(MotdList.remote);
370 /* This is called to report T-lines */
372 motd_report(struct Client *to)
376 for (ptr = MotdList.other; ptr; ptr = ptr->next)
377 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
378 ptr->hostmask, ptr->path);