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)
53 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
54 ReadConfig(false,NULL);
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); }
117 /* ConfProcess does the following things to a config line in the following order:
119 * Processes the line for syntax errors as shown below
120 * (1) Line void of quotes or equals (a malformed, illegal tag format)
121 * (2) Odd number of quotes on the line indicating a missing quote
122 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
123 * (4) Spaces between the opening bracket (<) and the keyword
124 * (5) Spaces between a keyword and an equals sign
125 * (6) Spaces between an equals sign and a quote
126 * Removes trailing spaces
127 * Removes leading spaces
128 * Converts tabs to spaces
129 * Turns multiple spaces that are outside of quotes into single spaces
132 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
134 long number_of_quotes = 0;
135 long number_of_equals = 0;
136 bool has_open_bracket = false;
137 bool in_quotes = false;
143 // firstly clean up the line by stripping spaces from the start and end
144 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
145 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
146 // empty lines are syntactically valid
147 if (!strcmp(buffer,""))
149 else if (buffer[0] == '#')
151 for (int c = 0; c < strlen(buffer); c++)
155 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
156 // search and replace later :)
157 if ((!in_quotes) && (buffer[c] == ' '))
159 if ((buffer[c] == '<') && (!in_quotes))
161 has_open_bracket = true;
162 if (strlen(buffer) == 1)
164 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
168 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
170 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
175 if (buffer[c] == '"')
178 in_quotes = (!in_quotes);
180 if ((buffer[c] == '=') && (!in_quotes))
183 if (strlen(buffer) == c)
185 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
189 else if (buffer[c+1] != '"')
191 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
197 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
201 else if (buffer[c-1] == '\xA0')
203 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
209 // no quotes, and no equals. something freaky.
210 if ((!number_of_quotes) || (!number_of_equals))
212 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
216 // odd number of quotes. thats just wrong.
217 if ((number_of_quotes % 2) != 0)
219 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
223 if (number_of_equals < (number_of_quotes/2))
225 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
227 if (number_of_equals > (number_of_quotes/2))
229 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
232 std::string parsedata = buffer;
233 // turn multispace into single space
234 while (parsedata.find("\xA0\xA0") != std::string::npos)
236 parsedata.erase(parsedata.find("\xA0\xA0"),1);
239 // turn our hardspace back into softspace
240 for (int d = 0; d < parsedata.length(); d++)
242 if (parsedata[d] == '\xA0')
246 // and we're done, the line is fine!
250 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
253 errorstream->str("");
255 FILE* conf = fopen(filename,"r");
256 if (!FileExists(filename))
258 *errorstream << "File " << filename << " not found." << endl;
266 if (fgets(buffer, MAXBUF, conf))
268 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
270 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
273 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
290 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
292 int EnumConf(std::stringstream *config, const char* tag)
295 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
296 int in_token, in_quotes, tptr, j, idx = 0;
299 const char* buf = config->str().c_str();
301 long len = strlen(buf);
311 if ((c == '#') && (lastc == '\n'))
313 while ((c != '\n') && (bptr<len))
319 if ((c == '<') && (!in_quotes))
334 in_quotes = (!in_quotes);
336 if ((c == '>') && (!in_quotes))
339 if (!strcmp(c_tag,tag))
341 /* correct tag, but wrong index */
351 if ((in_token) && (c != '\n') && (c != '\r'))
361 /* Counts the number of values within a certain tag */
363 int EnumValues(std::stringstream *config, const char* tag, int index)
366 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
367 int in_token, in_quotes, tptr, j, idx = 0;
370 bool correct_tag = false;
373 const char* buf = config->str().c_str();
375 long len = strlen(buf);
385 if ((c == '#') && (lastc == '\n'))
387 while ((c != '\n') && (bptr<len))
393 if ((c == '<') && (!in_quotes))
404 if ((!strcmp(c_tag,tag)) && (idx == index))
413 in_quotes = (!in_quotes);
416 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
420 if ((c == '>') && (!in_quotes))
425 if (!strcmp(c_tag,tag))
427 /* correct tag, but wrong index */
437 if ((in_token) && (c != '\n') && (c != '\r'))
449 int ConfValueEnum(char* tag, std::stringstream* config)
451 return EnumConf(config,tag);
456 /* Retrieves a value from the config file. If there is more than one value of the specified
457 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
459 * ConfValue("oper","name",2,result);
462 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
465 char buffer[65535], c_tag[MAXBUF], c, lastc;
466 int in_token, in_quotes, tptr, j, idx = 0;
469 const char* buf = config->str().c_str();
471 long len = strlen(buf);
483 // FIX: Treat tabs as spaces
486 if ((c == '<') && (!in_quotes))
497 // FIX: Tab can follow a tagname as well as space.
498 } while ((c != ' ') && (c != 9));
502 in_quotes = (!in_quotes);
504 if ((c == '>') && (!in_quotes))
509 if (!strcmp(c_tag,tag))
511 if ((buffer) && (c_tag) && (var))
513 key = strstr(buffer,var);
516 /* value not found in tag */
534 for (j = 0; j < strlen(key); j++)
541 strlcpy(result,key,MAXBUF);
547 if (!strcmp(c_tag,tag))
549 /* correct tag, but wrong index */
559 if ((in_token) && (c != '\n') && (c != '\r'))
566 strcpy(result,""); // value or its tag not found at all
572 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
574 ReadConf(config, tag, var, index, result);
580 /* This will bind a socket to a port. It works for UDP/TCP */
581 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
583 bzero((char *)&server,sizeof(server));
585 inet_aton(addr,&addy);
587 server.sin_family = AF_INET;
588 if (!strcmp(addr,""))
590 server.sin_addr.s_addr = htonl(INADDR_ANY);
594 server.sin_addr = addy;
597 server.sin_port = htons(port);
599 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
611 /* Open a TCP Socket */
612 int OpenTCPSocket (void)
616 struct linger linger = { 0 };
618 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
622 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
623 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
626 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));