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 ISSUPPORT: |
108 |
if (motdToPrint->contentsOfFile != NULL) |
109 |
{ |
110 |
for (linePointer = motdToPrint->contentsOfFile; linePointer; |
111 |
linePointer = linePointer->next) |
112 |
{ |
113 |
sendto_one(source_p, form_str(RPL_ISUPPORT), |
114 |
me.name, source_p->name, linePointer->line); |
115 |
} |
116 |
} |
117 |
break; |
118 |
|
119 |
default: |
120 |
break; |
121 |
} |
122 |
|
123 |
return(0); |
124 |
} |
125 |
|
126 |
/* |
127 |
* read_message_file() - original From CoMSTuD, added Aug 29, 1996 |
128 |
* |
129 |
* inputs - pointer to MessageFileptr |
130 |
* output - |
131 |
* side effects - |
132 |
*/ |
133 |
int |
134 |
read_message_file(MessageFile *MessageFileptr) |
135 |
{ |
136 |
struct stat sb; |
137 |
struct tm *local_tm; |
138 |
|
139 |
/* used to clear out old MessageFile entries */ |
140 |
MessageFileLine *mptr = 0; |
141 |
MessageFileLine *next_mptr = 0; |
142 |
|
143 |
/* used to add new MessageFile entries */ |
144 |
MessageFileLine *newMessageLine = 0; |
145 |
MessageFileLine *currentMessageLine = 0; |
146 |
|
147 |
char buffer[MESSAGELINELEN]; |
148 |
char *p; |
149 |
FILE *file; |
150 |
|
151 |
for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr) |
152 |
{ |
153 |
next_mptr = mptr->next; |
154 |
MyFree(mptr); |
155 |
} |
156 |
|
157 |
MessageFileptr->contentsOfFile = NULL; |
158 |
|
159 |
if (stat(MessageFileptr->fileName, &sb) < 0) |
160 |
return(-1); |
161 |
|
162 |
local_tm = localtime(&sb.st_mtime); |
163 |
|
164 |
if (local_tm) |
165 |
ircsprintf(MessageFileptr->lastChangedDate, |
166 |
"%d/%d/%d %d:%02d", |
167 |
local_tm->tm_mday, |
168 |
local_tm->tm_mon + 1, |
169 |
1900 + local_tm->tm_year, |
170 |
local_tm->tm_hour, |
171 |
local_tm->tm_min); |
172 |
|
173 |
if ((file = fopen(MessageFileptr->fileName, "r")) == NULL) |
174 |
return(-1); |
175 |
|
176 |
while (fgets(buffer, sizeof(buffer), file)) |
177 |
{ |
178 |
if ((p = strchr(buffer, '\n')) != NULL) |
179 |
*p = '\0'; |
180 |
|
181 |
newMessageLine = (MessageFileLine *)MyMalloc(sizeof(MessageFileLine)); |
182 |
strlcpy(newMessageLine->line, buffer, sizeof(newMessageLine->line)); |
183 |
newMessageLine->next = NULL; |
184 |
|
185 |
if (MessageFileptr->contentsOfFile != NULL) |
186 |
{ |
187 |
if (currentMessageLine) |
188 |
currentMessageLine->next = newMessageLine; |
189 |
|
190 |
currentMessageLine = newMessageLine; |
191 |
} |
192 |
else |
193 |
{ |
194 |
MessageFileptr->contentsOfFile = newMessageLine; |
195 |
currentMessageLine = newMessageLine; |
196 |
} |
197 |
} |
198 |
|
199 |
fclose(file); |
200 |
return(0); |
201 |
} |
202 |
|
203 |
/* |
204 |
* init_MessageLine |
205 |
* |
206 |
* inputs - NONE |
207 |
* output - pointer to new MessageFile |
208 |
* side effects - Use this when an internal Message File is wanted |
209 |
* without reading an actual file. The MessageFile |
210 |
* is init'ed, but must have content added to it through |
211 |
* addto_MessageLine() |
212 |
*/ |
213 |
|
214 |
MessageFile * |
215 |
init_MessageLine(void) |
216 |
{ |
217 |
MessageFile *mf; |
218 |
MessageFileLine *mptr = NULL; |
219 |
|
220 |
mf = MyMalloc(sizeof(MessageFile)); |
221 |
mf->motdType = ISSUPPORT; /* XXX maybe pass it alone in args? */ |
222 |
mptr = MyMalloc(sizeof(MessageFileLine)); |
223 |
mf->contentsOfFile = mptr; |
224 |
return(mf); |
225 |
} |
226 |
|
227 |
/* |
228 |
* addto_MessageLine |
229 |
* |
230 |
* inputs - Pointer to existing MessageFile |
231 |
* - New string to add to this MessageFile |
232 |
* output - NONE |
233 |
* side effects - Use this when an internal MessageFile is wanted |
234 |
* without reading an actual file. Content is added |
235 |
* to this MessageFile through this function. |
236 |
*/ |
237 |
|
238 |
void |
239 |
addto_MessageLine(MessageFile *mf, const char *str) |
240 |
{ |
241 |
MessageFileLine *mptr = mf->contentsOfFile; |
242 |
MessageFileLine *nmptr = NULL; |
243 |
|
244 |
if (mptr == NULL) |
245 |
{ |
246 |
mptr = MyMalloc(sizeof(MessageFileLine)); |
247 |
strcpy(mptr->line, str); |
248 |
mf->contentsOfFile = mptr; |
249 |
} |
250 |
else |
251 |
{ |
252 |
while (mptr->next != NULL) |
253 |
mptr = mptr->next; |
254 |
nmptr = MyMalloc(sizeof(MessageFileLine)); |
255 |
strcpy(nmptr->line, str); |
256 |
mptr->next = nmptr; |
257 |
} |
258 |
} |
259 |
|
260 |
/* |
261 |
* destroy_MessageLine(MessageFile *mf) |
262 |
* |
263 |
* inputs - pointer to the MessageFile to destroy |
264 |
* output - NONE |
265 |
* side effects - All the MessageLines attached to the given mf |
266 |
* Are freed then one MessageLine is recreated |
267 |
*/ |
268 |
void |
269 |
destroy_MessageLine(MessageFile *mf) |
270 |
{ |
271 |
MessageFileLine *mptr = mf->contentsOfFile; |
272 |
MessageFileLine *nmptr = NULL; |
273 |
|
274 |
if (mptr == NULL) |
275 |
return; |
276 |
|
277 |
for (mptr = mf->contentsOfFile; mptr != NULL; mptr = nmptr) |
278 |
{ |
279 |
nmptr = mptr->next; |
280 |
MyFree(mptr); |
281 |
} |
282 |
mf->contentsOfFile = NULL; |
283 |
} |