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