Communi  3.7.0
A cross-platform IRC framework written with Qt
ircconnection.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008-2020 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(int connectionCount READ connectionCount NOTIFY connectionCountChanged)
69  Q_PROPERTY(QAbstractSocket* socket READ socket WRITE setSocket)
70  Q_PROPERTY(bool secure READ isSecure WRITE setSecure NOTIFY secureChanged)
71  Q_PROPERTY(bool secureSupported READ isSecureSupported)
72  Q_PROPERTY(QString saslMechanism READ saslMechanism WRITE setSaslMechanism NOTIFY saslMechanismChanged)
73  Q_PROPERTY(QStringList supportedSaslMechanisms READ supportedSaslMechanisms CONSTANT)
74  Q_PROPERTY(QVariantMap ctcpReplies READ ctcpReplies WRITE setCtcpReplies NOTIFY ctcpRepliesChanged)
75  Q_PROPERTY(IrcNetwork* network READ network CONSTANT)
76  Q_PROPERTY(IrcProtocol* protocol READ protocol WRITE setProtocol)
77  Q_ENUMS(Status)
78 
79 public:
80  explicit IrcConnection(QObject* parent = nullptr);
81  explicit IrcConnection(const QString& host, QObject* parent = nullptr);
82  ~IrcConnection() override;
83 
84  Q_INVOKABLE IrcConnection* clone(QObject *parent = nullptr) const;
85 
86  QString host() const;
87  void setHost(const QString& host);
88 
89  int port() const;
90  void setPort(int port);
91 
92  QStringList servers() const;
93  void setServers(const QStringList& servers);
94 
95  Q_INVOKABLE static bool isValidServer(const QString& server);
96 
97  QString userName() const;
98  void setUserName(const QString& name);
99 
100  QString nickName() const;
101  void setNickName(const QString& name);
102 
103  QString realName() const;
104  void setRealName(const QString& name);
105 
106  QString password() const;
107  void setPassword(const QString& password);
108 
109  QStringList nickNames() const;
110  void setNickNames(const QStringList& names);
111 
112  QString displayName() const;
113  void setDisplayName(const QString& name);
114 
115  QVariantMap userData() const;
116  void setUserData(const QVariantMap& data);
117 
118  QByteArray encoding() const;
119  void setEncoding(const QByteArray& encoding);
120 
121  enum Status {
128  Error
129  };
130  Status status() const;
131  bool isActive() const;
132  bool isConnected() const;
133  bool isEnabled() const;
134 
135  int reconnectDelay() const;
136  void setReconnectDelay(int seconds);
137 
138  int connectionCount() const;
139 
140  QAbstractSocket* socket() const;
141  void setSocket(QAbstractSocket* socket);
142 
143  bool isSecure() const;
144  void setSecure(bool secure);
145  static bool isSecureSupported();
146 
147  QString saslMechanism() const;
148  void setSaslMechanism(const QString& mechanism);
149 
150  static QStringList supportedSaslMechanisms();
151 
152  QVariantMap ctcpReplies() const;
153  void setCtcpReplies(const QVariantMap& replies);
154 
155  IrcNetwork* network() const;
156 
157  IrcProtocol* protocol() const;
158  void setProtocol(IrcProtocol* protocol);
159 
160  void installMessageFilter(QObject* filter);
161  void removeMessageFilter(QObject* filter);
162 
163  void installCommandFilter(QObject* filter);
164  void removeCommandFilter(QObject* filter);
165 
166  Q_INVOKABLE QByteArray saveState(int version = 0) const;
167  Q_INVOKABLE bool restoreState(const QByteArray& state, int version = 0);
168 
169 public Q_SLOTS:
170  void open();
171  void close();
172  void quit(const QString& reason = QString());
173  void setEnabled(bool enabled = true);
174  void setDisabled(bool disabled = true);
175 
176  bool sendCommand(IrcCommand* command);
177  bool sendData(const QByteArray& data);
178  bool sendRaw(const QString& message);
179 
180 Q_SIGNALS:
181  void connecting();
182  void connected();
183  void disconnected();
184  void statusChanged(IrcConnection::Status status);
185  void socketError(QAbstractSocket::SocketError error);
186  void socketStateChanged(QAbstractSocket::SocketState state);
187  void secureError();
188 
189  void nickNameReserved(QString* alternate); // deprecated
190  void nickNameRequired(const QString& reserved, QString* alternate);
191  void channelKeyRequired(const QString& channel, QString* key);
192 
193  void messageReceived(IrcMessage* message);
194 
195  void accountMessageReceived(IrcAccountMessage* message);
196  void awayMessageReceived(IrcAwayMessage* message);
197  void batchMessageReceived(IrcBatchMessage* message);
198  void capabilityMessageReceived(IrcCapabilityMessage* message);
199  void errorMessageReceived(IrcErrorMessage* message);
200  void hostChangeMessageReceived(IrcHostChangeMessage* message);
201  void inviteMessageReceived(IrcInviteMessage* message);
202  void joinMessageReceived(IrcJoinMessage* message);
203  void kickMessageReceived(IrcKickMessage* message);
204  void modeMessageReceived(IrcModeMessage* message);
205  void motdMessageReceived(IrcMotdMessage* message);
206  void namesMessageReceived(IrcNamesMessage* message);
207  void nickMessageReceived(IrcNickMessage* message);
208  void noticeMessageReceived(IrcNoticeMessage* message);
209  void numericMessageReceived(IrcNumericMessage* message);
210  void partMessageReceived(IrcPartMessage* message);
211  void pingMessageReceived(IrcPingMessage* message);
212  void pongMessageReceived(IrcPongMessage* message);
213  void privateMessageReceived(IrcPrivateMessage* message);
214  void quitMessageReceived(IrcQuitMessage* message);
215  void topicMessageReceived(IrcTopicMessage* message);
216  void whoisMessageReceived(IrcWhoisMessage* message);
217  void whowasMessageReceived(IrcWhowasMessage* message);
218  void whoReplyMessageReceived(IrcWhoReplyMessage* message);
219 
220  void hostChanged(const QString& host);
221  void portChanged(int port);
222  void serversChanged(const QStringList& servers);
223  void userNameChanged(const QString& name);
224  void nickNameChanged(const QString& name);
225  void realNameChanged(const QString& name);
226  void passwordChanged(const QString& password);
227  void nickNamesChanged(const QStringList& names);
228  void displayNameChanged(const QString& name);
229  void userDataChanged(const QVariantMap& data);
230  void reconnectDelayChanged(int seconds);
231  void connectionCountChanged(int count);
232  void enabledChanged(bool enabled);
233  void secureChanged(bool secure);
234  void saslMechanismChanged(const QString& mechanism);
235  void ctcpRepliesChanged(const QVariantMap& replies);
236 
237  void destroyed(IrcConnection* connection);
238 
239 protected Q_SLOTS:
240  virtual IrcCommand* createCtcpReply(IrcPrivateMessage* request) const;
241 
242 private:
243  friend class IrcProtocol;
244  friend class IrcProtocolPrivate;
245  QScopedPointer<IrcConnectionPrivate> d_ptr;
246  Q_DECLARE_PRIVATE(IrcConnection)
247  Q_DISABLE_COPY(IrcConnection)
248 
249  Q_PRIVATE_SLOT(d_func(), void _irc_connected())
250  Q_PRIVATE_SLOT(d_func(), void _irc_disconnected())
251  Q_PRIVATE_SLOT(d_func(), void _irc_error(QAbstractSocket::SocketError))
252  Q_PRIVATE_SLOT(d_func(), void _irc_state(QAbstractSocket::SocketState))
253  Q_PRIVATE_SLOT(d_func(), void _irc_sslErrors())
254  Q_PRIVATE_SLOT(d_func(), void _irc_reconnect())
255  Q_PRIVATE_SLOT(d_func(), void _irc_readData())
256  Q_PRIVATE_SLOT(d_func(), void _irc_filterDestroyed(QObject*))
257 };
258 
259 #ifndef QT_NO_DEBUG_STREAM
260 IRC_CORE_EXPORT QDebug operator<<(QDebug debug, IrcConnection::Status status);
261 IRC_CORE_EXPORT QDebug operator<<(QDebug debug, const IrcConnection* connection);
262 #endif // QT_NO_DEBUG_STREAM
263 
264 IRC_END_NAMESPACE
265 
266 Q_DECLARE_METATYPE(IRC_PREPEND_NAMESPACE(IrcConnection*))
267 Q_DECLARE_METATYPE(IRC_PREPEND_NAMESPACE(IrcConnection::Status))
268 
269 #endif // IRCCONNECTION_H
Represents an account notify message.
Definition: ircmessage.h:170
Represents an away message.
Definition: ircmessage.h:186
Represents a batch message.
Definition: ircmessage.h:206
Represents a capability message.
Definition: ircmessage.h:227
Provides the most common commands.
Definition: irccommand.h:45
Provides means to establish a connection to an IRC server.
Definition: ircconnection.h:49
Status
Definition: ircconnection.h:121
@ Connecting
The connection is being established.
Definition: ircconnection.h:124
@ Inactive
The connection is inactive.
Definition: ircconnection.h:122
@ Closed
The connection has been closed.
Definition: ircconnection.h:127
@ Connected
The connection has been established.
Definition: ircconnection.h:125
@ Closing
The connection is being closed.
Definition: ircconnection.h:126
@ Waiting
The connection is waiting for a reconnect.
Definition: ircconnection.h:123
void secureError()
void channelKeyRequired(const QString &channel, QString *key)
void messageReceived(IrcMessage *message)
void nickNameRequired(const QString &reserved, QString *alternate)
void socketStateChanged(QAbstractSocket::SocketState state)
void disconnected()
void socketError(QAbstractSocket::SocketError error)
void connecting()
Represents an error message.
Definition: ircmessage.h:245
Represents a host change message.
Definition: ircmessage.h:261
Represents an invite message.
Definition: ircmessage.h:279
Represents a join message.
Definition: ircmessage.h:299
Represents a kick message.
Definition: ircmessage.h:319
The base class of all messages.
Definition: ircmessage.h:48
Represents a mode message.
Definition: ircmessage.h:339
Represents a message of the day.
Definition: ircmessage.h:368
Represents a names list message.
Definition: ircmessage.h:384
Provides network information and capability management.
Definition: ircnetwork.h:44
Represents a nick message.
Definition: ircmessage.h:402
Represents a notice message.
Definition: ircmessage.h:420
Represents a numeric message.
Definition: ircmessage.h:444
Represents a part message.
Definition: ircmessage.h:462
Represents a ping message.
Definition: ircmessage.h:480
Represents a pong message.
Definition: ircmessage.h:496
Represents a private message.
Definition: ircmessage.h:512
Implements the IRC protocol and provides means for implementing support for custom protocols.
Definition: ircprotocol.h:46
Represents a quit message.
Definition: ircmessage.h:538
Represents a topic message.
Definition: ircmessage.h:554
Represents a reply message to a WHO command.
Definition: ircmessage.h:630
Represents a reply message to a WHOIS command.
Definition: ircmessage.h:574
Represents a reply message to a WHOWAS command.
Definition: ircmessage.h:608