]>
jfr.im git - irc/thales.git/blob - src/main.c
1 /* GNU Thales - IRC to Relational Database Gateway
2 * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net>
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.
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.
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
18 /* GNU Thales - main file */
30 /* Global variables */
32 int debug
= 0; /* -d , debug mode = don't fork + log to stderr */
34 int verbose
= 0; /* -v, verbose output */
36 /* Socket for talking to server */
42 /* At what time were we started? */
45 extern char *PIDFilename
;
46 extern char *RemoteServer
;
47 extern int RemotePort
;
48 extern char *LocalHost
;
50 extern char *RemotePassword
;
51 extern char *ServerName
;
52 extern char *ServerDesc
;
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");
64 /* Create our PID file and write the PID to it. */
65 void write_pidfile(void)
69 pidfile
= fopen(PIDFilename
, "w");
72 fprintf(pidfile
, "%d\n", (int) getpid());
77 mylog_perror("Warning: cannot write to PID file %s", PIDFilename
);
82 int main(int argc
, char **argv
)
87 /* record start time */
88 start_time
= time(NULL
);
89 /* Parse command line options */
90 while ((ch
= getopt(argc
, argv
, "dvV")) != -1)
102 printf("GNU Thales v.%s\n", VERSION
);
110 /* Read configuration file; exit if there are problems. */
114 /* Open logfile, and complain if we didn't. */
117 fprintf(stderr
, "Warning: unable to open log file : %s\n",
122 /* Detach ourselves if requested. */
125 if ((i
= fork()) < 0)
134 if (isatty(0) && isatty(1) && isatty(2))
143 /* Announce ourselves to the logfile. */
144 mylog("GNU Thales %s starting up with %s support%s", VERSION
,
145 #if defined(IRCD_UNREAL)
147 #elif defined(IRCD_HYBRID)
149 #elif defined (IRCD_BAHAMUT)
151 #elif defined (IRCD_IRCDRU)
153 #elif defined (IRCD_ULTIMATE)
155 #elif defined (IRCD_ULTI28)
160 verbose
? (debug
? " (options: debug)" : " (options: verbose") :
162 /* I should init databases here */
164 ch1
= db_escape(ServerName
);
165 ch2
= db_escape(ServerDesc
);
166 db_query("INSERT INTO " TBL_SERV
167 " (server, comment, connecttime) values('%s', '%s', NOW())",
169 db_addserver(ch1
, db_insertid());
172 /* connect to RemoteServer */
173 servsock
= conn(RemoteServer
, RemotePort
, LocalHost
, LocalPort
);
176 fatal_perror("Can't connect to server");
179 #if defined(IRCD_UNREAL)
180 send_cmd(NULL
, "PROTOCTL NICKv2 SJOIN SJOIN2 SJ3");
181 send_cmd(NULL
, "CAPAB NOQUIT");
183 #if defined(IRCD_HYBRID)
184 send_cmd(NULL
, "PASS %s :TS", RemotePassword
);
185 send_cmd(NULL
, "CAPAB :HOPS TBURST");
186 #elif defined (IRCD_BAHAMUT)
187 send_cmd(NULL
, "PASS %s :TS", RemotePassword
);
188 send_cmd(NULL
, "CAPAB TS3 SSJOIN NICKIP NOQUIT");
189 #elif defined (IRCD_IRCDRU)
190 send_cmd(NULL
, "PASS %s :TS", RemotePassword
);
191 send_cmd(NULL
, "CAPAB TS3 SSJOIN 8BNCI NICKIP");
193 #elif defined (IRCD_ULTIMATE)
194 send_cmd(NULL
, "PASS %s :TS", RemotePassword
);
195 send_cmd(NULL
, "CAPAB TS5 SSJ5 NICKIP");
196 #elif defined(IRCD_ULTI28) /* special case to prevent future errors */
197 send_cmd(NULL
, "PASS %s", RemotePassword
);
199 send_cmd(NULL
, "PASS %s", RemotePassword
);
202 #if defined(IRCD_IRCDRU)
203 send_cmd(NULL
, "SERVER %s 1 8 :%s", ServerName
, ServerDesc
);
205 send_cmd(NULL
, "SERVER %s 1 :%s", ServerName
, ServerDesc
);
208 #if defined(IRCD_BAHAMUT)||defined(IRCD_IRCDRU)
209 send_cmd(NULL
, "SVINFO 3 1 0 :%ld", time(NULL
));
210 #elif defined(IRCD_ULTIMATE)
211 send_cmd(NULL
, "SVINFO 5 3 0 :%ld", time(NULL
));
213 #if defined(IRCD_UNREAL)
214 send_cmd(NULL
, ":%s EOS", ServerName
);
216 sgets2(inbuf
, sizeof(inbuf
), servsock
);
217 if (strnicmp(inbuf
, "ERROR", 5) == 0)
219 /* Close server socket first to stop wallops, since the other
220 * server doesn't want to listen to us anyway */
223 fatal("Remote server returned: %s", inbuf
);
225 /* We have a line left over from earlier, process it now */
229 i
= (int) (long) sgets2(inbuf
, sizeof(inbuf
), servsock
);
236 int errno_save
= errno
;
237 quitmsg
= (char *) scalloc(BUFSIZE
, 1);
240 snprintf(quitmsg
, BUFSIZE
, "Read error from server: %s",
241 strerror(errno_save
));
245 quitmsg
= "Read error from server";
247 send_cmd(ServerName
, "SQUIT %s :%s", ServerName
, quitmsg
);