Author: Andres Miller <a1kmm@mware.virtualave.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 "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 *)MyRealloc(cache, sizeof(struct MotdCache) +
160                                               (MOTD_LINESIZE *
161                                                (cache->count - 1)));
162
163   /* now link it in... */
164   motd->cache->next = MotdList.cachelist;
165   motd->cache->prev_p = &MotdList.cachelist;
166   if (MotdList.cachelist)
167     MotdList.cachelist->prev_p = &motd->cache->next;
168   MotdList.cachelist = motd->cache;
169
170   return motd->cache;
171 }
172
173 static void
174 motd_decache(struct Motd *motd)
175 {
176   struct MotdCache* cache;
177
178   assert(0 != motd);
179
180   if (!(cache = motd->cache)) /* we can be called for records with no cache */
181     return;
182
183   motd->cache = 0; /* zero the cache */
184
185   if (!--cache->ref) { /* reduce reference count... */
186     if (cache->next) /* ref is 0, delink from list and free */
187       cache->next->prev_p = cache->prev_p;
188     *cache->prev_p = cache->next;
189
190     MyFree(cache->path); /* free path info... */
191
192     MyFree(cache); /* very simple for a reason... */
193   }
194 }
195
196 /* This function destroys a struct Motd, destroying the cache if needed */
197 static void
198 motd_destroy(struct Motd *motd)
199 {
200   assert(0 != motd);
201
202   MyFree(motd->path); /* we always must have a path */
203   MyFree(motd->hostmask);
204   if (motd->cache) /* drop the cache */
205     motd_decache(motd);
206
207   motd->next = MotdList.freelist;
208   MotdList.freelist = motd;
209 }
210
211 /* We use this routine to look up the struct Motd to send to any given
212  * user.
213  */
214 static struct Motd *
215 motd_lookup(struct Client *cptr)
216 {
217   struct Motd *ptr;
218   char *class = NULL;
219
220   assert(0 != cptr);
221
222   if (!MyUser(cptr)) /* not my user, always return remote motd */
223     return MotdList.remote;
224
225   class = get_client_class(cptr);
226
227   /* check the motd blocks first */
228   for (ptr = MotdList.other; ptr; ptr = ptr->next)
229   {
230     if (ptr->type == MOTD_CLASS &&
231         !match(ptr->hostmask, class))
232       return ptr;
233     else if (ptr->type == MOTD_HOSTMASK && class != NULL &&
234              !match(ptr->hostmask, cli_sockhost(cptr)))
235       return ptr;
236   }
237
238   return MotdList.local; /* Ok, return the default motd */
239 }
240
241 /* Here is a routine that takes a MotdCache and sends it to a user */
242 static int
243 motd_forward(struct Client *cptr, struct MotdCache *cache)
244 {
245   int i;
246
247   assert(0 != cptr);
248
249   if (!cache) /* no motd to send */
250     return send_reply(cptr, ERR_NOMOTD);
251
252   /* send the motd */
253   send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
254   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
255              cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
256              cache->modtime.tm_mday, cache->modtime.tm_hour,
257              cache->modtime.tm_min);
258
259   for (i = 0; i < cache->count; i++)
260     send_reply(cptr, RPL_MOTD, cache->motd[i]);
261
262   return send_reply(cptr, RPL_ENDOFMOTD); /* end */
263 }
264
265 /* This routine is used to send the MOTD off to a user. */
266 int
267 motd_send(struct Client* cptr)
268 {
269   assert(0 != cptr);
270
271   return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
272 }
273
274 /* This routine sends the MOTD or something to newly-registered users. */
275 void
276 motd_signon(struct Client* cptr)
277 {
278   struct MotdCache *cache;
279   const char *banner = NULL;
280
281   cache = motd_cache(motd_lookup(cptr));
282
283   if (!feature_bool(FEAT_NODEFAULTMOTD) || !cache)
284     motd_forward(cptr, cache);
285   else {
286     send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
287     if ((banner = feature_str(FEAT_MOTD_BANNER)))
288       send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
289     send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
290                "AUP before continuing using this service.\002");
291     send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
292                "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
293                cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
294                cache->modtime.tm_hour, cache->modtime.tm_min);
295     send_reply(cptr, RPL_ENDOFMOTD);
296   }
297 }
298
299 /* motd_recache causes all the MOTD caches to be cleared */
300 void
301 motd_recache(void)
302 {
303   struct Motd* tmp;
304
305   motd_decache(MotdList.local); /* decache local and remote MOTDs */
306   motd_decache(MotdList.remote);
307
308   for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
309     motd_decache(tmp);
310
311   /* now recache local and remote MOTDs */
312   motd_cache(MotdList.local);
313   motd_cache(MotdList.remote);
314 }
315
316 /* motd_init initializes the MOTD routines, including reading the
317  * ircd.motd and remote.motd files into cache
318  */
319 void
320 motd_init(void)
321 {
322   if (MotdList.local) /* destroy old local... */
323     motd_destroy(MotdList.local);
324
325   MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
326   motd_cache(MotdList.local); /* init local and cache it */
327
328   if (MotdList.remote) /* destroy old remote... */
329     motd_destroy(MotdList.remote);
330
331   MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
332   motd_cache(MotdList.remote); /* init remote and cache it */
333 }
334
335 /* This routine adds a MOTD */
336 void
337 motd_add(const char *hostmask, const char *path)
338 {
339   struct Motd *tmp;
340
341   tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
342
343   tmp->next = MotdList.other; /* link it into the list */
344   MotdList.other = tmp;
345 }
346
347 /* This routine clears the list of MOTDs */
348 void
349 motd_clear(void)
350 {
351   struct Motd *ptr, *next;
352
353   motd_decache(MotdList.local); /* decache local and remote MOTDs */
354   motd_decache(MotdList.remote);
355
356   if (MotdList.other) /* destroy other MOTDs */
357     for (ptr = MotdList.other; ptr; ptr = next)
358     { 
359       next = ptr->next;
360       motd_destroy(ptr);
361     }
362
363   MotdList.other = 0;
364
365   /* now recache local and remote MOTDs */
366   motd_cache(MotdList.local);
367   motd_cache(MotdList.remote);
368 }
369
370 /* This is called to report T-lines */
371 void
372 motd_report(struct Client *to)
373 {
374   struct Motd *ptr;
375
376   for (ptr = MotdList.other; ptr; ptr = ptr->next)
377     send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
378                ptr->hostmask, ptr->path);
379 }