4b8cd33406a8ae66fd6dcec3e70c23bb57347bbd
[quassel.git] / src / common / ircchannel.cpp
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 #include "ircchannel.h"
22
23 #include "network.h"
24 #include "ircuser.h"
25 #include "util.h"
26
27 #include <QMapIterator>
28 #include <QHashIterator>
29 #include <QTextCodec>
30
31 #include <QDebug>
32
33 INIT_SYNCABLE_OBJECT(IrcChannel)
34 IrcChannel::IrcChannel(const QString &channelname, Network *network)
35     : SyncableObject(network),
36     _initialized(false),
37     _name(channelname),
38     _topic(QString()),
39     _encrypted(false),
40     _network(network),
41     _codecForEncoding(0),
42     _codecForDecoding(0)
43 {
44     setObjectName(QString::number(network->networkId().toInt()) + "/" +  channelname);
45 }
46
47
48 IrcChannel::~IrcChannel()
49 {
50 }
51
52
53 // ====================
54 //  PUBLIC:
55 // ====================
56 bool IrcChannel::isKnownUser(IrcUser *ircuser) const
57 {
58     if (ircuser == 0) {
59         qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
60         return false;
61     }
62
63     if (!_userModes.contains(ircuser)) {
64         qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
65         return false;
66     }
67
68     return true;
69 }
70
71
72 bool IrcChannel::isValidChannelUserMode(const QString &mode) const
73 {
74     bool isvalid = true;
75     if (mode.size() > 1) {
76         qWarning() << "Channel" << name() << "received Channel User Mode which is longer than 1 Char:" << mode;
77         isvalid = false;
78     }
79     return isvalid;
80 }
81
82
83 QString IrcChannel::userModes(IrcUser *ircuser) const
84 {
85     if (_userModes.contains(ircuser))
86         return _userModes[ircuser];
87     else
88         return QString();
89 }
90
91
92 QString IrcChannel::userModes(const QString &nick) const
93 {
94     return userModes(network()->ircUser(nick));
95 }
96
97
98 void IrcChannel::setCodecForEncoding(const QString &name)
99 {
100     setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
101 }
102
103
104 void IrcChannel::setCodecForEncoding(QTextCodec *codec)
105 {
106     _codecForEncoding = codec;
107 }
108
109
110 void IrcChannel::setCodecForDecoding(const QString &name)
111 {
112     setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
113 }
114
115
116 void IrcChannel::setCodecForDecoding(QTextCodec *codec)
117 {
118     _codecForDecoding = codec;
119 }
120
121
122 QString IrcChannel::decodeString(const QByteArray &text) const
123 {
124     if (!codecForDecoding()) return network()->decodeString(text);
125     return ::decodeString(text, _codecForDecoding);
126 }
127
128
129 QByteArray IrcChannel::encodeString(const QString &string) const
130 {
131     if (codecForEncoding()) {
132         return _codecForEncoding->fromUnicode(string);
133     }
134     return network()->encodeString(string);
135 }
136
137
138 // ====================
139 //  PUBLIC SLOTS:
140 // ====================
141 void IrcChannel::setTopic(const QString &topic)
142 {
143     _topic = topic;
144     SYNC(ARG(topic))
145     emit topicSet(topic);
146 }
147
148
149 void IrcChannel::setPassword(const QString &password)
150 {
151     _password = password;
152     SYNC(ARG(password))
153 }
154
155 void IrcChannel::setEncrypted(bool encrypted)
156 {
157     _encrypted = encrypted;
158     SYNC(ARG(encrypted))
159     emit encryptedSet(encrypted);
160 }
161
162
163 void IrcChannel::joinIrcUsers(const QList<IrcUser *> &users, const QStringList &modes)
164 {
165     if (users.isEmpty())
166         return;
167
168     if (users.count() != modes.count()) {
169         qWarning() << "IrcChannel::addUsers(): number of nicks does not match number of modes!";
170         return;
171     }
172
173     QStringList newNicks;
174     QStringList newModes;
175     QList<IrcUser *> newUsers;
176
177     IrcUser *ircuser;
178     for (int i = 0; i < users.count(); i++) {
179         ircuser = users[i];
180         if (!ircuser || _userModes.contains(ircuser)) {
181             if (modes[i].count() > 1) {
182                 // Multiple modes received, do it one at a time
183                 // TODO Better way of syncing this without breaking protocol?
184                 for (int i_m = 0; i_m < modes[i].count(); ++i_m) {
185                     addUserMode(ircuser, modes[i][i_m]);
186                 }
187             } else {
188                 addUserMode(ircuser, modes[i]);
189             }
190             continue;
191         }
192
193         _userModes[ircuser] = modes[i];
194         ircuser->joinChannel(this, true);
195         connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString)));
196
197         // connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
198         // If you wonder why there is no counterpart to ircUserJoined:
199         // the joins are propagated by the ircuser. The signal ircUserJoined is only for convenience
200
201         newNicks << ircuser->nick();
202         newModes << modes[i];
203         newUsers << ircuser;
204     }
205
206     if (newNicks.isEmpty())
207         return;
208
209     SYNC_OTHER(joinIrcUsers, ARG(newNicks), ARG(newModes));
210     emit ircUsersJoined(newUsers);
211 }
212
213
214 void IrcChannel::joinIrcUsers(const QStringList &nicks, const QStringList &modes)
215 {
216     QList<IrcUser *> users;
217     foreach(QString nick, nicks)
218     users << network()->newIrcUser(nick);
219     joinIrcUsers(users, modes);
220 }
221
222
223 void IrcChannel::joinIrcUser(IrcUser *ircuser)
224 {
225     QList<IrcUser *> users;
226     users << ircuser;
227     QStringList modes;
228     modes << QString();
229     joinIrcUsers(users, modes);
230 }
231
232
233 void IrcChannel::part(IrcUser *ircuser)
234 {
235     if (isKnownUser(ircuser)) {
236         _userModes.remove(ircuser);
237         ircuser->partChannel(this);
238         // If you wonder why there is no counterpart to ircUserParted:
239         // the joins are propagted by the ircuser. The signal ircUserParted is only for convenience
240         disconnect(ircuser, 0, this, 0);
241         emit ircUserParted(ircuser);
242
243         if (network()->isMe(ircuser) || _userModes.isEmpty()) {
244             // in either case we're no longer in the channel
245             //  -> clean up the channel and destroy it
246             QList<IrcUser *> users = _userModes.keys();
247             _userModes.clear();
248             foreach(IrcUser *user, users) {
249                 disconnect(user, 0, this, 0);
250                 user->partChannel(this);
251             }
252             emit parted();
253             network()->removeIrcChannel(this);
254         }
255     }
256 }
257
258
259 void IrcChannel::part(const QString &nick)
260 {
261     part(network()->ircUser(nick));
262 }
263
264
265 // SET USER MODE
266 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes)
267 {
268     if (isKnownUser(ircuser)) {
269         _userModes[ircuser] = modes;
270         QString nick = ircuser->nick();
271         SYNC_OTHER(setUserModes, ARG(nick), ARG(modes))
272         emit ircUserModesSet(ircuser, modes);
273     }
274 }
275
276
277 void IrcChannel::setUserModes(const QString &nick, const QString &modes)
278 {
279     setUserModes(network()->ircUser(nick), modes);
280 }
281
282
283 // ADD USER MODE
284 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode)
285 {
286     if (!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
287         return;
288
289     if (!_userModes[ircuser].contains(mode)) {
290         _userModes[ircuser] += mode;
291         QString nick = ircuser->nick();
292         SYNC_OTHER(addUserMode, ARG(nick), ARG(mode))
293         emit ircUserModeAdded(ircuser, mode);
294     }
295 }
296
297
298 void IrcChannel::addUserMode(const QString &nick, const QString &mode)
299 {
300     addUserMode(network()->ircUser(nick), mode);
301 }
302
303
304 // REMOVE USER MODE
305 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode)
306 {
307     if (!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
308         return;
309
310     if (_userModes[ircuser].contains(mode)) {
311         _userModes[ircuser].remove(mode);
312         QString nick = ircuser->nick();
313         SYNC_OTHER(removeUserMode, ARG(nick), ARG(mode));
314         emit ircUserModeRemoved(ircuser, mode);
315     }
316 }
317
318
319 void IrcChannel::removeUserMode(const QString &nick, const QString &mode)
320 {
321     removeUserMode(network()->ircUser(nick), mode);
322 }
323
324
325 // INIT SET USER MODES
326 QVariantMap IrcChannel::initUserModes() const
327 {
328     QVariantMap usermodes;
329     QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
330     while (iter != _userModes.constEnd()) {
331         usermodes[iter.key()->nick()] = iter.value();
332         ++iter;
333     }
334     return usermodes;
335 }
336
337
338 void IrcChannel::initSetUserModes(const QVariantMap &usermodes)
339 {
340     QList<IrcUser *> users;
341     QStringList modes;
342     QVariantMap::const_iterator iter = usermodes.constBegin();
343     while (iter != usermodes.constEnd()) {
344         users << network()->newIrcUser(iter.key());
345         modes << iter.value().toString();
346         ++iter;
347     }
348     joinIrcUsers(users, modes);
349 }
350
351
352 QVariantMap IrcChannel::initChanModes() const
353 {
354     QVariantMap channelModes;
355
356     QVariantMap A_modes;
357     QHash<QChar, QStringList>::const_iterator A_iter = _A_channelModes.constBegin();
358     while (A_iter != _A_channelModes.constEnd()) {
359         A_modes[A_iter.key()] = A_iter.value();
360         ++A_iter;
361     }
362     channelModes["A"] = A_modes;
363
364     QVariantMap B_modes;
365     QHash<QChar, QString>::const_iterator B_iter = _B_channelModes.constBegin();
366     while (B_iter != _B_channelModes.constEnd()) {
367         B_modes[B_iter.key()] = B_iter.value();
368         ++B_iter;
369     }
370     channelModes["B"] = B_modes;
371
372     QVariantMap C_modes;
373     QHash<QChar, QString>::const_iterator C_iter = _C_channelModes.constBegin();
374     while (C_iter != _C_channelModes.constEnd()) {
375         C_modes[C_iter.key()] = C_iter.value();
376         ++C_iter;
377     }
378     channelModes["C"] = C_modes;
379
380     QString D_modes;
381     QSet<QChar>::const_iterator D_iter = _D_channelModes.constBegin();
382     while (D_iter != _D_channelModes.constEnd()) {
383         D_modes += *D_iter;
384         ++D_iter;
385     }
386     channelModes["D"] = D_modes;
387
388     return channelModes;
389 }
390
391
392 void IrcChannel::initSetChanModes(const QVariantMap &channelModes)
393 {
394     QVariantMap::const_iterator iter = channelModes["A"].toMap().constBegin();
395     QVariantMap::const_iterator iterEnd = channelModes["A"].toMap().constEnd();
396     while (iter != iterEnd) {
397         _A_channelModes[iter.key()[0]] = iter.value().toStringList();
398         ++iter;
399     }
400
401     iter = channelModes["B"].toMap().constBegin();
402     iterEnd = channelModes["B"].toMap().constEnd();
403     while (iter != iterEnd) {
404         _B_channelModes[iter.key()[0]] = iter.value().toString();
405         ++iter;
406     }
407
408     iter = channelModes["C"].toMap().constBegin();
409     iterEnd = channelModes["C"].toMap().constEnd();
410     while (iter != iterEnd) {
411         _C_channelModes[iter.key()[0]] = iter.value().toString();
412         ++iter;
413     }
414
415     QString D_modes = channelModes["D"].toString();
416     for (int i = 0; i < D_modes.count(); i++) {
417         _D_channelModes << D_modes[i];
418     }
419 }
420
421
422 void IrcChannel::ircUserDestroyed()
423 {
424     IrcUser *ircUser = static_cast<IrcUser *>(sender());
425     Q_ASSERT(ircUser);
426     _userModes.remove(ircUser);
427     // no further propagation.
428     // this leads only to fuck ups.
429 }
430
431
432 void IrcChannel::ircUserNickSet(QString nick)
433 {
434     IrcUser *ircUser = qobject_cast<IrcUser *>(sender());
435     Q_ASSERT(ircUser);
436     emit ircUserNickSet(ircUser, nick);
437 }
438
439
440 /*******************************************************************************
441  *
442  * 3.3 CHANMODES
443  *
444  *    o  CHANMODES=A,B,C,D
445  *
446  *    The CHANMODES token specifies the modes that may be set on a channel.
447  *    These modes are split into four categories, as follows:
448  *
449  *    o  Type A: Modes that add or remove an address to or from a list.
450  *       These modes always take a parameter when sent by the server to a
451  *       client; when sent by a client, they may be specified without a
452  *       parameter, which requests the server to display the current
453  *       contents of the corresponding list on the channel to the client.
454  *    o  Type B: Modes that change a setting on the channel.  These modes
455  *       always take a parameter.
456  *    o  Type C: Modes that change a setting on the channel. These modes
457  *       take a parameter only when set; the parameter is absent when the
458  *       mode is removed both in the client's and server's MODE command.
459  *    o  Type D: Modes that change a setting on the channel. These modes
460  *       never take a parameter.
461  *
462  *    If the server sends any additional types after these 4, the client
463  *    MUST ignore them; this is intended to allow future extension of this
464  *    token.
465  *
466  *    The IRC server MUST NOT list modes in CHANMODES which are also
467  *    present in the PREFIX parameter; however, for completeness, modes
468  *    described in PREFIX may be treated as type B modes.
469  *
470  ******************************************************************************/
471
472 /*******************************************************************************
473  * Short Version:
474  * A --> add/remove from List
475  * B --> set value or remove
476  * C --> set value or remove
477  * D --> on/off
478  *
479  * B and C behave very similar... we store the data in different datastructures
480  * for future compatibility
481  ******************************************************************************/
482
483 // NOTE: the behavior of addChannelMode and removeChannelMode depends on the type of mode
484 // see list above for chanmode types
485 void IrcChannel::addChannelMode(const QChar &mode, const QString &value)
486 {
487     Network::ChannelModeType modeType = network()->channelModeType(mode);
488
489     switch (modeType) {
490     case Network::NOT_A_CHANMODE:
491         return;
492     case Network::A_CHANMODE:
493         if (!_A_channelModes.contains(mode))
494             _A_channelModes[mode] = QStringList(value);
495         else if (!_A_channelModes[mode].contains(value))
496             _A_channelModes[mode] << value;
497         break;
498
499     case Network::B_CHANMODE:
500         _B_channelModes[mode] = value;
501         break;
502
503     case Network::C_CHANMODE:
504         _C_channelModes[mode] = value;
505         break;
506
507     case Network::D_CHANMODE:
508         _D_channelModes << mode;
509         break;
510     }
511     SYNC(ARG(mode), ARG(value))
512 }
513
514
515 void IrcChannel::removeChannelMode(const QChar &mode, const QString &value)
516 {
517     Network::ChannelModeType modeType = network()->channelModeType(mode);
518
519     switch (modeType) {
520     case Network::NOT_A_CHANMODE:
521         return;
522     case Network::A_CHANMODE:
523         if (_A_channelModes.contains(mode))
524             _A_channelModes[mode].removeAll(value);
525         break;
526
527     case Network::B_CHANMODE:
528         _B_channelModes.remove(mode);
529         break;
530
531     case Network::C_CHANMODE:
532         _C_channelModes.remove(mode);
533         break;
534
535     case Network::D_CHANMODE:
536         _D_channelModes.remove(mode);
537         break;
538     }
539     SYNC(ARG(mode), ARG(value))
540 }
541
542
543 bool IrcChannel::hasMode(const QChar &mode) const
544 {
545     Network::ChannelModeType modeType = network()->channelModeType(mode);
546
547     switch (modeType) {
548     case Network::NOT_A_CHANMODE:
549         return false;
550     case Network::A_CHANMODE:
551         return _A_channelModes.contains(mode);
552     case Network::B_CHANMODE:
553         return _B_channelModes.contains(mode);
554     case Network::C_CHANMODE:
555         return _C_channelModes.contains(mode);
556     case Network::D_CHANMODE:
557         return _D_channelModes.contains(mode);
558     }
559     return false;
560 }
561
562
563 QString IrcChannel::modeValue(const QChar &mode) const
564 {
565     Network::ChannelModeType modeType = network()->channelModeType(mode);
566
567     switch (modeType) {
568     case Network::B_CHANMODE:
569         if (_B_channelModes.contains(mode))
570             return _B_channelModes[mode];
571         else
572             return QString();
573     case Network::C_CHANMODE:
574         if (_C_channelModes.contains(mode))
575             return _C_channelModes[mode];
576         else
577             return QString();
578     default:
579         return QString();
580     }
581 }
582
583
584 QStringList IrcChannel::modeValueList(const QChar &mode) const
585 {
586     Network::ChannelModeType modeType = network()->channelModeType(mode);
587
588     switch (modeType) {
589     case Network::A_CHANMODE:
590         if (_A_channelModes.contains(mode))
591             return _A_channelModes[mode];
592         break;
593     default:
594         ;
595     }
596     return {};
597 }
598
599
600 QString IrcChannel::channelModeString() const
601 {
602     QStringList params;
603     QString modeString;
604
605     QSet<QChar>::const_iterator D_iter = _D_channelModes.constBegin();
606     while (D_iter != _D_channelModes.constEnd()) {
607         modeString += *D_iter;
608         ++D_iter;
609     }
610
611     QHash<QChar, QString>::const_iterator BC_iter = _C_channelModes.constBegin();
612     while (BC_iter != _C_channelModes.constEnd()) {
613         modeString += BC_iter.key();
614         params << BC_iter.value();
615         ++BC_iter;
616     }
617
618     BC_iter = _B_channelModes.constBegin();
619     while (BC_iter != _B_channelModes.constEnd()) {
620         modeString += BC_iter.key();
621         params << BC_iter.value();
622         ++BC_iter;
623     }
624     if (modeString.isEmpty())
625         return modeString;
626     else
627         return QString("+%1 %2").arg(modeString).arg(params.join(" "));
628 }