1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* |
4 |
* Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu> |
5 |
* Copyright (C) 2013 by the Hybrid Development Team. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
*/ |
22 |
|
23 |
/*! \file motd.c |
24 |
* \brief Message-of-the-day manipulation implementation. |
25 |
* \version $Id$ |
26 |
*/ |
27 |
|
28 |
#include "stdinc.h" |
29 |
#include "list.h" |
30 |
#include "ircd.h" |
31 |
#include "conf.h" |
32 |
#include "send.h" |
33 |
#include "s_serv.h" |
34 |
#include "numeric.h" |
35 |
#include "client.h" |
36 |
#include "irc_string.h" |
37 |
#include "memory.h" |
38 |
#include "log.h" |
39 |
#include "motd.h" |
40 |
|
41 |
|
42 |
/** Global list of messages of the day. */ |
43 |
static struct |
44 |
{ |
45 |
struct Motd *local; /**< Local MOTD. */ |
46 |
struct Motd *remote; /**< Remote MOTD. */ |
47 |
dlink_list other; /**< MOTDs specified in configuration file. */ |
48 |
dlink_list cachelist; /**< List of MotdCache entries. */ |
49 |
} MotdList; |
50 |
|
51 |
|
52 |
/*! \brief Create a struct Motd and initialize it. |
53 |
* \param hostmask Hostmask (or connection class name) to filter on. |
54 |
* \param path Path to MOTD file. |
55 |
*/ |
56 |
static struct Motd * |
57 |
motd_create(const char *hostmask, const char *path) |
58 |
{ |
59 |
struct Motd *tmp = MyMalloc(sizeof(struct Motd)); |
60 |
|
61 |
if (EmptyString(hostmask)) |
62 |
tmp->type = MOTD_UNIVERSAL; |
63 |
else if (class_find(hostmask, 1)) |
64 |
tmp->type = MOTD_CLASS; |
65 |
else |
66 |
tmp->type = MOTD_HOSTMASK; |
67 |
|
68 |
if (hostmask != NULL) |
69 |
tmp->hostmask = xstrdup(hostmask); |
70 |
|
71 |
tmp->path = xstrdup(path); |
72 |
tmp->maxcount = MOTD_MAXLINES; |
73 |
|
74 |
return tmp; |
75 |
} |
76 |
|
77 |
/*! brief\ This function reads a motd out of a file (if needed) and caches it. |
78 |
* If a matching cache entry already exists, reuse it. Otherwise, |
79 |
* allocate and populate a new MotdCache for it. |
80 |
* \param motd Specification for MOTD file. |
81 |
* \return Matching MotdCache entry. |
82 |
*/ |
83 |
static struct MotdCache * |
84 |
motd_cache(struct Motd *motd) |
85 |
{ |
86 |
FILE *file = NULL; |
87 |
struct MotdCache *cache = NULL; |
88 |
struct stat sb; |
89 |
char line[MOTD_LINESIZE + 2]; /* \r\n */ |
90 |
char *tmp = NULL; |
91 |
int i; |
92 |
dlink_node *ptr = NULL; |
93 |
|
94 |
assert(motd); |
95 |
assert(motd->path); |
96 |
|
97 |
if (motd->cache) |
98 |
return motd->cache; |
99 |
|
100 |
/* try to find it in the list of cached files... */ |
101 |
DLINK_FOREACH(ptr, MotdList.cachelist.head) |
102 |
{ |
103 |
cache = ptr->data; |
104 |
|
105 |
if (!strcmp(cache->path, motd->path) && cache->maxcount == motd->maxcount) |
106 |
{ |
107 |
cache->ref++; /* increase reference count... */ |
108 |
motd->cache = cache; /* remember cache... */ |
109 |
return motd->cache; /* return it */ |
110 |
} |
111 |
} |
112 |
|
113 |
/* gotta read in the file, now */ |
114 |
if ((file = fopen(motd->path, "r")) == NULL) |
115 |
{ |
116 |
ilog(LOG_TYPE_IRCD, "Couldn't open \"%s\": %s", motd->path, |
117 |
strerror(errno)); |
118 |
return 0; |
119 |
} |
120 |
|
121 |
/* need the file's modification time */ |
122 |
if (stat(motd->path, &sb) == -1) |
123 |
{ |
124 |
fclose(file); |
125 |
return 0; |
126 |
} |
127 |
|
128 |
/* Ok, allocate a structure; we'll realloc later to trim memory */ |
129 |
cache = MyMalloc(sizeof(struct MotdCache) + (MOTD_LINESIZE * (MOTD_MAXLINES - 1))); |
130 |
cache->ref = 1; |
131 |
cache->path = xstrdup(motd->path); |
132 |
cache->maxcount = motd->maxcount; |
133 |
cache->modtime = *localtime((time_t *)&sb.st_mtime); /* store modtime */ |
134 |
|
135 |
while (cache->count < cache->maxcount && fgets(line, sizeof(line), file)) |
136 |
{ |
137 |
/* copy over line, stopping when we overflow or hit line end */ |
138 |
for (tmp = line, i = 0; i < (MOTD_LINESIZE - 1) && *tmp && |
139 |
*tmp != '\r' && *tmp != '\n'; tmp++, i++) |
140 |
cache->motd[cache->count][i] = *tmp; |
141 |
cache->motd[cache->count][i] = '\0'; |
142 |
|
143 |
cache->count++; |
144 |
} |
145 |
|
146 |
fclose(file); /* close the file */ |
147 |
|
148 |
/* trim memory usage a little */ |
149 |
motd->cache = MyMalloc(sizeof(struct MotdCache) + |
150 |
(MOTD_LINESIZE * (cache->count - 1))); |
151 |
memcpy(motd->cache, cache, sizeof(struct MotdCache) + |
152 |
(MOTD_LINESIZE * (cache->count - 1))); |
153 |
MyFree(cache); |
154 |
|
155 |
/* now link it in... */ |
156 |
dlinkAdd(motd->cache, &motd->cache->node, &MotdList.cachelist); |
157 |
|
158 |
return motd->cache; |
159 |
} |
160 |
|
161 |
/*! \brief Clear and dereference the Motd::cache element of \a motd. |
162 |
* If the MotdCache::ref count goes to zero, free it. |
163 |
* \param motd MOTD to uncache. |
164 |
*/ |
165 |
static void |
166 |
motd_decache(struct Motd *motd) |
167 |
{ |
168 |
struct MotdCache *cache = NULL; |
169 |
|
170 |
assert(motd); |
171 |
|
172 |
if ((cache = motd->cache) == NULL) /* we can be called for records with no cache */ |
173 |
return; |
174 |
|
175 |
motd->cache = NULL; /* zero the cache */ |
176 |
|
177 |
if (!--cache->ref) /* reduce reference count... */ |
178 |
{ |
179 |
dlinkDelete(&cache->node, &MotdList.cachelist); |
180 |
MyFree(cache->path); /* free path info... */ |
181 |
MyFree(cache); /* very simple for a reason... */ |
182 |
} |
183 |
} |
184 |
|
185 |
/*! \brief Deallocate a MOTD structure. |
186 |
* If it has cached content, uncache it. |
187 |
* \param motd MOTD to destroy. |
188 |
*/ |
189 |
static void |
190 |
motd_destroy(struct Motd *motd) |
191 |
{ |
192 |
assert(motd); |
193 |
|
194 |
if (motd->cache) /* drop the cache */ |
195 |
motd_decache(motd); |
196 |
|
197 |
dlinkDelete(&motd->node, &MotdList.other); |
198 |
MyFree(motd->path); /* we always must have a path */ |
199 |
MyFree(motd->hostmask); |
200 |
MyFree(motd); |
201 |
} |
202 |
|
203 |
/*! \brief Find the first matching MOTD block for a user. |
204 |
* If the user is remote, always use remote MOTD. |
205 |
* Otherwise, if there is a hostmask- or class-based MOTD that matches |
206 |
* the user, use it. |
207 |
* Otherwise, use the local MOTD. |
208 |
* \param client_p Client to find MOTD for. |
209 |
* \return Pointer to first matching MOTD for the client. |
210 |
*/ |
211 |
static struct Motd * |
212 |
motd_lookup(struct Client *client_p) |
213 |
{ |
214 |
dlink_node *ptr = NULL; |
215 |
const struct ClassItem *class = NULL; |
216 |
|
217 |
assert(client_p); |
218 |
|
219 |
if (!MyClient(client_p)) /* not my user, always return remote motd */ |
220 |
return MotdList.remote; |
221 |
|
222 |
class = get_class_ptr(&client_p->localClient->confs); |
223 |
assert(class); |
224 |
|
225 |
/* check the motd blocks first */ |
226 |
DLINK_FOREACH(ptr, MotdList.other.head) |
227 |
{ |
228 |
struct Motd *motd = ptr->data; |
229 |
|
230 |
switch (motd->type) |
231 |
{ |
232 |
case MOTD_CLASS: |
233 |
if (!match(motd->hostmask, class->name)) |
234 |
return motd; |
235 |
case MOTD_HOSTMASK: |
236 |
if (!match(motd->hostmask, client_p->host)) |
237 |
return motd; |
238 |
default: break; |
239 |
} |
240 |
} |
241 |
|
242 |
return MotdList.local; /* Ok, return the default motd */ |
243 |
} |
244 |
|
245 |
/*! \brief Send the content of a MotdCache to a user. |
246 |
* If \a cache is NULL, simply send ERR_NOMOTD to the client. |
247 |
* \param source_p Client to send MOTD to. |
248 |
* \param cache MOTD body to send to client. |
249 |
*/ |
250 |
static void |
251 |
motd_forward(struct Client *source_p, const struct MotdCache *cache) |
252 |
{ |
253 |
int i; |
254 |
const char *from = ID_or_name(&me, source_p->from); |
255 |
const char *to = ID_or_name(source_p, source_p->from); |
256 |
|
257 |
assert(source_p); |
258 |
|
259 |
if (!cache) /* no motd to send */ |
260 |
{ |
261 |
sendto_one(source_p, form_str(ERR_NOMOTD), from, to); |
262 |
return; |
263 |
} |
264 |
|
265 |
/* send the motd */ |
266 |
sendto_one(source_p, form_str(RPL_MOTDSTART), |
267 |
from, to, me.name); |
268 |
|
269 |
for (i = 0; i < cache->count; i++) |
270 |
sendto_one(source_p, form_str(RPL_MOTD), |
271 |
from, to, cache->motd[i]); |
272 |
sendto_one(source_p, form_str(RPL_ENDOFMOTD), from, to); |
273 |
} |
274 |
|
275 |
/*! \brief Find the MOTD for a client and send it. |
276 |
* \param client_p Client being greeted. |
277 |
*/ |
278 |
void |
279 |
motd_send(struct Client *client_p) |
280 |
{ |
281 |
assert(client_p); |
282 |
|
283 |
motd_forward(client_p, motd_cache(motd_lookup(client_p))); |
284 |
} |
285 |
|
286 |
/*! \brief Send the signon MOTD to a user. |
287 |
* If FEAT_NODEFAULTMOTD is true and a matching MOTD exists for the |
288 |
* user, direct the client to type /MOTD to read it. Otherwise, call |
289 |
* motd_forward() for the user. |
290 |
* \param source_p Client that has just connected. |
291 |
*/ |
292 |
void |
293 |
motd_signon(struct Client *source_p) |
294 |
{ |
295 |
const struct MotdCache *cache = motd_cache(motd_lookup(source_p)); |
296 |
|
297 |
if (!ConfigFileEntry.short_motd || !cache) |
298 |
motd_forward(source_p, cache); |
299 |
else |
300 |
{ |
301 |
sendto_one(source_p, ":%s NOTICE %s :*** Notice -- motd was last changed at %d/%d/%d %d:%02d", |
302 |
me.name, source_p->name, cache->modtime.tm_year + 1900, |
303 |
cache->modtime.tm_mon + 1, |
304 |
cache->modtime.tm_mday, |
305 |
cache->modtime.tm_hour, |
306 |
cache->modtime.tm_min); |
307 |
sendto_one(source_p, |
308 |
":%s NOTICE %s :*** Notice -- Please read the motd if you haven't " |
309 |
"read it", me.name, source_p->name); |
310 |
sendto_one(source_p, form_str(RPL_MOTDSTART), |
311 |
me.name, source_p->name, me.name); |
312 |
sendto_one(source_p, form_str(RPL_MOTD), |
313 |
me.name, source_p->name, |
314 |
"*** This is the short motd ***"); |
315 |
sendto_one(source_p, form_str(RPL_ENDOFMOTD), |
316 |
me.name, source_p->name); |
317 |
} |
318 |
} |
319 |
|
320 |
/*! \brief Clear all cached MOTD bodies. |
321 |
* The local and remote MOTDs are re-cached immediately. |
322 |
*/ |
323 |
void |
324 |
motd_recache(void) |
325 |
{ |
326 |
dlink_node *ptr = NULL; |
327 |
|
328 |
motd_decache(MotdList.local); /* decache local and remote MOTDs */ |
329 |
motd_decache(MotdList.remote); |
330 |
|
331 |
DLINK_FOREACH(ptr, MotdList.other.head) /* now all the others */ |
332 |
motd_decache(ptr->data); |
333 |
|
334 |
/* now recache local and remote MOTDs */ |
335 |
motd_cache(MotdList.local); |
336 |
motd_cache(MotdList.remote); |
337 |
} |
338 |
|
339 |
/*! \brief Re-cache the local and remote MOTDs. |
340 |
* If they already exist, they are deallocated first. |
341 |
*/ |
342 |
void |
343 |
motd_init(void) |
344 |
{ |
345 |
if (MotdList.local) /* destroy old local... */ |
346 |
motd_destroy(MotdList.local); |
347 |
|
348 |
MotdList.local = motd_create(0, MPATH); |
349 |
motd_cache(MotdList.local); /* init local and cache it */ |
350 |
|
351 |
if (MotdList.remote) /* destroy old remote... */ |
352 |
motd_destroy(MotdList.remote); |
353 |
|
354 |
MotdList.remote = motd_create(0, MPATH); |
355 |
motd_cache(MotdList.remote); /* init remote and cache it */ |
356 |
} |
357 |
|
358 |
/* \brief Add a new MOTD. |
359 |
* \param hostmask Hostmask (or connection class name) to send this to. |
360 |
* \param path Pathname of file to send. |
361 |
*/ |
362 |
void |
363 |
motd_add(const char *hostmask, const char *path) |
364 |
{ |
365 |
struct Motd *motd = motd_create(hostmask, path); /* create the motd */ |
366 |
|
367 |
dlinkAdd(motd, &motd->node, &MotdList.other); |
368 |
} |
369 |
|
370 |
/*! \brief Clear out all MOTDs. |
371 |
* Compared to motd_recache(), this destroys all hostmask- or |
372 |
* class-based MOTDs rather than simply uncaching them. |
373 |
* Re-cache the local and remote MOTDs. |
374 |
*/ |
375 |
void |
376 |
motd_clear(void) |
377 |
{ |
378 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
379 |
|
380 |
motd_decache(MotdList.local); /* decache local and remote MOTDs */ |
381 |
motd_decache(MotdList.remote); |
382 |
|
383 |
DLINK_FOREACH_SAFE(ptr, ptr_next, MotdList.other.head) /* destroy other MOTDs */ |
384 |
motd_destroy(ptr->data); |
385 |
|
386 |
/* now recache local and remote MOTDs */ |
387 |
motd_cache(MotdList.local); |
388 |
motd_cache(MotdList.remote); |
389 |
} |
390 |
|
391 |
/*! \brief Report list of non-default MOTDs. |
392 |
* \param source_p Client requesting statistics. |
393 |
*/ |
394 |
void |
395 |
motd_report(struct Client *source_p) |
396 |
{ |
397 |
const dlink_node *ptr = NULL; |
398 |
|
399 |
DLINK_FOREACH(ptr, MotdList.other.head) |
400 |
{ |
401 |
const struct Motd *motd = ptr->data; |
402 |
|
403 |
sendto_one(source_p, form_str(RPL_STATSTLINE), |
404 |
me.name, source_p->name, |
405 |
motd->hostmask, motd->path); |
406 |
} |
407 |
} |
408 |
|
409 |
/*! \brief Report MOTD memory usage to a client. |
410 |
* \param source_p Client requesting memory usage. |
411 |
*/ |
412 |
void |
413 |
motd_memory_count(struct Client *source_p) |
414 |
{ |
415 |
const dlink_node *ptr = NULL; |
416 |
unsigned int mt = 0; /* motd count */ |
417 |
unsigned int mtc = 0; /* motd cache count */ |
418 |
size_t mtm = 0; /* memory consumed by motd */ |
419 |
size_t mtcm = 0; /* memory consumed by motd cache */ |
420 |
|
421 |
if (MotdList.local) |
422 |
{ |
423 |
mt++; |
424 |
mtm += sizeof(struct Motd); |
425 |
mtm += MotdList.local->path ? (strlen(MotdList.local->path) + 1) : 0; |
426 |
} |
427 |
|
428 |
if (MotdList.remote) |
429 |
{ |
430 |
mt++; |
431 |
mtm += sizeof(struct Motd); |
432 |
mtm += MotdList.remote->path ? (strlen(MotdList.remote->path) + 1) : 0; |
433 |
} |
434 |
|
435 |
DLINK_FOREACH(ptr, MotdList.other.head) |
436 |
{ |
437 |
const struct MotdCache *motd = ptr->data; |
438 |
|
439 |
mt++; |
440 |
mtm += sizeof(struct Motd); |
441 |
mtm += motd->path ? (strlen(motd->path) + 1) : 0; |
442 |
} |
443 |
|
444 |
DLINK_FOREACH(ptr, MotdList.cachelist.head) |
445 |
{ |
446 |
const struct MotdCache *cache = ptr->data; |
447 |
|
448 |
mtc++; |
449 |
mtcm += sizeof(struct MotdCache) + (MOTD_LINESIZE * (cache->count - 1)); |
450 |
} |
451 |
|
452 |
sendto_one(source_p, ":%s %d %s z :Motds %u(%u) Cache %u(%u)", |
453 |
me.name, RPL_STATSDEBUG, source_p->name, |
454 |
mt, mtm, mtc, mtcm); |
455 |
} |