ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/motd.c
Revision: 1793
Committed: Sun Mar 31 14:06:08 2013 UTC (12 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 6743 byte(s)
Log Message:
- Replaced all occurrences of ircsprintf with sprintf/snprintf
  and killed sprintf_irc.(c|h)

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision