File: win32.c

package info (click to toggle)
bitlbee 3.0.5-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,568 kB
  • sloc: ansic: 50,906; xml: 2,072; perl: 1,045; sh: 979; makefile: 613; python: 512
file content (331 lines) | stat: -rw-r--r-- 8,118 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  /********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway					 *
  *																	*
  * Copyright 2002-2004 Wilmer van der Gaast and others				*
  \********************************************************************/

/* Main file (Windows specific part)								   */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License with
  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  Suite 330, Boston, MA  02111-1307  USA
*/

#define BITLBEE_CORE
#include "bitlbee.h"
#include "commands.h"
#include "protocols/nogaim.h"
#include "help.h"
#include <signal.h>
#include <windows.h>

global_t global;	/* Against global namespace pollution */

static void WINAPI service_ctrl (DWORD dwControl)
{
	switch (dwControl)
	{
		case SERVICE_CONTROL_STOP:
			/* FIXME */
			break;

		case SERVICE_CONTROL_INTERROGATE:
			break;

		default:
			break;

	}
}

static void bitlbee_init(int argc, char **argv)
{
	int i = -1;
	memset( &global, 0, sizeof( global_t ) );

	b_main_init();
	
	global.conf = conf_load( argc, argv );
	if( global.conf == NULL )
		return;
	
	if( global.conf->runmode == RUNMODE_INETD )
	{
		i = bitlbee_inetd_init();
		log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION );

	}
	else if( global.conf->runmode == RUNMODE_DAEMON )
	{
		i = bitlbee_daemon_init();
		log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION );
	} 
	else 
	{
		log_message( LOGLVL_INFO, "No bitlbee mode specified...");
	}
	
	if( i != 0 )
		return;
 	
	if( access( global.conf->configdir, F_OK ) != 0 )
		log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", global.conf->configdir );
	else if( access( global.conf->configdir, 06 ) != 0 )
		log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
	if( help_init( &(global.help), HELP_FILE ) == NULL )
		log_message( LOGLVL_WARNING, "Error opening helpfile %s.", global.helpfile );
}

void service_main (DWORD argc, LPTSTR *argv)
{
	SERVICE_STATUS_HANDLE handle;
	SERVICE_STATUS status;

	handle = RegisterServiceCtrlHandler("bitlbee", service_ctrl);

	if (!handle)
		return;

	status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	status.dwServiceSpecificExitCode = 0;

	bitlbee_init(argc, argv);

	SetServiceStatus(handle, &status);
	
	b_main_run( );
}

SERVICE_TABLE_ENTRY dispatch_table[] =
{
   { TEXT("bitlbee"), (LPSERVICE_MAIN_FUNCTION)service_main },
   { NULL, NULL }
};

static int debug = 0;

static void usage()
{
	printf("Options:\n");
	printf("-h   Show this help message\n");
	printf("-d   Debug mode (simple console program)\n");
}

int main( int argc, char **argv)
{	
	int i;
	WSADATA WSAData;

	nogaim_init( );

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-d")) debug = 1;
		if (!strcmp(argv[i], "-h")) {
			usage();
			return 0;
		}
	}

	WSAStartup(MAKEWORD(1,1), &WSAData);

	if (!debug) {
		if (!StartServiceCtrlDispatcher(dispatch_table))
			log_message( LOGLVL_ERROR, "StartServiceCtrlDispatcher failed.");
	} else {
			bitlbee_init(argc, argv);
 			b_main_run();
	}
	
	return 0;
}

double gettime()
{
	return (GetTickCount() / 1000);
}

void conf_get_string(HKEY section, const char *name, const char *def, char **dest)
{
	char buf[4096];
	long x;
	if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) {
		*dest = g_strdup(buf);
	} else if (!def) {
		*dest = NULL;
	} else {
		*dest = g_strdup(def);
	}
}


void conf_get_int(HKEY section, const char *name, int def, int *dest)
{
	char buf[20];
	long x;
	DWORD y;
	if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) {
		memcpy(&y, buf, sizeof(DWORD));
		*dest = y;
	} else {
		*dest = def;
	}
}

