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 qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
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++) {
183 if (!ircuser || _userModes.contains(ircuser)) {
184 if (sortedModes[i].count() > 1) {
185 // Multiple modes received, do it one at a time
186 // TODO Better way of syncing this without breaking protocol?
187 for (int i_m = 0; i_m < sortedModes[i].count(); ++i_m) {
188 addUserMode(ircuser, sortedModes[i][i_m]);
191 addUserMode(ircuser, sortedModes[i]);
196 _userModes[ircuser] = sortedModes[i];
197 ircuser->joinChannel(this, true);
198 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString)));
200 // connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
201 // If you wonder why there is no counterpart to ircUserJoined:
202 // the joins are propagated by the ircuser. The signal ircUserJoined is only for convenience
204 newNicks << ircuser->nick();
205 newModes << sortedModes[i];
209 if (newNicks.isEmpty())
212 SYNC_OTHER(joinIrcUsers, ARG(newNicks), ARG(newModes));
213 emit ircUsersJoined(newUsers);
217 void IrcChannel::joinIrcUsers(const QStringList &nicks, const QStringList &modes)
219 QList<IrcUser *> users;
220 foreach(QString nick, nicks)
221 users << network()->newIrcUser(nick);
222 joinIrcUsers(users, modes);
226 void IrcChannel::joinIrcUser(IrcUser *ircuser)
228 QList<IrcUser *> users;
232 joinIrcUsers(users, modes);
236 void IrcChannel::part(IrcUser *ircuser)
238 if (isKnownUser(ircuser)) {
239 _userModes.remove(ircuser);
240 ircuser->partChannel(this);
241 // If you wonder why there is no counterpart to ircUserParted:
242 // the joins are propagted by the ircuser. The signal ircUserParted is only for convenience
243 disconnect(ircuser, 0, this, 0);
244 emit ircUserParted(ircuser);
246 if (network()->isMe(ircuser) || _userModes.isEmpty()) {
247 // in either case we're no longer in the channel
248 // -> clean up the channel and destroy it
249 QList<IrcUser *> users = _userModes.keys();
251 foreach(IrcUser *user, users) {
252 disconnect(user, 0, this, 0);
253 user->partChannel(this);
256 network()->removeIrcChannel(this);
262 void IrcChannel::part(const QString &nick)
264 part(network()->ircUser(nick));
269 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes)
271 if (isKnownUser(ircuser)) {
272 // Keep user modes sorted
273 _userModes[ircuser] = network()->sortPrefixModes(modes);
274 QString nick = ircuser->nick();
275 SYNC_OTHER(setUserModes, ARG(nick), ARG(modes))
276 emit ircUserModesSet(ircuser, modes);
281 void IrcChannel::setUserModes(const QString &nick, const QString &modes)
283 setUserModes(network()->ircUser(nick), modes);
288 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode)
290 if (!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
293 if (!_userModes[ircuser].contains(mode)) {
294 // Keep user modes sorted
295 _userModes[ircuser] = network()->sortPrefixModes(_userModes[ircuser] + mode);
296 QString nick = ircuser->nick();
297 SYNC_OTHER(addUserMode, ARG(nick), ARG(mode))
298 emit ircUserModeAdded(ircuser, mode);
303 void IrcChannel::addUserMode(const QString &nick, const QString &mode)
305 addUserMode(network()->ircUser(nick), mode);
310 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode)
312 if (!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
315 if (_userModes[ircuser].contains(mode)) {
316 // Removing modes shouldn't mess up ordering
317 _userModes[ircuser].remove(mode);
318 QString nick = ircuser->nick();
319 SYNC_OTHER(removeUserMode, ARG(nick), ARG(mode));
320 emit ircUserModeRemoved(ircuser, mode);
325 void IrcChannel::removeUserMode(const QString &nick, const QString &mode)
327 removeUserMode(network()->ircUser(nick), mode);
331 // INIT SET USER MODES
332 QVariantMap IrcChannel::initUserModes() const
334 QVariantMap usermodes;
335 QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
336 while (iter != _userModes.constEnd()) {
337 usermodes[iter.key()->nick()] = iter.value();
344 void IrcChannel::initSetUserModes(const QVariantMap &usermodes)
346 QList<IrcUser *> users;
348 QVariantMap::const_iterator iter = usermodes.constBegin();
349 while (iter != usermodes.constEnd()) {
350 users << network()->newIrcUser(iter.key());
351 modes << iter.value().toString();
354 // joinIrcUsers handles sorting modes
355 joinIrcUsers(users, modes);
359 QVariantMap IrcChannel::initChanModes() const
361 QVariantMap channelModes;
364 QHash<QChar, QStringList>::const_iterator A_iter = _A_channelModes.constBegin();
365 while (A_iter != _A_channelModes.constEnd()) {
366 A_modes[A_iter.key()] = A_iter.value();
369 channelModes["A"] = A_modes;
372 QHash<QChar, QString>::const_iterator B_iter = _B_channelModes.constBegin();
373 while (B_iter != _B_channelModes.constEnd()) {
374 B_modes[B_iter.key()] = B_iter.value();
377 channelModes["B"] = B_modes;
380 QHash<QChar, QString>::const_iterator C_iter = _C_channelModes.constBegin();
381 while (C_iter != _C_channelModes.constEnd()) {
382 C_modes[C_iter.key()] = C_iter.value();
385 channelModes["C"] = C_modes;
388 QSet<QChar>::const_iterator D_iter = _D_channelModes.constBegin();
389 while (D_iter != _D_channelModes.constEnd()) {
393 channelModes["D"] = D_modes;
399 void IrcChannel::initSetChanModes(const QVariantMap &channelModes)
401 QVariantMap::const_iterator iter = channelModes["A"].toMap().constBegin();
402 QVariantMap::const_iterator iterEnd = channelModes["A"].toMap().constEnd();
403 while (iter != iterEnd) {
404 _A_channelModes[iter.key()[0]] = iter.value().toStringList();
408 iter = channelModes["B"].toMap().constBegin();
409 iterEnd = channelModes["B"].toMap().constEnd();
410 while (iter != iterEnd) {
411 _B_channelModes[iter.key()[0]] = iter.value().toString();
415 iter = channelModes["C"].toMap().constBegin();
416 iterEnd = channelModes["C"].toMap().constEnd();
417 while (iter != iterEnd) {
418 _C_channelModes[iter.key()[0]] = iter.value().toString();
422 QString D_modes = channelModes["D"].toString();
423 for (int i = 0; i < D_modes.count(); i++) {
424 _D_channelModes << D_modes[i];
429 void IrcChannel::ircUserDestroyed()
431 IrcUser *ircUser = static_cast<IrcUser *>(sender());
433 _userModes.remove(ircUser);
434 // no further propagation.
435 // this leads only to fuck ups.
439 void IrcChannel::ircUserNickSet(QString nick)
441 IrcUser *ircUser = qobject_cast<IrcUser *>(sender());
443 emit ircUserNickSet(ircUser, nick);
447 /*******************************************************************************
451 * o CHANMODES=A,B,C,D
453 * The CHANMODES token specifies the modes that may be set on a channel.
454 * These modes are split into four categories, as follows:
456 * o Type A: Modes that add or remove an address to or from a list.
457 * These modes always take a parameter when sent by the server to a
458 * client; when sent by a client, they may be specified without a
459 * parameter, which requests the server to display the current
460 * contents of the corresponding list on the channel to the client.
461 * o Type B: Modes that change a setting on the channel. These modes
462 * always take a parameter.
463 * o Type C: Modes that change a setting on the channel. These modes
464 * take a parameter only when set; the parameter is absent when the
465 * mode is removed both in the client's and server's MODE command.
466 * o Type D: Modes that change a setting on the channel. These modes
467 * never take a parameter.
469 * If the server sends any additional types after these 4, the client
470 * MUST ignore them; this is intended to allow future extension of this
473 * The IRC server MUST NOT list modes in CHANMODES which are also
474 * present in the PREFIX parameter; however, for completeness, modes
475 * described in PREFIX may be treated as type B modes.
477 ******************************************************************************/
479 /*******************************************************************************
481 * A --> add/remove from List
482 * B --> set value or remove
483 * C --> set value or remove
486 * B and C behave very similar... we store the data in different datastructures
487 * for future compatibility
488 ******************************************************************************/
490 // NOTE: the behavior of addChannelMode and removeChannelMode depends on the type of mode
491 // see list above for chanmode types
492 void IrcChannel::addChannelMode(const QChar &mode, const QString &value)
494 Network::ChannelModeType modeType = network()->channelModeType(mode);
497 case Network::NOT_A_CHANMODE:
499 case Network::A_CHANMODE:
500 if (!_A_channelModes.contains(mode))
501 _A_channelModes[mode] = QStringList(value);
502 else if (!_A_channelModes[mode].contains(value))
503 _A_channelModes[mode] << value;
506 case Network::B_CHANMODE:
507 _B_channelModes[mode] = value;
510 case Network::C_CHANMODE:
511 _C_channelModes[mode] = value;
514 case Network::D_CHANMODE:
515 _D_channelModes << mode;
518 SYNC(ARG(mode), ARG(value))
522 void IrcChannel::removeChannelMode(const QChar &mode, const QString &value)
524 Network::ChannelModeType modeType = network()->channelModeType(mode);
527 case Network::NOT_A_CHANMODE:
529 case Network::A_CHANMODE:
530 if (_A_channelModes.contains(mode))
531 _A_channelModes[mode].removeAll(value);
534 case Network::B_CHANMODE:
535 _B_channelModes.remove(mode);
538 case Network::C_CHANMODE:
539 _C_channelModes.remove(mode);
542 case Network::D_CHANMODE:
543 _D_channelModes.remove(mode);
546 SYNC(ARG(mode), ARG(value))
550 bool IrcChannel::hasMode(const QChar &mode) const
552 Network::ChannelModeType modeType = network()->channelModeType(mode);
555 case Network::NOT_A_CHANMODE:
557 case Network::A_CHANMODE:
558 return _A_channelModes.contains(mode);
559 case Network::B_CHANMODE:
560 return _B_channelModes.contains(mode);
561 case Network::C_CHANMODE:
562 return _C_channelModes.contains(mode);
563 case Network::D_CHANMODE:
564 return _D_channelModes.contains(mode);
570 QString IrcChannel::modeValue(const QChar &mode) const
572 Network::ChannelModeType modeType = network()->channelModeType(mode);
575 case Network::B_CHANMODE:
576 if (_B_channelModes.contains(mode))
577 return _B_channelModes[mode];
580 case Network::C_CHANMODE:
581 if (_C_channelModes.contains(mode))
582 return _C_channelModes[mode];
591 QStringList IrcChannel::modeValueList(const QChar &mode) const
593 Network::ChannelModeType modeType = network()->channelModeType(mode);
596 case Network::A_CHANMODE:
597 if (_A_channelModes.contains(mode))
598 return _A_channelModes[mode];
607 QString IrcChannel::channelModeString() const
612 QSet<QChar>::const_iterator D_iter = _D_channelModes.constBegin();
613 while (D_iter != _D_channelModes.constEnd()) {
614 modeString += *D_iter;
618 QHash<QChar, QString>::const_iterator BC_iter = _C_channelModes.constBegin();
619 while (BC_iter != _C_channelModes.constEnd()) {
620 modeString += BC_iter.key();
621 params << BC_iter.value();
625 BC_iter = _B_channelModes.constBegin();
626 while (BC_iter != _B_channelModes.constEnd()) {
627 modeString += BC_iter.key();
628 params << BC_iter.value();
631 if (modeString.isEmpty())
634 return QString("+%1 %2").arg(modeString).arg(params.join(" "));