1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include <sys/resource.h>
19 #include <sys/types.h>
26 #include "inspircd_io.h"
27 #include "inspircd_util.h"
31 extern FILE *log_file;
33 void WriteOpers(char* text, ...);
35 void Exit (int status)
39 send_error("Server shutdown.");
43 void Killed(int status)
47 send_error("Server terminated.");
51 void Rehash(int status)
54 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
61 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
62 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
63 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug\n");
64 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug, w00t\n");
65 printf("\033[1;37mTesters:\033[0;37m typobox43, piggles, Lord_Zathras, CC\n");
66 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
70 void DeadPipe(int status)
72 signal (SIGPIPE, DeadPipe);
78 signal (SIGALRM, SIG_IGN);
79 signal (SIGHUP, Rehash);
80 signal (SIGPIPE, DeadPipe);
81 signal (SIGTERM, Exit);
82 signal (SIGABRT, Exit);
83 signal (SIGSEGV, Error);
84 signal (SIGURG, Exit);
85 signal (SIGKILL, Exit);
86 if ((childpid = fork ()) < 0)
88 else if (childpid > 0)
92 /* close stdout, stdin, stderr */
97 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
103 /* Make Sure Modules Are Avaliable!
104 * (BugFix By Craig.. See? I do work! :p)
105 * Modified by brain, requires const char*
106 * to work with other API functions
109 bool FileExists (const char* file)
113 if ((input = fopen (file, "r")) == NULL) { return(false); }
114 else { fclose (input); return(true); }
118 bool LoadConf(const char* filename, std::stringstream *target)
121 FILE* conf = fopen(filename,"r");
122 if (!FileExists(filename))
131 if (fgets(buffer, MAXBUF, conf))
133 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
135 if (buffer[0] != '#')
137 *target << std::string(buffer);
148 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
150 int EnumConf(std::stringstream *config, const char* tag)
153 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
154 int in_token, in_quotes, tptr, j, idx = 0;
157 const char* buf = config->str().c_str();
159 long len = strlen(buf);
169 if ((c == '#') && (lastc == '\n'))
171 while ((c != '\n') && (bptr<len))
177 if ((c == '<') && (!in_quotes))
192 in_quotes = (!in_quotes);
194 if ((c == '>') && (!in_quotes))
197 if (!strcmp(c_tag,tag))
199 /* correct tag, but wrong index */
209 if ((in_token) && (c != '\n') && (c != '\r'))
219 /* Counts the number of values within a certain tag */
221 int EnumValues(std::stringstream *config, const char* tag, int index)
224 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
225 int in_token, in_quotes, tptr, j, idx = 0;
228 bool correct_tag = false;
231 const char* buf = config->str().c_str();
233 long len = strlen(buf);
243 if ((c == '#') && (lastc == '\n'))
245 while ((c != '\n') && (bptr<len))
251 if ((c == '<') && (!in_quotes))
262 if ((!strcmp(c_tag,tag)) && (idx == index))
271 in_quotes = (!in_quotes);
274 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
278 if ((c == '>') && (!in_quotes))
283 if (!strcmp(c_tag,tag))
285 /* correct tag, but wrong index */
295 if ((in_token) && (c != '\n') && (c != '\r'))
307 int ConfValueEnum(char* tag, std::stringstream* config)
309 return EnumConf(config,tag);
314 /* Retrieves a value from the config file. If there is more than one value of the specified
315 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
317 * ConfValue("oper","name",2,result);
320 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
323 char buffer[65535], c_tag[MAXBUF], c, lastc;
324 int in_token, in_quotes, tptr, j, idx = 0;
327 const char* buf = config->str().c_str();
329 long len = strlen(buf);
341 // FIX: Treat tabs as spaces
344 if ((c == '<') && (!in_quotes))
355 // FIX: Tab can follow a tagname as well as space.
356 } while ((c != ' ') && (c != 9));
360 in_quotes = (!in_quotes);
362 if ((c == '>') && (!in_quotes))
367 if (!strcmp(c_tag,tag))
369 if ((buffer) && (c_tag) && (var))
371 key = strstr(buffer,var);
374 /* value not found in tag */
392 for (j = 0; j < strlen(key); j++)
399 strlcpy(result,key,MAXBUF);
405 if (!strcmp(c_tag,tag))
407 /* correct tag, but wrong index */
417 if ((in_token) && (c != '\n') && (c != '\r'))
424 strcpy(result,""); // value or its tag not found at all
430 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
432 ReadConf(config, tag, var, index, result);
438 /* This will bind a socket to a port. It works for UDP/TCP */
439 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
441 bzero((char *)&server,sizeof(server));
443 inet_aton(addr,&addy);
445 server.sin_family = AF_INET;
446 if (!strcmp(addr,""))
448 server.sin_addr.s_addr = htonl(INADDR_ANY);
452 server.sin_addr = addy;
455 server.sin_port = htons(port);
457 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
469 /* Open a TCP Socket */
470 int OpenTCPSocket (void)
474 struct linger linger = { 0 };
476 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
480 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
481 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
484 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));