ircctcpparser.cpp
1 //------------------------------------------------------------------------------
2 // ircctcpparser.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircctcpparser.h"
24 
25 #include <QDateTime>
26 #include <QDebug>
27 #include <QStringList>
28 #include "irc/ircnetworkadapter.h"
29 #include "version.h"
30 
31 DClass<IRCCtcpParser>
32 {
33  public:
34  QString command;
35  IRCCtcpParser::CtcpEcho echo;
37  QString msg;
38  IRCNetworkAdapter *network;
39  QStringList params;
40  QString printable;
41  QString recipient;
42  QString reply;
43  QString sender;
44 };
45 
46 DPointered(IRCCtcpParser)
47 
48 IRCCtcpParser::IRCCtcpParser(IRCNetworkAdapter *network, const QString &sender,
49  const QString &recipient, const QString &msg, MessageType msgType)
50 {
51  d->echo = DontShow;
52  d->msg = msg;
53  d->msgType = msgType;
54  d->network = network;
55  d->recipient = recipient;
56  d->sender = sender;
57 }
58 
59 IRCCtcpParser::~IRCCtcpParser()
60 {
61 }
62 
63 IRCCtcpParser::CtcpEcho IRCCtcpParser::echo() const
64 {
65  return d->echo;
66 }
67 
68 bool IRCCtcpParser::isCommand(const QString &candidate)
69 {
70  return d->command.compare(candidate, Qt::CaseInsensitive) == 0;
71 }
72 
73 bool IRCCtcpParser::isCtcp() const
74 {
75  if (d->msg.length() <= 2)
76  {
77  return false;
78  }
79  return d->msg[0].unicode() == 0x1 && d->msg[d->msg.length() - 1].unicode() == 0x1;
80 }
81 
82 bool IRCCtcpParser::parse()
83 {
84  if (!isCtcp())
85  {
86  return false;
87  }
88  tokenizeMsg();
89  d->printable = tr("CTCP %1: [%2] %3 %4").arg(typeToName(), d->sender, d->command, d->params.join(" "));
90  if (isCommand("action"))
91  {
92  d->echo = PrintAsNormalMessage;
93  d->printable = tr("%1 %2").arg(d->sender, d->params.join(" "));
94  }
95  else
96  {
97  if (d->msgType == Request)
98  {
99  d->echo = DisplayInServerTab;
100  if (isCommand("clientinfo"))
101  {
102  d->reply = "CLIENTINFO ACTION VERSION TIME PING";
103  }
104  else if (isCommand("version"))
105  {
106  d->reply = QString("VERSION %1").arg(Version::fullVersionInfoWithOs());
107  }
108  else if (isCommand("time"))
109  {
110  d->reply = QString("TIME %1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
111  }
112  else if (isCommand("ping"))
113  {
114  d->reply = QString("PING %1").arg(d->params[0]);
115  }
116  }
117  else if (d->msgType == Reply)
118  {
119  d->echo = DisplayThroughGlobalMessage;
120  if (isCommand("ping"))
121  {
122  // "ping" CTCP is echoed in server tab as further processing
123  // displays user ping in a customized message to currently open
124  // network window.
125  d->echo = DisplayInServerTab;
126  qint64 timestamp = d->params.takeFirst().toLongLong();
127  d->network->userPing(d->sender, timestamp);
128  }
129  }
130  else
131  {
132  qDebug() << "Unknown d->msgType in IRCCtcpParser";
133  }
134  }
135  return true;
136 }
137 
138 const QString &IRCCtcpParser::printable() const
139 {
140  return d->printable;
141 }
142 
143 const QString &IRCCtcpParser::reply() const
144 {
145  return d->reply;
146 }
147 
148 void IRCCtcpParser::tokenizeMsg()
149 {
150  QString stripped = d->msg.mid(1, d->msg.length() - 2);
151  QStringList tokens = stripped.split(" ");
152  d->command = tokens.takeFirst();
153  d->params = tokens;
154 }
155 
156 QString IRCCtcpParser::typeToName() const
157 {
158  switch (d->msgType)
159  {
160  case Request:
161  return tr("REQUEST");
162  case Reply:
163  return tr("REPLY");
164  default:
165  return tr("????");
166  }
167 }
Interprets communication between the client and the IRC server.
An answer is being sent through NOTICE.
Definition: ircctcpparser.h:55
A question is being asked through PRIVMSG.
Definition: ircctcpparser.h:51
static QString fullVersionInfoWithOs()
Combines program&#39;s name, versionRevision() and operatingSystem().
Definition: version.h:50