ircuserinfo.cpp
1 //------------------------------------------------------------------------------
2 // ircuserinfo.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 "ircuserinfo.h"
24 
25 #include "irc/entities/ircuserprefix.h"
26 #include "irc/ircglobal.h"
27 #include "irc/ircnetworkadapter.h"
28 #include <cassert>
29 
31 {
32  this->parentNetwork = NULL;
33 }
34 
35 IRCUserInfo::IRCUserInfo(const QString& nickname, const IRCNetworkAdapter *parentNetwork,
36  const QString& fullSignature)
37 {
38  this->fullSignature = fullSignature;
39  this->parentNetwork = parentNetwork;
40 
41  if (nickname.isEmpty() || parentNetwork == NULL)
42  {
43  return;
44  }
45 
46  this->userName = parentNetwork->userPrefixes().cleanNickname(nickname);
47  this->userModes << parentNetwork->userPrefixes().modeFromNickname(nickname);
48 }
49 
51 {
52  return prefixes().cleanNickname(userName);
53 }
54 
56 {
57  return IRCGlobal::toIrcLower(this->cleanNickname());
58 }
59 
60 QString IRCUserInfo::extractHostnameFromFullSignature() const
61 {
62  if (!this->fullSignature.isEmpty())
63  {
64  int indexOfDelimiterChar = this->fullSignature.indexOf('@');
65  QString hostName = this->fullSignature;
66  hostName.remove(0, indexOfDelimiterChar + 1);
67 
68  return hostName;
69  }
70 
71  return "";
72 }
73 
74 bool IRCUserInfo::isOp() const
75 {
76  return modes().contains('o');
77 }
78 
79 bool IRCUserInfo::isSameNickname(const IRCUserInfo& otherUser) const
80 {
81  return ((*this) == otherUser);
82 }
83 
84 bool IRCUserInfo::isSameNickname(const QString& otherNickname) const
85 {
86  IRCUserInfo otherUser(otherNickname, network());
87  return isSameNickname(otherUser);
88 }
89 
90 bool IRCUserInfo::isValid() const
91 {
92  return !userName.isEmpty() && parentNetwork != NULL;
93 }
94 
95 const QList<char> &IRCUserInfo::modes() const
96 {
97  return userModes;
98 }
99 
100 const IRCNetworkAdapter *IRCUserInfo::network() const
101 {
102  return parentNetwork;
103 }
104 
105 void IRCUserInfo::setPrefixedNickname(const QString &nickname)
106 {
107  this->userName = prefixes().cleanNickname(nickname);
108  if (prefixes().modeFromNickname(nickname) != 0)
109  {
110  setMode(prefixes().modeFromNickname(nickname));
111  }
112 }
113 
114 bool IRCUserInfo::operator==(const IRCUserInfo& otherUser) const
115 {
116  QString thisNickname = this->cleanNicknameLowerCase();
117  QString otherNickname = otherUser.cleanNicknameLowerCase();
118 
119  return (thisNickname.compare(otherNickname) == 0);
120 }
121 
122 bool IRCUserInfo::operator<=(const IRCUserInfo& otherUser) const
123 {
124  assert(parentNetwork != NULL);
125  char mode1 = prefixes().topMostMode(modes());
126  char mode2 = prefixes().topMostMode(otherUser.modes());
127  if (prefixes().compare(mode1, mode2) != 0)
128  {
129  return prefixes().compare(mode1, mode2) < 0;
130  }
131 
132  QString thisNickname = this->cleanNicknameLowerCase();
133  QString otherNickname = otherUser.cleanNicknameLowerCase();
134 
135  bool bIsThisAlphabeticallySmaller = (thisNickname.compare(otherNickname) <= 0);
136  return bIsThisAlphabeticallySmaller;
137 }
138 
140 {
141  assert(parentNetwork != NULL);
142  char mode = prefixes().topMostMode(modes());
143  if (mode != 0)
144  {
145  return QString("%1%2").arg(prefixes().prefixForMode(mode))
146  .arg(cleanNickname());
147  }
148  else
149  {
150  return cleanNickname();
151  }
152 }
153 
155 {
156  return IRCGlobal::toIrcLower(this->prefixedName());
157 }
158 
159 const IRCUserPrefix &IRCUserInfo::prefixes() const
160 {
161  return parentNetwork->userPrefixes();
162 }
163 
164 void IRCUserInfo::setModes(const QList<char> &modes)
165 {
166  this->userModes = modes;
167 }
168 
169 void IRCUserInfo::setMode(char mode)
170 {
171  if (!userModes.contains(mode))
172  {
173  userModes << mode;
174  }
175 }
176 
177 void IRCUserInfo::unsetMode(char mode)
178 {
179  userModes.removeAll(mode);
180 }
QString fullSignature
Full user signature with nickname and hostname.
Definition: ircuserinfo.h:47
Interprets communication between the client and the IRC server.
char topMostMode(const QList< char > &candidates) const
Out of list of possible modes picks mode with highest priority.
QString cleanNickname() const
Returns nickname with no prefixes, contrary to the prefixedName() .
Definition: ircuserinfo.cpp:50
QString prefixedName() const
Will generate prefix based on the user flags.
bool isSameNickname(const IRCUserInfo &otherUser) const
Check if this user and user specified as parameter are the same user.
Definition: ircuserinfo.cpp:79
QString cleanNicknameLowerCase() const
Returns cleanNickname() with a call to IRCGlobal::toIrcLower() .
Definition: ircuserinfo.cpp:55
static QString toIrcLower(const QString &str)
RFC 1459 compliant toLower() method.
Definition: ircglobal.cpp:44
Holds information flags about given nickname.
Definition: ircuserinfo.h:35
const IRCUserPrefix & userPrefixes() const
All allowed modes with their nickname prefixes for this network.
QString prefixedNameLowerCase() const
Returns prefixedName() with a call to IRCGlobal::toIrcLower() .
One-to-one association of visible prefixes to user mode.
Definition: ircuserprefix.h:37
IRCUserInfo()
Creates invalid IRCUserInfo.
Definition: ircuserinfo.cpp:30
bool operator<=(const IRCUserInfo &otherUser) const
This operator can be used for sorting.