source: win32.c @ d1d6776

Last change on this file since d1d6776 was d1d6776, checked in by Jelmer Vernooij <jelmer@…>, at 2005-11-07T16:20:37Z

Import win32 branch

  • Property mode set to 100644
File size: 5.6 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 (Unix 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#include "bitlbee.h"
27#include "commands.h"
28#include "crypting.h"
29#include "protocols/nogaim.h"
30#include "help.h"
31#include <signal.h>
32#include <unistd.h>
33#include <sys/time.h>
34#include <winreg.h>
35#include <winbase.h>
36
37global_t global;        /* Against global namespace pollution */
38
39int main( int argc, char *argv[] )
40{
41        int i = -1;
42        memset( &global, 0, sizeof( global_t ) );
43       
44        global.loop = g_main_new( FALSE );
45       
46        log_init( );
47        nogaim_init( );
48       
49        global.conf = conf_load( argc, argv );
50        if( global.conf == NULL )
51                return( 1 );
52       
53        if( global.conf->runmode == RUNMODE_INETD )
54        {
55                i = bitlbee_inetd_init();
56                log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION );
57
58        }
59        else if( global.conf->runmode == RUNMODE_DAEMON )
60        {
61                i = bitlbee_daemon_init();
62                log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION );
63        } 
64        else 
65        {
66                log_message( LOGLVL_INFO, "No bitlbee mode specified...");
67        }
68       
69        if( i != 0 )
70                return( i );
71       
72        if( access( global.conf->configdir, F_OK ) != 0 )
73                log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", global.conf->configdir );
74        else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 )
75                log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
76        if( help_init( &(global.help) ) == NULL )
77                log_message( LOGLVL_WARNING, "Error opening helpfile %s.", global.helpfile );
78       
79        g_main_run( global.loop );
80       
81        return( 0 );
82}
83
84double gettime()
85{
86        return (GetTickCount() / 1000);
87}
88
89void conf_get_string(HKEY section, const char *name, const char *def, char **dest)
90{
91        char buf[4096];
92        long x;
93        if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) {
94                *dest = g_strdup(buf);
95        } else if (!def) {
96                *dest = NULL;
97        } else {
98                *dest = g_strdup(def);
99        }
100}
101
102
103void conf_get_int(HKEY section, const char *name, int def, int *dest)
104{
105        char buf[20];
106        long x;
107        DWORD y;
108        if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) {
109                memcpy(&y, buf, sizeof(DWORD));
110                *dest = y;
111        } else {
112                *dest = def;
113        }
114}
115
116conf_t *conf_load( int argc, char *argv[] ) 
117{
118        conf_t *conf;
119        HKEY key, key_main, key_proxy;
120        char *tmp;
121        RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee", &key);
122        RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\main", &key_main);
123        RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\proxy", &key_proxy);
124       
125        memset( &global, 0, sizeof( global_t ) );
126        global.loop = g_main_new(FALSE);
127        nogaim_init();
128
129        conf = g_new0( conf_t,1 );
130        global.conf = conf;
131        conf_get_string(key_main, "interface", "0.0.0.0", &global.conf->iface);
132        conf_get_int(key_main, "port", 6667, &global.conf->port);
133        conf_get_int(key_main, "verbose", 0, &global.conf->verbose);
134        conf_get_string(key_main, "password", "", &global.conf->password);
135        conf_get_int(key_main, "ping_interval_timeout", 60, &global.conf->ping_interval);
136        conf_get_string(key_main, "hostname", "localhost", &global.conf->hostname);
137        conf_get_string(key_main, "configdir", NULL, &global.conf->configdir);
138        conf_get_string(key_main, "motdfile", NULL, &global.conf->motdfile);
139        conf_get_string(key_main, "helpfile", NULL, &global.helpfile);
140        global.conf->runmode = RUNMODE_INETD;
141        conf_get_int(key_main, "AuthMode", AUTHMODE_CLOSED, &global.conf->authmode);
142        conf_get_string(key_proxy, "host", "", &tmp); strcpy(proxyhost, tmp);
143        conf_get_string(key_proxy, "user", "", &tmp); strcpy(proxyuser, tmp);
144        conf_get_string(key_proxy, "password", "", &tmp); strcpy(proxypass, tmp);
145        conf_get_int(key_proxy, "type", PROXY_NONE, &proxytype);
146        conf_get_int(key_proxy, "port", 3128, &proxyport);
147
148        RegCloseKey(key);
149        RegCloseKey(key_main);
150        RegCloseKey(key_proxy);
151
152        return conf;
153}
154
155void conf_loaddefaults( irc_t *irc )
156{
157        HKEY key_defaults;
158        int i;
159        char name[4096], data[4096];
160        DWORD namelen = sizeof(name), datalen = sizeof(data);
161        DWORD type;
162        if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\defaults", &key_defaults) != ERROR_SUCCESS) {
163                return;
164        }
165
166        for (i = 0; RegEnumValue(key_defaults, i, name, &namelen, NULL, &type, data, &datalen) == ERROR_SUCCESS; i++) {
167                set_t *s = set_find( irc, name );
168                       
169                if( s )
170                {
171                        if( s->def ) g_free( s->def );
172                        s->def = g_strdup( data );
173                }
174
175                namelen = sizeof(name);
176                datalen = sizeof(data);
177        }
178
179        RegCloseKey(key_defaults);
180}
181
182#ifndef INADDR_NONE
183#define INADDR_NONE 0xffffffff
184#endif
185
186int
187inet_aton(const char *cp, struct in_addr *addr)
188{
189  addr->s_addr = inet_addr(cp);
190  return (addr->s_addr == INADDR_NONE) ? 0 : 1;
191}
Note: See TracBrowser for help on using the repository browser.