[project @ 2002-07-09 14:28:08 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 #include <getopt.h>
29
30 #ifdef USESCRIPTS
31 #include "Interp.H"
32 #endif
33
34 #include "Bot.H"
35
36 namespace
37 {
38 Bot *b;
39
40 option bot_options[] =
41 {
42 { "help", no_argument, 0, 'h' },
43 { "version", no_argument, 0, 'v' },
44 { "no-background", no_argument, 0, 'b'},
45 { "config-file", required_argument, 0, 'f'},
46 { "config-dir", required_argument, 0, 'd'},
47 { "config", required_argument, 0, 'c'},
48 { "sys-config", required_argument, 0, 's'},
49 { "user-config", required_argument, 0, 'u'},
50 { "debug", no_argument, 0, 'D' }
51 };
52
53 void sig_hup(int) {
54 if (b) {
55 b->userList->read();
56 b->userList->addUserFirst("bidon", "bidon", 0, 0, false, 0, "");
57 b->reconnect();
58 }
59 }
60
61 void print_version ()
62 {
63 std::cerr << VERSION_STRING << std::endl
64 << COPYRIGHT_STRING << std::endl
65 << PACKAGE" comes with ABSOLUTELY NO WARRANTY\n"
66 << "This is free software, and you are welcome to redistribute it\n"
67 << "under certain conditions; See the COPYING file for details.\n";
68 }
69 void print_short_help (const char *name)
70 {
71 std::cerr << "Usage: " << name << " [--help] [--version] [--no-background]\n\t"
72 << "[--config-file file] [--config-dir dir] [--debug]\n\t"
73 << "[--config dir-under-configpath]\n\t"
74 << "[--sys-config dir-in-sysconfdir]\n\t"
75 << "[--user-config dir-userdir] [--debug]\n"
76 << "\n--help shows long help.\n";
77 /* " -b Do not run in background.\n"
78 " -f file Use file as config file.\n"
79 " -d dir Use dir as current dir.\n"
80 " -c config Use config as config\n"
81 " -s config Use config as config (only search sysdir)\n"
82 " -u config Use config as config (only search userdir)\n"
83 " -D Debug mode (input/output printing and no background mode.\n";
84 */
85 }
86
87 void print_long_help (const char *name)
88 {
89 std::cerr << "Long Help for " << PACKAGE << std::endl;
90 std::cerr <<
91 "The manual (info bobot++) will contain the best information on general\n"
92 "usage of Bobot++. Here is a detailed summary of the command line\n"
93 "arguments: (in long arg short arg format). All args are optional.\n";
94 print_short_help (name);
95 std::cerr <<
96 "[--help][-h] Shows this help and exits\n"
97 "[--version][-v] Shows version information and exits\n"
98 "[--no-background][-b] Run bobot++ in the foreground\n"
99 "[--config-file file][-f] Use file instead of bot.conf\n"
100 "[--config-dir dir][-d] Use dir as dir to load config file from\n"
101 "[--config dir][-c] Search your config path (defaults to\n"
102 " "
103 << getenv ("HOME")
104 << "/.bobotpp/config/ and then\n"
105 <<
106 " /etc/bobotpp/) for dir and\n"
107 " then loads your config data using dir\n"
108 "[--sys-config dir][-s] Looks for config in /etc/bobotpp/dir. Note\n"
109 " that the user dir is still searched first\n"
110 "[--user-config dir][-u] Looks for config in\n"
111 " "
112 << getenv("HOME")
113 << "/.bobotpp/config/dir/.\n"
114 <<
115 " Note that\n"
116 " the system dir is still searched after this if\n"
117 " dir is not found.\n"
118 "[--debug][-D] Makes Bobot++ print debugging info and run in\n"
119 " the foreground"
120 << std::endl
121 << std::endl
122 << "The default configuration is read from\n"
123 << getenv("HOME")
124 << "/.bobotpp/config/default/ and then\n"
125 "/etc/bobotpp/default/ if the user config is not found.\n";
126 }
127
128 static void real_main(void* DONOTUSE, int argc, char **argv)
129 {
130 // FIXME: User directory
131 int opt;
132 bool background = true;
133 String configFile = "bot.conf";
134 String cmd_directory = "";
135 String sys_directory = "/etc/bobotpp/";
136 String sys_subdir = "default";
137 String user_directory = String(getenv ("HOME")) + "/.bobotpp/config/";
138 String user_subdir = "default";
139 bool debug = false;
140
141 std::signal(SIGPIPE, SIG_IGN);
142 std::signal(SIGALRM, SIG_IGN);
143 std::signal(SIGHUP, sig_hup);
144
145 // We parse the command line options
146 while ((opt = getopt_long (argc,argv,"vhbf:d:c:D", bot_options, 0))
147 != -1)
148 switch (opt) {
149 case 'h':
150 print_long_help (argv[0]);
151 exit(0);
152 break;
153 case 'v':
154 print_version ();
155 exit (0);
156 break;
157 case 'b':
158 background = false;
159 break;
160 case 'f':
161 configFile = optarg;
162 break;
163 case 'd':
164 cmd_directory = optarg;
165 break;
166 case 'c':
167 sys_subdir = optarg;
168 user_subdir = optarg;
169 break;
170 case 'u':
171 user_subdir = optarg;
172 break;
173 case 's':
174 sys_subdir = optarg;
175 break;
176 case 'D':
177 debug = true;
178 break;
179 default:
180 print_short_help (argv[0]);
181 exit(1);
182 }
183
184 if (!cmd_directory.length ())
185 {
186 if (chdir(user_directory + user_subdir) < 0)
187 if (chdir(sys_directory + sys_subdir) < 0)
188 {
189 std::cerr << "Dirs don't exist! Exiting\n";
190 std::exit (2); // no execution
191 }
192 }
193 else
194 if (chdir(cmd_directory) < 0)
195 {
196 std::perror("Error: ");
197 std::cerr << "Exiting...\n";
198 std::exit (2);
199 }
200
201 // std::cout << COPYRIGHT_STRING <<
202 // "\n"PACKAGE" comes with ABSOLUTELY NO WARRANTY\n"
203 // "This is free software, and you are welcome to redistribute it\n"
204 // "under certain conditions; See the COPYING file for details.\n";
205 print_version ();
206
207 if (!debug) {
208 if (background)
209 switch (fork()) {
210 case -1:
211 std::cout << "Could not run in the background. Exiting...\n";
212 perror("fork");
213 exit(1);
214 case 0:
215 break;
216 default:
217 std::cout << "Running in the background...\n";
218 exit(0);
219 }
220 }
221
222 #ifdef USESCRIPTS
223 Interp::Startup();
224 #endif
225
226 b = new Bot(configFile, debug);
227 b->run();
228 delete b;
229
230 #ifdef USESCRIPTS
231 Interp::Shutdown();
232 #endif
233 }
234
235 } // static functions and data
236
237 int main(int argc, char **argv) {
238 #ifdef USESCRIPTS
239 scm_boot_guile (argc, argv, real_main, 0);
240 #else
241 real_main(0, argc, argv);
242 #endif
243
244 return 0;
245 }