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