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