1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "ircchannel.h"
27 #include <QMapIterator>
28 #include <QHashIterator>
33 INIT_SYNCABLE_OBJECT(IrcChannel)
34 IrcChannel::IrcChannel(const QString &channelname, Network *network)
35 : SyncableObject(network),
44 setObjectName(QString::number(network->networkId().toInt()) + "/" + channelname);
48 IrcChannel::~IrcChannel()
53 // ====================
55 // ====================
56 bool IrcChannel::isKnownUser(IrcUser *ircuser) const
59 qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
63 if (!_userModes.contains(ircuser)) {
64 // This can happen e.g. when disconnecting from a network, so don't log a warning
72 bool IrcChannel::isValidChannelUserMode(const QString &mode) const
75 if (mode.size() > 1) {
76 qWarning() << "Channel" << name() << "received Channel User Mode which is longer than 1 Char:" << mode;
83 QString IrcChannel::userModes(IrcUser *ircuser) const
85 if (_userModes.contains(ircuser))
86 return _userModes[ircuser];
92 QString IrcChannel::userModes(const QString &nick) const
94 return userModes(network()->ircUser(nick));
98 void IrcChannel::setCodecForEncoding(const QString &name)
100 setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
104 void IrcChannel::setCodecForEncoding(QTextCodec *codec)
106 _codecForEncoding = codec;
110 void IrcChannel::setCodecForDecoding(const QString &name)
112 setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
116 void IrcChannel::setCodecForDecoding(QTextCodec *codec)
118 _codecForDecoding = codec;
122 QString IrcChannel::decodeString(const QByteArray &text) const
124 if (!codecForDecoding()) return network()->decodeString(text);
125 return ::decodeString(text, _codecForDecoding);
129 QByteArray IrcChannel::encodeString(const QString &string) const
131 if (codecForEncoding()) {
132 return _codecForEncoding->fromUnicode(string);
134 return network()->encodeString(string);
138 // ====================
140 // ====================
141 void IrcChannel::setTopic(const QString &topic)
145 emit topicSet(topic);
149 void IrcChannel::setPassword(const QString &password)
151 _password = password;
155 void IrcChannel::setEncrypted(bool encrypted)
157 _encrypted = encrypted;
159 emit encryptedSet(encrypted);
163 void IrcChannel::joinIrcUsers(const QList<IrcUser *> &users, const QStringList &modes)
168 if (users.count() != modes.count()) {
169 qWarning() << "IrcChannel::addUsers(): number of nicks does not match number of modes!";
173 // Sort user modes first
174 const QStringList sortedModes = network()->sortPrefixModes(modes);
176 QStringList newNicks;
177 QStringList newModes;
178 QList<IrcUser *> newUsers;
181 for (int i = 0; i < users.count(); i++) {
185 if (_userModes.contains(ircuser)) {
186 if (sortedModes[i].count() > 1) {
187 // Multiple modes received, do it one at a time
188 // TODO Better way of syncing this without breaking protocol?
189 for (int i_m = 0; i_m < sortedModes[i].count(); ++i_m) {
190 addUserMode(ircuser, sortedModes[i][i_m]);
193 addUserMode(ircuser, sortedModes[i]);
198 _userModes[ircuser] = sortedModes[i];
199 ircuser->joinChannel(this, true);
200 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString)));
202 // connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
203 // If you wonder why there is no counterpart to ircUserJoined:
204 // the joins are propagated by the ircuser. The signal ircUserJoined is only for convenience
206 newNicks << ircuser->nick();
207 newModes << sortedModes[i];
211 if (newNicks.isEmpty())
214 SYNC_OTHER(joinIrcUsers, ARG(newNicks), ARG(newModes));
215 emit ircUsersJoined(newUsers);
219 void IrcChannel::joinIrcUsers(const QStringList &nicks, const QStringList &modes)
221 QList<IrcUser *> users;
222 foreach(QString nick, nicks)
223 users << network()->newIrcUser(nick);
224 joinIrcUsers(users, modes);
228 void IrcChannel::joinIrcUser(IrcUser *ircuser)
230 QList<IrcUser *> users;
234 joinIrcUsers(users, modes);
238 void IrcChannel::part(IrcUser *ircuser)
240 if (isKnownUser(ircuser)) {
241 _userModes.remove(ircuser);
242 ircuser->partChannel(this);
243 // If you wonder why there is no counterpart to ircUserParted:
244 // the joins are propagted by the ircuser. The signal ircUserParted is only for convenience
245 disconnect(ircuser, 0, this, 0);
246 emit ircUserParted(ircuser);
248 if (network()->isMe(ircuser) || _userModes.isEmpty()) {
249 // in either case we're no longer in the channel
250 // -> clean up the channel and destroy it
251 QList<IrcUser *> users = _userModes.keys();
253 foreach(IrcUser *user, users) {
254 disconnect(user, 0, this, 0);
255 user->partChannel(this);
258 network()->removeIrcChannel(this);
264 void IrcChannel::part(const QString &nick)
266 part(network()->ircUser(nick));
271 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes)
273 if (isKnownUser(ircuser)) {
274 // Keep user modes sorted
275 _userModes[ircuser] = network()->sortPrefixModes(modes);
276 QString nick = ircuser->nick();
277 SYNC_OTHER(setUserModes, ARG(nick), ARG(modes))
278 emit ircUserModesSet(ircuser, modes);
283 void IrcChannel::setUserModes(const QString &nick, const QString &modes)
285 setUserModes(network()->ircUser(nick), modes);
290 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode)
292 if (!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
295 if (!_userModes[ircuser].contains(mode)) {
296 // Keep user modes sorted
297 _userModes[ircuser] = network()->sortPrefixModes(_userModes[ircuser] + mode);
298 QString nick = ircuser->nick();
299 SYNC_OTHER(addUserMode, ARG(nick), ARG(mode))
300 emit ircUserModeAdded(ircuser, mode);
305 void IrcChannel::addUserMode(const QString &nick, const QString &mode)
307 addUserMode(network()->ircUser(nick), mode);
312 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode)
314 if (!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
317 if (_userModes[ircuser].contains(mode)) {
318 // Removing modes shouldn't mess up ordering
319 _userModes[ircuser].remove(mode);
320 QString nick = ircuser->nick();
321 SYNC_OTHER(removeUserMode, ARG(nick), ARG(mode));
322 emit ircUserModeRemoved(ircuser, mode);
327 void IrcChannel::removeUserMode(const QString &nick, const QString &mode)
329 removeUserMode(network()->ircUser(nick), mode);
333 // INIT SET USER MODES
334 QVariantMap IrcChannel::initUserModes() const
336 QVariantMap usermodes;
337 QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
338 while (iter != _userModes.constEnd()) {
339 usermodes[iter.key()->nick()] = iter.value();
346 void IrcChannel::initSetUserModes(const QVariantMap &usermodes)
348 QList<IrcUser *> users;
350 QVariantMap::const_iterator iter = usermodes.constBegin();
351 while (iter != usermodes.constEnd()) {
352 users << network()->newIrcUser(iter.key());
353 modes << iter.value().toString();
356 // joinIrcUsers handles sorting modes
357 joinIrcUsers(users, modes);
361 QVariantMap IrcChannel::initChanModes() const
363 QVariantMap channelModes;
366 QHash<QChar, QStringList>::const_iterator A_iter = _A_channelModes.constBegin();
367 while (A_iter != _A_channelModes.constEnd()) {
368 A_modes[A_iter.key()] = A_iter.value();
371 channelModes["A"] = A_modes;
374 QHash<QChar, QString>::const_iterator B_iter = _B_channelModes.constBegin();
375 while (B_iter != _B_channelModes.constEnd()) {
376 B_modes[B_iter.key()] = B_iter.value();
379 channelModes["B"] = B_modes;
382 QHash<QChar, QString>::const_iterator C_iter = _C_channelModes.constBegin();
383 while (C_iter != _C_channelModes.constEnd()) {
384 C_modes[C_iter.key()] = C_iter.value();
387 channelModes["C"] = C_modes;
390 QSet<QChar>::const_iterator D_iter = _D_channelModes.constBegin();
391 while (D_iter != _D_channelModes.constEnd()) {
395 channelModes["D"] = D_modes;
401 void IrcChannel::initSetChanModes(const QVariantMap &channelModes)
403 QVariantMap::const_iterator iter = channelModes["A"].toMap().constBegin();
404 QVariantMap::const_iterator iterEnd = channelModes["A"].toMap().constEnd();
405 while (iter != iterEnd) {
406 _A_channelModes[iter.key()[0]] = iter.value().toStringList();
410 iter = channelModes["B"].toMap().constBegin();
411 iterEnd = channelModes["B"].toMap().constEnd();
412 while (iter != iterEnd) {
413 _B_channelModes[iter.key()[0]] = iter.value().toString();
417 iter = channelModes["C"].toMap().constBegin();
418 iterEnd = channelModes["C"].toMap().constEnd();
419 while (iter != iterEnd) {
420 _C_channelModes[iter.key()[0]] = iter.value().toString();
424 QString D_modes = channelModes["D"].toString();
425 for (int i = 0; i < D_modes.count(); i++) {
426 _D_channelModes << D_modes[i];
431 void IrcChannel::ircUserDestroyed()
433 IrcUser *ircUser = static_cast<IrcUser *>(sender());
435 _userModes.remove(ircUser);
436 // no further propagation.
437 // this leads only to fuck ups.
441 void IrcChannel::ircUserNickSet(QString nick)
443 IrcUser *ircUser = qobject_cast<IrcUser *>(sender());
445 emit ircUserNickSet(ircUser, nick);
449 /*******************************************************************************
453 * o CHANMODES=A,B,C,D
455 * The CHANMODES token specifies the modes that may be set on a channel.
456 * These modes are split into four categories, as follows:
458 * o Type A: Modes that add or remove an address to or from a list.
459 * These modes always take a parameter when sent by the server to a
460 * client; when sent by a client, they may be specified without a
461 * parameter, which requests the server to display the current
462 * contents of the corresponding list on the channel to the client.
463 * o Type B: Modes that change a setting on the channel. These modes
464 * always take a parameter.
465 * o Type C: Modes that change a setting on the channel. These modes
466 * take a parameter only when set; the parameter is absent when the
467 * mode is removed both in the client's and server's MODE command.
468 * o Type D: Modes that change a setting on the channel. These modes
469 * never take a parameter.
471 * If the server sends any additional types after these 4, the client
472 * MUST ignore them; this is intended to allow future extension of this
475 * The IRC server MUST NOT list modes in CHANMODES which are also
476 * present in the PREFIX parameter; however, for completeness, modes
477 * described in PREFIX may be treated as type B modes.
479 ******************************************************************************/
481 /*******************************************************************************
483 * A --> add/remove from List
484 * B --> set value or remove
485 * C --> set value or remove
488 * B and C behave very similar... we store the data in different datastructures
489 * for future compatibility
490 ******************************************************************************/
492 // NOTE: the behavior of addChannelMode and removeChannelMode depends on the type of mode
493 // see list above for chanmode types
494 void IrcChannel::addChannelMode(const QChar &mode, const QString &value)
496 Network::ChannelModeType modeType = network()->channelModeType(mode);
499 case Network::NOT_A_CHANMODE:
501 case Network::A_CHANMODE:
502 if (!_A_channelModes.contains(mode))
503 _A_channelModes[mode] = QStringList(value);
504 else if (!_A_channelModes[mode].contains(value))
505 _A_channelModes[mode] << value;
508 case Network::B_CHANMODE:
509 _B_channelModes[mode] = value;
512 case Network::C_CHANMODE:
513 _C_channelModes[mode] = value;
516 case Network::D_CHANMODE:
517 _D_channelModes << mode;
520 SYNC(ARG(mode), ARG(value))
524 void IrcChannel::removeChannelMode(const QChar &mode, const QString &value)
526 Network::ChannelModeType modeType = network()->channelModeType(mode);
529 case Network::NOT_A_CHANMODE:
531 case Network::A_CHANMODE:
532 if (_A_channelModes.contains(mode))
533 _A_channelModes[mode].removeAll(value);
536 case Network::B_CHANMODE:
537 _B_channelModes.remove(mode);
540 case Network::C_CHANMODE:
541 _C_channelModes.remove(mode);
544 case Network::D_CHANMODE:
545 _D_channelModes.remove(mode);
548 SYNC(ARG(mode), ARG(value))
552 bool IrcChannel::hasMode(const QChar &mode) const
554 Network::ChannelModeType modeType = network()->channelModeType(mode);
557 case Network::NOT_A_CHANMODE:
559 case Network::A_CHANMODE:
560 return _A_channelModes.contains(mode);
561 case Network::B_CHANMODE:
562 return _B_channelModes.contains(mode);
563 case Network::C_CHANMODE:
564 return _C_channelModes.contains(mode);
565 case Network::D_CHANMODE:
566 return _D_channelModes.contains(mode);
572 QString IrcChannel::modeValue(const QChar &mode) const
574 Network::ChannelModeType modeType = network()->channelModeType(mode);
577 case Network::B_CHANMODE:
578 if (_B_channelModes.contains(mode))
579 return _B_channelModes[mode];
582 case Network::C_CHANMODE:
583 if (_C_channelModes.contains(mode))
584 return _C_channelModes[mode];
593 QStringList IrcChannel::modeValueList(const QChar &mode) const
595 Network::ChannelModeType modeType = network()->channelModeType(mode);
598 case Network::A_CHANMODE:
599 if (_A_channelModes.contains(mode))
600 return _A_channelModes[mode];
609 QString IrcChannel::channelModeString() const
614 QSet<QChar>::const_iterator D_iter = _D_channelModes.constBegin();
615 while (D_iter != _D_channelModes.constEnd()) {
616 modeString += *D_iter;
620 QHash<QChar, QString>::const_iterator BC_iter = _C_channelModes.constBegin();
621 while (BC_iter != _C_channelModes.constEnd()) {
622 modeString += BC_iter.key();
623 params << BC_iter.value();
627 BC_iter = _B_channelModes.constBegin();
628 while (BC_iter != _B_channelModes.constEnd()) {
629 modeString += BC_iter.key();
630 params << BC_iter.value();
633 if (modeString.isEmpty())
636 return QString("+%1 %2").arg(modeString).arg(params.join(" "));