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