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