1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Main file (Windows specific part) */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "bitlbee.h" |
---|
28 | #include "commands.h" |
---|
29 | #include "protocols/nogaim.h" |
---|
30 | #include "help.h" |
---|
31 | #include <signal.h> |
---|
32 | #include <windows.h> |
---|
33 | |
---|
34 | global_t global; /* Against global namespace pollution */ |
---|
35 | |
---|
36 | static void WINAPI service_ctrl (DWORD dwControl) |
---|
37 | { |
---|
38 | switch (dwControl) |
---|
39 | { |
---|
40 | case SERVICE_CONTROL_STOP: |
---|
41 | /* FIXME */ |
---|
42 | break; |
---|
43 | |
---|
44 | case SERVICE_CONTROL_INTERROGATE: |
---|
45 | break; |
---|
46 | |
---|
47 | default: |
---|
48 | break; |
---|
49 | |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | static void bitlbee_init(int argc, char **argv) |
---|
54 | { |
---|
55 | int i = -1; |
---|
56 | memset( &global, 0, sizeof( global_t ) ); |
---|
57 | |
---|
58 | b_main_init(); |
---|
59 | |
---|
60 | global.conf = conf_load( argc, argv ); |
---|
61 | if( global.conf == NULL ) |
---|
62 | return; |
---|
63 | |
---|
64 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
65 | { |
---|
66 | i = bitlbee_inetd_init(); |
---|
67 | log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION ); |
---|
68 | |
---|
69 | } |
---|
70 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
71 | { |
---|
72 | i = bitlbee_daemon_init(); |
---|
73 | log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION ); |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | log_message( LOGLVL_INFO, "No bitlbee mode specified..."); |
---|
78 | } |
---|
79 | |
---|
80 | if( i != 0 ) |
---|
81 | return; |
---|
82 | |
---|
83 | if( access( global.conf->configdir, F_OK ) != 0 ) |
---|
84 | log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", global.conf->configdir ); |
---|
85 | else if( access( global.conf->configdir, 06 ) != 0 ) |
---|
86 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir ); |
---|
87 | if( help_init( &(global.help), HELP_FILE ) == NULL ) |
---|
88 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", global.helpfile ); |
---|
89 | } |
---|
90 | |
---|
91 | void service_main (DWORD argc, LPTSTR *argv) |
---|
92 | { |
---|
93 | SERVICE_STATUS_HANDLE handle; |
---|
94 | SERVICE_STATUS status; |
---|
95 | |
---|
96 | handle = RegisterServiceCtrlHandler("bitlbee", service_ctrl); |
---|
97 | |
---|
98 | if (!handle) |
---|
99 | return; |
---|
100 | |
---|
101 | status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; |
---|
102 | status.dwServiceSpecificExitCode = 0; |
---|
103 | |
---|
104 | bitlbee_init(argc, argv); |
---|
105 | |
---|
106 | SetServiceStatus(handle, &status); |
---|
107 | |
---|
108 | b_main_run( ); |
---|
109 | } |
---|
110 | |
---|
111 | SERVICE_TABLE_ENTRY dispatch_table[] = |
---|
112 | { |
---|
113 | { TEXT("bitlbee"), (LPSERVICE_MAIN_FUNCTION)service_main }, |
---|
114 | { NULL, NULL } |
---|
115 | }; |
---|
116 | |
---|
117 | static int debug = 0; |
---|
118 | |
---|
119 | static void usage() |
---|
120 | { |
---|
121 | printf("Options:\n"); |
---|
122 | printf("-h Show this help message\n"); |
---|
123 | printf("-d Debug mode (simple console program)\n"); |
---|
124 | } |
---|
125 | |
---|
126 | int main( int argc, char **argv) |
---|
127 | { |
---|
128 | int i; |
---|
129 | WSADATA WSAData; |
---|
130 | |
---|
131 | nogaim_init( ); |
---|
132 | |
---|
133 | for (i = 1; i < argc; i++) { |
---|
134 | if (!strcmp(argv[i], "-d")) debug = 1; |
---|
135 | if (!strcmp(argv[i], "-h")) { |
---|
136 | usage(); |
---|
137 | return 0; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | WSAStartup(MAKEWORD(1,1), &WSAData); |
---|
142 | |
---|
143 | if (!debug) { |
---|
144 | if (!StartServiceCtrlDispatcher(dispatch_table)) |
---|
145 | log_message( LOGLVL_ERROR, "StartServiceCtrlDispatcher failed."); |
---|
146 | } else { |
---|
147 | bitlbee_init(argc, argv); |
---|
148 | b_main_run(); |
---|
149 | } |
---|
150 | |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | double gettime() |
---|
155 | { |
---|
156 | return (GetTickCount() / 1000); |
---|
157 | } |
---|
158 | |
---|
159 | void conf_get_string(HKEY section, const char *name, const char *def, char **dest) |
---|
160 | { |
---|
161 | char buf[4096]; |
---|
162 | long x; |
---|
163 | if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) { |
---|
164 | *dest = g_strdup(buf); |
---|
165 | } else if (!def) { |
---|
166 | *dest = NULL; |
---|
167 | } else { |
---|
168 | *dest = g_strdup(def); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | void conf_get_int(HKEY section, const char *name, int def, int *dest) |
---|
174 | { |
---|
175 | char buf[20]; |
---|
176 | long x; |
---|
177 | DWORD y; |
---|
178 | if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) { |
---|
179 | memcpy(&y, buf, sizeof(DWORD)); |
---|
180 | *dest = y; |
---|
181 | } else { |
---|
182 | *dest = def; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | conf_t *conf_load( int argc, char *argv[] ) |
---|
187 | { |
---|
188 | conf_t *conf; |
---|
189 | HKEY key, key_main, key_proxy; |
---|
190 | char *tmp; |
---|
191 | |
---|
192 | RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\Bitlbee", &key); |
---|
193 | RegOpenKey(key, "main", &key_main); |
---|
194 | RegOpenKey(key, "proxy", &key_proxy); |
---|
195 | |
---|
196 | memset( &global, 0, sizeof( global_t ) ); |
---|
197 | b_main_init(); |
---|
198 | |
---|
199 | conf = g_new0( conf_t,1 ); |
---|
200 | global.conf = conf; |
---|
201 | conf_get_string(key_main, "interface_in", "0.0.0.0", &global.conf->iface_in); |
---|
202 | conf_get_string(key_main, "interface_out", "0.0.0.0", &global.conf->iface_out); |
---|
203 | conf_get_string(key_main, "port", "6667", &global.conf->port); |
---|
204 | conf_get_int(key_main, "verbose", 0, &global.conf->verbose); |
---|
205 | conf_get_string(key_main, "auth_pass", "", &global.conf->auth_pass); |
---|
206 | conf_get_string(key_main, "oper_pass", "", &global.conf->oper_pass); |
---|
207 | conf_get_int(key_main, "ping_interval_timeout", 60, &global.conf->ping_interval); |
---|
208 | conf_get_string(key_main, "hostname", "localhost", &global.conf->hostname); |
---|
209 | conf_get_string(key_main, "configdir", NULL, &global.conf->configdir); |
---|
210 | conf_get_string(key_main, "motdfile", NULL, &global.conf->motdfile); |
---|
211 | conf_get_string(key_main, "helpfile", NULL, &global.helpfile); |
---|
212 | global.conf->runmode = RUNMODE_DAEMON; |
---|
213 | conf_get_int(key_main, "AuthMode", AUTHMODE_OPEN, (int *)&global.conf->authmode); |
---|
214 | conf_get_string(key_proxy, "host", "", &tmp); strcpy(proxyhost, tmp); |
---|
215 | conf_get_string(key_proxy, "user", "", &tmp); strcpy(proxyuser, tmp); |
---|
216 | conf_get_string(key_proxy, "password", "", &tmp); strcpy(proxypass, tmp); |
---|
217 | conf_get_int(key_proxy, "type", PROXY_NONE, &proxytype); |
---|
218 | conf_get_int(key_proxy, "port", 3128, &proxyport); |
---|
219 | |
---|
220 | RegCloseKey(key); |
---|
221 | RegCloseKey(key_main); |
---|
222 | RegCloseKey(key_proxy); |
---|
223 | |
---|
224 | return conf; |
---|
225 | } |
---|
226 | |
---|
227 | void conf_loaddefaults( irc_t *irc ) |
---|
228 | { |
---|
229 | HKEY key_defaults; |
---|
230 | int i; |
---|
231 | char name[4096], data[4096]; |
---|
232 | DWORD namelen = sizeof(name), datalen = sizeof(data); |
---|
233 | DWORD type; |
---|
234 | if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\defaults", &key_defaults) != ERROR_SUCCESS) { |
---|
235 | return; |
---|
236 | } |
---|
237 | |
---|
238 | for (i = 0; RegEnumValue(key_defaults, i, name, &namelen, NULL, &type, data, &datalen) == ERROR_SUCCESS; i++) { |
---|
239 | set_t *s = set_find( &irc->set, name ); |
---|
240 | |
---|
241 | if( s ) |
---|
242 | { |
---|
243 | if( s->def ) g_free( s->def ); |
---|
244 | s->def = g_strdup( data ); |
---|
245 | } |
---|
246 | |
---|
247 | namelen = sizeof(name); |
---|
248 | datalen = sizeof(data); |
---|
249 | } |
---|
250 | |
---|
251 | RegCloseKey(key_defaults); |
---|
252 | } |
---|
253 | |
---|
254 | #ifndef INADDR_NONE |
---|
255 | #define INADDR_NONE 0xffffffff |
---|
256 | #endif |
---|
257 | |
---|
258 | int |
---|
259 | inet_aton(const char *cp, struct in_addr *addr) |
---|
260 | { |
---|
261 | addr->s_addr = inet_addr(cp); |
---|
262 | return (addr->s_addr == INADDR_NONE) ? 0 : 1; |
---|
263 | } |
---|
264 | |
---|
265 | void log_error(char *msg) |
---|
266 | { |
---|
267 | log_message(LOGLVL_ERROR, "%s", msg); |
---|
268 | } |
---|
269 | |
---|
270 | void log_message(int level, char *message, ...) |
---|
271 | { |
---|
272 | HANDLE hEventSource; |
---|
273 | LPTSTR lpszStrings[2]; |
---|
274 | WORD elevel; |
---|
275 | va_list ap; |
---|
276 | |
---|
277 | va_start(ap, message); |
---|
278 | |
---|
279 | if (debug) { |
---|
280 | vprintf(message, ap); |
---|
281 | putchar('\n'); |
---|
282 | va_end(ap); |
---|
283 | return; |
---|
284 | } |
---|
285 | |
---|
286 | hEventSource = RegisterEventSource(NULL, TEXT("bitlbee")); |
---|
287 | |
---|
288 | lpszStrings[0] = TEXT("bitlbee"); |
---|
289 | lpszStrings[1] = g_strdup_vprintf(message, ap); |
---|
290 | va_end(ap); |
---|
291 | |
---|
292 | switch (level) { |
---|
293 | case LOGLVL_ERROR: elevel = EVENTLOG_ERROR_TYPE; break; |
---|
294 | case LOGLVL_WARNING: elevel = EVENTLOG_WARNING_TYPE; break; |
---|
295 | case LOGLVL_INFO: elevel = EVENTLOG_INFORMATION_TYPE; break; |
---|
296 | #ifdef DEBUG |
---|
297 | case LOGLVL_DEBUG: elevel = EVENTLOG_AUDIT_SUCCESS; break; |
---|
298 | #endif |
---|
299 | } |
---|
300 | |
---|
301 | if (hEventSource != NULL) { |
---|
302 | ReportEvent(hEventSource, |
---|
303 | elevel, |
---|
304 | 0, |
---|
305 | 0, |
---|
306 | NULL, |
---|
307 | 2, |
---|
308 | 0, |
---|
309 | lpszStrings, |
---|
310 | NULL); |
---|
311 | |
---|
312 | DeregisterEventSource(hEventSource); |
---|
313 | } |
---|
314 | |
---|
315 | g_free(lpszStrings[1]); |
---|
316 | } |
---|
317 | |
---|
318 | void log_link(int level, int output) { /* FIXME */ } |
---|
319 | |
---|
320 | struct tm * |
---|
321 | gmtime_r (const time_t *timer, struct tm *result) |
---|
322 | { |
---|
323 | struct tm *local_result; |
---|
324 | local_result = gmtime (timer); |
---|
325 | |
---|
326 | if (local_result == NULL || result == NULL) |
---|
327 | return NULL; |
---|
328 | |
---|
329 | memcpy (result, local_result, sizeof (result)); |
---|
330 | return result; |
---|
331 | } |
---|