modernize: Use '= default' instead of empty ctor/dtor bodies
[quassel.git] / src / common / ircuser.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program 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         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include "common-export.h"
24
25 #include <QSet>
26 #include <QString>
27 #include <QStringList>
28 #include <QVariantMap>
29 #include <QDateTime>
30
31 #include "syncableobject.h"
32 #include "types.h"
33
34 class SignalProxy;
35 class Network;
36 class IrcChannel;
37
38 class COMMON_EXPORT IrcUser : public SyncableObject
39 {
40     Q_OBJECT
41     SYNCABLE_OBJECT
42
43     Q_PROPERTY(QString user READ user WRITE setUser)
44     Q_PROPERTY(QString host READ host WRITE setHost)
45     Q_PROPERTY(QString nick READ nick WRITE setNick)
46     Q_PROPERTY(QString realName READ realName WRITE setRealName)
47     Q_PROPERTY(QString account READ account WRITE setAccount)
48     Q_PROPERTY(bool away READ isAway WRITE setAway)
49     Q_PROPERTY(QString awayMessage READ awayMessage WRITE setAwayMessage)
50     Q_PROPERTY(QDateTime idleTime READ idleTime WRITE setIdleTime)
51     Q_PROPERTY(QDateTime loginTime READ loginTime WRITE setLoginTime)
52     Q_PROPERTY(QString server READ server WRITE setServer)
53     Q_PROPERTY(QString ircOperator READ ircOperator WRITE setIrcOperator)
54     Q_PROPERTY(QDateTime lastAwayMessageTime READ lastAwayMessageTime WRITE setLastAwayMessageTime)
55     Q_PROPERTY(QString whoisServiceReply READ whoisServiceReply WRITE setWhoisServiceReply)
56     Q_PROPERTY(QString suserHost READ suserHost WRITE setSuserHost)
57     Q_PROPERTY(bool encrypted READ encrypted WRITE setEncrypted)
58
59     Q_PROPERTY(QStringList channels READ channels)
60     Q_PROPERTY(QString userModes READ userModes WRITE setUserModes)
61
62 public :
63     IrcUser(const QString &hostmask, Network *network);
64
65     inline QString user() const { return _user; }
66     inline QString host() const { return _host; }
67     inline QString nick() const { return _nick; }
68     inline QString realName() const { return _realName; }
69     /**
70      * Account name, e.g. NickServ/SASL account
71      *
72      * @return Account name if logged in, * if logged out, or empty string if unknown
73      */
74     inline QString account() const { return _account; }
75     QString hostmask() const;
76     inline bool isAway() const { return _away; }
77     inline QString awayMessage() const { return _awayMessage; }
78     QDateTime idleTime();
79     inline QDateTime loginTime() const { return _loginTime; }
80     inline QString server() const { return _server; }
81     inline QString ircOperator() const { return _ircOperator; }
82     inline QDateTime lastAwayMessageTime() const { return _lastAwayMessageTime; }
83     inline QString whoisServiceReply() const { return _whoisServiceReply; }
84     inline QString suserHost() const { return _suserHost; }
85     inline bool encrypted() const { return _encrypted; }
86     inline Network *network() const { return _network; }
87
88     inline QString userModes() const { return _userModes; }
89
90     QStringList channels() const;
91
92     // user-specific encodings
93     inline QTextCodec *codecForEncoding() const { return _codecForEncoding; }
94     inline QTextCodec *codecForDecoding() const { return _codecForDecoding; }
95     void setCodecForEncoding(const QString &codecName);
96     void setCodecForEncoding(QTextCodec *codec);
97     void setCodecForDecoding(const QString &codecName);
98     void setCodecForDecoding(QTextCodec *codec);
99
100     QString decodeString(const QByteArray &text) const;
101     QByteArray encodeString(const QString &string) const;
102
103     // only valid on client side, these are not synced!
104     inline QDateTime lastChannelActivity(BufferId id) const { return _lastActivity.value(id); }
105     void setLastChannelActivity(BufferId id, const QDateTime &time);
106     inline QDateTime lastSpokenTo(BufferId id) const { return _lastSpokenTo.value(id); }
107     void setLastSpokenTo(BufferId id, const QDateTime &time);
108
109     /**
110      * Gets whether or not the away state has changed since it was last acknowledged
111      *
112      * Away state is marked as changed by any modification to away status (away/here, message)
113      *
114      * NOTE: On servers lacking support for IRCv3 away-notify, this won't update until an autoWHO-
115      * run for away/here changes, or until sending a message to the user for away message changes.
116      *
117      * @see IrcUser::acknowledgeAwayChanged()
118      *
119      * @return True if current away state is unchanged from last acknowledgement, otherwise false
120      */
121     inline bool hasAwayChanged() const { return _awayChanged; }
122
123     /**
124      * Sets the last away state change as acknowledged
125      *
126      * @see IrcUser::hasAwayChanged()
127      */
128     inline void acknowledgeAwayChanged()
129     {
130         // Don't sync this as individual clients may suppress different kinds of behaviors
131         _awayChanged = false;
132     }
133
134 public slots:
135     void setUser(const QString &user);
136     void setHost(const QString &host);
137     void setNick(const QString &nick);
138     void setRealName(const QString &realName);
139     /**
140      * Set account name, e.g. NickServ/SASL account
141      *
142      * @param[in] account Account name if logged in, * if logged out, or empty string if unknown
143      */
144     void setAccount(const QString &account);
145     void setAway(bool away);
146     void setAwayMessage(const QString &awayMessage);
147     void setIdleTime(const QDateTime &idleTime);
148     void setLoginTime(const QDateTime &loginTime);
149     void setServer(const QString &server);
150     void setIrcOperator(const QString &ircOperator);
151     // setLastAwayMessage is only called by legacy (pre-0.13) cores, which automatically gets
152     // converted to setting the appropriate lastAwayMessageTime.  Do not use this in new code.
153     void setLastAwayMessage(int lastAwayMessage);
154     void setLastAwayMessageTime(const QDateTime &lastAwayMessageTime);
155     void setWhoisServiceReply(const QString &whoisServiceReply);
156     void setSuserHost(const QString &suserHost);
157     void setEncrypted(bool encrypted);
158     void updateHostmask(const QString &mask);
159
160     void setUserModes(const QString &modes);
161
162     /*!
163      * \brief joinChannel Called when user joins some channel, this function inserts the channel to internal list of channels this user is in.
164      * \param channel Pointer to a channel this user just joined
165      * \param skip_channel_join If this is false, this function will also call IrcChannel::joinIrcUser, can be set to true as a performance tweak.
166      */
167     void joinChannel(IrcChannel *channel, bool skip_channel_join = false);
168     void joinChannel(const QString &channelname);
169     void partChannel(IrcChannel *channel);
170     void partChannel(const QString &channelname);
171     void quit();
172
173     void addUserModes(const QString &modes);
174     void removeUserModes(const QString &modes);
175
176 signals:
177 //   void userSet(QString user);
178 //   void hostSet(QString host);
179     void nickSet(QString newnick); // needed in NetworkModel
180 //   void realNameSet(QString realName);
181     void awaySet(bool away); // needed in NetworkModel
182 //   void awayMessageSet(QString awayMessage);
183 //   void idleTimeSet(QDateTime idleTime);
184 //   void loginTimeSet(QDateTime loginTime);
185 //   void serverSet(QString server);
186 //   void ircOperatorSet(QString ircOperator);
187 //   void lastAwayMessageTimeSet(QDateTime lastAwayMessageTime);
188 //   void whoisServiceReplySet(QString whoisServiceReply);
189 //   void suserHostSet(QString suserHost);
190     void encryptedSet(bool encrypted);
191
192     void userModesSet(QString modes);
193     void userModesAdded(QString modes);
194     void userModesRemoved(QString modes);
195
196     // void channelJoined(QString channel);
197     void channelParted(QString channel);
198     void quited();
199
200     void lastChannelActivityUpdated(BufferId id, const QDateTime &newTime);
201     void lastSpokenToUpdated(BufferId id, const QDateTime &newTime);
202
203 private slots:
204     void updateObjectName();
205     void channelDestroyed();
206
207 private:
208     inline bool operator==(const IrcUser &ircuser2)
209     {
210         return (_nick.toLower() == ircuser2.nick().toLower());
211     }
212
213
214     inline bool operator==(const QString &nickname)
215     {
216         return (_nick.toLower() == nickname.toLower());
217     }
218
219     /**
220      * Sets the last away state change as unacknowledged
221      *
222      * @see IrcUser::hasAwayChanged()
223      */
224     inline void markAwayChanged()
225     {
226         _awayChanged = true;
227     }
228
229     bool _initialized;
230
231     QString _nick;
232     QString _user;
233     QString _host;
234     QString _realName;
235     QString _account;      /// Account name, e.g. NickServ/SASL account
236     QString _awayMessage;
237     bool _away;
238     QString _server;
239     QDateTime _idleTime;
240     QDateTime _idleTimeSet;
241     QDateTime _loginTime;
242     QString _ircOperator;
243     QDateTime _lastAwayMessageTime;
244     QString _whoisServiceReply;
245     QString _suserHost;
246     bool _encrypted;
247
248     // QSet<QString> _channels;
249     QSet<IrcChannel *> _channels;
250     QString _userModes;
251
252     Network *_network;
253
254     QTextCodec *_codecForEncoding;
255     QTextCodec *_codecForDecoding;
256
257     QHash<BufferId, QDateTime> _lastActivity;
258     QHash<BufferId, QDateTime> _lastSpokenTo;
259
260     // Given it's never been acknowledged, assume changes exist on IrcUser creation
261     /// Tracks if changes in away state (away/here, message) have yet to be acknowledged
262     bool _awayChanged = true;
263 };