Author: Kev <klmitch@mit.edu>
[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 "motd.h"
27 #include "class.h"
28 #include "client.h"
29 #include "fileio.h"
30 #include "ircd.h"
31 #include "ircd_alloc.h"
32 #include "ircd_reply.h"
33 #include "ircd_string.h"
34 #include "match.h"
35 #include "msg.h"
36 #include "numeric.h"
37 #include "numnicks.h"
38 #include "s_conf.h"
39 #include "s_debug.h"
40 #include "s_user.h"
41 #include "send.h"
42
43 #include <assert.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/stat.h>
48
49 static struct {
50   struct Motd*  local;
51   struct Motd*  remote;
52   struct Motd*  other;
53 } MotdList;
54
55 /* Create a struct Motd and initialize it */
56 static struct Motd *
57 motd_create(const char *hostmask, const char *path, int maxcount)
58 {
59   struct Motd* tmp;
60   int type = MOTD_UNIVERSAL;
61   const char* s;
62
63   assert(0 != path);
64
65   if (hostmask) { /* figure out if it's a class or hostmask */
66     type = MOTD_CLASS; /* all digits, convert to class */
67
68     for (s = hostmask; *s; s++)
69       if (!IsDigit(*s)) { /* not a digit, not a class... */
70         type = MOTD_HOSTMASK;
71         break;
72       }
73   }
74
75   /* allocate memory and initialize the structure */
76   tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
77
78   tmp->next = 0;
79   tmp->type = type;
80
81   switch (type) {
82   case MOTD_HOSTMASK:
83     DupString(tmp->id.hostmask, hostmask);
84     break;
85
86   case MOTD_CLASS:
87     tmp->id.class = atoi(hostmask);
88     break;
89   }
90
91   DupString(tmp->path, path);
92   tmp->maxcount = maxcount;
93   tmp->cache = 0;
94
95   return tmp;
96 }
97
98 /* This function reads a motd out of a file (if needed) and caches it */
99 static struct MotdCache *
100 motd_cache(struct Motd *motd)
101 {
102   FBFILE*               file;
103   struct MotdCache*     cache;
104   struct stat           sb;
105   char                  line[MOTD_LINESIZE + 2]; /* \r\n */
106   char*                 tmp;
107   int                   i;
108
109   assert(0 != motd);
110   assert(0 != motd->path);
111
112   if (motd->cache)
113     return motd->cache;
114
115   /* gotta read in the file, now */
116   if (!(file = fbopen(motd->path, "r"))) {
117     Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motd->path,
118            strerror(errno)));
119     return 0;
120   }
121
122   /* need the file's modification time */
123   if (-1 == fbstat(&sb, file)) {
124     fbclose(file);
125     return 0;
126   }
127
128   /* Ok, allocate a structure; we'll realloc later to trim memory */
129   cache = (struct MotdCache *)MyMalloc(sizeof(struct MotdCache) +
130                                        (MOTD_LINESIZE * (MOTD_MAXLINES - 1)));
131
132   cache->modtime = *localtime((time_t *) &sb.st_mtime); /* store modtime */
133
134   cache->count = 0;
135   while (cache->count < motd->maxcount && fbgets(line, sizeof(line), file)) {
136     /* copy over line, stopping when we overflow or hit line end */
137     for (tmp = line, i = 0;
138          i < (MOTD_LINESIZE - 1) && *tmp && *tmp != '\r' && *tmp != '\n';
139          tmp++, i++)
140       cache->motd[cache->count][i] = *tmp;
141     cache->motd[cache->count][i] = '\0';
142
143     cache->count++;
144   }
145
146   fbclose(file); /* close the file */
147
148   /* trim memory usage a little */
149   motd->cache = (struct MotdCache *)MyRealloc(cache, sizeof(struct MotdCache) +
150                                               (MOTD_LINESIZE *
151                                                (cache->count - 1)));
152
153   return motd->cache;
154 }
155
156 static void
157 motd_decache(struct Motd *motd)
158 {
159   struct MotdCache* cache;
160
161   assert(0 != motd);
162
163   if (!(cache = motd->cache)) /* we can be called for records with no cache */
164     return;
165
166   motd->cache = 0; /* zero the cache */
167
168   MyFree(cache); /* very simple for a reason... */
169 }
170
171 /* This function destroys a struct Motd, destroying the cache if needed */
172 static void
173 motd_destroy(struct Motd *motd)
174 {
175   assert(0 != motd);
176
177   MyFree(motd->path); /* we always must have a path */
178   if (motd->type == MOTD_HOSTMASK) /* free a host mask if any */
179     MyFree(motd->id.hostmask);
180   if (motd->cache) /* drop the cache */
181     motd_decache(motd);
182
183   MyFree(motd); /* free the structure */
184 }
185
186 /* We use this routine to look up the struct Motd to send to any given
187  * user.
188  */
189 static struct Motd *
190 motd_lookup(struct Client *cptr)
191 {
192   struct Motd *ptr;
193   int class = -1;
194
195   assert(0 != cptr);
196
197   if (!MyUser(cptr)) /* not my user, always return remote motd */
198     return MotdList.remote;
199
200   class = get_client_class(cptr);
201
202   /* check the T-lines first */
203   for (ptr = MotdList.other; ptr; ptr = ptr->next) {
204     if (ptr->type == MOTD_CLASS && ptr->id.class == class)
205       return ptr;
206     else if (ptr->type == MOTD_HOSTMASK &&
207              !match(ptr->id.hostmask, cptr->sockhost))
208       return ptr;
209   }
210
211   return MotdList.local; /* Ok, return the default motd */
212 }
213
214 /* Here is a routine that takes a MotdCache and sends it to a user */
215 static int
216 motd_forward(struct Client *cptr, struct MotdCache *cache)
217 {
218   int i;
219
220   assert(0 != cptr);
221
222   if (!cache) /* no motd to send */
223     return send_reply(cptr, ERR_NOMOTD);
224
225   /* send the motd */
226   send_reply(cptr, RPL_MOTDSTART, me.name);
227   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
228              cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
229              cache->modtime.tm_mday, cache->modtime.tm_hour,
230              cache->modtime.tm_min);
231
232   for (i = 0; i < cache->count; i++)
233     send_reply(cptr, RPL_MOTD, cache->motd[i]);
234
235   return send_reply(cptr, RPL_ENDOFMOTD); /* end */
236 }
237
238 /* This routine is used to send the MOTD off to a user. */
239 int
240 motd_send(struct Client* cptr)
241 {
242   assert(0 != cptr);
243
244   return motd_forward(cptr, motd_cache(motd_lookup(cptr)));
245 }
246
247 /* This routine sends the MOTD or something to newly-registered users. */
248 void
249 motd_signon(struct Client* cptr)
250 {
251   struct MotdCache *cache;
252
253   cache = motd_cache(motd_lookup(cptr));
254
255 #ifdef NODEFAULTMOTD
256   send_reply(cptr, RPL_MOTDSTART, me.name);
257   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the AUP "
258              "before continuing using this service.\002");
259   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was last "
260              "changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
261              cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
262              cache->modtime.tm_hour, cache->modtime.tm_min);
263   send_reply(cptr, RPL_ENDOFMOTD);
264 #else
265   motd_forward(cptr, cache);
266 #endif
267 }
268
269 /* motd_recache causes all the MOTD caches to be cleared */
270 void
271 motd_recache(void)
272 {
273   struct Motd* tmp;
274
275   motd_decache(MotdList.local); /* decache local and remote MOTDs */
276   motd_decache(MotdList.remote);
277
278   for (tmp = MotdList.other; tmp; tmp = tmp->next) /* now all the others */
279     motd_decache(tmp);
280
281   /* now recache local and remote MOTDs */
282   motd_cache(MotdList.local);
283   motd_cache(MotdList.remote);
284 }
285
286 /* motd_init initializes the MOTD routines, including reading the
287  * ircd.motd and remote.motd files into cache
288  */
289 void
290 motd_init(void)
291 {
292   MotdList.local = motd_create(0, MPATH, MOTD_MAXLINES); /* init local */
293   motd_cache(MotdList.local); /* and cache it */
294
295   MotdList.remote = motd_create(0, RPATH, MOTD_MAXREMOTE); /* init remote */
296   motd_cache(MotdList.remote); /* and cache it */
297
298   MotdList.other = 0; /* no T-lines processed yet */
299 }
300
301 /* This routine adds a MOTD */
302 void
303 motd_add(const char *hostmask, const char *path)
304 {
305   struct Motd *tmp;
306
307   tmp = motd_create(hostmask, path, MOTD_MAXLINES); /* create the motd */
308
309   tmp->next = MotdList.other; /* link it into the list */
310   MotdList.other = tmp;
311 }
312
313 /* This routine clears the list of MOTDs */
314 void
315 motd_clear(void)
316 {
317   struct Motd *ptr, *next;
318
319   motd_decache(MotdList.local); /* decache local and remote MOTDs */
320   motd_decache(MotdList.remote);
321
322   if (MotdList.other) /* destroy other MOTDs */
323     for (ptr = MotdList.other; ptr; ptr = next) {
324       next = ptr->next;
325       motd_destroy(ptr);
326     }
327
328   MotdList.other = 0;
329
330   /* now recache local and remote MOTDs */
331   motd_cache(MotdList.local);
332   motd_cache(MotdList.remote);
333 }
334
335 /* This is called to report T-lines */
336 void
337 motd_report(struct Client *to)
338 {
339   struct Motd *ptr;
340
341   for (ptr = MotdList.other; ptr; ptr = ptr->next) {
342     if (ptr->type == MOTD_CLASS) /* class requires special handling */
343       send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %d %s", ptr->id.class,
344                  ptr->path);
345     else if (ptr->type == MOTD_HOSTMASK)
346       send_reply(to, RPL_STATSTLINE, 'T', ptr->id.hostmask, ptr->path);
347   }
348 }