912c36239d79f6ca67aa4a327627fe83fe189468
[quassel.git] / network / messages.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 by The Quassel Team                             *
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) 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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "messages.h"
22 #include <QtDebug>
23
24 extern BuiltinCmd builtins[];
25
26 recvHandlerType Message::defaultRecvHandler;
27 sendHandlerType Message::defaultSendHandler;
28 QHash<QString, CmdType> Message::cmdTypes;
29
30 Message::Message(QString _cmd, QStringList args) {
31   cmd = _cmd; qDebug() << "cmd: " << cmd;
32   params = args;
33 }
34
35 void Message::init(recvHandlerType _r, sendHandlerType _s) {
36   defaultRecvHandler = _r;
37   defaultSendHandler = _s;
38
39   // Register builtin commands
40   for(int i = 0; ; i++) {
41     if(builtins[i].cmd.isEmpty()) break;
42     CmdType c;
43     c.cmd = builtins[i].cmd.toLower();
44     c.cmdDescr = builtins[i].cmdDescr;
45     c.args = builtins[i].args;
46     c.argsDescr = builtins[i].argsDescr;
47     c.recvHandler = ( builtins[i].recvHandler ? builtins[i].recvHandler : defaultRecvHandler);
48     c.sendHandler = ( builtins[i].sendHandler ? builtins[i].sendHandler : defaultSendHandler);
49     cmdTypes.insert(c.cmd, c);
50   }
51
52 }
53
54 recvHandlerType Message::getRecvHandler() {
55   CmdType c = cmdTypes[cmd];
56   if(c.recvHandler) return c.recvHandler;
57   qDebug() << "No recvHandler defined for " << cmd << "!";
58   return 0;
59 }
60
61 sendHandlerType Message::getSendHandler() {
62   CmdType c = cmdTypes[cmd];
63   if(c.sendHandler) return c.sendHandler;
64   qDebug() << "No sendHandler defined for " << cmd << "!";
65   return 0;
66 }
67
68 /*
69 void Message::parseParams(QString str) {
70   QString left = str.section(':', 0, 0);
71   QString trailing = str.section(':', 1);
72   if(left.size()) {
73     params << left.split(' ', QString::SkipEmptyParts);
74   }
75   if(trailing.size()) {
76     params << trailing;
77   }
78   qDebug() << params;
79
80 }
81 */