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 "irc/ircnetworkadapter.h"
26 #include "version.h"
27 #include <QDateTime>
28 #include <QDebug>
29 #include <QStringList>
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  return false;
77  return d->msg[0].unicode() == 0x1 && d->msg[d->msg.length() - 1].unicode() == 0x1;
78 }
79 
80 bool IRCCtcpParser::parse()
81 {
82  if (!isCtcp())
83  return false;
84  tokenizeMsg();
85  d->printable = tr("CTCP %1: [%2] %3 %4").arg(typeToName(), d->sender, d->command, d->params.join(" "));
86  if (isCommand("action"))
87  {
88  d->echo = PrintAsNormalMessage;
89  d->printable = tr("%1 %2").arg(d->sender, d->params.join(" "));
90  }
91  else
92  {
93  if (d->msgType == Request)
94  {
95  d->echo = DisplayInServerTab;
96  if (isCommand("clientinfo"))
97  d->reply = "CLIENTINFO ACTION VERSION TIME PING";
98  else if (isCommand("version"))
99  d->reply = QString("VERSION %1").arg(Version::fullVersionInfoWithOs());
100  else if (isCommand("time"))
101  d->reply = QString("TIME %1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
102  else if (isCommand("ping"))
103  d->reply = QString("PING %1").arg(d->params[0]);
104  }
105  else if (d->msgType == Reply)
106  {
107  d->echo = DisplayThroughGlobalMessage;
108  if (isCommand("ping"))
109  {
110  // "ping" CTCP is echoed in server tab as further processing
111  // displays user ping in a customized message to currently open
112  // network window.
113  d->echo = DisplayInServerTab;
114  qint64 timestamp = d->params.takeFirst().toLongLong();
115  d->network->userPing(d->sender, timestamp);
116  }
117  }
118  else
119  qDebug() << "Unknown d->msgType in IRCCtcpParser";
120  }
121  return true;
122 }
123 
124 const QString &IRCCtcpParser::printable() const
125 {
126  return d->printable;
127 }
128 
129 const QString &IRCCtcpParser::reply() const
130 {
131  return d->reply;
132 }
133 
134 void IRCCtcpParser::tokenizeMsg()
135 {
136  QString stripped = d->msg.mid(1, d->msg.length() - 2);
137  QStringList tokens = stripped.split(" ");
138  d->command = tokens.takeFirst();
139  d->params = tokens;
140 }
141 
142 QString IRCCtcpParser::typeToName() const
143 {
144  switch (d->msgType)
145  {
146  case Request:
147  return tr("REQUEST");
148  case Reply:
149  return tr("REPLY");
150  default:
151  return tr("????");
152  }
153 }