ircisupportparser.cpp
1 //------------------------------------------------------------------------------
2 // ircisupportparser.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
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 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  foreach (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  foreach (const QString &element, d->iSupportParams)
74  {
75  if (element.startsWith(key))
76  {
77  return element.mid(key.length());
78  }
79  }
80  return QString();
81 }
82 
83 void IRCISupportParser::parse()
84 {
86  parsePrefix();
87 }
88 
89 void IRCISupportParser::parsePrefix()
90 {
91  QString value = findValue("PREFIX=");
92  if (value.isNull())
93  {
94  return;
95  }
96  QRegExp regex("\\((\\S+)\\)(\\S+)$");
97  regex.setMinimal(true);
98  if (regex.indexIn(value) < 0)
99  {
100  return;
101  }
102  QByteArray modes = regex.cap(1).toUtf8();
103  QByteArray prefixes = regex.cap(2).toUtf8();
104  if (modes.size() != prefixes.size())
105  {
106  return;
107  }
108  IRCUserPrefix userPrefixes;
109  for (int i = 0; i < modes.size(); ++i)
110  {
111  userPrefixes.assignPrefix(modes[i], prefixes[i]);
112  }
113  d->state.userPrefixes = userPrefixes;
114 }
115 
116 const IRCUserPrefix &IRCISupportParser::userPrefixes() const
117 {
118  return d->state.userPrefixes;
119 }
static IRCUserPrefix mkDefault()
User modes: op, half-op and voice.
Definition: dptr.h:31
One-to-one association of visible prefixes to user mode.
Definition: ircuserprefix.h:37