]> jfr.im git - irc/thales.git/blame - src/main.c
- server.lastsplit is not overwritten when a server rejoins.
[irc/thales.git] / src / main.c
CommitLineData
18038256 1/* GNU Thales - IRC to Relational Database Gateway
2ace9480 2 * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18038256 18/* GNU Thales - main file */
2ace9480 19
20#include "thales.h"
21#include "conf.h"
22#include "log.h"
23#include "send.h"
24#include "sockutil.h"
25#include "compat.h"
26#include "memory.h"
27#include "process.h"
28#include "db.h"
29
30/* Global variables */
31
32int debug = 0; /* -d , debug mode = don't fork + log to stderr */
33
34int verbose = 0; /* -v, verbose output */
35
36/* Socket for talking to server */
37int servsock = -1;
38char *quitmsg;
39
40char inbuf[BUFSIZE];
41
42/* At what time were we started? */
43time_t start_time;
44
45extern char *PIDFilename;
46extern char *RemoteServer;
47extern int RemotePort;
48extern char *LocalHost;
49extern int LocalPort;
50extern char *RemotePassword;
51extern char *ServerName;
52extern char *ServerDesc;
53
54/* display usage */
55void usage()
56{
57 printf("\n Syntax : thales\n");
58 printf(" -v : enable verbose mode\n");
59 printf(" -d : enable debugging mode\n");
60 printf(" -V : display version and exit\n");
61 exit(1);
62}
63
64/* Create our PID file and write the PID to it. */
65void write_pidfile(void)
66{
67 FILE *pidfile;
68
69 pidfile = fopen(PIDFilename, "w");
70 if (pidfile)
71 {
72 fprintf(pidfile, "%d\n", (int) getpid());
73 fclose(pidfile);
74 }
75 else
76 {
18038256 77 mylog_perror("Warning: cannot write to PID file %s", PIDFilename);
2ace9480 78 }
79}
80
81/* Main routine */
82int main(int argc, char **argv)
83{
84 char ch;
85 int i;
86 char *ch1, *ch2;
87 /* record start time */
88 start_time = time(NULL);
89 /* Parse command line options */
90 while ((ch = getopt(argc, argv, "dvV")) != -1)
91 {
92 switch (ch)
93 {
94 case 'd':
95 debug = 1;
96 verbose = 1;
97 break;
98 case 'v':
99 verbose = 1;
100 break;
101 case 'V':
18038256 102 printf("GNU Thales v.%s\n", VERSION);
2ace9480 103 exit(0);
104 break;
105 default:
106 usage();
107 }
108 }
109
110 /* Read configuration file; exit if there are problems. */
111 if (!read_config())
112 exit(-1);
113
114 /* Open logfile, and complain if we didn't. */
115 if (open_log() < 0)
116 {
117 fprintf(stderr, "Warning: unable to open log file : %s\n",
118 strerror(errno));
119 exit(1);
120 }
121
122 /* Detach ourselves if requested. */
123 if (!debug)
124 {
125 if ((i = fork()) < 0)
126 {
127 perror("fork()");
128 return -1;
129 }
130 else if (i != 0)
131 {
132 exit(0);
133 }
134 if (isatty(0) && isatty(1) && isatty(2))
135 {
136 close(0);
137 close(1);
138 close(2);
139 }
140 }
141 write_pidfile();
142
143 /* Announce ourselves to the logfile. */
18038256 144 mylog("GNU Thales %s starting up with %s support%s", VERSION,
2ace9480 145#if defined(IRCD_UNREAL)
146 "unreal",
147#elif defined(IRCD_HYBRID)
148 "hybrid",
149#elif defined(IRCD_SEQUANA)
150 "sequana",
151#elif defined (IRCD_BAHAMUT)
152 "bahamut",
153#elif defined (IRCD_IRCDRU)
154 "ircdru",
155#elif defined (IRCD_ULTIMATE)
156 "ultimate30",
157#elif defined (IRCD_ULTI28)
158 "ultimate28",
159#else
160 "unknown (!)",
161#endif
162 verbose ? (debug ? " (options: debug)" : " (options: verbose") :
163 "");
164 /* I should init databases here */
165 db_connect();
166 ch1 = db_escape(ServerName);
167 ch2 = db_escape(ServerDesc);
168 db_query("INSERT INTO " TBL_SERV
169 " (server, comment, connecttime) values('%s', '%s', NOW())",
170 ch1, ch2);
171 db_addserver(ch1, db_insertid());
172 free(ch1);
173 free(ch2);
174 /* connect to RemoteServer */
175 servsock = conn(RemoteServer, RemotePort, LocalHost, LocalPort);
176 if (servsock < 0)
177 {
178 fatal_perror("Can't connect to server");
179 exit(-1);
180 }
181#if defined(IRCD_UNREAL)
182 send_cmd(NULL, "PROTOCTL NICKv2 SJOIN SJOIN2 SJ3");
183#endif
184#if defined(IRCD_SEQUANA)
185 send_cmd(NULL, "PASS %s :TS", RemotePassword);
186 send_cmd(NULL, "CAPAB NICKIP SSJOIN TS3");
187#elif defined(IRCD_HYBRID)
188 send_cmd(NULL, "PASS %s :TS", RemotePassword);
189 send_cmd(NULL, "CAPAB :HOPS TBURST");
190#elif defined (IRCD_BAHAMUT)
191 send_cmd(NULL, "PASS %s :TS", RemotePassword);
192 send_cmd(NULL, "CAPAB TS3 SSJOIN NICKIP");
193#elif defined (IRCD_IRCDRU)
194 send_cmd(NULL, "PASS %s :TS", RemotePassword);
195 send_cmd(NULL, "CAPAB TS3 SSJOIN 8BNCI NICKIP");
196
197#elif defined (IRCD_ULTIMATE)
198 send_cmd(NULL, "PASS %s :TS", RemotePassword);
199 send_cmd(NULL, "CAPAB TS5 SSJ5 NICKIP");
200#elif defined(IRCD_ULTI28) /* special case to prevent future errors */
201 send_cmd(NULL, "PASS %s", RemotePassword);
202#else
203 send_cmd(NULL, "PASS %s", RemotePassword);
204#endif
205
206#if defined(IRCD_IRCDRU)
207 send_cmd(NULL, "SERVER %s 1 8 :%s", ServerName, ServerDesc);
208#else
209 send_cmd(NULL, "SERVER %s 1 :%s", ServerName, ServerDesc);
210#endif
211
212#if defined(IRCD_SEQUANA)||defined(IRCD_BAHAMUT)||defined(IRCD_IRCDRU)
213 send_cmd(NULL, "SVINFO 3 1 0 :%ld", time(NULL));
214#elif defined(IRCD_ULTIMATE)
215 send_cmd(NULL, "SVINFO 5 3 0 :%ld", time(NULL));
216#endif
217 sgets2(inbuf, sizeof(inbuf), servsock);
218 if (strnicmp(inbuf, "ERROR", 5) == 0)
219 {
220 /* Close server socket first to stop wallops, since the other
221 * server doesn't want to listen to us anyway */
222 disconn(servsock);
223 servsock = -1;
224 fatal("Remote server returned: %s", inbuf);
225 }
226 /* We have a line left over from earlier, process it now */
227 process();
228 while (1)
229 {
230 i = (int) (long) sgets2(inbuf, sizeof(inbuf), servsock);
231 if (i > 0)
232 {
233 process();
234 }
235 else if (i == 0)
236 {
237 int errno_save = errno;
238 quitmsg = (char *) scalloc(BUFSIZE, 1);
239 if (quitmsg)
240 {
241 snprintf(quitmsg, BUFSIZE, "Read error from server: %s",
242 strerror(errno_save));
243 }
244 else
245 {
246 quitmsg = "Read error from server";
247 }
248 send_cmd(ServerName, "SQUIT %s :%s", ServerName, quitmsg);
249 disconn(servsock);
250 close_log();
251 return 0;
252 }
253 }
254}