ircuserinfo.cpp
1 //------------------------------------------------------------------------------
2 // ircuserinfo.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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 = nullptr;
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 == nullptr)
42  return;
43 
44  this->userName = parentNetwork->userPrefixes().cleanNickname(nickname);
45  this->userModes << parentNetwork->userPrefixes().modeFromNickname(nickname);
46 }
47 
49 {
50  return prefixes().cleanNickname(userName);
51 }
52 
54 {
55  return IRCGlobal::toIrcLower(this->cleanNickname());
56 }
57 
58 QString IRCUserInfo::extractHostnameFromFullSignature() const
59 {
60  if (!this->fullSignature.isEmpty())
61  {
62  int indexOfDelimiterChar = this->fullSignature.indexOf('@');
63  QString hostName = this->fullSignature;
64  hostName.remove(0, indexOfDelimiterChar + 1);
65 
66  return hostName;
67  }
68 
69  return "";
70 }
71 
72 bool IRCUserInfo::isOp() const
73 {
74  return modes().contains('o');
75 }
76 
77 bool IRCUserInfo::isSameNickname(const IRCUserInfo &otherUser) const
78 {
79  return (*this) == otherUser;
80 }
81 
82 bool IRCUserInfo::isSameNickname(const QString &otherNickname) const
83 {
84  IRCUserInfo otherUser(otherNickname, network());
85  return isSameNickname(otherUser);
86 }
87 
88 bool IRCUserInfo::isValid() const
89 {
90  return !userName.isEmpty() && parentNetwork != nullptr;
91 }
92 
93 const QList<char> &IRCUserInfo::modes() const
94 {
95  return userModes;
96 }
97 
98 const IRCNetworkAdapter *IRCUserInfo::network() const
99 {
100  return parentNetwork;
101 }
102 
103 void IRCUserInfo::setPrefixedNickname(const QString &nickname)
104 {
105  this->userName = prefixes().cleanNickname(nickname);
106  if (prefixes().modeFromNickname(nickname) != 0)
107  setMode(prefixes().modeFromNickname(nickname));
108 }
109 
110 bool IRCUserInfo::operator==(const IRCUserInfo &otherUser) const
111 {
112  QString thisNickname = this->cleanNicknameLowerCase();
113  QString otherNickname = otherUser.cleanNicknameLowerCase();
114 
115  return thisNickname.compare(otherNickname) == 0;
116 }
117 
118 bool IRCUserInfo::operator<=(const IRCUserInfo &otherUser) const
119 {
120  assert(parentNetwork != nullptr);
121  char mode1 = prefixes().topMostMode(modes());
122  char mode2 = prefixes().topMostMode(otherUser.modes());
123  if (prefixes().compare(mode1, mode2) != 0)
124  return prefixes().compare(mode1, mode2) < 0;
125 
126  QString thisNickname = this->cleanNicknameLowerCase();
127  QString otherNickname = otherUser.cleanNicknameLowerCase();
128 
129  bool bIsThisAlphabeticallySmaller = (thisNickname.compare(otherNickname) <= 0);
130  return bIsThisAlphabeticallySmaller;
131 }
132 
134 {
135  assert(parentNetwork != nullptr);
136  char mode = prefixes().topMostMode(modes());
137  if (mode != 0)
138  {
139  return QString("%1%2").arg(prefixes().prefixForMode(mode))
140  .arg(cleanNickname());
141  }
142  else
143  return cleanNickname();
144 }
145 
147 {
148  return IRCGlobal::toIrcLower(this->prefixedName());
149 }
150 
151 const IRCUserPrefix &IRCUserInfo::prefixes() const
152 {
153  return parentNetwork->userPrefixes();
154 }
155 
156 void IRCUserInfo::setModes(const QList<char> &modes)
157 {
158  this->userModes = modes;
159 }
160 
161 void IRCUserInfo::setMode(char mode)
162 {
163  if (!userModes.contains(mode))
164  userModes << mode;
165 }
166 
167 void IRCUserInfo::unsetMode(char mode)
168 {
169  userModes.removeAll(mode);
170 }