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.
31 #include "ircd_alloc.h"
32 #include "ircd_features.h"
33 #include "ircd_reply.h"
34 #include "ircd_string.h"
54 struct Motd* freelist;
55 struct MotdCache* cachelist;
56 } MotdList = { 0, 0, 0, 0, 0 };
58 /* Create a struct Motd and initialize it */
60 motd_create(const char *hostmask, const char *path, int maxcount)
63 int type = MOTD_UNIVERSAL;
68 if (hostmask) { /* figure out if it's a class or hostmask */
69 type = MOTD_CLASS; /* all digits, convert to class */
71 for (s = hostmask; *s; s++)
72 if (!IsDigit(*s)) { /* not a digit, not a class... */
78 /* allocate memory and initialize the structure */
79 if (MotdList.freelist) {
80 tmp = MotdList.freelist;
81 MotdList.freelist = tmp->next;
83 tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
90 DupString(tmp->id.hostmask, hostmask);
94 tmp->id.class = atoi(hostmask);
98 DupString(tmp->path, path);
99 tmp->maxcount = maxcount;
105 /* This function reads a motd out of a file (if needed) and caches it */
106 static struct MotdCache *
107 motd_cache(struct Motd *motd)
110 struct MotdCache* cache;
112 char line[MOTD_LINESIZE + 2]; /* \r\n */
117 assert(0 != motd->path);
122 /* try to find it in the list of cached files... */
123 for (cache = MotdList.cachelist; cache; cache = cache->next) {
124 if (!strcmp(cache->path, motd->path) &&
125 cache->maxcount == motd->maxcount) { /* found one... */
126 cache->ref++; /* increase reference count... */
127 motd->cache = cache; /* remember cache... */
128 return motd->cache; /* return it */
132 /* gotta read in the file, now */
133 if (!(file = fbopen(motd->path, "r"))) {
134 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
139 /* need the file's modification time */
140 if (-1 == fbstat(&sb, file)) {
145 /* Ok, allocate a structure; we'll realloc later to trim memory */
146 cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
147 (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
150 DupString(cache->path, motd->path);
151 cache->maxcount = motd->maxcount;
153 cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
156 while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
157 /* copy over line, stopping when we overflow or hit line end */
158 for (tmp = line, i = 0;
159 i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
161 cache->motd[cache->count][i] = *tmp;
162 cache->motd[cache->count][i] = '\0';
167 fbclose(file); /* close the file */
169 /* trim memory usage a little */
170 motd->cache = (struct MotdCache *)MyRealloc(cache, sizeof(struct MotdCache) +
172 (cache->count - 1)));
174 /* now link it in... */
175 motd->cache->next = MotdList.cachelist;
176 motd->cache->prev_p = &MotdList.cachelist;
177 if (MotdList.cachelist)
178 MotdList.cachelist->prev_p = &motd->cache->next;
179 MotdList.cachelist = motd->cache;
185 motd_decache(struct Motd *motd)
187 struct MotdCache* cache;
191 if (!(cache = motd->cache)) /* we can be called for records with no cache */
194 motd->cache = 0; /* zero the cache */
196 if (!--cache->ref) { /* reduce reference count... */
197 if (cache->next) /* ref is 0, delink from list and free */
198 cache->next->prev_p = cache->prev_p;
199 *cache->prev_p = cache->next;
201 MyFree(cache->path); /* free path info... */
203 MyFree(cache); /* very simple for a reason... */
207 /* This function destroys a struct Motd, destroying the cache if needed */
209 motd_destroy(struct Motd *motd)
213 MyFree(motd->path); /* we always must have a path */
214 if (motd->type == MOTD_HOSTMASK) /* free a host mask if any */
215 MyFree(motd->id.hostmask);
216 if (motd->cache) /* drop the cache */
219 motd->next = MotdList.freelist;
220 MotdList.freelist = motd;
223 /* We use this routine to look up the struct Motd to send to any given
227 motd_lookup(struct Client *cptr)
234 if (!MyUser(cptr)) /* not my user, always return remote motd */
235 return MotdList.remote;
237 class = get_client_class(cptr);
239 /* check the T-lines first */
240 for (ptr = MotdList.other; ptr; ptr = ptr->next) {
241 if (ptr->type == MOTD_CLASS && ptr->id.class == class)
243 else if (ptr->type == MOTD_HOSTMASK &&
244 !match(ptr->id.hostmask, cli_sockhost(cptr)))
248 return MotdList.local; /* Ok, return the default motd */
251 /* Here is a routine that takes a MotdCache and sends it to a user */
253 motd_forward(struct Client *cptr, struct MotdCache *cache)
259 if (!cache) /* no motd to send */
260 return send_reply(cptr, ERR_NOMOTD);
263 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
264 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
265 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
266 cache->modtime.tm_mday, cache->modtime.tm_hour,
267 cache->modtime.tm_min);
269 for (i = 0; i < cache->count; i++)
270 send_reply(cptr, RPL_MOTD, cache->motd[i]);
272 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
275 /* This routine is used to send the MOTD off to a user. */
277 motd_send(struct Client* cptr)
281 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
284 /* This routine sends the MOTD or something to newly-registered users. */
286 motd_signon(struct Client* cptr)
288 struct MotdCache *cache;
290 cache = motd_cache(motd_lookup(cptr));
292 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
293 motd_forward(cptr, cache);
295 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
296 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
297 "AUP before continuing using this service.\002");
298 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
299 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
300 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
301 cache->modtime.tm_hour, cache->modtime.tm_min);
302 send_reply(cptr, RPL_ENDOFMOTD);
306 /* motd_recache causes all the MOTD caches to be cleared */
312 motd_decache(MotdList.local); /* decache local and remote MOTDs */
313 motd_decache(MotdList.remote);
315 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
318 /* now recache local and remote MOTDs */
319 motd_cache(MotdList.local);
320 motd_cache(MotdList.remote);
323 /* motd_init initializes the MOTD routines, including reading the
324 * ircd.motd and remote.motd files into cache
329 if (MotdList.local) /* destroy old local... */
330 motd_destroy(MotdList.local);
332 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
333 motd_cache(MotdList.local); /* init local and cache it */
335 if (MotdList.remote) /* destroy old remote... */
336 motd_destroy(MotdList.remote);
338 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
339 motd_cache(MotdList.remote); /* init remote and cache it */
342 /* This routine adds a MOTD */
344 motd_add(const char *hostmask, const char *path)
348 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
350 tmp->next = MotdList.other; /* link it into the list */
351 MotdList.other = tmp;
354 /* This routine clears the list of MOTDs */
358 struct Motd *ptr, *next;
360 motd_decache(MotdList.local); /* decache local and remote MOTDs */
361 motd_decache(MotdList.remote);
363 if (MotdList.other) /* destroy other MOTDs */
364 for (ptr = MotdList.other; ptr; ptr = next) {
371 /* now recache local and remote MOTDs */
372 motd_cache(MotdList.local);
373 motd_cache(MotdList.remote);
376 /* This is called to report T-lines */
378 motd_report(struct Client *to)
382 for (ptr = MotdList.other; ptr; ptr = ptr->next) {
383 if (ptr->type == MOTD_CLASS) /* class requires special handling */
384 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %d %s", ptr->id.class,
386 else if (ptr->type == MOTD_HOSTMASK)
387 send_reply(to, RPL_STATSTLINE, 'T', ptr->id.hostmask, ptr->path);