[project @ 2002-07-08 21:11:15 by unknown_lamer]
[clinton/bobotpp.git] / source / Main.C
1 // Main.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002 Clinton Ebadi
4
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <unistd.h>
24 #include <iostream>
25 #include <cstdio>
26 #include <csignal>
27 #include <cstdlib>
28
29 #ifdef USESCRIPTS
30 #include "Interp.H"
31 #endif
32
33 #include "Bot.H"
34
35 Bot *b;
36
37 void sig_hup(int) {
38 if (b) {
39 b->userList->read();
40 b->userList->addUserFirst("bidon", "bidon", 0, 0, false, 0, "");
41 b->reconnect();
42 }
43 }
44
45 void printUsage(char *name)
46 {
47 std::cout << "Usage: " << name << " [-h] [-b] [-f file] [-d dir] [-D]\n"
48 " -h Shows this help.\n"
49 " -b Do not run in background.\n"
50 " -f file Use file as config file.\n"
51 " -d dir Use dir as current dir.\n"
52 " -c config Use config as config\n"
53 " -s config Use config as config (only search sysdir)\n"
54 " -u config Use config as config (only search userdir)\n"
55 " -D Debug mode (input/output printing and no background mode.\n";
56 }
57
58 static void real_main(void* DONOTUSE, int argc, char **argv)
59 {
60 // FIXME: User directory
61 int opt;
62 extern char *optarg;
63 bool background = true;
64 String configFile = "bot.conf";
65 String cmd_directory = "";
66 String sys_directory = "/etc/bobotpp/";
67 String sys_subdir = "default";
68 String user_directory = String(getenv ("HOME")) + "/.bobotpp/config/";
69 String user_subdir = "default";
70 bool debug = false;
71
72 std::signal(SIGPIPE, SIG_IGN);
73 std::signal(SIGALRM, SIG_IGN);
74 std::signal(SIGHUP, sig_hup);
75
76 // We parse the command line options
77 while ((opt = getopt(argc,argv,"hbf:d:c:D")) != EOF)
78 switch (opt) {
79 case 'h':
80 printUsage(argv[0]); exit(0);
81 case 'b':
82 background = false;
83 break;
84 case 'f':
85 configFile = optarg;
86 break;
87 case 'd':
88 cmd_directory = optarg;
89 break;
90 case 'c':
91 sys_subdir = optarg;
92 user_subdir = optarg;
93 break;
94 case 'u':
95 user_subdir = optarg;
96 break;
97 case 's':
98 sys_subdir = optarg;
99 break;
100 case 'D':
101 debug = true;
102 break;
103 default:
104 printUsage(argv[0]); exit(1);
105 }
106
107 if (!cmd_directory.length ())
108 {
109 if (chdir(user_directory + user_subdir) < 0)
110 if (chdir(sys_directory + sys_subdir) < 0)
111 {
112 std::cerr << "Dirs don't exist! Exiting\n";
113 std::exit (2); // no execution
114 }
115 }
116 else
117 if (chdir(cmd_directory) < 0)
118 {
119 std::perror("Error: ");
120 std::cerr << "Exiting...\n";
121 std::exit (2);
122 }
123
124 std::cout << COPYRIGHT_STRING <<
125 "\n"PACKAGE" comes with ABSOLUTELY NO WARRANTY\n"
126 "This is free software, and you are welcome to redistribute it\n"
127 "under certain conditions; See the COPYING file for details.\n";
128
129 if (!debug) {
130 if (background)
131 switch (fork()) {
132 case -1:
133 std::cout << "Could not run in the background. Exiting...\n";
134 perror("fork");
135 exit(1);
136 case 0:
137 break;
138 default:
139 std::cout << "Running in the background...\n";
140 exit(0);
141 }
142 }
143
144 #ifdef USESCRIPTS
145 Interp::Startup();
146 #endif
147
148 b = new Bot(configFile, debug);
149 b->run();
150 delete b;
151
152 #ifdef USESCRIPTS
153 Interp::Shutdown();
154 #endif
155 }
156
157 int main(int argc, char **argv) {
158 #ifdef USESCRIPTS
159 scm_boot_guile (argc, argv, real_main, 0);
160 #else
161 real_main(0, argc, argv);
162 #endif
163
164 return 0;
165 }