ircchanneladapter.cpp
1 //------------------------------------------------------------------------------
2 // ircchanneladapter.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
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 Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircchanneladapter.h"
24 #include "irc/configuration/ircconfig.h"
25 #include "irc/ircglobal.h"
26 #include "irc/ircmessageclass.h"
27 #include "irc/ircnetworkadapter.h"
28 #include "irc/ircuserinfo.h"
29 #include "irc/ircuserlist.h"
30 
31 IRCChannelAdapter::IRCChannelAdapter(IRCNetworkAdapter* pNetwork, const QString& recipient)
32 : IRCChatAdapter(pNetwork, recipient)
33 {
34  users = new IRCUserList();
35 }
36 
37 IRCChannelAdapter::~IRCChannelAdapter()
38 {
39  if (this->pNetwork != NULL)
40  {
41  sendMessage("/part " + this->recipientName + " " + gIRCConfig.personal.quitMessage);
42  }
43 
44  delete users;
45 }
46 
48 {
49  const QString& myNickname = pNetwork->myNickname();
50  return isOperator(myNickname);
51 }
52 
54 {
55  if (users->appendNameToCachedList(IRCUserInfo(name, network())))
56  {
57  IRCUserInfo user = users->userCopy(name);
58  emit nameAdded(user);
59  }
60 }
61 
62 void IRCChannelAdapter::appendNamesToCachedList(const QStringList& names)
63 {
64  foreach (const QString &name, names)
65  {
67  }
68 }
69 
70 void IRCChannelAdapter::banUser(const QString& nickname, const QString& reason)
71 {
72  pNetwork->banUser(nickname, reason, this->recipientName);
73 }
74 
76 {
77  emit nameListUpdated(*users);
78 }
79 
80 void IRCChannelAdapter::emitChatMessage(const QString& sender, const QString& content)
81 {
82  // Ensure that all nickname artifacts are preserved.
83  const IRCUserInfo* pUserInfo = users->user(sender);
84 
85  QString actualSenderName = sender;
86  if (pUserInfo != NULL)
87  {
88  actualSenderName = pUserInfo->prefixedName();
89  }
90 
91  // Check if content has our nickname.
92  // (do not play sounds for our own messages)
93  const QString& myNickname = pNetwork->myNickname();
94  if (content.contains(myNickname, Qt::CaseInsensitive)
95  && sender.compare(myNickname, Qt::CaseInsensitive) != 0)
96  {
97  emit myNicknameUsed();
98  }
99 
100  IRCChatAdapter::emitChatMessage(actualSenderName, content);
101 }
102 
103 bool IRCChannelAdapter::hasUser(const QString& nickname)
104 {
105  return users->hasUser(nickname);
106 }
107 
108 bool IRCChannelAdapter::isOperator(const QString& nickname) const
109 {
110  const IRCUserInfo* pUser = users->user(nickname);
111  if (pUser != NULL)
112  {
113  return pUser->isOp();
114  }
115 
116  return false;
117 }
118 
119 void IRCChannelAdapter::kickUser(const QString& nickname, const QString& reason)
120 {
121  if (hasUser(nickname))
122  {
123  QString cleanNickname = IRCUserInfo(nickname, pNetwork).cleanNickname();
124  this->sendMessage(QString("/kick %1 %2 %3").arg(this->recipientName, cleanNickname, reason));
125  }
126 }
127 
129 {
130  IRCUserInfo user = users->userCopy(name);
131 
132  if (!users->removeNameFromCachedList(name))
133  {
134  emit error(QString("Attempted to remove name \"%1\" from the \"%2\" channel's name list but no such name is on the list.").arg(name, this->recipientName));
135  }
136  else
137  {
138  emit nameRemoved(user);
139  }
140 }
141 
142 void IRCChannelAdapter::setHalfOp(const QString& nickname, bool bSet)
143 {
144  pNetwork->setChannelMode(this->recipientName, nickname, "h", bSet);
145 }
146 
147 void IRCChannelAdapter::setOp(const QString& nickname, bool bSet)
148 {
149  pNetwork->setChannelMode(this->recipientName, nickname, "o", bSet);
150 }
151 
152 void IRCChannelAdapter::setVoiced(const QString& nickname, bool bSet)
153 {
154  pNetwork->setChannelMode(this->recipientName, nickname, "v", bSet);
155 }
156 
157 void IRCChannelAdapter::userChangesNickname(const QString& oldNickname, const QString& newNickname)
158 {
159  if (hasUser(oldNickname))
160  {
161  IRCUserInfo oldName = users->userCopy(oldNickname);
162 
163  users->changeNick(oldNickname, newNickname);
164  emit nameRemoved(oldName);
165  emit nameAdded(users->userCopy(newNickname));
166 
167  emit messageWithClass(tr("%1 is now known as %2").arg(oldNickname, newNickname),
168  IRCMessageClass::ChannelAction);
169  }
170 }
171 
172 void IRCChannelAdapter::userJoins(const QString& nickname, const QString& fullSignature)
173 {
174  appendNameToCachedList(nickname);
175 
176  emit messageWithClass(tr("User %1 [%2] has joined the channel.").arg(nickname, fullSignature),
177  IRCMessageClass::ChannelAction);
178 }
179 
180 void IRCChannelAdapter::userLeaves(const QString& nickname, const QString& farewellMessage, IRCQuitType quitType)
181 {
182  if (!hasUser(nickname))
183  {
184  // Nothing to do here. This user was not even on the channel.
185  return;
186  }
187 
188  removeNameFromCachedList(nickname);
189 
190  switch (quitType)
191  {
192  case IRCChatAdapter::ChannelPart:
193  emit messageWithClass(tr("User %1 has left the channel. (PART: %2)").arg(nickname, farewellMessage),
194  IRCMessageClass::ChannelAction);
195  break;
196 
197  case IRCChatAdapter::NetworkKill:
198  emit messageWithClass(tr("Connection for user %1 has been killed. (KILL: %2)").arg(nickname, farewellMessage),
199  IRCMessageClass::NetworkAction);
200  break;
201 
202  case IRCChatAdapter::NetworkQuit:
203  emit messageWithClass(tr("User %1 has quit the network. (QUIT: %2)").arg(nickname, farewellMessage),
204  IRCMessageClass::NetworkAction);
205  break;
206 
207  default:
208  emit error(tr("Unknown quit type from user %1.").arg(nickname));
209  break;
210  }
211 }
212 
213 void IRCChannelAdapter::userModeChanges(const QString& nickname,
214  const QList<char> &addedFlags, const QList<char> &removedFlags)
215 {
216  const IRCUserInfo* pUserInfo = this->users->user(nickname);
217  if (pUserInfo != NULL)
218  {
219  IRCUserInfo newUserInfo = *pUserInfo;
220  foreach (char mode, addedFlags)
221  {
222  newUserInfo.setMode(mode);
223  }
224  foreach (char mode, removedFlags)
225  {
226  newUserInfo.unsetMode(mode);
227  }
228 
229  this->users->setUserModes(nickname, newUserInfo.modes());
230  emit nameUpdated(newUserInfo);
231  }
232 }
Interprets communication between the client and the IRC server.
Allows to perform operation on a list of users.
Definition: ircuserlist.h:42
void emitCachedNameListUpdated()
Emits cached list of names. This should be called when end of names list message is received for this...
void userJoins(const QString &nickname, const QString &fullSignature)
Use this to register the fact that user has joined the chat.
void myNicknameUsed()
Emitted when nickname is used in a message.
void userModeChanges(const QString &nickname, const QList< char > &addedFlags, const QList< char > &removedFlags)
void kickUser(const QString &nickname, const QString &reason)
Kicks user from the channel.
void removeNameFromCachedList(const QString &name)
Removes a name from the sortecd cachedNames list.
void setHalfOp(const QString &nickname, bool bSet)
Sets half op mode for given user.
void appendNamesToCachedList(const QStringList &names)
Appends a list of names to cachedNames list. This ensures that no duplicate names are found on the li...
void banUser(const QString &nickname, const QString &reason, const QString &channel)
Bans specified user from a channel.
QString cleanNickname() const
Returns nickname with no prefixes, contrary to the prefixedName() .
Definition: ircuserinfo.cpp:50
virtual void emitChatMessage(const QString &sender, const QString &content)
Emits message() signal formatting it to present sender's message.
QString prefixedName() const
Will generate prefix based on the user flags.
bool amIOperator() const
Checks if this client is an operator on this channel.
Handles chatting through IRC.
void banUser(const QString &nickname, const QString &reason)
Bans and kicks user from the channel.
void userLeaves(const QString &nickname, const QString &farewellMessage, IRCQuitType quitType)
Use this to register the fact that user has left the chat.
bool appendNameToCachedList(const IRCUserInfo &userInfo)
Appends a single name to the users array.
Definition: ircuserlist.cpp:34
IRCNetworkAdapter * network()
The idea of the adapter system is that each adapter is either a network or is a child of a network...
void userChangesNickname(const QString &oldNickname, const QString &newNickname)
bool changeNick(const QString &oldNickname, const QString &newNickname)
Changes a nickname while preserving user flags.
Definition: ircuserlist.cpp:50
IRCUserInfo userCopy(const QString &nickname) const
Gets a copy of the IRCUserInfo for user with given name.
bool removeNameFromCachedList(const QString &nickname)
Removes a name from the sortecd cachedNames list.
Definition: ircuserlist.cpp:84
void setVoiced(const QString &nickname, bool bSet)
Sets voice mode for given user.
Holds information flags about given nickname.
Definition: ircuserinfo.h:35
void setOp(const QString &nickname, bool bSet)
Sets op mode for given user.
void setChannelMode(const QString &channel, const QString &nickname, const QString &flag, bool bSet)
Sets channel flags.
bool isOperator(const QString &nickname) const
Checks if user is an operator on this channel.
void emitChatMessage(const QString &sender, const QString &content)
Emits message() signal formatting it to present sender's message.
void appendNameToCachedList(const QString &name)
Appends a single name to the sorted cachedNames list.