1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2003 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>
21 #include <sys/types.h>
28 #include "inspircd_io.h"
29 #include "inspircd_util.h"
33 extern FILE *log_file;
35 void WriteOpers(char* text, ...);
37 void Exit (int status)
41 send_error("Server shutdown.");
45 void Killed(int status)
49 send_error("Server terminated.");
53 void Rehash(int status)
56 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
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");
72 void DeadPipe(int status)
74 signal (SIGPIPE, DeadPipe);
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)
90 else if (childpid > 0)
94 /* close stdout, stdin, stderr */
100 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
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
113 bool FileExists (const char* file)
117 if ((input = fopen (file, "r")) == NULL) { return(false); }
118 else { fclose (input); return(true); }
122 bool LoadConf(const char* filename, std::stringstream *target)
124 FILE* conf = fopen(filename,"r");
125 if (!FileExists(filename))
135 if (fgets(buffer, MAXBUF, conf))
137 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
139 if (buffer[0] != '#')
141 *target << std::string(buffer);
152 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
154 int EnumConf(std::stringstream *config, const char* tag)
157 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
158 int in_token, in_quotes, tptr, j, idx = 0;
161 const char* buf = config->str().c_str();
163 long len = strlen(buf);
173 if ((c == '#') && (lastc == '\n'))
175 while ((c != '\n') && (bptr<len))
181 if ((c == '<') && (!in_quotes))
196 in_quotes = (!in_quotes);
198 if ((c == '>') && (!in_quotes))
201 if (!strcmp(c_tag,tag))
203 /* correct tag, but wrong index */
213 if ((in_token) && (c != '\n') && (c != '\r'))
223 /* Counts the number of values within a certain tag */
225 int EnumValues(std::stringstream *config, const char* tag, int index)
228 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
229 int in_token, in_quotes, tptr, j, idx = 0;
232 bool correct_tag = false;
235 const char* buf = config->str().c_str();
237 long len = strlen(buf);
247 if ((c == '#') && (lastc == '\n'))
249 while ((c != '\n') && (bptr<len))
255 if ((c == '<') && (!in_quotes))
266 if ((!strcmp(c_tag,tag)) && (idx == index))
275 in_quotes = (!in_quotes);
278 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
282 if ((c == '>') && (!in_quotes))
287 if (!strcmp(c_tag,tag))
289 /* correct tag, but wrong index */
299 if ((in_token) && (c != '\n') && (c != '\r'))
311 int ConfValueEnum(char* tag, std::stringstream* config)
313 return EnumConf(config,tag);
318 /* Retrieves a value from the config file. If there is more than one value of the specified
319 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
321 * ConfValue("oper","name",2,result);
324 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
327 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
328 int in_token, in_quotes, tptr, j, idx = 0;
331 const char* buf = config->str().c_str();
333 long len = strlen(buf);
343 if ((c == '#') && (lastc == '\n'))
345 while ((c != '\n') && (bptr<len))
351 if ((c == '<') && (!in_quotes))
366 in_quotes = (!in_quotes);
368 if ((c == '>') && (!in_quotes))
373 if (!strcmp(c_tag,tag))
375 if ((buffer) && (c_tag) && (var))
377 key = strstr(buffer,var);
380 /* value not found in tag */
398 for (j = 0; j < strlen(key); j++)
411 if (!strcmp(c_tag,tag))
413 /* correct tag, but wrong index */
423 if ((in_token) && (c != '\n') && (c != '\r'))
430 strcpy(result,""); // value or its tag not found at all
436 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
438 ReadConf(config, tag, var, index, result);
444 /* This will bind a socket to a port. It works for UDP/TCP */
445 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
447 bzero((char *)&server,sizeof(server));
449 inet_aton(addr,&addy);
451 server.sin_family = AF_INET;
452 if (!strcmp(addr,""))
454 server.sin_addr.s_addr = htonl(INADDR_ANY);
458 server.sin_addr = addy;
461 server.sin_port = htons(port);
463 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
475 /* Open a TCP Socket */
476 int OpenTCPSocket (void)
480 struct linger linger = { 0 };
482 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
486 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
487 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
490 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));