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