summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_dnsmanager.cpp
blob: 478b7ad2fb99170ced19c09b4335bed774785466 (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
170
171
172
173
174
175
176
177
178
179
180
//=============================================================================
//
//   File : kvi_kvs_dnsmanager.cpp
//   Created on Wed 04 Aug 2004 04:38:31 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2004 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 __KVIRC__

#include "kvi_kvs_dnsmanager.h"
#include "kvi_dns.h"
#include "kvi_app.h"
#include "kvi_window.h"
#include "kvi_out.h"
#include "kvi_iconmanager.h"
#include "kvi_kvs_script.h"
#include "kvi_kvs_variantlist.h"
#include "kvi_string.h"
#include "kvi_locale.h"
#include "kvi_error.h"

KviKvsDnsManager * KviKvsDnsManager::m_pInstance = 0;


KviKvsDnsObject::KviKvsDnsObject(KviDns * pDns,KviWindow * pWnd,const TQString &szQuery,bool bRebindOnWindowClose,KviKvsScript * pCallback,KviKvsVariantList * pParameterList)
{
	m_pDns = pDns;
	m_pWindow = pWnd;
	m_szQuery = szQuery;
	m_pCallback = pCallback;
	m_pParameterList = pParameterList;
	m_bRebindOnWindowClose = bRebindOnWindowClose;
}

KviKvsDnsObject::~KviKvsDnsObject()
{
	TQObject::disconnect(m_pDns,0,0,0);
	if(m_pDns->isRunning())
	{
		g_pApp->collectGarbage(m_pDns);
	} else {
		delete m_pDns;
	}
	if(m_pCallback)delete m_pCallback;
	if(m_pParameterList)delete m_pParameterList;
}


KviKvsDnsManager::KviKvsDnsManager()
: TQObject()
{
	m_pDnsObjects = 0;
}

KviKvsDnsManager::~KviKvsDnsManager()
{
	if(m_pDnsObjects)delete m_pDnsObjects;
}

void KviKvsDnsManager::init()
{
	if(KviKvsDnsManager::m_pInstance)
	{
		debug("Trying to double init() the dns manager!");
		return;
	}
	KviKvsDnsManager::m_pInstance = new KviKvsDnsManager();
}

void KviKvsDnsManager::done()
{
	if(!KviKvsDnsManager::m_pInstance)
	{
		debug("Trying to call done() on a non existing dns manager!");
		return;
	}
	delete KviKvsDnsManager::m_pInstance;
	KviKvsDnsManager::m_pInstance = 0;
}

void KviKvsDnsManager::addDns(KviKvsDnsObject * pObject)
{
	if(!m_pDnsObjects)
	{
		m_pDnsObjects = new KviPointerHashTable<void *,KviKvsDnsObject>;
		m_pDnsObjects->setAutoDelete(true);
	}
	m_pDnsObjects->replace(pObject->dns(),pObject);
	connect(pObject->dns(),TQT_SIGNAL(lookupDone(KviDns *)),this,TQT_SLOT(dnsLookupTerminated(KviDns *)));
}

void KviKvsDnsManager::dnsLookupTerminated(KviDns * pDns)
{
	KviKvsDnsObject * o = m_pDnsObjects->find(pDns);
	if(!o)
	{
		debug("KviKvsDnsManager::dnsLookupTerminated(): can't find the KviKvsDnsObject structure");
		return;
	}

	if(!g_pApp->windowExists(o->window()))
	{
		if(o->rebindOnWindowClose())
		{
			o->setWindow(g_pActiveWindow);
		} else {
			// just kill it
			m_pDnsObjects->remove(pDns);
			return;
		}
	}

	if(o->callback())
	{
		KviKvsScript copy(*(o->callback()));
		if(!o->parameterList())
			o->setParameterList(new KviKvsVariantList());

		if(o->dns()->state() == KviDns::Failure)
		{
			// $4... is the magic data
			o->parameterList()->prepend(new KviKvsVariant()); // $3
			o->parameterList()->prepend(new KviKvsVariant(KviError::getDescription(o->dns()->error()))); // $2
			o->parameterList()->prepend(new KviKvsVariant((kvs_int_t)0)); // $1
		} else {
			TQString * fh = o->dns()->hostnameList()->first();
			TQString * fi = o->dns()->ipAddressList()->first();

			// $4... is the magic data
			o->parameterList()->prepend(new KviKvsVariant(fh ? *fh : TQString("?.?"))); // $3
			o->parameterList()->prepend(new KviKvsVariant(fi ? *fi : TQString("?.?.?.?"))); // $2
			o->parameterList()->prepend(new KviKvsVariant((kvs_int_t)1)); // $1
		}
		o->parameterList()->prepend(new KviKvsVariant(o->query())); // $0

		copy.run(o->window(),o->parameterList(),0,KviKvsScript::PreserveParams);

	} else {
		TQString szQuery = o->dns()->query();
		o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("DNS Lookup result for query \"%Q\""),&szQuery);
	
		if(o->dns()->state() == KviDns::Failure)
		{
			TQString szErr = KviError::getDescription(o->dns()->error());
			o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("Error: %Q"),&szErr);
		} else {
			int idx = 1;
			for(TQString * h = o->dns()->hostnameList()->first();h;h = o->dns()->hostnameList()->next())
			{
				o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("Hostname %d: %Q"),idx,h);
				idx++;
			}
			idx = 1;
			for(TQString * a = o->dns()->ipAddressList()->first();a;a = o->dns()->ipAddressList()->next())
			{
				o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("IP address %d: %Q"),idx,a);
				idx++;
			}
		}
	}

	m_pDnsObjects->remove(pDns);
}