summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kernel/kvi_userinput.cpp
blob: 3f4dbbc0e92a079a053e20d0078fb7de2ed93663 (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
//=============================================================================
//
//   File : kvi_userinput.cpp
//   Created on Sun 25 Sep 2005 05:27:57 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC Client distribution
//   Copyright (C) 2005 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__

#include "kvi_userinput.h"
#include "kvi_kvs_variantlist.h"
#include "kvi_console.h"
#include "kvi_kvs_script.h"
#include "kvi_locale.h"
#include "kvi_ircconnection.h"
#include "kvi_ircconnectionuserinfo.h"
#include "kvi_out.h"
#include "kvi_options.h"
#include "kvi_kvs_eventtriggers.h"

namespace KviUserInput
{
	bool parse(TQString &szData,KviWindow * pWindow,const TQString &szContext,bool bUserFriendlyCommandline)
	{
		const TQChar * b = KviTQString::nullTerminatedArray(szData);
		const TQChar * c = b;
		if(!c)return true; // empty
		
		while(c->isSpace())c++;
		if(!c->unicode())return true; // empty
		
		if(c->unicode() == '\\')
		{
			c++;
			if(c->unicode() != '/')c--;
		} else {
			if(c->unicode() == '/')
			{
				c++;
				if(c->unicode() != '/')
				{
					szData.remove(0,c-b);
					return parseCommand(szData,pWindow,szContext,bUserFriendlyCommandline);
				} else {
					// C++ comment, probably
					c--;
				}
			}
		}

		if(KVS_TRIGGER_EVENT_1_HALTED(KviEvent_OnTextInput,pWindow,szData))
			return true; // halted
		
		if(c != b)szData.remove(0,c-b);
		parseNonCommand(szData,pWindow);
		return true;
	}
	
	bool parseCommand(const TQString &szData,KviWindow * pWindow,const TQString &szContext,bool bUserFriendlyCommandline)
	{
		if(bUserFriendlyCommandline)
		{
			static TQString szUserFriendlyCommandlineContext(__tr2qs("commandline::userfriendly"));
			TQString szCmd=szData;
			// escape any -$;\%(
			szCmd.replace("\\","\\\\");
			szCmd.replace("\"","\\\"");
			szCmd.replace("$","\\$");
			szCmd.replace("%","\\%");
			szCmd.replace("(","\\(");
			szCmd.replace(";","\\;");
			szCmd.replace("-","\\-");
			szCmd.replace("+","\\+");
			KviKvsScript kvs(szContext.isEmpty() ? szUserFriendlyCommandlineContext : szContext,szCmd);
			return (kvs.run(pWindow,0,0) != KviKvsScript::Error);
		} else {
			static TQString szCommandlineContext(__tr2qs("commandline::kvs"));
			KviKvsScript kvs(szContext.isEmpty() ? szCommandlineContext : szContext,szData);
			return (kvs.run(pWindow,0,0/*,KviKvsScript::AssumeLocals*/) != KviKvsScript::Error);
		}
	}
	
	void parseNonCommand(TQString &szData,KviWindow * pWindow)
	{
		const TQChar * aux = KviTQString::nullTerminatedArray(szData);
		const TQChar * beg = aux;
		if(!beg)return; // empty
	
		while(aux->unicode())
		{
			while(aux->unicode() && (aux->unicode() != '\n'))aux++;
			TQString buf(beg,aux-beg);
			if(aux->unicode() == '\n')aux++;
			beg = aux;
	
			if(buf.isEmpty())buf = " "; // avoid "No text to send" (d3vah)
	
			switch(pWindow->type())
			{
				case KVI_WINDOW_TYPE_CONSOLE:
					if(pWindow->connection())
					{
						KviTQCString data = pWindow->connection()->encodeText(buf);
						if(((KviConsole *)pWindow)->connection()->sendData(data.data()))
						{
							pWindow->output(KVI_OUT_RAW,"[RAW]: %Q",&buf);
							return;
						}
					}
					pWindow->output(KVI_OUT_PARSERERROR,__tr2qs("You are not connected to a server"));
				break;
				case KVI_WINDOW_TYPE_CHANNEL:
				case KVI_WINDOW_TYPE_QUERY:
					if(pWindow->connection())
					{
						if(KVI_OPTION_BOOL(KviOption_boolExitAwayOnInput)) 
							if(pWindow->connection()->userInfo()->isAway())
								parseCommand("back",pWindow->console());
					}
					pWindow->ownMessage(buf);
				break;
				case KVI_WINDOW_TYPE_DCCCHAT:
					pWindow->ownMessage(buf);
				break;
				default:
					// FIXME: Should pass the message somewhere ?.. a KviWindow handler ?
				break;
			}
		}
	}
};