1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* motd.c: Message of the day functions. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
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 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "stdinc.h" |
26 |
#include "list.h" |
27 |
#include "motd.h" |
28 |
#include "ircd.h" |
29 |
#include "fdlist.h" |
30 |
#include "s_bsd.h" |
31 |
#include "conf.h" |
32 |
#include "send.h" |
33 |
#include "numeric.h" |
34 |
#include "client.h" |
35 |
#include "irc_string.h" |
36 |
#include "sprintf_irc.h" |
37 |
#include "memory.h" |
38 |
#include "s_serv.h" |
39 |
|
40 |
/* |
41 |
** init_message_file |
42 |
** |
43 |
*/ |
44 |
void |
45 |
init_message_file(MotdType motdType, const char *fileName, MessageFile *motd) |
46 |
{ |
47 |
strlcpy(motd->fileName, fileName, sizeof(motd->fileName)); |
48 |
motd->motdType = motdType; |
49 |
motd->contentsOfFile = NULL; |
50 |
motd->lastChangedDate[0] = '\0'; |
51 |
} |
52 |
|
53 |
/* |
54 |
** send_message_file |
55 |
** |
56 |
** This function split off so a server notice could be generated on a |
57 |
** user requested motd, but not on each connecting client. |
58 |
*/ |
59 |
int |
60 |
send_message_file(struct Client *source_p, MessageFile *motdToPrint) |
61 |
{ |
62 |
MessageFileLine *linePointer; |
63 |
MotdType motdType; |
64 |
const char *from, *to; |
65 |
|
66 |
if (motdToPrint == NULL) |
67 |
return(-1); |
68 |
|
69 |
motdType = motdToPrint->motdType; |
70 |
|
71 |
from = ID_or_name(&me, source_p->from); |
72 |
to = ID_or_name(source_p, source_p->from); |
73 |
|
74 |
switch (motdType) |
75 |
{ |
76 |
case USER_MOTD: |
77 |
if (motdToPrint->contentsOfFile == NULL) |
78 |
sendto_one(source_p, form_str(ERR_NOMOTD), from, to); |
79 |
else |
80 |
{ |
81 |
sendto_one(source_p, form_str(RPL_MOTDSTART), |
82 |
from, to, me.name); |
83 |
|
84 |
for (linePointer = motdToPrint->contentsOfFile; linePointer; |
85 |
linePointer = linePointer->next) |
86 |
{ |
87 |
sendto_one(source_p, form_str(RPL_MOTD), |
88 |
from, to, linePointer->line); |
89 |
} |
90 |
|
91 |
sendto_one(source_p, form_str(RPL_ENDOFMOTD), from, to); |
92 |
} |
93 |
break; |
94 |
|
95 |
case USER_LINKS: |
96 |
if (motdToPrint->contentsOfFile != NULL) |
97 |
{ |
98 |
for (linePointer = motdToPrint->contentsOfFile; linePointer; |
99 |
linePointer = linePointer->next) |
100 |
{ |
101 |
sendto_one(source_p, ":%s 364 %s %s", /* XXX */ |
102 |
from, to, linePointer->line); |
103 |
} |
104 |
} |
105 |
break; |
106 |
|
107 |
case OPER_MOTD: |
108 |
if (motdToPrint->contentsOfFile != NULL) |
109 |
{ |
110 |
sendto_one(source_p, form_str(RPL_OMOTDSTART), |
111 |
me.name, source_p->name, me.name); |
112 |
|
113 |
sendto_one(source_p, form_str(RPL_OMOTD), |
114 |
me.name, source_p->name, motdToPrint->lastChangedDate); |
115 |
|
116 |
for (linePointer = motdToPrint->contentsOfFile; linePointer; |
117 |
linePointer = linePointer->next) |
118 |
{ |
119 |
sendto_one(source_p, form_str(RPL_OMOTD), |
120 |
me.name, source_p->name, linePointer->line); |
121 |
} |
122 |
sendto_one(source_p, form_str(RPL_ENDOFOMOTD), |
123 |
me.name, source_p->name); |
124 |
} |
125 |
break; |
126 |
|
127 |
case ISSUPPORT: |
128 |
if (motdToPrint->contentsOfFile != NULL) |
129 |
{ |
130 |
for (linePointer = motdToPrint->contentsOfFile; linePointer; |
131 |
linePointer = linePointer->next) |
132 |
{ |
133 |
sendto_one(source_p, form_str(RPL_ISUPPORT), |
134 |
me.name, source_p->name, linePointer->line); |
135 |
} |
136 |
} |
137 |
break; |
138 |
|
139 |
default: |
140 |
break; |
141 |
} |
142 |
|
143 |
return(0); |
144 |
} |
145 |
|
146 |
/* |
147 |
* read_message_file() - original From CoMSTuD, added Aug 29, 1996 |
148 |
* |
149 |
* inputs - pointer to MessageFileptr |
150 |
* output - |
151 |
* side effects - |
152 |
*/ |
153 |
int |
154 |
read_message_file(MessageFile *MessageFileptr) |
155 |
{ |
156 |
struct stat sb; |
157 |
struct tm *local_tm; |
158 |
|
159 |
/* used to clear out old MessageFile entries */ |
160 |
MessageFileLine *mptr = 0; |
161 |
MessageFileLine *next_mptr = 0; |
162 |
|
163 |
/* used to add new MessageFile entries */ |
164 |
MessageFileLine *newMessageLine = 0; |
165 |
MessageFileLine *currentMessageLine = 0; |
166 |
|
167 |
char buffer[MESSAGELINELEN]; |
168 |
char *p; |
169 |
FILE *file; |
170 |
|
171 |
for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr) |
172 |
{ |
173 |
next_mptr = mptr->next; |
174 |
MyFree(mptr); |
175 |
} |
176 |
|
177 |
MessageFileptr->contentsOfFile = NULL; |
178 |
|
179 |
if (stat(MessageFileptr->fileName, &sb) < 0) |
180 |
return(-1); |
181 |
|
182 |
local_tm = localtime(&sb.st_mtime); |
183 |
|
184 |
if (local_tm) |
185 |
ircsprintf(MessageFileptr->lastChangedDate, |
186 |
"%d/%d/%d %d:%02d", |
187 |
local_tm->tm_mday, |
188 |
local_tm->tm_mon + 1, |
189 |
1900 + local_tm->tm_year, |
190 |
local_tm->tm_hour, |
191 |
local_tm->tm_min); |
192 |
|
193 |
if ((file = fopen(MessageFileptr->fileName, "r")) == NULL) |
194 |
return(-1); |
195 |
|
196 |
while (fgets(buffer, sizeof(buffer), file)) |
197 |
{ |
198 |
if ((p = strchr(buffer, '\n')) != NULL) |
199 |
*p = '\0'; |
200 |
|
201 |
newMessageLine = (MessageFileLine *)MyMalloc(sizeof(MessageFileLine)); |
202 |
strlcpy(newMessageLine->line, buffer, sizeof(newMessageLine->line)); |
203 |
newMessageLine->next = NULL; |
204 |
|
205 |
if (MessageFileptr->contentsOfFile != NULL) |
206 |
{ |
207 |
if (currentMessageLine) |
208 |
currentMessageLine->next = newMessageLine; |
209 |
|
210 |
currentMessageLine = newMessageLine; |
211 |
} |
212 |
else |
213 |
{ |
214 |
MessageFileptr->contentsOfFile = newMessageLine; |
215 |
currentMessageLine = newMessageLine; |
216 |
} |
217 |
} |
218 |
|
219 |
fclose(file); |
220 |
return(0); |
221 |
} |
222 |
|
223 |
/* |
224 |
* init_MessageLine |
225 |
* |
226 |
* inputs - NONE |
227 |
* output - pointer to new MessageFile |
228 |
* side effects - Use this when an internal Message File is wanted |
229 |
* without reading an actual file. The MessageFile |
230 |
* is init'ed, but must have content added to it through |
231 |
* addto_MessageLine() |
232 |
*/ |
233 |
|
234 |
MessageFile * |
235 |
init_MessageLine(void) |
236 |
{ |
237 |
MessageFile *mf; |
238 |
MessageFileLine *mptr = NULL; |
239 |
|
240 |
mf = MyMalloc(sizeof(MessageFile)); |
241 |
mf->motdType = ISSUPPORT; /* XXX maybe pass it alone in args? */ |
242 |
mptr = MyMalloc(sizeof(MessageFileLine)); |
243 |
mf->contentsOfFile = mptr; |
244 |
return(mf); |
245 |
} |
246 |
|
247 |
/* |
248 |
* addto_MessageLine |
249 |
* |
250 |
* inputs - Pointer to existing MessageFile |
251 |
* - New string to add to this MessageFile |
252 |
* output - NONE |
253 |
* side effects - Use this when an internal MessageFile is wanted |
254 |
* without reading an actual file. Content is added |
255 |
* to this MessageFile through this function. |
256 |
*/ |
257 |
|
258 |
void |
259 |
addto_MessageLine(MessageFile *mf, const char *str) |
260 |
{ |
261 |
MessageFileLine *mptr = mf->contentsOfFile; |
262 |
MessageFileLine *nmptr = NULL; |
263 |
|
264 |
if (mptr == NULL) |
265 |
{ |
266 |
mptr = MyMalloc(sizeof(MessageFileLine)); |
267 |
strcpy(mptr->line, str); |
268 |
mf->contentsOfFile = mptr; |
269 |
} |
270 |
else |
271 |
{ |
272 |
while (mptr->next != NULL) |
273 |
mptr = mptr->next; |
274 |
nmptr = MyMalloc(sizeof(MessageFileLine)); |
275 |
strcpy(nmptr->line, str); |
276 |
mptr->next = nmptr; |
277 |
} |
278 |
} |
279 |
|
280 |
/* |
281 |
* destroy_MessageLine(MessageFile *mf) |
282 |
* |
283 |
* inputs - pointer to the MessageFile to destroy |
284 |
* output - NONE |
285 |
* side effects - All the MessageLines attached to the given mf |
286 |
* Are freed then one MessageLine is recreated |
287 |
*/ |
288 |
void |
289 |
destroy_MessageLine(MessageFile *mf) |
290 |
{ |
291 |
MessageFileLine *mptr = mf->contentsOfFile; |
292 |
MessageFileLine *nmptr = NULL; |
293 |
|
294 |
if (mptr == NULL) |
295 |
return; |
296 |
|
297 |
for (mptr = mf->contentsOfFile; mptr != NULL; mptr = nmptr) |
298 |
{ |
299 |
nmptr = mptr->next; |
300 |
MyFree(mptr); |
301 |
} |
302 |
mf->contentsOfFile = NULL; |
303 |
} |