Fixes #960 - "Core crashes after deleting a Network without disconnecting the Network...
[quassel.git] / src / core / corebasichandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include "corebasichandler.h"
21
22 #include "util.h"
23 #include "logger.h"
24
25 CoreBasicHandler::CoreBasicHandler(CoreNetwork *parent)
26   : BasicHandler(parent),
27     _network(parent)
28 {
29   connect(this, SIGNAL(displayMsg(Message::Type, BufferInfo::Type, const QString &, const QString &, const QString &, Message::Flags)),
30          network(), SLOT(displayMsg(Message::Type, BufferInfo::Type, const QString &, const QString &, const QString &, Message::Flags)));
31
32   connect(this, SIGNAL(putCmd(QString, const QList<QByteArray> &, const QByteArray &)),
33     network(), SLOT(putCmd(QString, const QList<QByteArray> &, const QByteArray &)));
34
35   connect(this, SIGNAL(putRawLine(const QByteArray &)),
36           network(), SLOT(putRawLine(const QByteArray &)));
37 }
38
39 QString CoreBasicHandler::serverDecode(const QByteArray &string) {
40   return network()->serverDecode(string);
41 }
42
43 QStringList CoreBasicHandler::serverDecode(const QList<QByteArray> &stringlist) {
44   QStringList list;
45   foreach(QByteArray s, stringlist) list << network()->serverDecode(s);
46   return list;
47 }
48
49 QString CoreBasicHandler::channelDecode(const QString &bufferName, const QByteArray &string) {
50   return network()->channelDecode(bufferName, string);
51 }
52
53 QStringList CoreBasicHandler::channelDecode(const QString &bufferName, const QList<QByteArray> &stringlist) {
54   QStringList list;
55   foreach(QByteArray s, stringlist) list << network()->channelDecode(bufferName, s);
56   return list;
57 }
58
59 QString CoreBasicHandler::userDecode(const QString &userNick, const QByteArray &string) {
60   return network()->userDecode(userNick, string);
61 }
62
63 QStringList CoreBasicHandler::userDecode(const QString &userNick, const QList<QByteArray> &stringlist) {
64   QStringList list;
65   foreach(QByteArray s, stringlist) list << network()->userDecode(userNick, s);
66   return list;
67 }
68
69 /*** ***/
70
71 QByteArray CoreBasicHandler::serverEncode(const QString &string) {
72   return network()->serverEncode(string);
73 }
74
75 QList<QByteArray> CoreBasicHandler::serverEncode(const QStringList &stringlist) {
76   QList<QByteArray> list;
77   foreach(QString s, stringlist) list << network()->serverEncode(s);
78   return list;
79 }
80
81 QByteArray CoreBasicHandler::channelEncode(const QString &bufferName, const QString &string) {
82   return network()->channelEncode(bufferName, string);
83 }
84
85 QList<QByteArray> CoreBasicHandler::channelEncode(const QString &bufferName, const QStringList &stringlist) {
86   QList<QByteArray> list;
87   foreach(QString s, stringlist) list << network()->channelEncode(bufferName, s);
88   return list;
89 }
90
91 QByteArray CoreBasicHandler::userEncode(const QString &userNick, const QString &string) {
92   return network()->userEncode(userNick, string);
93 }
94
95 QList<QByteArray> CoreBasicHandler::userEncode(const QString &userNick, const QStringList &stringlist) {
96   QList<QByteArray> list;
97   foreach(QString s, stringlist) list << network()->userEncode(userNick, s);
98   return list;
99 }
100
101 // ====================
102 //  protected:
103 // ====================
104 BufferInfo::Type CoreBasicHandler::typeByTarget(const QString &target) const {
105   if(target.isEmpty())
106     return BufferInfo::StatusBuffer;
107
108   if(network()->isChannelName(target))
109     return BufferInfo::ChannelBuffer;
110
111   return BufferInfo::QueryBuffer;
112 }
113
114 void CoreBasicHandler::putCmd(const QString &cmd, const QByteArray &param, const QByteArray &prefix) {
115   QList<QByteArray> list;
116   list << param;
117   emit putCmd(cmd, list, prefix);
118 }
119
120 void CoreBasicHandler::displayMsg(Message::Type msgType, QString target, const QString &text, const QString &sender, Message::Flags flags) {
121   IrcChannel *channel = network()->ircChannel(target);
122   if(!channel) {
123     if(!target.isEmpty() && network()->prefixes().contains(target[0]))
124       target = target.mid(1);
125
126     if(target.startsWith('$') || target.startsWith('#'))
127       target = nickFromMask(sender);
128   }
129
130   emit displayMsg(msgType, typeByTarget(target), target, text, sender, flags);
131 }