ircisupportparser.cpp
1 //------------------------------------------------------------------------------
2 // ircisupportparser.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 "ircisupportparser.h"
24 
25 #include "irc/entities/ircuserprefix.h"
26 #include <QByteArray>
27 #include <QRegularExpression>
28 #include <QStringList>
29 
30 DClass<IRCISupportParser>
31 {
32 public:
33  class State
34  {
35  public:
36  IRCUserPrefix userPrefixes;
37 
38  State()
39  {
40  userPrefixes = IRCUserPrefix::mkDefault();
41  }
42  };
43 
44  QStringList iSupportParams;
45  State state;
46 };
47 
48 DPointered(IRCISupportParser)
49 
51 {
52 }
53 
54 IRCISupportParser::~IRCISupportParser()
55 {
56 }
57 
58 void IRCISupportParser::appendLine(const QString &line)
59 {
60  for (const QString &element : line.split(" "))
61  {
62  if (element == ":are")
63  {
64  // Start of ":are supported by this server".
65  // We don't need this.
66  break;
67  }
68  d->iSupportParams << element;
69  }
70 }
71 
72 QString IRCISupportParser::findValue(const QString &key)
73 {
74  for (const QString &element : d->iSupportParams)
75  {
76  if (element.startsWith(key))
77  return element.mid(key.length());
78  }
79  return QString();
80 }
81 
82 void IRCISupportParser::parse()
83 {
85  parsePrefix();
86 }
87 
88 void IRCISupportParser::parsePrefix()
89 {
90  QString value = findValue("PREFIX=");
91  if (value.isNull())
92  return;
93  QRegularExpression regex(R"(\((\S+)\)(\S+)$)", QRegularExpression::InvertedGreedinessOption);
94  auto match = regex.match(value);
95  if (!match.hasMatch())
96  return;
97  QByteArray modes = match.captured(1).toUtf8();
98  QByteArray prefixes = match.captured(2).toUtf8();
99  if (modes.size() != prefixes.size())
100  return;
101  IRCUserPrefix userPrefixes;
102  for (int i = 0; i < modes.size(); ++i)
103  userPrefixes.assignPrefix(modes[i], prefixes[i]);
104  d->state.userPrefixes = userPrefixes;
105 }
106 
107 const IRCUserPrefix &IRCISupportParser::userPrefixes() const
108 {
109  return d->state.userPrefixes;
110 }