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