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