]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/motd.c
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>
7 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
25 * @brief Message-of-the-day manipulation implementation.
26 * @version $Id: motd.c,v 1.22 2005/05/10 03:43:08 entrope Exp $
35 #include "ircd_alloc.h"
36 #include "ircd_features.h"
38 #include "ircd_reply.h"
39 #include "ircd_string.h"
50 /* #include <assert.h> -- Now using assert in ircd_log.h */
56 /** Global list of messages of the day. */
58 struct Motd
* local
; /**< Local MOTD. */
59 struct Motd
* remote
; /**< Remote MOTD. */
60 struct Motd
* other
; /**< MOTDs specified in configuration file. */
61 struct Motd
* freelist
; /**< Currently unused Motd structs. */
62 struct MotdCache
* cachelist
; /**< List of MotdCache entries. */
63 } MotdList
= { 0, 0, 0, 0, 0 };
65 /** Create a struct Motd and initialize it.
66 * @param[in] hostmask Hostmask (or connection class name) to filter on.
67 * @param[in] path Path to MOTD file.
68 * @param[in] maxcount Maximum number of lines permitted for MOTD.
71 motd_create(const char *hostmask
, const char *path
, int maxcount
)
77 /* allocate memory and initialize the structure */
78 if (MotdList
.freelist
)
80 tmp
= MotdList
.freelist
;
81 MotdList
.freelist
= tmp
->next
;
83 tmp
= (struct Motd
*)MyMalloc(sizeof(struct Motd
));
87 tmp
->type
= MOTD_UNIVERSAL
;
88 else if (find_class(hostmask
))
89 tmp
->type
= MOTD_CLASS
;
90 else if (ipmask_parse(hostmask
, &tmp
->address
, &tmp
->addrbits
))
91 tmp
->type
= MOTD_IPMASK
;
93 tmp
->type
= MOTD_HOSTMASK
;
96 DupString(tmp
->hostmask
, hostmask
);
100 DupString(tmp
->path
, path
);
101 tmp
->maxcount
= maxcount
;
107 /** This function reads a motd out of a file (if needed) and caches it.
108 * If a matching cache entry already exists, reuse it. Otherwise,
109 * allocate and populate a new MotdCache for it.
110 * @param[in] motd Specification for MOTD file.
111 * @return Matching MotdCache entry.
113 static struct MotdCache
*
114 motd_cache(struct Motd
*motd
)
117 struct MotdCache
* cache
;
119 char line
[MOTD_LINESIZE
+ 2]; /* \r\n */
124 assert(0 != motd
->path
);
129 /* try to find it in the list of cached files... */
130 for (cache
= MotdList
.cachelist
; cache
; cache
= cache
->next
) {
131 if (!strcmp(cache
->path
, motd
->path
) &&
132 cache
->maxcount
== motd
->maxcount
) { /* found one... */
133 cache
->ref
++; /* increase reference count... */
134 motd
->cache
= cache
; /* remember cache... */
135 return motd
->cache
; /* return it */
139 /* gotta read in the file, now */
140 if (!(file
= fbopen(motd
->path
, "r"))) {
141 Debug((DEBUG_ERROR
, "Couldn't open \"%s\": %s", motd
->path
,
146 /* need the file's modification time */
147 if (-1 == fbstat(&sb
, file
)) {
152 /* Ok, allocate a structure; we'll realloc later to trim memory */
153 cache
= (struct MotdCache
*)MyMalloc(sizeof(struct MotdCache
) +
154 (MOTD_LINESIZE
* (MOTD_MAXLINES
- 1)));
157 DupString(cache
->path
, motd
->path
);
158 cache
->maxcount
= motd
->maxcount
;
160 cache
->modtime
= *localtime((time_t *) &sb
.st_mtime
); /* store modtime */
163 while (cache
->count
< cache
->maxcount
&& fbgets(line
, sizeof(line
), file
)) {
164 /* copy over line, stopping when we overflow or hit line end */
165 for (tmp
= line
, i
= 0;
166 i
< (MOTD_LINESIZE
- 1) && *tmp
&& *tmp
!= '\r' && *tmp
!= '\n';
168 cache
->motd
[cache
->count
][i
] = *tmp
;
169 cache
->motd
[cache
->count
][i
] = '\0';
174 fbclose(file
); /* close the file */
176 /* trim memory usage a little */
177 motd
->cache
= (struct MotdCache
*)MyMalloc(sizeof(struct MotdCache
) +
178 (MOTD_LINESIZE
* (cache
->count
- 1)));
179 memcpy(motd
->cache
, cache
, sizeof(struct MotdCache
) +
180 (MOTD_LINESIZE
* (cache
->count
- 1)));
183 /* now link it in... */
184 motd
->cache
->next
= MotdList
.cachelist
;
185 motd
->cache
->prev_p
= &MotdList
.cachelist
;
186 if (MotdList
.cachelist
)
187 MotdList
.cachelist
->prev_p
= &motd
->cache
->next
;
188 MotdList
.cachelist
= motd
->cache
;
193 /** Clear and dereference the Motd::cache element of \a motd.
194 * If the MotdCache::ref count goes to zero, free it.
195 * @param[in] motd MOTD to uncache.
198 motd_decache(struct Motd
*motd
)
200 struct MotdCache
* cache
;
204 if (!(cache
= motd
->cache
)) /* we can be called for records with no cache */
207 motd
->cache
= 0; /* zero the cache */
209 if (!--cache
->ref
) { /* reduce reference count... */
210 if (cache
->next
) /* ref is 0, delink from list and free */
211 cache
->next
->prev_p
= cache
->prev_p
;
212 *cache
->prev_p
= cache
->next
;
214 MyFree(cache
->path
); /* free path info... */
216 MyFree(cache
); /* very simple for a reason... */
220 /** Deallocate a MOTD structure.
221 * If it has cached content, uncache it.
222 * @param[in] motd MOTD to destroy.
225 motd_destroy(struct Motd
*motd
)
229 MyFree(motd
->path
); /* we always must have a path */
230 MyFree(motd
->hostmask
);
231 if (motd
->cache
) /* drop the cache */
234 motd
->next
= MotdList
.freelist
;
235 MotdList
.freelist
= motd
;
238 /** Find the first matching MOTD block for a user.
239 * If the user is remote, always use remote MOTD.
240 * Otherwise, if there is a hostmask- or class-based MOTD that matches
242 * Otherwise, use the local MOTD.
243 * @param[in] cptr Client to find MOTD for.
244 * @return Pointer to first matching MOTD for the client.
247 motd_lookup(struct Client
*cptr
)
250 char *c_class
= NULL
;
254 if (!MyUser(cptr
)) /* not my user, always return remote motd */
255 return MotdList
.remote
;
257 c_class
= get_client_class(cptr
);
258 assert(c_class
!= NULL
);
260 /* check the motd blocks first */
261 for (ptr
= MotdList
.other
; ptr
; ptr
= ptr
->next
)
263 if (ptr
->type
== MOTD_CLASS
264 && !match(ptr
->hostmask
, c_class
))
266 else if (ptr
->type
== MOTD_HOSTMASK
267 && !match(ptr
->hostmask
, cli_sockhost(cptr
)))
269 else if (ptr
->type
== MOTD_IPMASK
270 && ipmask_check(&cli_ip(cptr
), &ptr
->address
, ptr
->addrbits
))
274 return MotdList
.local
; /* Ok, return the default motd */
277 /** Send the content of a MotdCache to a user.
278 * If \a cache is NULL, simply send ERR_NOMOTD to the client.
279 * @param[in] cptr Client to send MOTD to.
280 * @param[in] cache MOTD body to send to client.
283 motd_forward(struct Client
*cptr
, struct MotdCache
*cache
)
289 if (!cache
) /* no motd to send */
290 return send_reply(cptr
, ERR_NOMOTD
);
293 send_reply(cptr
, RPL_MOTDSTART
, cli_name(&me
));
294 send_reply(cptr
, SND_EXPLICIT
| RPL_MOTD
, ":- %d-%d-%d %d:%02d",
295 cache
->modtime
.tm_year
+ 1900, cache
->modtime
.tm_mon
+ 1,
296 cache
->modtime
.tm_mday
, cache
->modtime
.tm_hour
,
297 cache
->modtime
.tm_min
);
299 for (i
= 0; i
< cache
->count
; i
++)
300 send_reply(cptr
, RPL_MOTD
, cache
->motd
[i
]);
302 return send_reply(cptr
, RPL_ENDOFMOTD
); /* end */
305 /** Find the MOTD for a client and send it.
306 * @param[in] cptr Client being greeted.
309 motd_send(struct Client
* cptr
)
313 return motd_forward(cptr
, motd_cache(motd_lookup(cptr
)));
316 /** Send the signon MOTD to a user.
317 * If FEAT_NODEFAULTMOTD is true and a matching MOTD exists for the
318 * user, direct the client to type /MOTD to read it. Otherwise, call
319 * motd_forward() for the user.
320 * @param[in] cptr Client that has just connected.
323 motd_signon(struct Client
* cptr
)
325 struct MotdCache
*cache
;
326 const char *banner
= NULL
;
328 cache
= motd_cache(motd_lookup(cptr
));
330 if (!feature_bool(FEAT_NODEFAULTMOTD
) || !cache
)
331 motd_forward(cptr
, cache
);
333 send_reply(cptr
, RPL_MOTDSTART
, cli_name(&me
));
334 if ((banner
= feature_str(FEAT_MOTD_BANNER
)))
335 send_reply(cptr
, SND_EXPLICIT
| RPL_MOTD
, ":%s", banner
);
336 send_reply(cptr
, SND_EXPLICIT
| RPL_MOTD
, ":\002Type /MOTD to read the "
337 "AUP before continuing using this service.\002");
338 send_reply(cptr
, SND_EXPLICIT
| RPL_MOTD
, ":The message of the day was "
339 "last changed: %d-%d-%d %d:%d", cache
->modtime
.tm_year
+ 1900,
340 cache
->modtime
.tm_mon
+ 1, cache
->modtime
.tm_mday
,
341 cache
->modtime
.tm_hour
, cache
->modtime
.tm_min
);
342 send_reply(cptr
, RPL_ENDOFMOTD
);
346 /** Clear all cached MOTD bodies.
347 * The local and remote MOTDs are re-cached immediately.
354 motd_decache(MotdList
.local
); /* decache local and remote MOTDs */
355 motd_decache(MotdList
.remote
);
357 for (tmp
= MotdList
.other
; tmp
; tmp
= tmp
->next
) /* now all the others */
360 /* now recache local and remote MOTDs */
361 motd_cache(MotdList
.local
);
362 motd_cache(MotdList
.remote
);
365 /** Re-cache the local and remote MOTDs.
366 * If they already exist, they are deallocated first.
371 if (MotdList
.local
) /* destroy old local... */
372 motd_destroy(MotdList
.local
);
374 MotdList
.local
= motd_create(0, feature_str(FEAT_MPATH
), MOTD_MAXLINES
);
375 motd_cache(MotdList
.local
); /* init local and cache it */
377 if (MotdList
.remote
) /* destroy old remote... */
378 motd_destroy(MotdList
.remote
);
380 MotdList
.remote
= motd_create(0, feature_str(FEAT_RPATH
), MOTD_MAXREMOTE
);
381 motd_cache(MotdList
.remote
); /* init remote and cache it */
385 * @param[in] hostmask Hostmask (or connection class name) to send this to.
386 * @param[in] path Pathname of file to send.
389 motd_add(const char *hostmask
, const char *path
)
393 tmp
= motd_create(hostmask
, path
, MOTD_MAXLINES
); /* create the motd */
395 tmp
->next
= MotdList
.other
; /* link it into the list */
396 MotdList
.other
= tmp
;
399 /** Clear out all MOTDs.
400 * Compared to motd_recache(), this destroys all hostmask- or
401 * class-based MOTDs rather than simply uncaching them.
402 * Re-cache the local and remote MOTDs.
407 struct Motd
*ptr
, *next
;
409 motd_decache(MotdList
.local
); /* decache local and remote MOTDs */
410 motd_decache(MotdList
.remote
);
412 if (MotdList
.other
) /* destroy other MOTDs */
413 for (ptr
= MotdList
.other
; ptr
; ptr
= next
)
421 /* now recache local and remote MOTDs */
422 motd_cache(MotdList
.local
);
423 motd_cache(MotdList
.remote
);
426 /** Report list of non-default MOTDs.
427 * @param[in] to Client requesting statistics.
428 * @param[in] sd Stats descriptor for request (ignored).
429 * @param[in] param Extra parameter from user (ignored).
432 motd_report(struct Client
*to
, const struct StatDesc
*sd
, char *param
)
436 for (ptr
= MotdList
.other
; ptr
; ptr
= ptr
->next
)
437 send_reply(to
, SND_EXPLICIT
| RPL_STATSTLINE
, "T %s %s",
438 ptr
->hostmask
, ptr
->path
);
441 /** Report MOTD memory usage to a client.
442 * @param[in] cptr Client requesting memory usage.
445 motd_memory_count(struct Client
*cptr
)
448 struct MotdCache
*cache
;
449 unsigned int mt
= 0, /* motd count */
450 mtc
= 0, /* motd cache count */
451 mtf
= 0; /* motd free list count */
452 size_t mtm
= 0, /* memory consumed by motd */
453 mtcm
= 0; /* memory consumed by motd cache */
457 mtm
+= sizeof(struct Motd
);
458 mtm
+= MotdList
.local
->path
? (strlen(MotdList
.local
->path
) + 1) : 0;
464 mtm
+= sizeof(struct Motd
);
465 mtm
+= MotdList
.remote
->path
? (strlen(MotdList
.remote
->path
) + 1) : 0;
468 for (ptr
= MotdList
.other
; ptr
; ptr
= ptr
->next
)
471 mtm
+= sizeof(struct Motd
);
472 mtm
+= ptr
->path
? (strlen(ptr
->path
) + 1) : 0;
475 for (cache
= MotdList
.cachelist
; cache
; cache
= cache
->next
)
478 mtcm
+= sizeof(struct MotdCache
) + (MOTD_LINESIZE
* (cache
->count
- 1));
481 if (MotdList
.freelist
)
482 for (ptr
= MotdList
.freelist
; ptr
; ptr
= ptr
->next
)
485 send_reply(cptr
, SND_EXPLICIT
| RPL_STATSDEBUG
,
486 ":Motds %d(%zu) Cache %d(%zu) Free %d(%zu)",
487 mt
, mtm
, mtc
, mtcm
, mtf
, (mtf
* sizeof(struct Motd
)));