File: KviKvsCoreSimpleCommands.cpp

package info (click to toggle)
kvirc 4:5.0.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 31,704 kB
  • sloc: cpp: 230,732; perl: 2,106; pascal: 1,000; sh: 841; ansic: 244; makefile: 61; python: 54; xml: 19
file content (283 lines) | stat: -rw-r--r-- 9,350 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//=============================================================================
//
//   File : KviKvsCoreSimpleCommands.cpp
//   Creation date : Fri 31 Oct 2003 00:04:25 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2003-2010 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 option) 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.
//
//=============================================================================

#include "KviIrcConnectionServerInfo.h"
#include "KviKvsCoreSimpleCommands.h"

#include "KviKvsKernel.h"
#include "KviOptions.h"
#include "KviIrcConnection.h"
#include "KviLocale.h"
#include "KviKvsTreeNodeCommand.h"

#include <QRegExp>
#include <QStringList>

namespace KviKvsCoreSimpleCommands
{
	void init()
	{
		KviKvsKernel * pKern = KviKvsKernel::instance();

#define _REGCMD(__cmdName, __routine)                                                    \
	{                                                                                    \
		KviKvsCoreSimpleCommandExecRoutine * r = new KviKvsCoreSimpleCommandExecRoutine; \
		r->proc = KVI_PTR2MEMBER(KviKvsCoreSimpleCommands::__routine);                   \
		pKern->registerCoreSimpleCommandExecRoutine(QString(__cmdName), r);              \
	}
		// a_f
		_REGCMD("admin", rfc2812wrapper)
		_REGCMD("away", away)
		_REGCMD("back", back)
		_REGCMD("ban", ban)
		_REGCMD("beep", beep)
		_REGCMD("buttonctl", buttonctl)
		_REGCMD("cap", cap)
		_REGCMD("chanadmin", chanadmin)
		_REGCMD("chanowner", chanowner)
		_REGCMD("codepage", rfc2812wrapper)
		_REGCMD("ctcp", ctcp)
		_REGCMD("debug", debugCKEYWORDWORKAROUND)
		_REGCMD("dechanadmin", dechanadmin)
		_REGCMD("dechanowner", dechanowner)
		_REGCMD("dehalfop", dehalfop)
		_REGCMD("delete", deleteCKEYWORDWORKAROUND)
		_REGCMD("delpopupitem", delpopupitem)
		_REGCMD("destroy", deleteCKEYWORDWORKAROUND)
		_REGCMD("deop", deop)
		_REGCMD("deuserop", deuserop)
		_REGCMD("devoice", devoice)
		_REGCMD("die", error)
		_REGCMD("echoprivmsg", echoprivmsg)
		_REGCMD("echo", echo)
		_REGCMD("error", error)
		_REGCMD("eval", eval)
		_REGCMD("eventctl", eventctl)
		_REGCMD("exit", exit)
		// g_l
		_REGCMD("halfop", halfop)
		_REGCMD("halt", halt)
		_REGCMD("help", help)
		_REGCMD("host", host)
		_REGCMD("include", parse)
		_REGCMD("info", rfc2812wrapper)
		_REGCMD("inject", inject)
		_REGCMD("invite", rfc2812wrapper)
		_REGCMD("ison", rfc2812wrapper)
		_REGCMD("join", join)
		_REGCMD("kick", kick)
		_REGCMD("killtimer", killtimer)
		_REGCMD("leave", part)
		_REGCMD("links", rfc2812wrapper)
		_REGCMD("list", rfc2812wrapper)
		_REGCMD("listtimers", listtimers)
		_REGCMD("lusers", rfc2812wrapper)
		// m_r
		_REGCMD("map", rfc2812wrapper) // Technically not RFC
		_REGCMD("me", me)
		_REGCMD("mode", mode)
		_REGCMD("motd", rfc2812wrapper)
		_REGCMD("msg", privmsg)
		_REGCMD("nick", nick)
		_REGCMD("notice", notice)
		_REGCMD("op", op)
		_REGCMD("openurl", openurl)
		_REGCMD("oper", rfc2812wrapper)
		_REGCMD("operwall", operwall)
		_REGCMD("option", option)
		_REGCMD("parse", parse)
		_REGCMD("part", part)
		_REGCMD("pass", rfc2812wrapper)
		_REGCMD("play", play)
		_REGCMD("popup", popup)
		_REGCMD("privmsg", privmsg)
		_REGCMD("query", query)
		_REGCMD("quit", quit)
		_REGCMD("quote", raw)
		_REGCMD("raise", raise)
		_REGCMD("raw", raw)
		_REGCMD("return", returnCKEYWORDWORKAROUND)
		_REGCMD("rebind", rebind)
		// s_z
		_REGCMD("run", run)
		_REGCMD("say", say)
		_REGCMD("server", server)
		_REGCMD("service", rfc2812wrapper)
		_REGCMD("setmenu", setmenu)
		_REGCMD("setreturn", setreturn)
		_REGCMD("sockets", rfc2812wrapper)
		_REGCMD("squery", squery)
		_REGCMD("squit", rfc2812wrapper)
		_REGCMD("srand", srand)
		_REGCMD("stats", rfc2812wrapper)
		_REGCMD("time", rfc2812wrapper)
		_REGCMD("topic", topic)
		_REGCMD("trace", rfc2812wrapper)
		_REGCMD("trload", trload)
		_REGCMD("trunload", trunload)
		_REGCMD("unban", unban)
		_REGCMD("userop", userop)
		_REGCMD("version", rfc2812wrapper)
		_REGCMD("voice", voice)
		_REGCMD("warning", warning)
		_REGCMD("wallops", wallops)
		_REGCMD("who", who)
		_REGCMD("whois", whois)
		_REGCMD("whowas", whowas)

#undef _REGCMD
	}

