ircresponsetype.cpp
1 //------------------------------------------------------------------------------
2 // ircresponsetype.cpp
3 //
4 // Copyright (C) 2011 "Zalewa" <zalewapl@gmail.com>
5 //------------------------------------------------------------------------------
6 #include "ircresponsetype.h"
7 
9 {
10  d.type = Invalid;
11  d.numericType = -1;
12 }
13 
15 {
16  d.type = type;
17  d.numericType = toRfcNumber(type);
18 }
19 
21 {
22  d.type = typeFromRfcString(type);
23 
24  // Get numeric type from string.
25  bool bOk = false;
26  int numericType = type.toInt(&bOk);
27  if (!bOk)
28  {
29  numericType = -1;
30  }
31  d.numericType = numericType;
32 }
33 
35 {
36  // Response type is always a 3-digit number. Conver the passed integer with
37  // leading zeros if necessary and then pass it to typeFromRfcString().
38 
39  // The result here is d.numericType always set to responseType, and the
40  // d.type set to either Invalid or appropriate value if the number was
41  // recognized.
42  QString strResponseType = QString("%1").arg(responseType, 3, 10, QChar('0'));
43 
44  IRCResponseType newIRCResponseType;
45  newIRCResponseType.d.type = typeFromRfcString(strResponseType);
46  newIRCResponseType.d.numericType = responseType;
47 
48  return newIRCResponseType;
49 }
50 
51 bool IRCResponseType::operator==(const IRCResponseType& other) const
52 {
53  return d.type == other.d.type;
54 }
55 
56 bool IRCResponseType::operator!=(const IRCResponseType& other) const
57 {
58  return !((*this) == other);
59 }
60 
62 {
63  if (type == Invalid)
64  {
65  return -1;
66  }
67 
68  QString str = toRfcString(type);
69 
70  bool bOk = false;
71  int val = str.toInt(&bOk);
72 
73  return bOk ? val : -1;
74 }
75 
77 {
78  switch (type)
79  {
80  case HelloClient:
81  return "001";
82 
83  case RPLISupport:
84  return "005";
85 
86  case RPLLUserClient:
87  return "251";
88 
89  case RPLLUserOp:
90  return "252";
91 
92  case RPLLUserUnknown:
93  return "253";
94 
95  case RPLLUserChannels:
96  return "254";
97 
98  case RPLLUserMe:
99  return "255";
100 
101  case RPLAway:
102  return "301";
103 
104  case RPLWhoIsRegnick:
105  return "307";
106 
107  case RPLWhoIsUser:
108  return "311";
109 
110  case RPLWhoIsServer:
111  return "312";
112 
113  case RPLWhoIsOperator:
114  return "313";
115 
116  case RPLWhoIsIdle:
117  return "317";
118 
119  case RPLEndOfWhoIs:
120  return "318";
121 
122  case RPLWhoIsChannels:
123  return "319";
124 
125  case RPLWhoIsSpecial:
126  return "320";
127 
128  case RPLChannelUrl:
129  return "328";
130 
131  case RPLCreationTime:
132  return "329";
133 
134  case RPLWhoIsAccount:
135  return "330";
136 
137  case RPLTopic:
138  return "332";
139 
140  case RPLTopicWhoTime:
141  return "333";
142 
143  case RPLWhoIsBot:
144  return "335";
145 
146  case RPLWhoIsActually:
147  return "338";
148 
149  case RPLNamReply:
150  return "353";
151 
152  case RPLEndOfNames:
153  return "366";
154 
155  case RPLMOTD:
156  return "372";
157 
158  case RPLMOTDStart:
159  return "375";
160 
161  case RPLEndOfMOTD:
162  return "376";
163 
164  case RPLWhoIsHost:
165  return "378";
166 
167  case RPLWhoIsModes:
168  return "379";
169 
170  case ERRNoSuchNick:
171  return "401";
172 
173  case ERRCannotSendToChan:
174  return "404";
175 
177  return "432";
178 
179  case ERRNicknameInUse:
180  return "433";
181 
182  case ERRChannelIsFull:
183  return "471";
184 
185  case ERRInviteOnlyChan:
186  return "473";
187 
188  case ERRBannedFromChan:
189  return "474";
190 
191  case ERRBadChannelKey:
192  return "475";
193 
194  case ERRBadChannelMask:
195  return "476";
196 
197  case ERRNoChanModes:
198  return "477";
199 
201  return "482";
202 
203  case RPLWhoIsSecure:
204  return "671";
205 
206  case Join:
207  return "JOIN";
208 
209  case Kick:
210  return "KICK";
211 
212  case Kill:
213  return "KILL";
214 
215  case Mode:
216  return "MODE";
217 
218  case Nick:
219  return "NICK";
220 
221  case Notice:
222  return "NOTICE";
223 
224  case Part:
225  return "PART";
226 
227  case Ping:
228  return "PING";
229 
230  case PrivMsg:
231  return "PRIVMSG";
232 
233  case Quit:
234  return "QUIT";
235 
236  case Topic:
237  return "TOPIC";
238 
239  case Invalid:
240  default:
241  return QString();
242  }
243 }
244 
246 {
247  QString strTypeUpper = typeRepresentation.trimmed().toUpper();
248 
249  // Compare passed string against all known types.
250  for (int i = 0; i < NUM_TYPES; ++i)
251  {
252  MsgType enumType = (MsgType) i;
253  QString strCurrent = toRfcString(enumType);
254  if (strTypeUpper == strCurrent)
255  {
256  return enumType;
257  }
258  }
259 
260  return Invalid;
261 }
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.