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  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #ifndef XMPP_H
22 #define XMPP_H
23 
24 #include <QPair>
25 #include <QUrl>
26 #include <QObject>
27 #include <QString>
28 #include <QHostAddress>
29 #include <QString>
30 #include <QDomDocument>
31 
32 #include "addressresolver.h"
33 #include "xmpp/jid/jid.h"
34 #include "xmpp_stanza.h"
35 #include "xmpp_stream.h"
36 #include "xmpp_clientstream.h"
37 
38 namespace QCA
39 {
40 	class TLS;
41 };
42 
43 #ifndef CS_XMPP
44 class ByteStream;
45 #endif
46 
47 #include <QtCrypto> // For QCA::SASL::Params
48 
49 namespace XMPP
50 {
51 	// CS_IMPORT_BEGIN cutestuff/bytestream.h
52 #ifdef CS_XMPP
53 	class ByteStream;
54 #endif
55 	// CS_IMPORT_END
56 
57 	class Debug
58 	{
59 	public:
60 		virtual ~Debug();
61 
62 		virtual void msg(const QString &)=0;
63 		virtual void outgoingTag(const QString &)=0;
64 		virtual void incomingTag(const QString &)=0;
65 		virtual void outgoingXml(const QDomElement &)=0;
66 		virtual void incomingXml(const QDomElement &)=0;
67 	};
68 
69 	void setDebug(Debug *);
70 
71 	class Connector : public QObject
72 	{
73 		Q_OBJECT
74 	public:
75 		Connector(QObject *parent=0);
76 		virtual ~Connector();
77 
78 		virtual void setOptHostPort(const QString &host, quint16 port)=0;
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; // a flag to start ssl handshake immediately
101 		bool haveaddr;
102 		QHostAddress addr;
103 		quint16 port;
104 	};
105 
106 	class AdvancedConnector : public Connector
107 	{
108 		Q_OBJECT
109 	public:
110 		enum Error { ErrConnectionRefused, ErrHostNotFound, ErrProxyConnect, ErrProxyNeg, ErrProxyAuth, ErrStream };
111 		AdvancedConnector(QObject *parent=0);
112 		virtual ~AdvancedConnector();
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 			QUrl 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 QUrl &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 			QUrl v_url;
138 			QString v_host;
139 			quint16 v_port;
140 			QString v_user, v_pass;
141 			int v_poll;
142 		};
143 
144 		void setProxy(const Proxy &proxy);
145 		void setOptProbe(bool);
146 		void setOptSSL(bool);
147 
148 		void changePollInterval(int secs);
149 
150 		void setOptHostPort(const QString &host, quint16 port);
151 		void connectToServer(const QString &server);
152 		ByteStream *stream() const;
153 		void done();
154 
155 		int errorCode() const;
156 
157 		virtual QString host() const;
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 bs_connected();
167 		void bs_error(int);
168 		void http_syncStarted();
169 		void http_syncFinished();
170 		void t_timeout();
171 
172 	private:
173 		class Private;
174 		Private *d;
175 
176 		void cleanup();
177 	};
178 
179 	class TLSHandler : public QObject
180 	{
181 		Q_OBJECT
182 	public:
183 		TLSHandler(QObject *parent=0);
184 		virtual ~TLSHandler();
185 
186 		virtual void reset()=0;
187 		virtual void startClient(const QString &host)=0;
188 		virtual void write(const QByteArray &a)=0;
189 		virtual void writeIncoming(const QByteArray &a)=0;
190 
191 	signals:
192 		void success();
193 		void fail();
194 		void closed();
195 		void readyRead(const QByteArray &a);
196 		void readyReadOutgoing(const QByteArray &a, int plainBytes);
197 	};
198 
199 	class QCATLSHandler : public TLSHandler
200 	{
201 		Q_OBJECT
202 	public:
203 		QCATLSHandler(QCA::TLS *parent);
204 		~QCATLSHandler();
205 
206 		QCA::TLS *tls() const;
207 		int tlsError() const;
208 
209 		void setXMPPCertCheck(bool enable);
210 		bool XMPPCertCheck();
211 		bool certMatchesHostname();
212 
213 		void reset();
214 		void startClient(const QString &host);
215 		void write(const QByteArray &a);
216 		void writeIncoming(const QByteArray &a);
217 
218 	signals:
219 		void tlsHandshaken();
220 
221 	public slots:
222 		void continueAfterHandshake();
223 
224 	private slots:
225 		void tls_handshaken();
226 		void tls_readyRead();
227 		void tls_readyReadOutgoing();
228 		void tls_closed();
229 		void tls_error();
230 
231 	private:
232 		class Private;
233 		Private *d;
234 	};
235 };
236 
237 #endif
238