- Added some tools to detect memory leaks.
[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 "send.h"
45
46 #include <assert.h>
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sys/stat.h>
51
52 static struct {
53   struct Motd*      local;
54   struct Motd*      remote;
55   struct Motd*      other;
56   struct Motd*      freelist;
57   struct MotdCache* cachelist;
58 } MotdList = { 0, 0, 0, 0, 0 };
59
60 /* Create a struct Motd and initialize it */
61 static struct Motd *
62 motd_create(const char *hostmask, const char *path, int maxcount)
63 {
64   struct Motd* tmp;
65   int type = MOTD_UNIVERSAL;
66   
67   assert(0 != path);
68   
69   if (hostmask != NULL && find_class(hostmask))
70     type = MOTD_CLASS;
71   else
72     type = MOTD_HOSTMASK;
73   /* allocate memory and initialize the structure */
74   if (MotdList.freelist)
75   {
76     tmp = MotdList.freelist;
77     MotdList.freelist = tmp->next;
78   } else
79     tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
80   
81   tmp->next = 0;
82   tmp->type = type;
83   
84   if (hostmask != NULL)
85     DupString(tmp->hostmask, hostmask);
86   
87   DupString(tmp->path, path);
88   tmp->maxcount = maxcount;
89   tmp->cache = 0;
90
91   return tmp;
92 }
93
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)
97 {
98   FBFILE*               file;
99   struct MotdCache*     cache;
100   struct stat           sb;
101   char                  line[MOTD_LINESIZE + 2]; /* \r\n */
102   char*                 tmp;
103   int                   i;
104
105   assert(0 != motd);
106   assert(0 != motd->path);
107
108   if (motd->cache)
109     return motd->cache;
110
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 */
118     }
119   }
120
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,
124            strerror(errno)));
125     return 0;
126   }
127
128   /* need the file's modification time */
129   if (-1 == fbstat(&sb, file)) {
130     fbclose(file);
131     return 0;
132   }
133
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)));
137
138   cache->ref = 1;
139   DupString(cache->path, motd->path);
140   cache->maxcount = motd->maxcount;
141
142   cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
143
144   cache->count = 0;
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';
149          tmp++, i++)
150       cache->motd[cache->count][i] = *tmp;
151     cache->motd[cache->count][i] = '\0';
152
153     cache->count++;
154   }
155
156   fbclose(file); /* close the file */
157
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)));
163   MyFree(cache);
164
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;
171
172   return motd->cache;
173 }
174
175 static void
176 motd_decache(struct Motd *motd)
177 {
178   struct MotdCache* cache;
179
180   assert(0 != motd);
181
182   if (!(cache = motd->cache)) /* we can be called for records with no cache */
183     return;
184
185   motd->cache = 0; /* zero the cache */
186
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;
191
192     MyFree(cache->path); /* free path info... */
193
194     MyFree(cache); /* very simple for a reason... */
195   }
196 }
197
198 /* This function destroys a struct Motd, destroying the cache if needed */
199 static void
200 motd_destroy(struct Motd *motd)
201 {
202   assert(0 != motd);
203
204   MyFree(motd->path); /* we always must have a path */
205   MyFree(motd->hostmask);
206   if (motd->cache) /* drop the cache */
207     motd_decache(motd);
208
209   motd->next = MotdList.freelist;
210   MotdList.freelist = motd;
211 }
212
213 /* We use this routine to look up the struct Motd to send to any given
214  * user.
215  */
216 static struct Motd *
217 motd_lookup(struct Client *cptr)
218 {
219   struct Motd *ptr;
220   char *class = NULL;
221
222   assert(0 != cptr);
223
224   if (!MyUser(cptr)) /* not my user, always return remote motd */
225     return MotdList.remote;
226
227   class = get_client_class(cptr);
228
229   /* check the motd blocks first */
230   for (ptr = MotdList.other; ptr; ptr = ptr->next)
231   {
232     if (ptr->type == MOTD_CLASS &&
233         !match(ptr->hostmask, class))
234       return ptr;
235     else if (ptr->type == MOTD_HOSTMASK && class != NULL &&
236              !match(ptr->hostmask, cli_sockhost(cptr)))
237       return ptr;
238   }
239
240   return MotdList.local; /* Ok, return the default motd */
241 }
242
243 /* Here is a routine that takes a MotdCache and sends it to a user */
244 static int
245 motd_forward(struct Client *cptr, struct MotdCache *cache)
246 {
247   int i;
248
249   assert(0 != cptr);
250
251   if (!cache) /* no motd to send */
252     return send_reply(cptr, ERR_NOMOTD);
253
254   /* send the motd */
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);
260
261   for (i = 0; i < cache->count; i++)
262     send_reply(cptr, RPL_MOTD, cache->motd[i]);
263
264   return send_reply(cptr, RPL_ENDOFMOTD); /* end */
265 }
266
267 /* This routine is used to send the MOTD off to a user. */
268 int
269 motd_send(struct Client* cptr)
270 {
271   assert(0 != cptr);
272
273   return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
274 }
275
276 /* This routine sends the MOTD or something to newly-registered users. */
277 void
278 motd_signon(struct Client* cptr)
279 {
280   struct MotdCache *cache;
281   const char *banner = NULL;
282
283   cache = motd_cache(motd_lookup(cptr));
284
285   if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
286     motd_forward(cptr, cache);
287   else {
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);
298   }
299 }
300
301 /* motd_recache causes all the MOTD caches to be cleared */
302 void
303 motd_recache(void)
304 {
305   struct Motd* tmp;
306
307   motd_decache(MotdList.local); /* decache local and remote MOTDs */
308   motd_decache(MotdList.remote);
309
310   for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
311     motd_decache(tmp);
312
313   /* now recache local and remote MOTDs */
314   motd_cache(MotdList.local);
315   motd_cache(MotdList.remote);
316 }
317
318 /* motd_init initializes the MOTD routines, including reading the
319  * ircd.motd and remote.motd files into cache
320  */
321 void
322 motd_init(void)
323 {
324   if (MotdList.local) /* destroy old local... */
325     motd_destroy(MotdList.local);
326
327   MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
328   motd_cache(MotdList.local); /* init local and cache it */
329
330   if (MotdList.remote) /* destroy old remote... */
331     motd_destroy(MotdList.remote);
332
333   MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
334   motd_cache(MotdList.remote); /* init remote and cache it */
335 }
336
337 /* This routine adds a MOTD */
338 void
339 motd_add(const char *hostmask, const char *path)
340 {
341   struct Motd *tmp;
342
343   tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
344
345   tmp->next = MotdList.other; /* link it into the list */
346   MotdList.other = tmp;
347 }
348
349 /* This routine clears the list of MOTDs */
350 void
351 motd_clear(void)
352 {
353   struct Motd *ptr, *next;
354
355   motd_decache(MotdList.local); /* decache local and remote MOTDs */
356   motd_decache(MotdList.remote);
357
358   if (MotdList.other) /* destroy other MOTDs */
359     for (ptr = MotdList.other; ptr; ptr = next)
360     { 
361       next = ptr->next;
362       motd_destroy(ptr);
363     }
364
365   MotdList.other = 0;
366
367   /* now recache local and remote MOTDs */
368   motd_cache(MotdList.local);
369   motd_cache(MotdList.remote);
370 }
371
372 /* This is called to report T-lines */
373 void
374 motd_report(struct Client *to)
375 {
376   struct Motd *ptr;
377
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);
381 }