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*)MyMalloc(sizeof(struct MotdCache) +
160 (MOTD_LINESIZE * (cache->count - 1)));
161 memcpy(motd->cache, cache, sizeof(struct MotdCache) +
162 (MOTD_LINESIZE * (cache->count - 1)));
165 /* now link it in... */
166 motd->cache->next = MotdList.cachelist;
167 motd->cache->prev_p = &MotdList.cachelist;
168 if (MotdList.cachelist)
169 MotdList.cachelist->prev_p = &motd->cache->next;
170 MotdList.cachelist = motd->cache;
176 motd_decache(struct Motd *motd)
178 struct MotdCache* cache;
182 if (!(cache = motd->cache)) /* we can be called for records with no cache */
185 motd->cache = 0; /* zero the cache */
187 if (!--cache->ref) { /* reduce reference count... */
188 if (cache->next) /* ref is 0, delink from list and free */
189 cache->next->prev_p = cache->prev_p;
190 *cache->prev_p = cache->next;
192 MyFree(cache->path); /* free path info... */
194 MyFree(cache); /* very simple for a reason... */
198 /* This function destroys a struct Motd, destroying the cache if needed */
200 motd_destroy(struct Motd *motd)
204 MyFree(motd->path); /* we always must have a path */
205 MyFree(motd->hostmask);
206 if (motd->cache) /* drop the cache */
209 motd->next = MotdList.freelist;
210 MotdList.freelist = motd;
213 /* We use this routine to look up the struct Motd to send to any given
217 motd_lookup(struct Client *cptr)
224 if (!MyUser(cptr)) /* not my user, always return remote motd */
225 return MotdList.remote;
227 class = get_client_class(cptr);
229 /* check the motd blocks first */
230 for (ptr = MotdList.other; ptr; ptr = ptr->next)
232 if (ptr->type == MOTD_CLASS &&
233 !match(ptr->hostmask, class))
235 else if (ptr->type == MOTD_HOSTMASK && class != NULL &&
236 !match(ptr->hostmask, cli_sockhost(cptr)))
240 return MotdList.local; /* Ok, return the default motd */
243 /* Here is a routine that takes a MotdCache and sends it to a user */
245 motd_forward(struct Client *cptr, struct MotdCache *cache)
251 if (!cache) /* no motd to send */
252 return send_reply(cptr, ERR_NOMOTD);
255 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
256 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
257 cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
258 cache->modtime.tm_mday, cache->modtime.tm_hour,
259 cache->modtime.tm_min);
261 for (i = 0; i < cache->count; i++)
262 send_reply(cptr, RPL_MOTD, cache->motd[i]);
264 return send_reply(cptr, RPL_ENDOFMOTD); /* end */
267 /* This routine is used to send the MOTD off to a user. */
269 motd_send(struct Client* cptr)
273 return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
276 /* This routine sends the MOTD or something to newly-registered users. */
278 motd_signon(struct Client* cptr)
280 struct MotdCache *cache;
281 const char *banner = NULL;
283 cache = motd_cache(motd_lookup(cptr));
285 if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
286 motd_forward(cptr, cache);
288 send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
289 if ((banner = feature_str(FEAT_MOTD_BANNER)))
290 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
291 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
292 "AUP before continuing using this service.\002");
293 send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
294 "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
295 cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
296 cache->modtime.tm_hour, cache->modtime.tm_min);
297 send_reply(cptr, RPL_ENDOFMOTD);
301 /* motd_recache causes all the MOTD caches to be cleared */
307 motd_decache(MotdList.local); /* decache local and remote MOTDs */
308 motd_decache(MotdList.remote);
310 for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
313 /* now recache local and remote MOTDs */
314 motd_cache(MotdList.local);
315 motd_cache(MotdList.remote);
318 /* motd_init initializes the MOTD routines, including reading the
319 * ircd.motd and remote.motd files into cache
324 if (MotdList.local) /* destroy old local... */
325 motd_destroy(MotdList.local);
327 MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
328 motd_cache(MotdList.local); /* init local and cache it */
330 if (MotdList.remote) /* destroy old remote... */
331 motd_destroy(MotdList.remote);
333 MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
334 motd_cache(MotdList.remote); /* init remote and cache it */
337 /* This routine adds a MOTD */
339 motd_add(const char *hostmask, const char *path)
343 tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
345 tmp->next = MotdList.other; /* link it into the list */
346 MotdList.other = tmp;
349 /* This routine clears the list of MOTDs */
353 struct Motd *ptr, *next;
355 motd_decache(MotdList.local); /* decache local and remote MOTDs */
356 motd_decache(MotdList.remote);
358 if (MotdList.other) /* destroy other MOTDs */
359 for (ptr = MotdList.other; ptr; ptr = next)
367 /* now recache local and remote MOTDs */
368 motd_cache(MotdList.local);
369 motd_cache(MotdList.remote);
372 /* This is called to report T-lines */
374 motd_report(struct Client *to)
378 for (ptr = MotdList.other; ptr; ptr = ptr->next)
379 send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
380 ptr->hostmask, ptr->path);