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 RPLWhoIsAccount:
129  return "330";
130 
131  case RPLTopic:
132  return "332";
133 
134  case RPLTopicWhoTime:
135  return "333";
136 
137  case RPLWhoIsBot:
138  return "335";
139 
140  case RPLNamReply:
141  return "353";
142 
143  case RPLEndOfNames:
144  return "366";
145 
146  case RPLMOTD:
147  return "372";
148 
149  case RPLMOTDStart:
150  return "375";
151 
152  case RPLEndOfMOTD:
153  return "376";
154 
155  case RPLWhoIsHost:
156  return "378";
157 
158  case RPLWhoIsModes:
159  return "379";
160 
161  case ERRNoSuchNick:
162  return "401";
163 
164  case ERRCannotSendToChan:
165  return "404";
166 
168  return "432";
169 
170  case ERRNicknameInUse:
171  return "433";
172 
174  return "482";
175 
176  case RPLWhoIsSecure:
177  return "671";
178 
179  case Join:
180  return "JOIN";
181 
182  case Kick:
183  return "KICK";
184 
185  case Kill:
186  return "KILL";
187 
188  case Mode:
189  return "MODE";
190 
191  case Nick:
192  return "NICK";
193 
194  case Notice:
195  return "NOTICE";
196 
197  case Part:
198  return "PART";
199 
200  case Ping:
201  return "PING";
202 
203  case PrivMsg:
204  return "PRIVMSG";
205 
206  case Quit:
207  return "QUIT";
208 
209  case Topic:
210  return "TOPIC";
211 
212  case Invalid:
213  default:
214  return QString();
215  }
216 }
217 
219 {
220  QString strTypeUpper = typeRepresentation.trimmed().toUpper();
221 
222  // Compare passed string against all known types.
223  for (int i = 0; i < NUM_TYPES; ++i)
224  {
225  MsgType enumType = (MsgType) i;
226  QString strCurrent = toRfcString(enumType);
227  if (strTypeUpper == strCurrent)
228  {
229  return enumType;
230  }
231  }
232 
233  return Invalid;
234 }
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'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'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.