Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / motd.c
1 /*
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>
6  *
7  * See file AUTHORS in IRC package for additional names of
8  * the programmers.
9  *
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)
13  * any later version.
14  *
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.
19  *
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.
23  *
24  * $Id$
25  */
26 #include "motd.h"
27 #include "class.h"
28 #include "client.h"
29 #include "fileio.h"
30 #include "ircd.h"
31 #include "ircd_alloc.h"
32 #include "ircd_features.h"
33 #include "ircd_reply.h"
34 #include "ircd_string.h"
35 #include "match.h"
36 #include "msg.h"
37 #include "numeric.h"
38 #include "numnicks.h"
39 #include "s_conf.h"
40 #include "s_debug.h"
41 #include "s_user.h"
42 #include "send.h"
43
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/stat.h>
49
50 static struct {
51   struct Motd*      local;
52   struct Motd*      remote;
53   struct Motd*      other;
54   struct Motd*      freelist;
55   struct MotdCache* cachelist;
56 } MotdList;
57
58 /* Create a struct Motd and initialize it */
59 static struct Motd *
60 motd_create(const char *hostmask, const char *path, int maxcount)
61 {
62   struct Motd* tmp;
63   int type = MOTD_UNIVERSAL;
64   const char* s;
65
66   assert(0 != path);
67
68   if (hostmask) { /* figure out if it's a class or hostmask */
69     type = MOTD_CLASS; /* all digits, convert to class */
70
71     for (s = hostmask; *s; s++)
72       if (!IsDigit(*s)) { /* not a digit, not a class... */
73         type = MOTD_HOSTMASK;
74         break;
75       }
76   }
77
78   /* allocate memory and initialize the structure */
79   if (MotdList.freelist) {
80     tmp = MotdList.freelist;
81     MotdList.freelist = tmp->next;
82   } else
83     tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
84
85   tmp->next = 0;
86   tmp->type = type;
87
88   switch (type) {
89   case MOTD_HOSTMASK:
90     DupString(tmp->id.hostmask, hostmask);
91     break;
92
93   case MOTD_CLASS:
94     tmp->id.class = atoi(hostmask);
95     break;
96   }
97
98   DupString(tmp->path, path);
99   tmp->maxcount = maxcount;
100   tmp->cache = 0;
101
102   return tmp;
103 }
104
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)
108 {
109   FBFILE*               file;
110   struct MotdCache*     cache;
111   struct stat           sb;
112   char                  line[MOTD_LINESIZE + 2]; /* \r\n */
113   char*                 tmp;
114   int                   i;
115
116   assert(0 != motd);
117   assert(0 != motd->path);
118
119   if (motd->cache)
120     return motd->cache;
121
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 */
129     }
130   }
131
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,
135            strerror(errno)));
136     return 0;
137   }
138
139   /* need the file's modification time */
140   if (-1 == fbstat(&sb, file)) {
141     fbclose(file);
142     return 0;
143   }
144
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)));
148
149   cache->ref = 1;
150   DupString(cache->path, motd->path);
151   cache->maxcount = motd->maxcount;
152
153   cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
154
155   cache->count = 0;
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';
160          tmp++, i++)
161       cache->motd[cache->count][i] = *tmp;
162     cache->motd[cache->count][i] = '\0';
163
164     cache->count++;
165   }
166
167   fbclose(file); /* close the file */
168
169   /* trim memory usage a little */
170   motd->cache = (struct MotdCache *)MyRealloc(cache, sizeof(struct MotdCache) +
171                                               (MOTD_LINESIZE *
172                                                (cache->count - 1)));
173
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;
180
181   return motd->cache;
182 }
183
184 static void
185 motd_decache(struct Motd *motd)
186 {
187   struct MotdCache* cache;
188
189   assert(0 != motd);
190
191   if (!(cache = motd->cache)) /* we can be called for records with no cache */
192     return;
193
194   motd->cache = 0; /* zero the cache */
195
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;
200
201     MyFree(cache->path); /* free path info... */
202
203     MyFree(cache); /* very simple for a reason... */
204   }
205 }
206
207 /* This function destroys a struct Motd, destroying the cache if needed */
208 static void
209 motd_destroy(struct Motd *motd)
210 {
211   assert(0 != motd);
212
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 */
217     motd_decache(motd);
218
219   motd->next = MotdList.freelist;
220   MotdList.freelist = motd;
221 }
222
223 /* We use this routine to look up the struct Motd to send to any given
224  * user.
225  */
226 static struct Motd *
227 motd_lookup(struct Client *cptr)
228 {
229   struct Motd *ptr;
230   int class = -1;
231
232   assert(0 != cptr);
233
234   if (!MyUser(cptr)) /* not my user, always return remote motd */
235     return MotdList.remote;
236
237   class = get_client_class(cptr);
238
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)
242       return ptr;
243     else if (ptr->type == MOTD_HOSTMASK &&
244              !match(ptr->id.hostmask, cli_sockhost(cptr)))
245       return ptr;
246   }
247
248   return MotdList.local; /* Ok, return the default motd */
249 }
250
251 /* Here is a routine that takes a MotdCache and sends it to a user */
252 static int
253 motd_forward(struct Client *cptr, struct MotdCache *cache)
254 {
255   int i;
256
257   assert(0 != cptr);
258
259   if (!cache) /* no motd to send */
260     return send_reply(cptr, ERR_NOMOTD);
261
262   /* send the motd */
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);
268
269   for (i = 0; i < cache->count; i++)
270     send_reply(cptr, RPL_MOTD, cache->motd[i]);
271
272   return send_reply(cptr, RPL_ENDOFMOTD); /* end */
273 }
274
275 /* This routine is used to send the MOTD off to a user. */
276 int
277 motd_send(struct Client* cptr)
278 {
279   assert(0 != cptr);
280
281   return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
282 }
283
284 /* This routine sends the MOTD or something to newly-registered users. */
285 void
286 motd_signon(struct Client* cptr)
287 {
288   struct MotdCache *cache;
289
290   cache = motd_cache(motd_lookup(cptr));
291
292   if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
293     motd_forward(cptr, cache);
294   else {
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);
303   }
304 }
305
306 /* motd_recache causes all the MOTD caches to be cleared */
307 void
308 motd_recache(void)
309 {
310   struct Motd* tmp;
311
312   motd_decache(MotdList.local); /* decache local and remote MOTDs */
313   motd_decache(MotdList.remote);
314
315   for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
316     motd_decache(tmp);
317
318   /* now recache local and remote MOTDs */
319   motd_cache(MotdList.local);
320   motd_cache(MotdList.remote);
321 }
322
323 /* motd_init initializes the MOTD routines, including reading the
324  * ircd.motd and remote.motd files into cache
325  */
326 void
327 motd_init(void)
328 {
329   MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
330   motd_cache(MotdList.local); /* init local and cache it */
331
332   MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
333   motd_cache(MotdList.remote); /* init remote and cache it */
334
335   MotdList.other = 0; /* no T-lines processed yet */
336
337   MotdList.freelist = 0;
338   MotdList.cachelist = 0;
339 }
340
341 /* This routine adds a MOTD */
342 void
343 motd_add(const char *hostmask, const char *path)
344 {
345   struct Motd *tmp;
346
347   tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
348
349   tmp->next = MotdList.other; /* link it into the list */
350   MotdList.other = tmp;
351 }
352
353 /* This routine clears the list of MOTDs */
354 void
355 motd_clear(void)
356 {
357   struct Motd *ptr, *next;
358
359   motd_decache(MotdList.local); /* decache local and remote MOTDs */
360   motd_decache(MotdList.remote);
361
362   if (MotdList.other) /* destroy other MOTDs */
363     for (ptr = MotdList.other; ptr; ptr = next) {
364       next = ptr->next;
365       motd_destroy(ptr);
366     }
367
368   MotdList.other = 0;
369
370   /* now recache local and remote MOTDs */
371   motd_cache(MotdList.local);
372   motd_cache(MotdList.remote);
373 }
374
375 /* This is called to report T-lines */
376 void
377 motd_report(struct Client *to)
378 {
379   struct Motd *ptr;
380
381   for (ptr = MotdList.other; ptr; ptr = ptr->next) {
382     if (ptr->type == MOTD_CLASS) /* class requires special handling */
383       send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %d %s", ptr->id.class,
384                  ptr->path);
385     else if (ptr->type == MOTD_HOSTMASK)
386       send_reply(to, RPL_STATSTLINE, 'T', ptr->id.hostmask, ptr->path);
387   }
388 }