Communi  3.3.0
A cross-platform IRC framework written with Qt
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Macros Groups Pages
ircconnection.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008-2014 The Communi Project
3 
4  You may use this file under the terms of BSD license as follows:
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the copyright holder nor the names of its
14  contributors may be used to endorse or promote products derived
15  from this software without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
21  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef IRCCONNECTION_H
30 #define IRCCONNECTION_H
31 
32 #include <IrcGlobal>
33 #include <IrcMessage>
34 #include <IrcNetwork>
35 #include <QtCore/qobject.h>
36 #include <QtCore/qvariant.h>
37 #include <QtCore/qmetatype.h>
38 #include <QtCore/qstringlist.h>
39 #include <QtCore/qscopedpointer.h>
40 #include <QtNetwork/qabstractsocket.h>
41 
42 IRC_BEGIN_NAMESPACE
43 
44 class IrcCommand;
45 class IrcProtocol;
46 class IrcConnectionPrivate;
47 
48 class IRC_CORE_EXPORT IrcConnection : public QObject
49 {
50  Q_OBJECT
51 
52  Q_PROPERTY(QString host READ host WRITE setHost NOTIFY hostChanged)
53  Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
54  Q_PROPERTY(QStringList servers READ servers WRITE setServers NOTIFY serversChanged)
55  Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
56  Q_PROPERTY(QString nickName READ nickName WRITE setNickName NOTIFY nickNameChanged)
57  Q_PROPERTY(QString realName READ realName WRITE setRealName NOTIFY realNameChanged)
58  Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
59  Q_PROPERTY(QStringList nickNames READ nickNames WRITE setNickNames NOTIFY nickNamesChanged)
60  Q_PROPERTY(QString displayName READ displayName WRITE setDisplayName NOTIFY displayNameChanged)
61  Q_PROPERTY(QVariantMap userData READ userData WRITE setUserData NOTIFY userDataChanged)
62  Q_PROPERTY(QByteArray encoding READ encoding WRITE setEncoding)
63  Q_PROPERTY(Status status READ status NOTIFY statusChanged)
64  Q_PROPERTY(bool active READ isActive NOTIFY statusChanged)
65  Q_PROPERTY(bool connected READ isConnected NOTIFY statusChanged)
66  Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
67  Q_PROPERTY(int reconnectDelay READ reconnectDelay WRITE setReconnectDelay NOTIFY reconnectDelayChanged)
68  Q_PROPERTY(QAbstractSocket* socket READ socket WRITE setSocket)
69  Q_PROPERTY(bool secure READ isSecure WRITE setSecure NOTIFY secureChanged)
70  Q_PROPERTY(bool secureSupported READ isSecureSupported)
71  Q_PROPERTY(QString saslMechanism READ saslMechanism WRITE setSaslMechanism NOTIFY saslMechanismChanged)
72  Q_PROPERTY(QStringList supportedSaslMechanisms READ supportedSaslMechanisms CONSTANT)
73  Q_PROPERTY(IrcNetwork* network READ network CONSTANT)
74  Q_PROPERTY(IrcProtocol* protocol READ protocol WRITE setProtocol)
75  Q_ENUMS(Status)
76 
77 public:
78  explicit IrcConnection(QObject* parent = 0);
79  explicit IrcConnection(const QString& host, QObject* parent = 0);
80  virtual ~IrcConnection();
81 
82  QString host() const;
83  void setHost(const QString& host);
84 
85  int port() const;
86  void setPort(int port);
87 
88  QStringList servers() const;
89  void setServers(const QStringList& servers);
90 
91  Q_INVOKABLE static bool isValidServer(const QString& server);
92 
93  QString userName() const;
94  void setUserName(const QString& name);
95 
96  QString nickName() const;
97  void setNickName(const QString& name);
98 
99  QString realName() const;
100  void setRealName(const QString& name);
101 
102  QString password() const;
103  void setPassword(const QString& password);
104 
105  QStringList nickNames() const;
106  void setNickNames(const QStringList& names);
107 
108  QString displayName() const;
109  void setDisplayName(const QString& name);
110 
111  QVariantMap userData() const;
112  void setUserData(const QVariantMap& data);
113 
114  QByteArray encoding() const;
115  void setEncoding(const QByteArray& encoding);
116 
117  enum Status {
124  Error
125  };
126  Status status() const;
127  bool isActive() const;
128  bool isConnected() const;
129  bool isEnabled() const;
130 
131  int reconnectDelay() const;
132  void setReconnectDelay(int seconds);
133 
134  QAbstractSocket* socket() const;
135  void setSocket(QAbstractSocket* socket);
136 
137  bool isSecure() const;
138  void setSecure(bool secure);
139  static bool isSecureSupported();
140 
141  QString saslMechanism() const;
142  void setSaslMechanism(const QString& mechanism);
143 
144  static QStringList supportedSaslMechanisms();
145 
146  IrcNetwork* network() const;
147 
148  IrcProtocol* protocol() const;
149  void setProtocol(IrcProtocol* protocol);
150 
151  void installMessageFilter(QObject* filter);
152  void removeMessageFilter(QObject* filter);
153 
154  void installCommandFilter(QObject* filter);
155  void removeCommandFilter(QObject* filter);
156 
157  Q_INVOKABLE QByteArray saveState(int version = 0) const;
158  Q_INVOKABLE bool restoreState(const QByteArray& state, int version = 0);
159 
160 public Q_SLOTS:
161  void open();
162  void close();
163  void quit(const QString& reason = QString());
164  void setEnabled(bool enabled = true);
165  void setDisabled(bool disabled = true);
166 
167  bool sendCommand(IrcCommand* command);
168  bool sendData(const QByteArray& data);
169  bool sendRaw(const QString& message);
170 
171 Q_SIGNALS:
172  void connecting();
173  void connected();
174  void disconnected();
175  void statusChanged(IrcConnection::Status status);
176  void socketError(QAbstractSocket::SocketError error);
177  void socketStateChanged(QAbstractSocket::SocketState state);
178  void secureError();
179 
180  void nickNameReserved(QString* alternate); // deprecated
181  void nickNameRequired(const QString& reserved, QString* alternate);
182  void channelKeyRequired(const QString& channel, QString* key);
183 
184  void messageReceived(IrcMessage* message);
185 
186  void accountMessageReceived(IrcAccountMessage* message);
187  void awayMessageReceived(IrcAwayMessage* message);
188  void capabilityMessageReceived(IrcCapabilityMessage* message);
189  void errorMessageReceived(IrcErrorMessage* message);
190  void inviteMessageReceived(IrcInviteMessage* message);
191  void joinMessageReceived(IrcJoinMessage* message);
192  void kickMessageReceived(IrcKickMessage* message);
193  void modeMessageReceived(IrcModeMessage* message);
194  void motdMessageReceived(IrcMotdMessage* message);
195  void namesMessageReceived(IrcNamesMessage* message);
196  void nickMessageReceived(IrcNickMessage* message);
197  void noticeMessageReceived(IrcNoticeMessage* message);
198  void numericMessageReceived(IrcNumericMessage* message);
199  void partMessageReceived(IrcPartMessage* message);
200  void pingMessageReceived(IrcPingMessage* message);
201  void pongMessageReceived(IrcPongMessage* message);
202  void privateMessageReceived(IrcPrivateMessage* message);
203  void quitMessageReceived(IrcQuitMessage* message);
204  void topicMessageReceived(IrcTopicMessage* message);
205  void whoisMessageReceived(IrcWhoisMessage* message);
206  void whowasMessageReceived(IrcWhowasMessage* message);
207  void whoReplyMessageReceived(IrcWhoReplyMessage* message);
208 
209  void hostChanged(const QString& host);
210  void portChanged(int port);
211  void serversChanged(const QStringList& servers);
212  void userNameChanged(const QString& name);
213  void nickNameChanged(const QString& name);
214  void realNameChanged(const QString& name);
215  void passwordChanged(const QString& password);
216  void nickNamesChanged(const QStringList& names);
217  void displayNameChanged(const QString& name);
218  void userDataChanged(const QVariantMap& data);
219 
220  void reconnectDelayChanged(int seconds);
221  void enabledChanged(bool enabled);
222  void secureChanged(bool secure);
223  void saslMechanismChanged(const QString& mechanism);
224 
225  void destroyed(IrcConnection* connection);
226 
227 protected Q_SLOTS:
228  virtual IrcCommand* createCtcpReply(IrcPrivateMessage* request) const;
229 
230 private:
231  friend class IrcProtocol;
232  friend class IrcProtocolPrivate;
234  Q_DECLARE_PRIVATE(IrcConnection)
235  Q_DISABLE_COPY(IrcConnection)
236 
237  Q_PRIVATE_SLOT(d_func(), void _irc_connected())
238  Q_PRIVATE_SLOT(d_func(), void _irc_disconnected())
239  Q_PRIVATE_SLOT(d_func(), void _irc_error(QAbstractSocket::SocketError))
240  Q_PRIVATE_SLOT(d_func(), void _irc_state(QAbstractSocket::SocketState))
241  Q_PRIVATE_SLOT(d_func(), void _irc_sslErrors())
242  Q_PRIVATE_SLOT(d_func(), void _irc_reconnect())
243  Q_PRIVATE_SLOT(d_func(), void _irc_readData())
244  Q_PRIVATE_SLOT(d_func(), void _irc_filterDestroyed(QObject*))
245 };
246 
247 #ifndef QT_NO_DEBUG_STREAM
248 IRC_CORE_EXPORT QDebug operator<<(QDebug debug, IrcConnection::Status status);
249 IRC_CORE_EXPORT QDebug operator<<(QDebug debug, const IrcConnection* connection);
250 #endif // QT_NO_DEBUG_STREAM
251 
252 IRC_END_NAMESPACE
253 
254 Q_DECLARE_METATYPE(IRC_PREPEND_NAMESPACE(IrcConnection*))
255 Q_DECLARE_METATYPE(IRC_PREPEND_NAMESPACE(IrcConnection::Status))
256 
257 #endif // IRCCONNECTION_H
Represents an invite message.
Definition: ircmessage.h:222
Represents a private message.
Definition: ircmessage.h:453
Represents a names list message.
Definition: ircmessage.h:327
Represents a reply message to a WHOIS command.
Definition: ircmessage.h:513
Provides means to establish a connection to an IRC server.
Definition: ircconnection.h:48
Provides network information and capability management.
Definition: ircnetwork.h:43
The connection is being established.
Definition: ircconnection.h:120
Represents a reply message to a WHOWAS command.
Definition: ircmessage.h:546
Represents a numeric message.
Definition: ircmessage.h:385
Represents an error message.
Definition: ircmessage.h:206
Represents a quit message.
Definition: ircmessage.h:477
Represents a join message.
Definition: ircmessage.h:242
The connection is being closed.
Definition: ircconnection.h:122
Represents a pong message.
Definition: ircmessage.h:437
Q_INVOKABLE QString version()
Definition: irc.cpp:56
Represents a nick message.
Definition: ircmessage.h:345
Represents a ping message.
Definition: ircmessage.h:421
The connection is inactive.
Definition: ircconnection.h:118
The connection has been established.
Definition: ircconnection.h:121
Provides the most common commands.
Definition: irccommand.h:43
Status
Definition: ircconnection.h:117
The connection has been closed.
Definition: ircconnection.h:123
Represents a kick message.
Definition: ircmessage.h:262
Implements the IRC protocol and provides means for implementing support for custom protocols...
Definition: ircprotocol.h:45
Represents a reply message to a WHO command.
Definition: ircmessage.h:568
Represents a capability message.
Definition: ircmessage.h:188
Represents an account notify message.
Definition: ircmessage.h:152
Represents a part message.
Definition: ircmessage.h:403
Represents a topic message.
Definition: ircmessage.h:493
The base class of all messages.
Definition: ircmessage.h:47
Represents an away message.
Definition: ircmessage.h:168
Represents a mode message.
Definition: ircmessage.h:282
Represents a message of the day.
Definition: ircmessage.h:311
The connection is waiting for a reconnect.
Definition: ircconnection.h:119
void destroyed(QObject *obj)
Represents a notice message.
Definition: ircmessage.h:363