PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/ctcpparser.cpp

https://github.com/sandsmark/quassel-web
C++ | 346 lines | 254 code | 62 blank | 30 comment | 63 complexity | e699eec40c8213c48894c0a4a6cd3437 MD5 | raw file
  1. /***************************************************************************
  2. * Copyright (C) 2005-2013 by the Quassel Project *
  3. * devel@quassel-irc.org *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) version 3. *
  9. * *
  10. * This program 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 *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
  19. ***************************************************************************/
  20. #include "ctcpparser.h"
  21. #include "corenetworkconfig.h"
  22. #include "coresession.h"
  23. #include "ctcpevent.h"
  24. #include "messageevent.h"
  25. const QByteArray XDELIM = "\001";
  26. CtcpParser::CtcpParser(CoreSession *coreSession, QObject *parent)
  27. : QObject(parent),
  28. _coreSession(coreSession)
  29. {
  30. QByteArray MQUOTE = QByteArray("\020");
  31. _ctcpMDequoteHash[MQUOTE + '0'] = QByteArray(1, '\000');
  32. _ctcpMDequoteHash[MQUOTE + 'n'] = QByteArray(1, '\n');
  33. _ctcpMDequoteHash[MQUOTE + 'r'] = QByteArray(1, '\r');
  34. _ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
  35. setStandardCtcp(_coreSession->networkConfig()->standardCtcp());
  36. connect(_coreSession->networkConfig(), SIGNAL(standardCtcpSet(bool)), this, SLOT(setStandardCtcp(bool)));
  37. connect(this, SIGNAL(newEvent(Event *)), _coreSession->eventManager(), SLOT(postEvent(Event *)));
  38. }
  39. void CtcpParser::setStandardCtcp(bool enabled)
  40. {
  41. QByteArray XQUOTE = QByteArray("\134");
  42. if (enabled)
  43. _ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
  44. else
  45. _ctcpXDelimDequoteHash.remove(XQUOTE + XQUOTE);
  46. _ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
  47. }
  48. void CtcpParser::displayMsg(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
  49. const QString &target, Message::Flags msgFlags)
  50. {
  51. if (event->testFlag(EventManager::Silent))
  52. return;
  53. MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
  54. msgEvent->setTimestamp(event->timestamp());
  55. emit newEvent(msgEvent);
  56. }
  57. QByteArray CtcpParser::lowLevelQuote(const QByteArray &message)
  58. {
  59. QByteArray quotedMessage = message;
  60. QHash<QByteArray, QByteArray> quoteHash = _ctcpMDequoteHash;
  61. QByteArray MQUOTE = QByteArray("\020");
  62. quoteHash.remove(MQUOTE + MQUOTE);
  63. quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
  64. QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
  65. while (quoteIter != quoteHash.constEnd()) {
  66. quotedMessage.replace(quoteIter.value(), quoteIter.key());
  67. quoteIter++;
  68. }
  69. return quotedMessage;
  70. }
  71. QByteArray CtcpParser::lowLevelDequote(const QByteArray &message)
  72. {
  73. QByteArray dequotedMessage;
  74. QByteArray messagepart;
  75. QHash<QByteArray, QByteArray>::iterator ctcpquote;
  76. // copy dequote Message
  77. for (int i = 0; i < message.size(); i++) {
  78. messagepart = message.mid(i, 1);
  79. if (i+1 < message.size()) {
  80. for (ctcpquote = _ctcpMDequoteHash.begin(); ctcpquote != _ctcpMDequoteHash.end(); ++ctcpquote) {
  81. if (message.mid(i, 2) == ctcpquote.key()) {
  82. messagepart = ctcpquote.value();
  83. ++i;
  84. break;
  85. }
  86. }
  87. }
  88. dequotedMessage += messagepart;
  89. }
  90. return dequotedMessage;
  91. }
  92. QByteArray CtcpParser::xdelimQuote(const QByteArray &message)
  93. {
  94. QByteArray quotedMessage = message;
  95. QHash<QByteArray, QByteArray>::const_iterator quoteIter = _ctcpXDelimDequoteHash.constBegin();
  96. while (quoteIter != _ctcpXDelimDequoteHash.constEnd()) {
  97. quotedMessage.replace(quoteIter.value(), quoteIter.key());
  98. quoteIter++;
  99. }
  100. return quotedMessage;
  101. }
  102. QByteArray CtcpParser::xdelimDequote(const QByteArray &message)
  103. {
  104. QByteArray dequotedMessage;
  105. QByteArray messagepart;
  106. QHash<QByteArray, QByteArray>::iterator xdelimquote;
  107. for (int i = 0; i < message.size(); i++) {
  108. messagepart = message.mid(i, 1);
  109. if (i+1 < message.size()) {
  110. for (xdelimquote = _ctcpXDelimDequoteHash.begin(); xdelimquote != _ctcpXDelimDequoteHash.end(); ++xdelimquote) {
  111. if (message.mid(i, 2) == xdelimquote.key()) {
  112. messagepart = xdelimquote.value();
  113. i++;
  114. break;
  115. }
  116. }
  117. }
  118. dequotedMessage += messagepart;
  119. }
  120. return dequotedMessage;
  121. }
  122. void CtcpParser::processIrcEventRawNotice(IrcEventRawMessage *event)
  123. {
  124. parse(event, Message::Notice);
  125. }
  126. void CtcpParser::processIrcEventRawPrivmsg(IrcEventRawMessage *event)
  127. {
  128. parse(event, Message::Plain);
  129. }
  130. void CtcpParser::parse(IrcEventRawMessage *e, Message::Type messagetype)
  131. {
  132. //lowlevel message dequote
  133. QByteArray dequotedMessage = lowLevelDequote(e->rawMessage());
  134. CtcpEvent::CtcpType ctcptype = e->type() == EventManager::IrcEventRawNotice
  135. ? CtcpEvent::Reply
  136. : CtcpEvent::Query;
  137. Message::Flags flags = (ctcptype == CtcpEvent::Reply && !e->network()->isChannelName(e->target()))
  138. ? Message::Redirected
  139. : Message::None;
  140. if (coreSession()->networkConfig()->standardCtcp())
  141. parseStandard(e, messagetype, dequotedMessage, ctcptype, flags);
  142. else
  143. parseSimple(e, messagetype, dequotedMessage, ctcptype, flags);
  144. }
  145. // only accept CTCPs in their simplest form, i.e. one ctcp, from start to
  146. // end, no text around it; not as per the 'specs', but makes people happier
  147. void CtcpParser::parseSimple(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
  148. {
  149. if (dequotedMessage.count(XDELIM) != 2 || dequotedMessage[0] != '\001' || dequotedMessage[dequotedMessage.count() -1] != '\001') {
  150. displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
  151. } else {
  152. int spacePos = -1;
  153. QString ctcpcmd, ctcpparam;
  154. QByteArray ctcp = xdelimDequote(dequotedMessage.mid(1, dequotedMessage.count() - 2));
  155. spacePos = ctcp.indexOf(' ');
  156. if (spacePos != -1) {
  157. ctcpcmd = targetDecode(e, ctcp.left(spacePos));
  158. ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
  159. } else {
  160. ctcpcmd = targetDecode(e, ctcp);
  161. ctcpparam = QString();
  162. }
  163. ctcpcmd = ctcpcmd.toUpper();
  164. // we don't want to block /me messages by the CTCP ignore list
  165. if (ctcpcmd == QLatin1String("ACTION") || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
  166. QUuid uuid = QUuid::createUuid();
  167. _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
  168. CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
  169. ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
  170. emit newEvent(event);
  171. CtcpEvent *flushEvent = new CtcpEvent(EventManager::CtcpEventFlush, e->network(), e->prefix(), e->target(),
  172. ctcptype, "INVALID", QString(), e->timestamp(), uuid);
  173. emit newEvent(flushEvent);
  174. }
  175. }
  176. }
  177. void CtcpParser::parseStandard(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
  178. {
  179. QByteArray ctcp;
  180. QList<CtcpEvent *> ctcpEvents;
  181. QUuid uuid; // needed to group all replies together
  182. // extract tagged / extended data
  183. int xdelimPos = -1;
  184. int xdelimEndPos = -1;
  185. int spacePos = -1;
  186. while ((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
  187. if (xdelimPos > 0)
  188. displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
  189. xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
  190. if (xdelimEndPos == -1) {
  191. // no matching end delimiter found... treat rest of the message as ctcp
  192. xdelimEndPos = dequotedMessage.count();
  193. }
  194. ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
  195. dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
  196. //dispatch the ctcp command
  197. QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
  198. QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
  199. spacePos = ctcp.indexOf(' ');
  200. if (spacePos != -1) {
  201. ctcpcmd = targetDecode(e, ctcp.left(spacePos));
  202. ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
  203. }
  204. else {
  205. ctcpcmd = targetDecode(e, ctcp);
  206. ctcpparam = QString();
  207. }
  208. ctcpcmd = ctcpcmd.toUpper();
  209. // we don't want to block /me messages by the CTCP ignore list
  210. if (ctcpcmd == QLatin1String("ACTION") || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
  211. if (uuid.isNull())
  212. uuid = QUuid::createUuid();
  213. CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
  214. ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
  215. ctcpEvents << event;
  216. }
  217. }
  218. if (!ctcpEvents.isEmpty()) {
  219. _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
  220. CtcpEvent *flushEvent = new CtcpEvent(EventManager::CtcpEventFlush, e->network(), e->prefix(), e->target(),
  221. ctcptype, "INVALID", QString(), e->timestamp(), uuid);
  222. ctcpEvents << flushEvent;
  223. foreach(CtcpEvent *event, ctcpEvents) {
  224. emit newEvent(event);
  225. }
  226. }
  227. if (!dequotedMessage.isEmpty())
  228. displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
  229. }
  230. void CtcpParser::sendCtcpEvent(CtcpEvent *e)
  231. {
  232. CoreNetwork *net = coreNetwork(e);
  233. if (e->type() == EventManager::CtcpEvent) {
  234. QByteArray quotedReply;
  235. QString bufname = nickFromMask(e->prefix());
  236. if (e->ctcpType() == CtcpEvent::Query && !e->reply().isNull()) {
  237. if (_replies.contains(e->uuid()))
  238. _replies[e->uuid()].replies << lowLevelQuote(pack(net->serverEncode(e->ctcpCmd()),
  239. net->userEncode(bufname, e->reply())));
  240. else
  241. // reply not caused by a request processed in here, so send it off immediately
  242. reply(net, bufname, e->ctcpCmd(), e->reply());
  243. }
  244. }
  245. else if (e->type() == EventManager::CtcpEventFlush && _replies.contains(e->uuid())) {
  246. CtcpReply reply = _replies.take(e->uuid());
  247. if (reply.replies.count())
  248. packedReply(net, reply.bufferName, reply.replies);
  249. }
  250. }
  251. QByteArray CtcpParser::pack(const QByteArray &ctcpTag, const QByteArray &message)
  252. {
  253. if (message.isEmpty())
  254. return XDELIM + ctcpTag + XDELIM;
  255. return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
  256. }
  257. void CtcpParser::query(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
  258. {
  259. QList<QByteArray> params;
  260. params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
  261. net->putCmd("PRIVMSG", params);
  262. }
  263. void CtcpParser::reply(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
  264. {
  265. QList<QByteArray> params;
  266. params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
  267. net->putCmd("NOTICE", params);
  268. }
  269. void CtcpParser::packedReply(CoreNetwork *net, const QString &bufname, const QList<QByteArray> &replies)
  270. {
  271. QList<QByteArray> params;
  272. int answerSize = 0;
  273. for (int i = 0; i < replies.count(); i++) {
  274. answerSize += replies.at(i).size();
  275. }
  276. QByteArray quotedReply;
  277. quotedReply.reserve(answerSize);
  278. for (int i = 0; i < replies.count(); i++) {
  279. quotedReply.append(replies.at(i));
  280. }
  281. params << net->serverEncode(bufname) << quotedReply;
  282. // FIXME user proper event
  283. net->putCmd("NOTICE", params);
  284. }