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