1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include "common-export.h"
29 class COMMON_EXPORT IrcDecoder
33 * Parses an IRC message
34 * @param decode Decoder to be used for decoding the message
35 * @param rawMsg Raw Message
36 * @param tags[out] Parsed map of IRCv3 message tags
37 * @param prefix[out] Parsed prefix
38 * @param command[out] Parsed command
39 * @param parameters[out] Parsed list of parameters
41 static void parseMessage(const std::function<QString(const QByteArray&)>& decode, const QByteArray& raw, QHash<IrcTagKey, QString>& tags, QString& prefix, QString& command, QList<QByteArray>& parameters);
44 * Extracts a space-delimited fragment from an IRC message
45 * @param raw Raw Message
46 * @param start Current index into the message, will be advanced automatically
47 * @param end End of fragment, if already known. Default is -1, in which case it will be set to the next whitespace
48 * character or the end of the string
49 * @param prefix Required prefix. Default is 0. If set, this only parses a fragment if it starts with the given prefix.
52 static QByteArray extractFragment(const QByteArray& raw, int& start, int end = -1, char prefix = 0);
55 * Skips empty parts in the message
56 * @param raw Raw Message
57 * @param start Current index into the message, will be advanced automatically
59 static void skipEmptyParts(const QByteArray& raw, int& start);
62 * Parses an encoded IRCv3 message tag value
63 * @param value encoded IRCv3 message tag value
64 * @return decoded string
66 static QString parseTagValue(const QString& value);
68 * Parses IRCv3 message tags given a message
69 * @param net Decoder to be used for decoding the message
70 * @param raw Raw Message
71 * @param start Current index into the message, will be advanced automatically
72 * @return Parsed message tags
74 static QHash<IrcTagKey, QString> parseTags(const std::function<QString(const QByteArray&)>& decode, const QByteArray& raw, int& start);
76 * Parses an IRC prefix, if available
77 * @param net Decoder to be used for decoding the message
78 * @param raw Raw Message
79 * @param start Current index into the message, will be advanced automatically
80 * @return Parsed prefix or empty string
82 static QString parsePrefix(const std::function<QString(const QByteArray&)>& decode, const QByteArray& raw, int& start);
84 * Parses an IRC named command or numeric RPL
85 * @param net Decoder to be used for decoding the message
86 * @param raw Raw Message
87 * @param start Current index into the message, will be advanced automatically
88 * @return Parsed command
90 static QString parseCommand(const std::function<QString(const QByteArray&)>& decode, const QByteArray& raw, int& start);
92 * Parses an IRC parameter
93 * @param net Decoder to be used for decoding the message
94 * @param raw Raw Message
95 * @param start Current index into the message, will be advanced automatically
96 * @return Parsed parameter
98 static QByteArray parseParameter(const QByteArray& raw, int& start);