source: win32.c @ 99318ad

Last change on this file since 99318ad was 99318ad, checked in by Jelmer Vernooij <jelmer@…>, at 2005-11-07T16:32:40Z

Import work on services-based Win32 port

  • Property mode set to 100644
File size: 7.4 KB
Line 
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 "crypting.h"
30#include "protocols/nogaim.h"
31#include "help.h"
32#include <signal.h>
33#include <windows.h>
34
35global_t global;        /* Against global namespace pollution */
36
37static void WINAPI service_ctrl (DWORD dwControl)
38{
39        switch (dwControl)
40        {
41        case SERVICE_CONTROL_STOP:
42                        /* FIXME */
43            break;
44
45        case SERVICE_CONTROL_INTERROGATE:
46            break;
47
48        default:
49            break;
50
51    }
52}
53
54void service_main (DWORD argc, LPTSTR *argv)
55{
56        int i = -1;
57        SERVICE_STATUS_HANDLE handle;
58        SERVICE_STATUS status;
59
60    handle = RegisterServiceCtrlHandler("bitlbee", service_ctrl);
61
62    if (!handle)
63                return;
64
65    status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
66    status.dwServiceSpecificExitCode = 0;
67
68        memset( &global, 0, sizeof( global_t ) );
69       
70        global.loop = g_main_new( FALSE );
71       
72        global.conf = conf_load( argc, argv );
73        if( global.conf == NULL )
74                return;
75       
76        if( global.conf->runmode == RUNMODE_INETD )
77        {
78                i = bitlbee_inetd_init();
79                log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION );
80
81        }
82        else if( global.conf->runmode == RUNMODE_DAEMON )
83        {
84                i = bitlbee_daemon_init();
85                log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION );
86        } 
87        else 
88        {
89                log_message( LOGLVL_INFO, "No bitlbee mode specified...");
90        }
91       
92        if( i != 0 )
93                return;
94       
95        if( access( global.conf->configdir, F_OK ) != 0 )
96                log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", global.conf->configdir );
97        else if( access( global.conf->configdir, 06 ) != 0 )
98                log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
99        if( help_init( &(global.help) ) == NULL )
100                log_message( LOGLVL_WARNING, "Error opening helpfile %s.", global.helpfile );
101
102        SetServiceStatus(handle, &status);
103       
104        g_main_run( global.loop );
105}
106
107int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
108{
109    SERVICE_TABLE_ENTRY dispatch_table[] =
110    {
111        { TEXT("bitlbee"), (LPSERVICE_MAIN_FUNCTION)service_main },
112        { NULL, NULL }
113    };
114
115        nogaim_init( );
116
117    if (!StartServiceCtrlDispatcher(dispatch_table))
118                log_message( LOGLVL_ERROR, "StartServiceCtrlDispatcher failed.");
119       
120        return 0;
121}
122
123double gettime()
124{
125        return (GetTickCount() / 1000);
126}
127
128void conf_get_string(HKEY section, const char *name, const char *def, char **dest)
129{
130        char buf[4096];
131        long x;
132        if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) {
133                *dest = g_strdup(buf);
134        } else if (!def) {
135                *dest = NULL;
136        } else {
137                *dest = g_strdup(def);
138        }
139}
140
141
142void conf_get_int(HKEY section, const char *name, int def, int *dest)
143{
144        char buf[20];
145        long x;
146        DWORD y;
147        if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) {
148                memcpy(&y, buf, sizeof(DWORD));
149                *dest = y;
150        } else {
151                *dest = def;
152        }
153}
154
155conf_t *conf_load( int argc, char *argv[] ) 
156{
157        conf_t *conf;
158        HKEY key, key_main, key_proxy;
159        char *tmp;
160
161        RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\Bitlbee", &key);
162        RegOpenKey(key, "main", &key_main);
163        RegOpenKey(key, "proxy", &key_proxy);
164       
165        memset( &global, 0, sizeof( global_t ) );
166        global.loop = g_main_new(FALSE);
167        nogaim_init();
168
169        conf = g_new0( conf_t,1 );
170        global.conf = conf;
171        conf_get_string(key_main, "interface", "0.0.0.0", &global.conf->iface);
172        conf_get_int(key_main, "port", 6667, &global.conf->port);
173        conf_get_int(key_main, "verbose", 0, &global.conf->verbose);
174        conf_get_string(key_main, "password", "", &global.conf->password);
175        conf_get_int(key_main, "ping_interval_timeout", 60, &global.conf->ping_interval);
176        conf_get_string(key_main, "hostname", "localhost", &global.conf->hostname);
177        conf_get_string(key_main, "configdir", NULL, &global.conf->configdir);
178        conf_get_string(key_main, "motdfile", NULL, &global.conf->motdfile);
179        conf_get_string(key_main, "helpfile", NULL, &global.helpfile);
180        global.conf->runmode = RUNMODE_INETD;
181        conf_get_int(key_main, "AuthMode", AUTHMODE_CLOSED, &global.conf->authmode);
182        conf_get_string(key_proxy, "host", "", &tmp); strcpy(proxyhost, tmp);
183        conf_get_string(key_proxy, "user", "", &tmp); strcpy(proxyuser, tmp);
184        conf_get_string(key_proxy, "password", "", &tmp); strcpy(proxypass, tmp);
185        conf_get_int(key_proxy, "type", PROXY_NONE, &proxytype);
186        conf_get_int(key_proxy, "port", 3128, &proxyport);
187
188        RegCloseKey(key);
189        RegCloseKey(key_main);
190        RegCloseKey(key_proxy);
191
192        return conf;
193}
194
195void conf_loaddefaults( irc_t *irc )
196{
197        HKEY key_defaults;
198        int i;
199        char name[4096], data[4096];
200        DWORD namelen = sizeof(name), datalen = sizeof(data);
201        DWORD type;
202        if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\defaults", &key_defaults) != ERROR_SUCCESS) {
203                return;
204        }
205
206        for (i = 0; RegEnumValue(key_defaults, i, name, &namelen, NULL, &type, data, &datalen) == ERROR_SUCCESS; i++) {
207                set_t *s = set_find( irc, name );
208                       
209                if( s )
210                {
211                        if( s->def ) g_free( s->def );
212                        s->def = g_strdup( data );
213                }
214
215                namelen = sizeof(name);
216                datalen = sizeof(data);
217        }
218
219        RegCloseKey(key_defaults);
220}
221
222#ifndef INADDR_NONE
223#define INADDR_NONE 0xffffffff
224#endif
225
226int
227inet_aton(const char *cp, struct in_addr *addr)
228{
229  addr->s_addr = inet_addr(cp);
230  return (addr->s_addr == INADDR_NONE) ? 0 : 1;
231}
232
233void log_error(char *msg)
234{
235        log_message(LOGLVL_ERROR, "%s", msg);
236}
237
238void log_message(int level, char *message, ...)
239{
240    HANDLE  hEventSource;
241    LPTSTR  lpszStrings[2];
242        WORD elevel;
243    va_list ap;
244
245    va_start(ap, message);
246
247    hEventSource = RegisterEventSource(NULL, TEXT("bitlbee"));
248
249    lpszStrings[0] = TEXT("bitlbee");
250    lpszStrings[1] = g_strdup_vprintf(message, ap);
251    va_end(ap);
252
253        switch (level) {
254        case LOGLVL_ERROR: elevel = EVENTLOG_ERROR_TYPE; break;
255        case LOGLVL_WARNING: elevel = EVENTLOG_WARNING_TYPE; break;
256        case LOGLVL_INFO: elevel = EVENTLOG_INFORMATION_TYPE; break;
257#ifdef DEBUG
258        case LOGLVL_DEBUG: elevel = EVENTLOG_AUDIT_SUCCESS; break;
259#endif
260        }
261
262    if (hEventSource != NULL) {
263        ReportEvent(hEventSource, 
264        elevel,
265        0,                   
266        0,                   
267        NULL,                 
268        2,                   
269        0,                   
270        lpszStrings,         
271        NULL);               
272
273        DeregisterEventSource(hEventSource);
274    }
275
276        g_free(lpszStrings[1]);
277}
Note: See TracBrowser for help on using the repository browser.