	bool multipleModeCommand(KviKvsRunTimeContext * __pContext, KviKvsVariantList * __pParams, KviKvsSwitchList * __pSwitches, char plusminus, char flag)
	{
		Q_UNUSED(__pSwitches);

		QString szTokens;
		KVSCSC_PARAMETERS_BEGIN
		KVSCSC_PARAMETER("tokens", KVS_PT_STRING, KVS_PF_APPENDREMAINING, szTokens)
		KVSCSC_PARAMETERS_END

		KVSCSC_REQUIRE_CONNECTION

		if(KVSCSC_pWindow->type() != KviWindow::Channel)
		{
			KVSCSC_pContext->warning(__tr2qs_ctx("This is not a channel", "kvs"));
			return true;
		}

		QStringList sl = szTokens.split(QRegExp("[, ]+"), QString::SkipEmptyParts);

		QByteArray szFlags;
		QByteArray szTarget = KVSCSC_pConnection->encodeText(KVSCSC_pWindow->windowName());
		QByteArray szMessage;

		int iMaxModeChangesPerLine = 3; // a good default
		KviIrcConnectionServerInfo * pServerInfo = KVSCSC_pConnection->serverInfo();
		if(pServerInfo)
		{
			iMaxModeChangesPerLine = pServerInfo->maxModeChanges();
			if(iMaxModeChangesPerLine < 1)
				iMaxModeChangesPerLine = 1;
		}

		int iCurModeChangesPerLine = iMaxModeChangesPerLine;

		// Calculate the max number of available characters in a MODE command
		// 512 (max) - 2 (cr, lf) - 4 (MODE) - 3 (spaces) - 1 (plus/minus)
		// since we already know the channel name, take it in account, too

		const int iMaxCharsPerLine = 502 - szTarget.size();
		int iCurCharsPerLine = iMaxCharsPerLine;

		for(QStringList::Iterator it = sl.begin(); it != sl.end(); ++it)
		{
			QByteArray szTxt = KVSCSC_pConnection->encodeText(*it);
			if(!szTxt.isEmpty())
			{
				szFlags += flag;
				iCurCharsPerLine--;

				if(szMessage.length() > 0)
				{
					szMessage += " ";
					iCurCharsPerLine--;
				}

				szMessage += szTxt;
				iCurCharsPerLine -= szTxt.size();

				iCurModeChangesPerLine--;
				if(iCurModeChangesPerLine < 0 || iCurCharsPerLine < 0)
				{
					if(!KVSCSC_pConnection->sendFmtData("MODE %s %c%s %s", szTarget.data(), plusminus, szFlags.data(), szMessage.data()))
						return KVSCSC_pContext->warningNoIrcConnection();

					// move back the iterator by one position
					// WARNING: this could lead to an infinite loop if a single specified mode
					// change is longer than iMaxCharsPerLine
					it--;

					// reset cycle variables
					iCurCharsPerLine = iMaxCharsPerLine;
					iCurModeChangesPerLine = iMaxModeChangesPerLine;
					szFlags = "";
					szMessage = "";
				}
			}
		}

		if(iCurModeChangesPerLine != iMaxModeChangesPerLine)
		{
			if(!KVSCSC_pConnection->sendFmtData("MODE %s %c%s %s", szTarget.data(), plusminus, szFlags.data(), szMessage.data()))
				return KVSCSC_pContext->warningNoIrcConnection();
		}

		return true;
	}

	/*
		@doc: rfc2812wrappers
		@title:
			RFC2812 Wrappers
		@type:
			generic
		@short:
			Wrappers for RFC2812 commands
		@body:
			Some less used KVIrc commands are implemented as simple wrappers
			around the standard [anchorlink:https://tools.ietf.org/html/rfc2812]rfc2812[/anchorlink] commands.[br]
			For example, the /[cmd:oper]OPER[/cmd] command will be used only by server operators
			and probably only once per connection.[br]
			These commands should only be needed by experienced IRC users.
			Due to this fact, these commands have no special kind of parsing performed by KVIrc;
			the parameters are extracted, the identifiers are parsed normally
			and are sent to the server without any semantic check.[br]
			This means that while [cmd:oper]OPER[/cmd] requires two parameters, KVIrc will not
			complain if you send a single parameter only or use ten parameters.[br]
			The exact parameter syntax/semantic checking is left to you.[br]
			This also means that if some of these commands accept parameters with spaces,
			it is left to you to add the leading ':' before the last parameter.[br]
			You should refer to [anchorlink:https://tools.ietf.org/html/rfc2812]rfc2812[/anchorlink] if any of these commands do not work properly for you.[br]
	*/

	KVSCSC(rfc2812wrapper)
	{
		Q_UNUSED(__pSwitches);

		QString szText;
		KVSCSC_PARAMETERS_BEGIN
		KVSCSC_PARAMETER("text", KVS_PT_STRING, KVS_PF_OPTIONAL | KVS_PF_APPENDREMAINING, szText)
		KVSCSC_PARAMETERS_END

		KVSCSC_REQUIRE_CONNECTION

		QByteArray txt = KVSCSC_pConnection->encodeText(szText);
		QByteArray cmd = KVSCSC_pConnection->encodeText(((KviKvsTreeNodeCommand *)(KVSCSC_pContext->defaultReportLocation()))->commandName());

		if(!KVSCSC_pConnection->sendFmtData("%s %s", cmd.data(), txt.data() ? txt.data() : ""))
			return KVSCSC_pContext->warningNoIrcConnection();

		return true;
	}
};