conf_t *conf_load( int argc, char *argv[] ) 
{
	conf_t *conf;
	HKEY key, key_main, key_proxy;
	char *tmp;

	RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\Bitlbee", &key);
	RegOpenKey(key, "main", &key_main);
	RegOpenKey(key, "proxy", &key_proxy);
	
	memset( &global, 0, sizeof( global_t ) );
	b_main_init();

	conf = g_new0( conf_t,1 );
	global.conf = conf;
	conf_get_string(key_main, "interface_in", "0.0.0.0", &global.conf->iface_in);
	conf_get_string(key_main, "interface_out", "0.0.0.0", &global.conf->iface_out);
	conf_get_string(key_main, "port", "6667", &global.conf->port);
	conf_get_int(key_main, "verbose", 0, &global.conf->verbose);
	conf_get_string(key_main, "auth_pass", "", &global.conf->auth_pass);
	conf_get_string(key_main, "oper_pass", "", &global.conf->oper_pass);
	conf_get_int(key_main, "ping_interval_timeout", 60, &global.conf->ping_interval);
	conf_get_string(key_main, "hostname", "localhost", &global.conf->hostname);
	conf_get_string(key_main, "configdir", NULL, &global.conf->configdir);
	conf_get_string(key_main, "motdfile", NULL, &global.conf->motdfile);
	conf_get_string(key_main, "helpfile", NULL, &global.helpfile);
	global.conf->runmode = RUNMODE_DAEMON;
	conf_get_int(key_main, "AuthMode", AUTHMODE_OPEN, (int *)&global.conf->authmode);
	conf_get_string(key_proxy, "host", "", &tmp); strcpy(proxyhost, tmp);
	conf_get_string(key_proxy, "user", "", &tmp); strcpy(proxyuser, tmp);
	conf_get_string(key_proxy, "password", "", &tmp); strcpy(proxypass, tmp);
	conf_get_int(key_proxy, "type", PROXY_NONE, &proxytype);
	conf_get_int(key_proxy, "port", 3128, &proxyport);

	RegCloseKey(key);
	RegCloseKey(key_main);
	RegCloseKey(key_proxy);

	return conf;
}

void conf_loaddefaults( irc_t *irc )
{
	HKEY key_defaults;
	int i;
	char name[4096], data[4096];
	DWORD namelen = sizeof(name), datalen = sizeof(data);
	DWORD type;
	if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\defaults", &key_defaults) != ERROR_SUCCESS) {
		return;
	}

	for (i = 0; RegEnumValue(key_defaults, i, name, &namelen, NULL, &type, data, &datalen) == ERROR_SUCCESS; i++) {
		set_t *s = set_find( &irc->set, name );
			
		if( s )
		{
			if( s->def ) g_free( s->def );
			s->def = g_strdup( data );
		}

		namelen = sizeof(name);
		datalen = sizeof(data);
	}

	RegCloseKey(key_defaults);
}

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

int
inet_aton(const char *cp, struct in_addr *addr)
{
  addr->s_addr = inet_addr(cp);
  return (addr->s_addr == INADDR_NONE) ? 0 : 1;
}

void log_error(char *msg)
{
	log_message(LOGLVL_ERROR, "%s", msg);
}

void log_message(int level, char *message, ...)
{
	HANDLE  hEventSource;
	LPTSTR  lpszStrings[2];
	WORD elevel;
	va_list ap;

	va_start(ap, message);

	if (debug) {
		vprintf(message, ap);
		putchar('\n');
		va_end(ap);
		return;
	}

	hEventSource = RegisterEventSource(NULL, TEXT("bitlbee"));

	lpszStrings[0] = TEXT("bitlbee");
	lpszStrings[1] = g_strdup_vprintf(message, ap);
	va_end(ap);

	switch (level) {
	case LOGLVL_ERROR: elevel = EVENTLOG_ERROR_TYPE; break;
	case LOGLVL_WARNING: elevel = EVENTLOG_WARNING_TYPE; break;
	case LOGLVL_INFO: elevel = EVENTLOG_INFORMATION_TYPE; break;
#ifdef DEBUG
	case LOGLVL_DEBUG: elevel = EVENTLOG_AUDIT_SUCCESS; break;
#endif
	}

	if (hEventSource != NULL) {
		ReportEvent(hEventSource, 
		elevel,
		0,					
		0,					
		NULL,				 
		2,					
		0,					
		lpszStrings,		  
		NULL);				

		DeregisterEventSource(hEventSource);
	}

	g_free(lpszStrings[1]);
}

void log_link(int level, int output) { /* FIXME */ }

struct tm *
gmtime_r (const time_t *timer, struct tm *result)
{
	struct tm *local_result;
	local_result = gmtime (timer);

	if (local_result == NULL || result == NULL)
		return NULL;

	memcpy (result, local_result, sizeof (result));
	return result;
}