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