summaryrefslogtreecommitdiffstats
path: root/src/modules/dcc/utils.cpp
blob: 8d3229e7bcc5f67d3a925c409a91cc21a8d4c02e (plain)
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
//
//   File : utils.cpp
//   Creation date : Tue Jul 23 02:54:44 2002 GMT by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//

#define _UTILS_CPP_

#include "utils.h"

#include "kvi_options.h"
#include "kvi_locale.h"
#include "kvi_netutils.h"
#include "kvi_ircsocket.h"
#include "kvi_qstring.h"

/*
bool dcc_module_get_listen_ip_address(KviCommand *c,KviConsole * pConsole,QString &szListenIp)
{
	//
	// Find an interface suitable for listening....
	// Either from user options or from the current connection...
	//

	if(KVI_OPTION_BOOL(KviOption_boolDccListenOnSpecifiedInterfaceByDefault))
	{
		KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).stripWhiteSpace();
		if(!KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).isEmpty())
		{
			if(kvi_isValidStringIp(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data()))
			{
				if(KviQString::equalCI(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface),"0.0.0.0"))
				{
					// Try to find the first available IpV4 interface
					if(!kvi_getLocalHostAddress(szListenIp))
					{
						if(c)c->warning(__tr2qs_ctx("Can't retrieve a suitable local IPV4 address","dcc"),
								KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data());
						return false;
					}
				} else {
					szListenIp = KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface);
				}
				return true;
			}
#ifdef COMPILE_IPV6_SUPPORT
			if(kvi_isValidStringIp_V6(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data()))
			{
				szListenIp = KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface);
			} else {
#endif
				if(!kvi_getInterfaceAddress(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data(),szListenIp))
				{
					KVI_OPTION_BOOL(KviOption_boolDccListenOnSpecifiedInterfaceByDefault) = false;
					if(c)c->warning(__tr2qs_ctx("Can't listen on default interface '%s': fix it in the options dialog, disabling the option (so the next dcc will work)","dcc"),
							KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data());
					return false;
				}
#ifdef COMPILE_IPV6_SUPPORT
			}
#endif
			return true;
		} else {
			// the option was empty.. disable it
			KVI_OPTION_BOOL(KviOption_boolDccListenOnSpecifiedInterfaceByDefault) = false;
		}
	}

	if(pConsole)
	{
		if(pConsole->isConnected())
		{
			//#warning "The IPV6 choice is not OK here.... and maybe allow to bind to specified ports"
			pConsole->socket()->getLocalHostIp(szListenIp,pConsole->isIpV6Connection());
		} else {
			szListenIp = "0.0.0.0"; // huh ? :)
		}
	} else {
		szListenIp = "0.0.0.0";
	}

	return true;
}
*/


bool dcc_kvs_get_listen_ip_address(KviKvsModuleCommandCall *c,KviConsole * pConsole,QString &szListenIp)
{
	//
	// Find an interface suitable for listening....
	// Either from user options or from the current connection...
	//

	if(KVI_OPTION_BOOL(KviOption_boolDccListenOnSpecifiedInterfaceByDefault))
	{
		KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).stripWhiteSpace();
		if(!KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).isEmpty())
		{
			if(kvi_isValidStringIp(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data()))
			{
				if(KviQString::equalCI(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface),"0.0.0.0"))
				{
					// Try to find the first available IpV4 interface
					if(!kvi_getLocalHostAddress(szListenIp))
					{
						if(c)c->warning(__tr2qs_ctx("Can't retrieve a suitable local IPV4 address","dcc"),
								KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data());
						return false;
					}
				} else {
					szListenIp = KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface);
				}
				return true;
			}
#ifdef COMPILE_IPV6_SUPPORT
			if(kvi_isValidStringIp_V6(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data()))
			{
				szListenIp = KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface);
			} else {
#endif
				if(!kvi_getInterfaceAddress(KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data(),szListenIp))
				{
					KVI_OPTION_BOOL(KviOption_boolDccListenOnSpecifiedInterfaceByDefault) = false;
					if(c)c->warning(__tr2qs_ctx("Can't listen on default interface '%s': fix it in the options dialog, disabling the option (so the next dcc will work)","dcc"),
							KVI_OPTION_STRING(KviOption_stringDccListenDefaultInterface).utf8().data());
					return false;
				}
#ifdef COMPILE_IPV6_SUPPORT
			}
#endif
			return true;
		} else {
			// the option was empty.. disable it
			KVI_OPTION_BOOL(KviOption_boolDccListenOnSpecifiedInterfaceByDefault) = false;
		}
	}

	if(pConsole)
	{
		if(pConsole->isConnected())
		{
			//#warning "The IPV6 choice is not OK here.... and maybe allow to bind to specified ports"
			pConsole->socket()->getLocalHostIp(szListenIp,pConsole->isIpV6Connection());
		} else {
			szListenIp = "0.0.0.0"; // huh ? :)
		}
	} else {
		szListenIp = "0.0.0.0";
	}

	return true;
}