Author: Bleep <tom.helvey@cox.net>
[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 "config.h"
27
28 #include "motd.h"
29 #include "class.h"
30 #include "client.h"
31 #include "fileio.h"
32 #include "ircd.h"
33 #include "ircd_alloc.h"
34 #include "ircd_features.h"
35 #include "ircd_reply.h"
36 #include "ircd_string.h"
37 #include "match.h"
38 #include "msg.h"
39 #include "numeric.h"
40 #include "numnicks.h"
41 #include "s_conf.h"
42 #include "s_debug.h"
43 #include "s_user.h"
44 #include "s_stats.h"
45 #include "send.h"
46
47 #include <assert.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/stat.h>
52
53 static struct {
54   struct Motd*      local;
55   struct Motd*      remote;
56   struct Motd*      other;
57   struct Motd*      freelist;
58   struct MotdCache* cachelist;
59 } MotdList = { 0, 0, 0, 0, 0 };
60
61 /* Create a struct Motd and initialize it */
62 static struct Motd *
63 motd_create(const char *hostmask, const char *path, int maxcount)
64 {
65   struct Motd* tmp;
66   int type = MOTD_UNIVERSAL;
67   
68   assert(0 != path);
69   
70   if (hostmask != NULL && find_class(hostmask))
71     type = MOTD_CLASS;
72   else
73     type = MOTD_HOSTMASK;
74   /* allocate memory and initialize the structure */
75   if (MotdList.freelist)
76   {
77     tmp = MotdList.freelist;
78     MotdList.freelist = tmp->next;
79   } else
80     tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
81   
82   tmp->next = 0;
83   tmp->type = type;
84   
85   if (hostmask != NULL)
86     DupString(tmp->hostmask, hostmask);
87   
88   DupString(tmp->path, path);
89   tmp->maxcount = maxcount;
90   tmp->cache = 0;
91
92   return tmp;
93 }
94
95 /* This function reads a motd out of a file (if needed) and caches it */
96 static struct MotdCache *
97 motd_cache(struct Motd *motd)
98 {
99   FBFILE*               file;
100   struct MotdCache*     cache;
101   struct stat           sb;
102   char                  line[MOTD_LINESIZE + 2]; /* \r\n */
103   char*                 tmp;
104   int                   i;
105
106   assert(0 != motd);
107   assert(0 != motd->path);
108
109   if (motd->cache)
110     return motd->cache;
111
112   /* try to find it in the list of cached files... */
113   for (cache = MotdList.cachelist; cache; cache = cache->next) {
114     if (!strcmp(cache->path, motd->path) &&
115         cache->maxcount == motd->maxcount) { /* found one... */
116       cache->ref++; /* increase reference count... */
117       motd->cache = cache; /* remember cache... */
118       return motd->cache; /* return it */
119     }
120   }
121
122   /* gotta read in the file, now */
123   if (!(file = fbopen(motd->path, "r"))) {
124     Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
125            strerror(errno)));
126     return 0;
127   }
128
129   /* need the file's modification time */
130   if (-1 == fbstat(&sb, file)) {
131     fbclose(file);
132     return 0;
133   }
134
135   /* Ok, allocate a structure; we'll realloc later to trim memory */
136   cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
137                                        (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
138
139   cache->ref = 1;
140   DupString(cache->path, motd->path);
141   cache->maxcount = motd->maxcount;
142
143   cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
144
145   cache->count = 0;
146   while (cache->count < cache->maxcount && fbgets(line, sizeof(line), file)) {
147     /* copy over line, stopping when we overflow or hit line end */
148     for (tmp = line, i = 0;
149          i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
150          tmp++, i++)
151       cache->motd[cache->count][i] = *tmp;
152     cache->motd[cache->count][i] = '\0';
153
154     cache->count++;
155   }
156
157   fbclose(file); /* close the file */
158
159   /* trim memory usage a little */
160   motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
161                                             (MOTD_LINESIZE * (cache->count - 1)));
162   memcpy(motd->cache, cache, sizeof(struct MotdCache) +
163          (MOTD_LINESIZE * (cache->count - 1)));
164   MyFree(cache);
165
166   /* now link it in... */
167   motd->cache->next = MotdList.cachelist;
168   motd->cache->prev_p = &MotdList.cachelist;
169   if (MotdList.cachelist)
170     MotdList.cachelist->prev_p = &motd->cache->next;
171   MotdList.cachelist = motd->cache;
172
173   return motd->cache;
174 }
175
176 static void
177 motd_decache(struct Motd *motd)
178 {
179   struct MotdCache* cache;
180
181   assert(0 != motd);
182
183   if (!(cache = motd->cache)) /* we can be called for records with no cache */
184     return;
185
186   motd->cache = 0; /* zero the cache */
187
188   if (!--cache->ref) { /* reduce reference count... */
189     if (cache->next) /* ref is 0, delink from list and free */
190       cache->next->prev_p = cache->prev_p;
191     *cache->prev_p = cache->next;
192
193     MyFree(cache->path); /* free path info... */
194
195     MyFree(cache); /* very simple for a reason... */
196   }
197 }
198
199 /* This function destroys a struct Motd, destroying the cache if needed */
200 static void
201 motd_destroy(struct Motd *motd)
202 {
203   assert(0 != motd);
204
205   MyFree(motd->path); /* we always must have a path */
206   MyFree(motd->hostmask);
207   if (motd->cache) /* drop the cache */
208     motd_decache(motd);
209
210   motd->next = MotdList.freelist;
211   MotdList.freelist = motd;
212 }
213
214 /* We use this routine to look up the struct Motd to send to any given
215  * user.
216  */
217 static struct Motd *
218 motd_lookup(struct Client *cptr)
219 {
220   struct Motd *ptr;
221   char *c_class = NULL;
222
223   assert(0 != cptr);
224
225   if (!MyUser(cptr)) /* not my user, always return remote motd */
226     return MotdList.remote;
227
228   c_class = get_client_class(cptr);
229
230   /* check the motd blocks first */
231   for (ptr = MotdList.other; ptr; ptr = ptr->next)
232   {
233     if (ptr->type == MOTD_CLASS &&
234         !match(ptr->hostmask, c_class))
235       return ptr;
236     else if (ptr->type == MOTD_HOSTMASK && c_class != NULL &&
237              !match(ptr->hostmask, cli_sockhost(cptr)))
238       return ptr;
239   }
240
241   return MotdList.local; /* Ok, return the default motd */
242 }
243
244 /* Here is a routine that takes a MotdCache and sends it to a user */
245 static int
246 motd_forward(struct Client *cptr, struct MotdCache *cache)
247 {
248   int i;
249
250   assert(0 != cptr);
251
252   if (!cache) /* no motd to send */
253     return send_reply(cptr, ERR_NOMOTD);
254
255   /* send the motd */
256   send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
257   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
258              cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
259              cache->modtime.tm_mday, cache->modtime.tm_hour,
260              cache->modtime.tm_min);
261
262   for (i = 0; i < cache->count; i++)
263     send_reply(cptr, RPL_MOTD, cache->motd[i]);
264
265   return send_reply(cptr, RPL_ENDOFMOTD); /* end */
266 }
267
268 /* This routine is used to send the MOTD off to a user. */
269 int
270 motd_send(struct Client* cptr)
271 {
272   assert(0 != cptr);
273
274   return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
275 }
276
277 /* This routine sends the MOTD or something to newly-registered users. */
278 void
279 motd_signon(struct Client* cptr)
280 {
281   struct MotdCache *cache;
282   const char *banner = NULL;
283
284   cache = motd_cache(motd_lookup(cptr));
285
286   if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
287     motd_forward(cptr, cache);
288   else {
289     send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
290     if ((banner = feature_str(FEAT_MOTD_BANNER)))
291       send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
292     send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
293                "AUP before continuing using this service.\002");
294     send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
295                "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
296                cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
297                cache->modtime.tm_hour, cache->modtime.tm_min);
298     send_reply(cptr, RPL_ENDOFMOTD);
299   }
300 }
301
302 /* motd_recache causes all the MOTD caches to be cleared */
303 void
304 motd_recache(void)
305 {
306   struct Motd* tmp;
307
308   motd_decache(MotdList.local); /* decache local and remote MOTDs */
309   motd_decache(MotdList.remote);
310
311   for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
312     motd_decache(tmp);
313
314   /* now recache local and remote MOTDs */
315   motd_cache(MotdList.local);
316   motd_cache(MotdList.remote);
317 }
318
319 /* motd_init initializes the MOTD routines, including reading the
320  * ircd.motd and remote.motd files into cache
321  */
322 void
323 motd_init(void)
324 {
325   if (MotdList.local) /* destroy old local... */
326     motd_destroy(MotdList.local);
327
328   MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
329   motd_cache(MotdList.local); /* init local and cache it */
330
331   if (MotdList.remote) /* destroy old remote... */
332     motd_destroy(MotdList.remote);
333
334   MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
335   motd_cache(MotdList.remote); /* init remote and cache it */
336 }
337
338 /* This routine adds a MOTD */
339 void
340 motd_add(const char *hostmask, const char *path)
341 {
342   struct Motd *tmp;
343
344   tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
345
346   tmp->next = MotdList.other; /* link it into the list */
347   MotdList.other = tmp;
348 }
349
350 /* This routine clears the list of MOTDs */
351 void
352 motd_clear(void)
353 {
354   struct Motd *ptr, *next;
355
356   motd_decache(MotdList.local); /* decache local and remote MOTDs */
357   motd_decache(MotdList.remote);
358
359   if (MotdList.other) /* destroy other MOTDs */
360     for (ptr = MotdList.other; ptr; ptr = next)
361     { 
362       next = ptr->next;
363       motd_destroy(ptr);
364     }
365
366   MotdList.other = 0;
367
368   /* now recache local and remote MOTDs */
369   motd_cache(MotdList.local);
370   motd_cache(MotdList.remote);
371 }
372
373 /* This is called to report T-lines */
374 void
375 motd_report(struct Client *to, struct StatDesc *sd, int stat, char *param)
376 {
377   struct Motd *ptr;
378
379   for (ptr = MotdList.other; ptr; ptr = ptr->next)
380     send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
381                ptr->hostmask, ptr->path);
382 }
383
384 void
385 motd_memory_count(struct Client *cptr)
386 {
387   struct Motd *ptr;
388   struct MotdCache *cache;
389   unsigned int mt = 0,   /* motd count */
390                mtm = 0,  /* memory consumed by motd */
391                mtc = 0,  /* motd cache count */
392                mtcm = 0, /* memory consumed by motd cache */
393                mtf = 0;  /* motd free list count */
394   if (MotdList.local)
395   {
396     mt++;
397     mtm += sizeof(struct Motd);
398     mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0;
399   }
400
401   if (MotdList.remote) 
402   {
403     mt++;
404     mtm += sizeof(struct Motd);
405     mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0;
406   }
407
408   for (ptr = MotdList.other; ptr; ptr = ptr->next)
409   {
410     mt++;
411     mtm += sizeof(struct Motd);
412     mtm += ptr->path ? (strlen(ptr->path) + 1) : 0;
413   }
414
415   for (cache = MotdList.cachelist; cache; cache = cache->next)
416   {
417     mtc++;
418     mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1));
419   }
420
421   if (MotdList.freelist)
422     for (ptr = MotdList.freelist; ptr; ptr = ptr->next)
423       mtf++;
424
425   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
426              ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)",
427              mt, mtm, mtc, mtcm, mtf, (mtf * sizeof(struct Motd)));
428 }