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 "client.h"
28 #include "ircd.h"
29 #include "ircd_reply.h"
30 #include "ircd_string.h"
31 #include "match.h"
32 #include "msg.h"
33 #include "numeric.h"
34 #include "numnicks.h"
35 #include "s_conf.h"
36 #include "class.h"
37 #include "s_user.h"
38 #include "send.h"
39
40 #include <stdlib.h>
41 #include <assert.h>
42
43 /* This routine returns the TRecord structure for a user, or 0 if there
44  * is no matching T-line--in which case, we should use the standard
45  * MOTD.
46  */
47 struct TRecord *
48 motd_find(struct Client* cptr)
49 {
50   struct TRecord *ptr;
51   int class = -1;
52
53   assert(0 != cptr);
54
55   if (MyUser(cptr))
56     class = get_client_class(cptr);
57
58   for (ptr = tdata; ptr; ptr = ptr->next) {
59     if (class >= 0 && IsDigit(*ptr->hostmask)) {
60       if (atoi(ptr->hostmask) == class)
61         return ptr;
62     } else if (!match(ptr->hostmask, cptr->sockhost))
63       return ptr;
64   }
65
66   return 0;
67 }
68
69 /* This routine is used to send the MOTD off to a user. */
70 int
71 motd_send(struct Client* cptr, struct TRecord* trec)
72 {
73   struct MotdItem *t_motd;
74   struct tm *t_tm;
75   int count;
76
77   assert(0 != cptr);
78
79   if (!MyUser(cptr)) { /* not our user, send the remote MOTD */
80     t_motd = rmotd;
81     t_tm = 0;
82   } else if (trec) { /* We were given a TRecord */
83     t_motd = trec->tmotd;
84     t_tm = &trec->tmotd_tm;
85   } else { /* use the basic MOTD */
86     t_motd = motd;
87     t_tm = &motd_tm;
88   }
89
90   if (!t_motd) /* No motd to send */
91     return send_reply(cptr, ERR_NOMOTD);
92
93   /* this is a change; we now always send the start numeric */
94   send_reply(cptr, RPL_MOTDSTART, me.name);
95
96   if (t_tm) { /* We should probably go for ISO dates here: yyyy-mm-dd. */
97     send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d/%d/%d %d:%02d",
98                t_tm->tm_mday, t_tm->tm_mon + 1, 1900 + t_tm->tm_year,
99                t_tm->tm_hour, t_tm->tm_min);
100     count = 100;
101   } else
102     count = 3;
103
104   for (; t_motd; t_motd = t_motd->next) { /* send along the MOTD */
105     send_reply(cptr, RPL_MOTD, t_motd->line);
106     if (!--count)
107       break;
108   }
109
110   send_reply(cptr, RPL_ENDOFMOTD); /* end */
111
112   return 0; /* Convenience return */
113 }
114
115 /* This routine sends the MOTD or something to newly-registered users. */
116 void
117 motd_signon(struct Client* cptr)
118 {
119   struct TRecord *trec;
120   struct tm *t_tm = &motd_tm;
121
122   if ((trec = motd_find(cptr)))
123     t_tm = &trec->tmotd_tm;
124
125 #ifdef NODEFAULTMOTD
126   send_reply(cptr, RPL_MOTDSTART, me.name);
127   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the AUP "
128              "before continuing using this service.\002");
129   /* Perhaps we should switch to an ISO date here? */
130   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was last "
131              "changed: %d/%d/%d", t_tm->tm_mday, t_tm->tm_mon + 1,
132              1900 + t_tm->tm_year);
133   send_reply(cptr, RPL_ENDOFMOTD);
134 #else
135   motd_send(cptr, trec);
136 #endif
137 }