summaryrefslogtreecommitdiffstats
path: root/src/kvirc/sparser/kvi_sparser.cpp
blob: b06267eccf212643975d0138dcdb6f2f9f04417c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//=============================================================================
//
//   File : kvi_sparser.cpp
//   Creation date : Sun Jun 30 2000 03:25:17 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#define __KVIRC__
#define _KVI_SPARSER_CPP_
 
#include "kvi_sparser.h"
#include "kvi_window.h"
#include "kvi_out.h"
#include "kvi_locale.h"
#include "kvi_ircsocket.h"
#include "kvi_options.h"
#include "kvi_kvs_eventmanager.h"
#include "kvi_kvs_eventtriggers.h"
#include "kvi_ircconnectionstatedata.h"

KviServerParser * g_pServerParser = 0;


KviServerParser::KviServerParser()
: TQObject(0,"server_parser")
{
}

KviServerParser::~KviServerParser()
{
}

void KviServerParser::parseMessage(const char * message,KviIrcConnection * pConnection)
{
	if(*message != 0 )
	{
		KviIrcMessage msg(message,pConnection);
		if(msg.isNumeric())
		{
			if(KviKvsEventManager::instance()->hasRawHandlers(msg.numeric()))
			{
				KviKvsVariantList parms;
				parms.append(pConnection->decodeText(msg.safePrefix()));
				parms.append(pConnection->decodeText(msg.command()));
	
				for(KviStr * str = msg.firstParam();str;str = msg.nextParam())
					parms.append(pConnection->console()->decodeText(str->ptr()));

				if(KviKvsEventManager::instance()->triggerRaw(msg.numeric(),pConnection->console(),&parms))
					msg.setHaltOutput();
			}
	
			messageParseProc proc = m_numericParseProcTable[msg.numeric()];
			if(proc)
			{
				(this->*proc)(&msg);
				if(!msg.unrecognized())return; // parsed
			} else {
				// we don't have a proc for this

				// special handling of unknown RPL_WHOIS* messages
				// if
				//      - we're in the middle of a RPL_WHOIS* sequence (i.e. have received a RPL_WHOIS* message since less than 10 seconds)
				//      - we have not received RPL_ENDOFWHOIS yet (the time of the last RPL_WHOIS* is reset to zero when a RPL_ENDOFWHOIS is received)
				//      - this message is unrecognized and looks like a RPL_WHOIS*
				// then pass it to the WhoisOther handler.
				//
				// Thnx Elephantman :)

				if(msg.paramCount() >= 3) // might look like :prefix RPL_WHOIS* <target> <nick> [?] :<something>
				{
					kvi_time_t tNow = kvi_unixTime();
					
					if((tNow - pConnection->stateData()->lastReceivedWhoisReply()) < 10)
					{
						// we're in the middle of a RPL_WHOIS* sequence and haven't
						// received a RPL_ENDOFWHOIS yet.
						parseNumericWhoisOther(&msg);
						if(!msg.unrecognized())return;
					}
				}
			}
		} else {
			for(int i=0;m_literalParseProcTable[i].msgName;i++)
			{
				if(kvi_strEqualCS(m_literalParseProcTable[i].msgName,msg.command()))
				{
					(this->*(m_literalParseProcTable[i].proc))(&msg);
					if(!msg.unrecognized())return; // parsed
				}
			}
	
	
			if(KviKvsEventManager::instance()->hasAppHandlers(KviEvent_OnUnhandledLiteral))
			{
				KviKvsVariantList parms;
				parms.append(pConnection->decodeText(msg.safePrefix()));
				parms.append(pConnection->decodeText(msg.command()));
	
				for(KviStr * str = msg.firstParam();str;str = msg.nextParam())
					parms.append(pConnection->console()->decodeText(str->ptr()));
					
				if(KviKvsEventManager::instance()->trigger(KviEvent_OnUnhandledLiteral,pConnection->console(),&parms))
					msg.setHaltOutput();
			}
	
		}
	
		// unhandled || unrecognized
		if(!msg.haltOutput() && !_OUTPUT_MUTE)
		{
			TQString szWText = pConnection->decodeText(msg.allParams());
			if(msg.unrecognized())
			{
				pConnection->console()->output(KVI_OUT_UNRECOGNIZED,
					__tr2qs("[Server parser]: Encountered problems while parsing the following message:"));
				pConnection->console()->output(KVI_OUT_UNRECOGNIZED,
					__tr2qs("[Server parser]: [%s][%s] %Q"),msg.prefix(),msg.command(),&szWText);
				pConnection->console()->output(KVI_OUT_UNRECOGNIZED,
					__tr2qs("[Server parser]: %s"),m_szLastParserError.ptr());
			} else {
				// ignore spurious CRLF pairs (some servers send them a lot) unless we want PARANOID output
				if((!msg.isEmpty()) || _OUTPUT_PARANOIC)
					pConnection->console()->output(KVI_OUT_UNHANDLED,
						"[%s][%s] %Q",msg.prefix(),msg.command(),&szWText);
			}
		}
	}
}




#include "kvi_sparser.moc"