ircresponsetype.cpp
1 //------------------------------------------------------------------------------
2 // ircresponsetype.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) 2011 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircresponsetype.h"
24 
26 {
27  d.type = Invalid;
28  d.numericType = -1;
29 }
30 
32 {
33  d.type = type;
34  d.numericType = toRfcNumber(type);
35 }
36 
38 {
39  d.type = typeFromRfcString(type);
40 
41  // Get numeric type from string.
42  bool bOk = false;
43  int numericType = type.toInt(&bOk);
44  if (!bOk)
45  {
46  numericType = -1;
47  }
48  d.numericType = numericType;
49 }
50 
52 {
53  // Response type is always a 3-digit number. Conver the passed integer with
54  // leading zeros if necessary and then pass it to typeFromRfcString().
55 
56  // The result here is d.numericType always set to responseType, and the
57  // d.type set to either Invalid or appropriate value if the number was
58  // recognized.
59  QString strResponseType = QString("%1").arg(responseType, 3, 10, QChar('0'));
60 
61  IRCResponseType newIRCResponseType;
62  newIRCResponseType.d.type = typeFromRfcString(strResponseType);
63  newIRCResponseType.d.numericType = responseType;
64 
65  return newIRCResponseType;
66 }
67 
68 bool IRCResponseType::operator==(const IRCResponseType& other) const
69 {
70  return d.type == other.d.type;
71 }
72 
73 bool IRCResponseType::operator!=(const IRCResponseType& other) const
74 {
75  return !((*this) == other);
76 }
77 
79 {
80  if (type == Invalid)
81  {
82  return -1;
83  }
84 
85  QString str = toRfcString(type);
86 
87  bool bOk = false;
88  int val = str.toInt(&bOk);
89 
90  return bOk ? val : -1;
91 }
92 
94 {
95  switch (type)
96  {
97  case HelloClient:
98  return "001";
99 
100  case RPLISupport:
101  return "005";
102 
103  case RPLLUserClient:
104  return "251";
105 
106  case RPLLUserOp:
107  return "252";
108 
109  case RPLLUserUnknown:
110  return "253";
111 
112  case RPLLUserChannels:
113  return "254";
114 
115  case RPLLUserMe:
116  return "255";
117 
118  case RPLAway:
119  return "301";
120 
121  case RPLWhoIsRegnick:
122  return "307";
123 
124  case RPLWhoIsUser:
125  return "311";
126 
127  case RPLWhoIsServer:
128  return "312";
129 
130  case RPLWhoIsOperator:
131  return "313";
132 
133  case RPLWhoIsIdle:
134  return "317";
135 
136  case RPLEndOfWhoIs:
137  return "318";
138 
139  case RPLWhoIsChannels:
140  return "319";
141 
142  case RPLWhoIsSpecial:
143  return "320";
144 
145  case RPLChannelUrl:
146  return "328";
147 
148  case RPLCreationTime:
149  return "329";
150 
151  case RPLWhoIsAccount:
152  return "330";
153 
154  case RPLTopic:
155  return "332";
156 
157  case RPLTopicWhoTime:
158  return "333";
159 
160  case RPLWhoIsBot:
161  return "335";
162 
163  case RPLWhoIsActually:
164  return "338";
165 
166  case RPLNamReply:
167  return "353";
168 
169  case RPLEndOfNames:
170  return "366";
171 
172  case RPLMOTD:
173  return "372";
174 
175  case RPLMOTDStart:
176  return "375";
177 
178  case RPLEndOfMOTD:
179  return "376";
180 
181  case RPLWhoIsHost:
182  return "378";
183 
184  case RPLWhoIsModes:
185  return "379";
186 
187  case ERRNoSuchNick:
188  return "401";
189 
190  case ERRCannotSendToChan:
191  return "404";
192 
194  return "432";
195 
196  case ERRNicknameInUse:
197  return "433";
198 
199  case ERRChannelIsFull:
200  return "471";
201 
202  case ERRInviteOnlyChan:
203  return "473";
204 
205  case ERRBannedFromChan:
206  return "474";
207 
208  case ERRBadChannelKey:
209  return "475";
210 
211  case ERRBadChannelMask:
212  return "476";
213 
214  case ERRNoChanModes:
215  return "477";
216 
218  return "482";
219 
220  case RPLWhoIsSecure:
221  return "671";
222 
223  case Join:
224  return "JOIN";
225 
226  case Kick:
227  return "KICK";
228 
229  case Kill:
230  return "KILL";
231 
232  case Mode:
233  return "MODE";
234 
235  case Nick:
236  return "NICK";
237 
238  case Notice:
239  return "NOTICE";
240 
241  case Part:
242  return "PART";
243 
244  case Ping:
245  return "PING";
246 
247  case PrivMsg:
248  return "PRIVMSG";
249 
250  case Quit:
251  return "QUIT";
252 
253  case Topic:
254  return "TOPIC";
255 
256  case Invalid:
257  default:
258  return QString();
259  }
260 }
261 
263 {
264  QString strTypeUpper = typeRepresentation.trimmed().toUpper();
265 
266  // Compare passed string against all known types.
267  for (int i = 0; i < NUM_TYPES; ++i)
268  {
269  MsgType enumType = (MsgType) i;
270  QString strCurrent = toRfcString(enumType);
271  if (strTypeUpper == strCurrent)
272  {
273  return enumType;
274  }
275  }
276 
277  return Invalid;
278 }
QString toRfcString() const
String representation of the message type.
375 - start of the message of the day
372 - message of the day
Not a real type, denotes number of all types.
254 - how many channels,
253 - how many unknown connections
366 - end of names list
251 - how many users on how many servers
320 - it&#39;s inconclusive what this code means.
Defines types of IRC network response message.
static IRCResponseType fromIntegerResponseValue(int responseType)
Creates IRCResponseType objects taking numeric value as the more important here.
static MsgType typeFromRfcString(const QString &typeRepresentation)
Returns MsgType basing on typeRepresentation.
353 - names list for a channel
376 - end of the message of the day
001 - sent when client connects.
005 - all sorts of server flags.
static int toRfcNumber(MsgType type)
If type can be represented as an integer, this will convert it.
MsgType
Represents types defined by RFC 1459.
307 - no idea what this is, but we&#39;ll treat it the same way we treat RPLWhoIsSpecial.
255 - how many clients on how many servers,
int numericType() const
If message type can be represented as number, this will contain its value.
IRCResponseType()
Initializes an invalid IRCResponseType object.
Type unknown to this IRC client.