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>
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)
125 FILE* conf = fopen(filename,"r");
126 if (!FileExists(filename))
136 if (fgets(buffer, MAXBUF, conf))
138 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
140 if (buffer[0] != '#')
142 *target << std::string(buffer);
153 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
155 int EnumConf(std::stringstream *config, const char* tag)
158 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
159 int in_token, in_quotes, tptr, j, idx = 0;
162 const char* buf = config->str().c_str();
164 long len = strlen(buf);
174 if ((c == '#') && (lastc == '\n'))
176 while ((c != '\n') && (bptr<len))
182 if ((c == '<') && (!in_quotes))
197 in_quotes = (!in_quotes);
199 if ((c == '>') && (!in_quotes))
202 if (!strcmp(c_tag,tag))
204 /* correct tag, but wrong index */
214 if ((in_token) && (c != '\n') && (c != '\r'))
224 /* Counts the number of values within a certain tag */
226 int EnumValues(std::stringstream *config, const char* tag, int index)
229 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
230 int in_token, in_quotes, tptr, j, idx = 0;
233 bool correct_tag = false;
236 const char* buf = config->str().c_str();
238 long len = strlen(buf);
248 if ((c == '#') && (lastc == '\n'))
250 while ((c != '\n') && (bptr<len))
256 if ((c == '<') && (!in_quotes))
267 if ((!strcmp(c_tag,tag)) && (idx == index))
276 in_quotes = (!in_quotes);
279 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
283 if ((c == '>') && (!in_quotes))
288 if (!strcmp(c_tag,tag))
290 /* correct tag, but wrong index */
300 if ((in_token) && (c != '\n') && (c != '\r'))
312 int ConfValueEnum(char* tag, std::stringstream* config)
314 return EnumConf(config,tag);
319 /* Retrieves a value from the config file. If there is more than one value of the specified
320 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
322 * ConfValue("oper","name",2,result);
325 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
328 char buffer[65535], c_tag[MAXBUF], c, lastc;
329 int in_token, in_quotes, tptr, j, idx = 0;
332 const char* buf = config->str().c_str();
334 long len = strlen(buf);
346 // FIX: Treat tabs as spaces
349 if ((c == '<') && (!in_quotes))
360 // FIX: Tab can follow a tagname as well as space.
361 } while ((c != ' ') && (c != 9));
365 in_quotes = (!in_quotes);
367 if ((c == '>') && (!in_quotes))
372 if (!strcmp(c_tag,tag))
374 if ((buffer) && (c_tag) && (var))
376 key = strstr(buffer,var);
379 /* value not found in tag */
397 for (j = 0; j < strlen(key); j++)
410 if (!strcmp(c_tag,tag))
412 /* correct tag, but wrong index */
422 if ((in_token) && (c != '\n') && (c != '\r'))
429 strcpy(result,""); // value or its tag not found at all
435 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
437 ReadConf(config, tag, var, index, result);
443 /* This will bind a socket to a port. It works for UDP/TCP */
444 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
446 bzero((char *)&server,sizeof(server));
448 inet_aton(addr,&addy);
450 server.sin_family = AF_INET;
451 if (!strcmp(addr,""))
453 server.sin_addr.s_addr = htonl(INADDR_ANY);
457 server.sin_addr = addy;
460 server.sin_port = htons(port);
462 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
474 /* Open a TCP Socket */
475 int OpenTCPSocket (void)
479 struct linger linger = { 0 };
481 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
485 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
486 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
489 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));