]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd_io.cpp
Attempted fixes to make ModuleReader::ReadValue return "" if string does not exist
[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
17 #ifdef __linux__ 
18 #include <sys/resource.h>
19 #endif
20
21 #include <sys/types.h>
22 #include <string>
23 #include <unistd.h>
24 #include <sstream>
25 #include <iostream>
26 #include <fstream>
27 #include "inspircd.h"
28 #include "inspircd_io.h"
29 #include "inspircd_util.h"
30
31 using namespace std;
32
33 extern FILE *log_file;
34
35 void WriteOpers(char* text, ...);
36
37 void Exit (int status)
38 {
39   if (log_file)
40         fclose(log_file);
41   send_error("Server shutdown.");
42   exit (status);
43 }
44
45 void Killed(int status)
46 {
47   if (log_file)
48         fclose(log_file);
49   send_error("Server terminated.");
50   exit(status);
51 }
52
53 void Rehash(int status)
54 {
55   ReadConfig();
56   WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
57 }
58
59
60
61 void Start (void)
62 {
63   printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
64   printf("(C) ChatSpike Development team.\033[0;37m\n\n");
65   printf("\033[1;37mDevelopers:\033[0;37m     Brain, FrostyCoolSlug, RD\n");
66   printf("\033[1;37mDocumentation:\033[0;37m  FrostyCoolSlug, w00t\n");
67   printf("\033[1;37mTesters:\033[0;37m        typobox43, piggles, Lord_Zathras, CC\n");
68   printf("\033[1;37mName concept:\033[0;37m   Lord_Zathras\n\n");
69 }
70
71
72 void DeadPipe(int status)
73 {
74   signal (SIGPIPE, DeadPipe);
75 }
76
77 int DaemonSeed (void)
78 {
79   int childpid;
80   signal (SIGALRM, SIG_IGN);
81   signal (SIGHUP, Rehash);
82   signal (SIGPIPE, DeadPipe);
83   signal (SIGTERM, Exit);
84   signal (SIGABRT, Exit);
85   signal (SIGSEGV, Error);
86   signal (SIGURG, Exit);
87   signal (SIGKILL, Exit);
88   if ((childpid = fork ()) < 0)
89     return (ERROR);
90   else if (childpid > 0)
91     exit (0);
92   setsid ();
93   umask (077);
94   /* close stdout, stdin, stderr */
95   close(0);
96   close(1);
97   close(2);
98
99   #ifdef __linux__ 
100   setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
101   #endif
102   
103   return (TRUE);
104 }
105
106
107 /* Make Sure Modules Are Avaliable!
108  * (BugFix By Craig.. See? I do work! :p)
109  * Modified by brain, requires const char*
110  * to work with other API functions
111  */
112
113 bool FileExists (const char* file)
114 {
115   FILE *input;
116   
117   if ((input = fopen (file, "r")) == NULL) { return(false); }
118   else { fclose (input); return(true); }
119 }
120
121
122 bool LoadConf(const char* filename, std::stringstream *target)
123 {
124         FILE* conf = fopen(filename,"r");
125         if (!FileExists(filename))
126         {
127                 return false;
128         }
129         char buffer[MAXBUF];
130         if (conf)
131         {
132                 target->clear();
133                 while (!feof(conf))
134                 {
135                         if (fgets(buffer, MAXBUF, conf))
136                         {
137                                 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
138                                 {
139                                         if (buffer[0] != '#')
140                                         {
141                                                 *target << std::string(buffer);
142                                         }
143                                 }
144                         }
145                 }
146                 fclose(conf);
147         }
148         target->seekg(0);
149         return true;
150 }
151
152 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
153
154 int EnumConf(std::stringstream *config, const char* tag)
155 {
156         int ptr = 0;
157         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
158         int in_token, in_quotes, tptr, j, idx = 0;
159         char* key;
160
161         const char* buf = config->str().c_str();
162         long bptr = 0;
163         long len = strlen(buf);
164         
165         ptr = 0;
166         in_token = 0;
167         in_quotes = 0;
168         lastc = '\0';
169         while (bptr<len)
170         {
171                 lastc = c;
172                 c = buf[bptr++];
173                 if ((c == '#') && (lastc == '\n'))
174                 {
175                         while ((c != '\n') && (bptr<len))
176                         {
177                                 lastc = c;
178                                 c = buf[bptr++];
179                         }
180                 }
181                 if ((c == '<') && (!in_quotes))
182                 {
183                         tptr = 0;
184                         in_token = 1;
185                         do {
186                                 c = buf[bptr++];
187                                 if (c != ' ')
188                                 {
189                                         c_tag[tptr++] = c;
190                                         c_tag[tptr] = '\0';
191                                 }
192                         } while (c != ' ');
193                 }
194                 if (c == '"')
195                 {
196                         in_quotes = (!in_quotes);
197                 }
198                 if ((c == '>') && (!in_quotes))
199                 {
200                         in_token = 0;
201                         if (!strcmp(c_tag,tag))
202                         {
203                                 /* correct tag, but wrong index */
204                                 idx++;
205                         }
206                         c_tag[0] = '\0';
207                         buffer[0] = '\0';
208                         ptr = 0;
209                         tptr = 0;
210                 }
211                 if (c != '>')
212                 {
213                         if ((in_token) && (c != '\n') && (c != '\r'))
214                         {
215                                 buffer[ptr++] = c;
216                                 buffer[ptr] = '\0';
217                         }
218                 }
219         }
220         return idx;
221 }
222
223
224
225 int ConfValueEnum(char* tag, std::stringstream* config)
226 {
227         return EnumConf(config,tag);
228 }
229
230
231
232 /* Retrieves a value from the config file. If there is more than one value of the specified
233  * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
234  *
235  * ConfValue("oper","name",2,result);
236  */
237
238 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
239 {
240         int ptr = 0;
241         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
242         int in_token, in_quotes, tptr, j, idx = 0;
243         char* key;
244
245         const char* buf = config->str().c_str();
246         long bptr = 0;
247         long len = strlen(buf);
248         
249         ptr = 0;
250         in_token = 0;
251         in_quotes = 0;
252         lastc = '\0';
253         while (bptr<len)
254         {
255                 lastc = c;
256                 c = buf[bptr++];
257                 if ((c == '#') && (lastc == '\n'))
258                 {
259                         while ((c != '\n') && (bptr<len))
260                         {
261                                 lastc = c;
262                                 c = buf[bptr++];
263                         }
264                 }
265                 if ((c == '<') && (!in_quotes))
266                 {
267                         tptr = 0;
268                         in_token = 1;
269                         do {
270                                 c = buf[bptr++];
271                                 if (c != ' ')
272                                 {
273                                         c_tag[tptr++] = c;
274                                         c_tag[tptr] = '\0';
275                                 }
276                         } while (c != ' ');
277                 }
278                 if (c == '"')
279                 {
280                         in_quotes = (!in_quotes);
281                 }
282                 if ((c == '>') && (!in_quotes))
283                 {
284                         in_token = 0;
285                         if (idx == index)
286                         {
287                                 if (!strcmp(c_tag,tag))
288                                 {
289                                         if ((buffer) && (c_tag) && (var))
290                                         {
291                                                 key = strstr(buffer,var);
292                                                 if (!key)
293                                                 {
294                                                         /* value not found in tag */
295                                                         strcpy(result,"");
296                                                         return 0;
297                                                 }
298                                                 else
299                                                 {
300                                                         key+=strlen(var);
301                                                         while (key[0] !='"')
302                                                         {
303                                                                 if (!strlen(key))
304                                                                 {
305                                                                         /* missing quote */
306                                                                         strcpy(result,"");
307                                                                         return 0;
308                                                                 }
309                                                                 key++;
310                                                         }
311                                                         key++;
312                                                         for (j = 0; j < strlen(key); j++)
313                                                         {
314                                                                 if (key[j] == '"')
315                                                                 {
316                                                                         key[j] = '\0';
317                                                                 }
318                                                         }
319                                                         strcpy(result,key);
320                                                         return 1;
321                                                 }
322                                         }
323                                 }
324                         }
325                         if (!strcmp(c_tag,tag))
326                         {
327                                 /* correct tag, but wrong index */
328                                 idx++;
329                         }
330                         c_tag[0] = '\0';
331                         buffer[0] = '\0';
332                         ptr = 0;
333                         tptr = 0;
334                 }
335                 if (c != '>')
336                 {
337                         if ((in_token) && (c != '\n') && (c != '\r'))
338                         {
339                                 buffer[ptr++] = c;
340                                 buffer[ptr] = '\0';
341                         }
342                 }
343         }
344         strcpy(result,""); // value or its tag not found at all
345         return 0;
346 }
347
348
349
350 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
351 {
352         ReadConf(config, tag, var, index, result);
353         return 0;
354 }
355
356
357
358 /* This will bind a socket to a port. It works for UDP/TCP */
359 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
360 {
361   bzero((char *)&server,sizeof(server));
362   struct in_addr addy;
363   inet_aton(addr,&addy);
364
365   server.sin_family = AF_INET;
366   if (!strcmp(addr,""))
367   {
368           server.sin_addr.s_addr = htonl(INADDR_ANY);
369   }
370   else
371   {
372           server.sin_addr = addy;
373   }
374
375   server.sin_port = htons(port);
376
377   if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
378   {
379     return(ERROR);
380   }
381   else
382   {
383     listen(sockfd,5);
384     return(TRUE);
385   }
386 }
387
388
389 /* Open a TCP Socket */
390 int OpenTCPSocket (void)
391 {
392   int sockfd;
393   int on = 0;
394   struct linger linger = { 0 };
395   
396   if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
397     return (ERROR);
398   else
399   {
400     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
401     /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
402     linger.l_onoff = 1;
403     linger.l_linger = 0;
404     setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
405     return (sockfd);
406   }
407 }
408