1 /*
2  * xmpp.h - XMPP "core" library API
3  * Copyright (C) 2003  Justin Karneges
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * either version 2
9    of the License, or (at your option) any later version.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 
22 #ifndef XMPP_H
23 #define XMPP_H
24 
25 #include <QPair>
26 #include <QObject>
27 #include <QString>
28 #include <QHostAddress>
29 #include <QString>
30 #include <qxml.h>
31 #include <qdom.h>
32 
33 #include "iris_export.h"
34 #include "xmpp/jid/jid.h"
35 #include "xmpp_stanza.h"
36 #include "xmpp_stream.h"
37 #include "xmpp_clientstream.h"
38 
39 namespace QCA
40 {
41 	class TLS;
42 }
43 
44 #ifndef CS_XMPP
45 class ByteStream;
46 #endif
47 
48 #include <QtCrypto> // For QCA::SASL::Params
49 
50 namespace XMPP
51 {
52 	// CS_IMPORT_BEGIN cutestuff/bytestream.h
53 #ifdef CS_XMPP
54 	class ByteStream;
55 #endif
56 	// CS_IMPORT_END
57 
58 	class Debug
59 	{
60 	public:
61 		virtual ~Debug();
62 
63 		virtual void msg(const QString &)=0;
64 		virtual void outgoingTag(const QString &)=0;
65 		virtual void incomingTag(const QString &)=0;
66 		virtual void outgoingXml(const QDomElement &)=0;
67 		virtual void incomingXml(const QDomElement &)=0;
68 	};
69 
70 	void setDebug(Debug *);
71 
72 	class IRIS_EXPORT Connector : public QObject
73 	{
74 		Q_OBJECT
75 	public:
76 		Connector(QObject *parent=0);
77 		~Connector() override;
78 
79 		virtual void connectToServer(const QString &server)=0;
80 		virtual ByteStream *stream() const=0;
81 		virtual void done()=0;
82 
83 		bool useSSL() const;
84 		bool havePeerAddress() const;
85 		QHostAddress peerAddress() const;
86 		quint16 peerPort() const;
87 
88 		virtual QString host() const;
89 
90 	signals:
91 		void connected();
92 		void error();
93 
94 	protected:
95 		void setUseSSL(bool b);
96 		void setPeerAddressNone();
97 		void setPeerAddress(const QHostAddress &addr, quint16 port);
98 
99 	private:
100 		bool ssl;
101 		bool haveaddr;
102 		QHostAddress addr;
103 		quint16 port;
104 	};
105 
106 	class IRIS_EXPORT AdvancedConnector : public Connector
107 	{
108 		Q_OBJECT
109 	public:
110 		enum Error { ErrConnectionRefused, ErrHostNotFound, ErrProxyConnect, ErrProxyNeg, ErrProxyAuth, ErrStream };
111 		AdvancedConnector(QObject *parent=0);
112 		~AdvancedConnector() override;
113 
114 		class Proxy
115 		{
116 		public:
117 			enum { None, HttpConnect, HttpPoll, Socks };
118 			Proxy();
119 			~Proxy();
120 
121 			int type() const;
122 			QString host() const;
123 			quint16 port() const;
124 			QString url() const;
125 			QString user() const;
126 			QString pass() const;
127 			int pollInterval() const;
128 
129 			void setHttpConnect(const QString &host, quint16 port);
130 			void setHttpPoll(const QString &host, quint16 port, const QString &url);
131 			void setSocks(const QString &host, quint16 port);
132 			void setUserPass(const QString &user, const QString &pass);
133 			void setPollInterval(int secs);
134 
135 		private:
136 			int t;
137 			QString v_host, v_url;
138 			quint16 v_port;
139 			QString v_user, v_pass;
140 			int v_poll;
141 		};
142 
143 		void setProxy(const Proxy &proxy);
144 		void setOptHostPort(const QString &host, quint16 port);
145 		void setOptHostsPort(const QStringList &hosts, quint16 port);
146 		void setOptProbe(bool);
147 		void setOptSSL(bool);
148 
149 		void changePollInterval(int secs);
150 
151 		void connectToServer(const QString &server) override;
152 		ByteStream *stream() const override;
153 		void done() override;
154 
155 		int errorCode() const;
156 
157 		QString host() const override;
158 
159 	signals:
160 		void srvLookup(const QString &server);
161 		void srvResult(bool success);
162 		void httpSyncStarted();
163 		void httpSyncFinished();
164 
165 	private slots:
166 		void dns_done();
167 		void srv_done();
168 		void bs_connected();
169 		void bs_error(int);
170 		void http_syncStarted();
171 		void http_syncFinished();
172 		void t_timeout();
173 
174 	private:
175 		class Private;
176 		Private *d;
177 
178 		void cleanup();
179 		void do_resolve();
180 		void do_connect();
181 		void tryNextSrv();
182 	};
183 
184 	class IRIS_EXPORT TLSHandler : public QObject
185 	{
186 		Q_OBJECT
187 	public:
188 		TLSHandler(QObject *parent=0);
189 		~TLSHandler() override;
190 
191 		virtual void reset()=0;
192 		virtual void startClient(const QString &host)=0;
193 		virtual void write(const QByteArray &a)=0;
194 		virtual void writeIncoming(const QByteArray &a)=0;
195 
196 	signals:
197 		void success();
198 		void fail();
199 		void closed();
200 		void readyRead(const QByteArray &a);
201 		void readyReadOutgoing(const QByteArray &a, int plainBytes);
202 	};
203 
204 	class IRIS_EXPORT QCATLSHandler : public TLSHandler
205 	{
206 		Q_OBJECT
207 	public:
208 		QCATLSHandler(QCA::TLS *parent);
209 		~QCATLSHandler() override;
210 
211 		QCA::TLS *tls() const;
212 		int tlsError() const;
213 
214 		void setXMPPCertCheck(bool enable);
215 		bool XMPPCertCheck();
216 		bool certMatchesHostname();
217 
218 		void reset() override;
219 		void startClient(const QString &host) override;
220 		void write(const QByteArray &a) override;
221 		void writeIncoming(const QByteArray &a) override;
222 
223 	signals:
224 		void tlsHandshaken();
225 
226 	public slots:
227 		void continueAfterHandshake();
228 
229 	private slots:
230 		void tls_handshaken();
231 		void tls_readyRead();
232 		void tls_readyReadOutgoing();
233 		void tls_closed();
234 		void tls_error();
235 
236 	private:
237 		class Private;
238 		Private *d;
239 	};
240 }
241 
242 #endif
243