]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd_io.cpp
Added /WHOWAS
[user/henk/code/inspircd.git] / src / inspircd_io.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2003 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  
16  $Log$
17  Revision 1.1  2003/01/23 19:45:58  brain
18  Initial revision
19
20  Revision 1.12  2003/01/22 20:49:16  brain
21  Added FileReader file-caching class
22  Changed m_randquote to use FileReader class
23
24  Revision 1.11  2003/01/21 20:31:24  brain
25  Modified to add documentation
26  Added ConfigReader class for modules
27
28  Revision 1.10  2003/01/16 20:11:55  brain
29  fixed some ugly pointer bugs (thanks dblack and a|KK|y!)
30
31  Revision 1.9  2003/01/09 13:05:58  brain
32
33  Fixed socket lingering problems (is BSD compatible)
34
35  Revision 1.8  2003/01/07 19:57:56  brain
36
37  Dynamix module support, preliminary release
38
39  Revision 1.7  2003/01/06 23:38:29  brain
40
41  just playing with header tags
42
43
44  * ---------------------------------------------------
45  */
46
47 #include "inspircd.h"
48 #include "inspircd_io.h"
49 #include "inspircd_util.h"
50
51 void WriteOpers(char* text, ...);
52
53 void Exit (int status)
54 {
55   send_error("Server shutdown.");
56   exit (status);
57 }
58
59 void Killed(int status)
60 {
61   send_error("Server terminated.");
62   exit(status);
63 }
64
65 void Rehash(int status)
66 {
67   ReadConfig();
68   WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
69 }
70
71
72
73 void Start (void)
74 {
75   printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
76   printf("(C) ChatSpike Development team.\033[0;37m\n\n");
77   printf("\033[1;37mDevelopers:\033[0;37m     Brain, FrostyCoolSlug, Raider, RD\n");
78   printf("\033[1;37mDocumentation:\033[0;37m  FrostyCoolSlug\n");
79   printf("\033[1;37mTesters:\033[0;37m        MrBOFH, piggles, Lord_Zathras, typobox43, CC\n");
80   printf("\033[1;37mName concept:\033[0;37m   Lord_Zathras\n\n");
81 }
82
83
84 int DaemonSeed (void)
85 {
86   int childpid;
87   signal (SIGALRM, SIG_IGN);
88   signal (SIGHUP, Rehash);
89   signal (SIGPIPE, SIG_IGN);
90   signal (SIGTERM, Exit);
91   signal (SIGABRT, Exit);
92   signal (SIGSEGV, Error);
93   signal (SIGURG, Exit);
94   signal (SIGKILL, Exit);
95   if ((childpid = fork ()) < 0)
96     return (ERROR);
97   else if (childpid > 0)
98     exit (0);
99   setsid ();
100   umask (077);
101   /* close stdout, stdin, stderr */
102   close(0);
103   close(1);
104   close(2);
105   return (TRUE);
106 }
107
108
109
110
111 /* Make sure the config file is available */
112 int CheckConfig (void)
113 {
114   FILE *input;
115
116   if ((input = fopen (CONFIG_FILE, "r")) == NULL)
117     {
118       printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
119       return(FALSE);
120     }
121   else
122     fclose (input);
123
124 return(TRUE);
125 }
126
127 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
128
129 int EnumConf(const char* filename, const char* tag)
130 {
131         FILE *config;
132         int ptr = 0;
133         char buffer[MAXBUF], c_tag[MAXBUF], c;
134         int in_token, in_quotes, tptr, j, idx = 0;
135         char* key;
136
137         if ((config = fopen (filename, "r")) == NULL)
138         {
139                 return 0;
140         }
141         else
142         {
143                 ptr = 0;
144                 in_token = 0;
145                 in_quotes = 0;
146                 while (!feof(config))
147                 {
148                         c = fgetc(config);
149                         if ((c == '<') && (!in_quotes))
150                         {
151                                 tptr = 0;
152                                 in_token = 1;
153                                 do {
154                                         c = fgetc(config);
155                                         if (c != ' ')
156                                         {
157                                                 c_tag[tptr++] = c;
158                                                 c_tag[tptr] = '\0';
159                                         }
160                                 } while (c != ' ');
161                         }
162                         if (c == '"')
163                         {
164                                 in_quotes = (!in_quotes);
165                         }
166                         if ((c == '>') && (!in_quotes))
167                         {
168                                 in_token = 0;
169                                 if (!strcmp(c_tag,tag))
170                                 {
171                                         /* correct tag, but wrong index */
172                                         idx++;
173                                 }
174                                 c_tag[0] = '\0';
175                                 buffer[0] = '\0';
176                                 ptr = 0;
177                                 tptr = 0;
178                         }
179                         if (c != '>')
180                         {
181                                 if ((in_token) && (c != '\n') && (c != '\r'))
182                                 {
183                                         buffer[ptr++] = c;
184                                         buffer[ptr] = '\0';
185                                 }
186                         }
187                 }
188         }
189         fclose(config);
190         return idx;
191 }
192
193
194
195 int ConfValueEnum(char* tag)
196 {
197         EnumConf(CONFIG_FILE,tag);
198 }
199
200
201
202 /* Retrieves a value from the config file. If there is more than one value of the specified
203  * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
204  *
205  * ConfValue("oper","name",2,result);
206  */
207
208 int ReadConf(const char* filename, const char* tag, const char* var, int index, char *result)
209 {
210         FILE *config;
211         int ptr = 0;
212         char buffer[MAXBUF], c_tag[MAXBUF], c;
213         int in_token, in_quotes, tptr, j, idx = 0;
214         char* key;
215
216         if ((config = fopen (filename, "r")) == NULL)
217         {
218                 return 0;
219         }
220         else
221         {
222                 ptr = 0;
223                 in_token = 0;
224                 in_quotes = 0;
225                 while (!feof(config))
226                 {
227                         c = fgetc(config);
228                         if ((c == '<') && (!in_quotes))
229                         {
230                                 tptr = 0;
231                                 in_token = 1;
232                                 do {
233                                         c = fgetc(config);
234                                         if (c != ' ')
235                                         {
236                                                 c_tag[tptr++] = c;
237                                                 c_tag[tptr] = '\0';
238                                         }
239                                 } while (c != ' ');
240                         }
241                         if (c == '"')
242                         {
243                                 in_quotes = (!in_quotes);
244                         }
245                         if ((c == '>') && (!in_quotes))
246                         {
247                                 in_token = 0;
248                                 if (idx == index)
249                                 {
250                                         if (!strcmp(c_tag,tag))
251                                         {
252                                                 if ((buffer) && (c_tag) && (var))
253                                                 {
254                                                         key = strstr(buffer,var);
255                                                         if (!key)
256                                                         {
257                                                                 /* value not found in tag */
258                                                                 fclose(config);
259                                                                 return 0;
260                                                         }
261                                                         else
262                                                         {
263                                                                 key+=strlen(var);
264                                                                 while (key[0] !='"')
265                                                                 {
266                                                                         if (!strlen(key))
267                                                                         {
268                                                                                 /* missing quote */
269                                                                                 return 0;
270                                                                         }
271                                                                         key++;
272                                                                 }
273                                                                 key++;
274                                                                 for (j = 0; j < strlen(key); j++)
275                                                                 {
276                                                                         if (key[j] == '"')
277                                                                         {
278                                                                                 key[j] = '\0';
279                                                                         }
280                                                                 }
281                                                                 fclose(config);
282                                                                 strcpy(result,key);
283                                                                 return 1;
284                                                         }
285                                                 }
286                                         }
287                                 }
288                                 if (!strcmp(c_tag,tag))
289                                 {
290                                         /* correct tag, but wrong index */
291                                         idx++;
292                                 }
293                                 c_tag[0] = '\0';
294                                 buffer[0] = '\0';
295                                 ptr = 0;
296                                 tptr = 0;
297                         }
298                         if (c != '>')
299                         {
300                                 if ((in_token) && (c != '\n') && (c != '\r'))
301                                 {
302                                         buffer[ptr++] = c;
303                                         buffer[ptr] = '\0';
304                                 }
305                         }
306                 }
307         }
308         fclose(config);
309         return 0;
310 }
311
312
313
314 int ConfValue(char* tag, char* var, int index, char *result)
315 {
316         ReadConf(CONFIG_FILE, tag, var, index, result);
317 }
318
319
320
321 /* This will bind a socket to a port. It works for UDP/TCP */
322 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
323 {
324   bzero((char *)&server,sizeof(server));
325   struct in_addr addy;
326   inet_aton(addr,&addy);
327
328   server.sin_family = AF_INET;
329   if (!strcmp(addr,""))
330   {
331           server.sin_addr.s_addr = htonl(INADDR_ANY);
332   }
333   else
334   {
335           server.sin_addr = addy;
336   }
337
338   server.sin_port = htons(port);
339
340   if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
341   {
342     return(ERROR);
343   }
344   else
345   {
346     listen(sockfd,5);
347     return(TRUE);
348   }
349 }
350
351
352 /* Open a TCP Socket */
353 int OpenTCPSocket (void)
354 {
355   int sockfd;
356   int on = 0;
357   struct linger linger = { 0 };
358   
359   if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
360     return (ERROR);
361   else
362   {
363     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
364     /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
365     linger.l_onoff = 1;
366     linger.l_linger = 0;
367     setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
368     return (sockfd);
369   }
370 }
371