1 //=============================================================================
2 //
3 //   File : KviKvsAsyncDnsOperation.cpp
4 //   Creation date : Sun 10 Jul 2005 04:36:15 by Szymon Stefanek
5 //
6 //   This file is part of the KVIrc IRC Client distribution
7 //   Copyright (C) 2005-2010 Szymon Stefanek <pragma at kvirc dot net>
8 //
9 //   This program is FREE software. You can redistribute it and/or
10 //   modify it under the terms of the GNU General Public License
11 //   as published by the Free Software Foundation; either version 2
12 //   of the License, or (at your option) any later version.
13 //
14 //   This program is distributed in the HOPE that it will be USEFUL,
15 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 //   See the GNU General Public License for more details.
18 //
19 //   You should have received a copy of the GNU General Public License
20 //   along with this program. If not, write to the Free Software Foundation,
21 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=============================================================================
24 
25 #include "KviKvsAsyncDnsOperation.h"
26 #include "KviDnsResolver.h"
27 #include "KviKvsScript.h"
28 #include "KviKvsVariant.h"
29 #include "KviApplication.h"
30 #include "kvi_out.h"
31 #include "KviLocale.h"
32 #include "KviError.h"
33 #include "KviKvsVariantList.h"
34 
35 #include <QTimer>
36 
KviKvsAsyncDnsOperation(KviWindow * pWnd,QString & szQuery,KviDnsResolver::QueryType eType,KviKvsScript * pCallback,KviKvsVariant * pMagic)37 KviKvsAsyncDnsOperation::KviKvsAsyncDnsOperation(KviWindow * pWnd, QString & szQuery, KviDnsResolver::QueryType eType, KviKvsScript * pCallback, KviKvsVariant * pMagic)
38     : KviKvsAsyncOperation(pWnd)
39 {
40 	m_pDns = new KviDnsResolver();
41 	m_szQuery = szQuery;
42 	m_eType = eType;
43 	m_pCallback = pCallback;
44 	m_pMagic = pMagic;
45 	connect(m_pDns, SIGNAL(lookupDone(KviDnsResolver *)), this, SLOT(lookupTerminated(KviDnsResolver *)));
46 	if(!m_pDns->lookup(szQuery, eType))
47 		QTimer::singleShot(10, this, SLOT(dnsStartFailed()));
48 }
49 
~KviKvsAsyncDnsOperation()50 KviKvsAsyncDnsOperation::~KviKvsAsyncDnsOperation()
51 {
52 	delete m_pDns;
53 	delete m_pMagic;
54 	delete m_pCallback;
55 }
56 
dnsStartFailed()57 void KviKvsAsyncDnsOperation::dnsStartFailed()
58 {
59 	lookupTerminated(m_pDns);
60 }
61 
lookupTerminated(KviDnsResolver *)62 void KviKvsAsyncDnsOperation::lookupTerminated(KviDnsResolver *)
63 {
64 	KviWindow * pWnd = window();
65 	if(!g_pApp->windowExists(pWnd))
66 		pWnd = g_pActiveWindow;
67 
68 	if(m_pCallback)
69 	{
70 		KviKvsVariantList params;
71 		params.setAutoDelete(true);
72 		if(m_pDns->state() == KviDnsResolver::Failure)
73 		{
74 			params.append(new KviKvsVariant(m_szQuery));
75 			params.append(new KviKvsVariant((kvs_int_t)0));
76 			params.append(new KviKvsVariant(m_pDns->errorString()));
77 			params.append(new KviKvsVariant());
78 			params.append(new KviKvsVariant(*m_pMagic));
79 		}
80 		else
81 		{
82 			QString szHostName = m_pDns->hostName();
83 			const auto & strL = m_pDns->ipAddressList();
84 			const QString & fi = strL.empty() ? QString("?.?.?.?") : strL.front();
85 
86 			params.append(new KviKvsVariant(m_szQuery));
87 			params.append(new KviKvsVariant((kvs_int_t)1));
88 			params.append(new KviKvsVariant(fi));
89 			params.append(new KviKvsVariant(szHostName.isEmpty() ? QString("?.?") : szHostName));
90 			params.append(new KviKvsVariant(*m_pMagic));
91 		}
92 
93 		m_pCallback->run(pWnd, &params, nullptr, KviKvsScript::PreserveParams);
94 
95 		delete this;
96 		return;
97 	}
98 
99 	// we have no callback : output the results
100 	QString szQuery = m_pDns->query();
101 	pWnd->output(KVI_OUT_HOSTLOOKUP, __tr2qs_ctx("DNS lookup result for query \"%Q\"", "kvs"), &szQuery);
102 
103 	if(m_pDns->state() == KviDnsResolver::Failure)
104 	{
105 		QString strDescription(m_pDns->errorString());
106 		pWnd->output(KVI_OUT_HOSTLOOKUP, __tr2qs_ctx("Error: %Q", "kvs"), &strDescription);
107 	}
108 	else
109 	{
110 		QString szHostName = m_pDns->hostName();
111 		pWnd->output(KVI_OUT_HOSTLOOKUP, __tr2qs_ctx("Hostname: %Q", "kvs"), &szHostName);
112 		int idx = 1;
113 		for(const auto & a : m_pDns->ipAddressList())
114 		{
115 			pWnd->output(KVI_OUT_HOSTLOOKUP, __tr2qs_ctx("IP address %d: %Q", "kvs"), idx, &a);
116 			idx++;
117 		}
118 	}
119 
120 	delete this;
121 }
122