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;
70 if (hostmask) { /* figure out if it's a class or hostmask */
71 type = MOTD_CLASS; /* all digits, convert to class */
73 for (s = hostmask; *s; s++)
74 if (!IsDigit(*s)) { /* not a digit, not a class... */
80 /* allocate memory and initialize the structure */
81 if (MotdList.freelist) {
82 tmp = MotdList.freelist;
83 MotdList.freelist = tmp->next;
85 tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
92 DupString(tmp->id.hostmask, hostmask);
96 tmp->id.class = atoi(hostmask);
100 DupString(tmp->path, path);
101 tmp->maxcount = maxcount;
107 /* This function reads a motd out of a file (if needed) and caches it */
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 *)MyRealloc(cache, sizeof(struct MotdCache) +
174 (cache->count - 1)));
176 /* now link it in... */
177 motd->cache->next = MotdList.cachelist;
178 motd->cache->prev_p = &MotdList.cachelist;
179 if (MotdList.cachelist)
180 MotdList.cachelist->prev_p = &motd->cache->next;
181 MotdList.cachelist = motd->cache;
187 motd_decache(struct Motd *motd)
189 struct MotdCache* cache;
193 if (!(cache = motd->cache)) /* we can be called for records with no cache */
196 motd->cache = 0; /* zero the cache */
198 if (!--cache->ref) { /* reduce reference count... */
199 if (cache->next) /* ref is 0, delink from list and free */
200 cache->next->prev_p = cache->prev_p;
201 *cache->prev_p = cache->next;
203 MyFree(cache->path); /* free path info... */
205 MyFree(cache); /* very simple for a reason... */
209 /* This function destroys a struct Motd, destroying the cache if needed */
211 motd_destroy(struct Motd *motd)
215 MyFree(motd->path); /* we always must have a path */
216 if (motd->type == MOTD_HOSTMASK) /* free a host mask if any */
217 MyFree(motd->id.hostmask);
218 if (motd->cache) /* drop the cache */
221 motd->next = MotdList.freelist;
222 MotdList.freelist = motd;
225 /* We use this routine to look up the struct Motd to send to any given
229 motd_lookup(struct Client *cptr)
236 if (!MyUser(cptr)) /* not my user, always return remote motd */
237 return MotdList.remote;
239 class = get_client_class(cptr);
241 /* check the T-lines first */
242 for (ptr = MotdList.other; ptr; ptr = ptr->next) {
243 if (ptr->type == MOTD_CLASS && ptr->id.class == class)
245 else if (ptr->type == MOTD_HOSTMASK &&
246 !match(ptr->id.hostmask, cli_sockhost(cptr)))
250 return MotdList.local; /* Ok, return the default motd */
253 /* Here is a routine that takes a MotdCache and sends it to a user */
255 motd_forward(struct Client *cptr, struct MotdCache *cache)
261 if (!cache) /* no motd to send */
262 return send_reply(cptr, ERR_NOMOTD);
265 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
266 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
267 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
268 cache->modtime.tm_mday, cache->modtime.tm_hour,
269 cache->modtime.tm_min);
271 for (i = 0; i < cache->count; i++)
272 send_reply(cptr, RPL_MOTD, cache->motd[i]);
274 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
277 /* This routine is used to send the MOTD off to a user. */
279 motd_send(struct Client* cptr)
283 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
286 /* This routine sends the MOTD or something to newly-registered users. */
288 motd_signon(struct Client* cptr)
290 struct MotdCache *cache;
292 cache = motd_cache(motd_lookup(cptr));
294 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
295 motd_forward(cptr, cache);
297 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
298 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
299 "AUP before continuing using this service.\002");
300 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
301 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
302 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
303 cache->modtime.tm_hour, cache->modtime.tm_min);
304 send_reply(cptr, RPL_ENDOFMOTD);
308 /* motd_recache causes all the MOTD caches to be cleared */
314 motd_decache(MotdList.local); /* decache local and remote MOTDs */
315 motd_decache(MotdList.remote);
317 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
320 /* now recache local and remote MOTDs */
321 motd_cache(MotdList.local);
322 motd_cache(MotdList.remote);
325 /* motd_init initializes the MOTD routines, including reading the
326 * ircd.motd and remote.motd files into cache
331 if (MotdList.local) /* destroy old local... */
332 motd_destroy(MotdList.local);
334 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
335 motd_cache(MotdList.local); /* init local and cache it */
337 if (MotdList.remote) /* destroy old remote... */
338 motd_destroy(MotdList.remote);
340 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
341 motd_cache(MotdList.remote); /* init remote and cache it */
344 /* This routine adds a MOTD */
346 motd_add(const char *hostmask, const char *path)
350 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
352 tmp->next = MotdList.other; /* link it into the list */
353 MotdList.other = tmp;
356 /* This routine clears the list of MOTDs */
360 struct Motd *ptr, *next;
362 motd_decache(MotdList.local); /* decache local and remote MOTDs */
363 motd_decache(MotdList.remote);
365 if (MotdList.other) /* destroy other MOTDs */
366 for (ptr = MotdList.other; ptr; ptr = next) {
373 /* now recache local and remote MOTDs */
374 motd_cache(MotdList.local);
375 motd_cache(MotdList.remote);
378 /* This is called to report T-lines */
380 motd_report(struct Client *to)
384 for (ptr = MotdList.other; ptr; ptr = ptr->next) {
385 if (ptr->type == MOTD_CLASS) /* class requires special handling */
386 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %d %s", ptr->id.class,
388 else if (ptr->type == MOTD_HOSTMASK)
389 send_reply(to, RPL_STATSTLINE, 'T', ptr->id.hostmask, ptr->path);