summaryrefslogtreecommitdiffstats
path: root/src/modules/spaste/libkvispaste.cpp
blob: 6b310324c96da96925f0f385a820a0838973ae44 (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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//   File : libkvispaste.cpp
//   Creation date : Thu Dec 27 2002 17:13:12 GMT by Juanjo �varez
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2002 Juanjo �varez ([email protected])
//   Copyright (C) 2002 Szymon Stefanek ([email protected])
//
//   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.
//


#include "libkvispaste.h"
#include "controller.h"

#include "kvi_module.h"

#include "kvi_fileutils.h"
#include "kvi_app.h"
#include "kvi_locale.h"

#include "kvi_console.h"
#include "kvi_options.h"
#include "kvi_out.h"

#include <qfile.h>
#include <qclipboard.h>

#ifndef COMPILE_ON_WINDOWS
    #include <unistd.h>
#endif

KviPointerList<SPasteController> * g_pControllerList = 0;
int ctrlId = 0;

static SPasteController * spaste_find_controller(KviWindow * w)
{
	for(SPasteController * spc = g_pControllerList->first();spc;spc = g_pControllerList->next())
	{
		if(spc->window() == w)return spc;
	}
	return 0;
}

static KviWindow * spaste_kvs_find_window(QString &win, KviKvsModuleCommandCall * c)
{
	KviWindow * w;
	if(!win.isEmpty())w = g_pApp->findWindow(win);
	else w = c->window();
	if(!w)
	{
		c->warning(__tr("Window with ID '%Q' not found"),&win);
		return 0;
	}
	if((w->type() == KVI_WINDOW_TYPE_CHANNEL) || (w->type() == KVI_WINDOW_TYPE_QUERY) || (w->type() == KVI_WINDOW_TYPE_DCCCHAT))return w;
	c->warning(__tr2qs("The specified window (%Q) is not a channel/query/DCC chat"),&win);
	return 0;
}
//-------------------------------------------------
/*
	@doc: spaste.file
	@type:
		command
	@title:
		spaste.file
	@short:
		Sends the contents of a file to a window, with a delay between each line
	@syntax:
		spaste.file <filename:string> [window:string]
	@description:
		Sends the contents of a file to a conversation window doing a pause between each line. [br]
		the optional window parameter must be a channel, query or DCC chat window, if [br]
		no window is specified the text will be send to the current window.
	@seealso:
		[cmd]spaste.clipboard[/cmd],
		[cmd]spaste.stop[/cmd],
		[cmd]spaste.list[/cmd],
		[cmd]spaste.setdelay[/cmd]
*/
//-------------------------------------------
// spaste.file
//-------------------------------------------

static bool spaste_kvs_cmd_file(KviKvsModuleCommandCall * c)
{
	QString szFile,szWindow;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("file name",KVS_PT_STRING,0,szFile)
		KVSM_PARAMETER("window",KVS_PT_STRING,KVS_PF_OPTIONAL,szWindow)
	KVSM_PARAMETERS_END(c)

	KviWindow * window = spaste_kvs_find_window(szWindow,c);
	if( (!window) || window->console()->isNotConnected())return false;

	if(szFile.isEmpty() || (!KviFileUtils::fileExists(szFile.ascii())))
	{
        c->warning(__tr2qs("File not found or empty"));
		return false;
	}

	QFile tmp(szFile);
	if(!tmp.open(IO_ReadOnly)) {
		c->warning(__tr2qs("I can't open that file"));
		return false;
	}
	tmp.close();

	SPasteController * controller = spaste_find_controller(window);
	if(!controller)controller = new SPasteController(window,++ctrlId);
	if(!controller->pasteFileInit(szFile)) {
		c->warning(__tr2qs("Could not paste file"));
		return false;
	}
	return true;
}


/*
	@doc: spaste.clipboard
	@type:
		command
	@title:
		spaste.clipboard
	@short:
		Sends the contents of the clipboard to a window, pausing between each line
	@syntax:
		spaste.clipboard [window:string]
	@description:
		Sends the contents of the clipboard to a conversation window, with a delay between each line. [br]
		the optional window parameter must be a channel, query or DCC chat window. [br]
		If no window is specified, the text will be sent to the current window.
	@seealso:
		[cmd]spaste.file[/cmd],
		[cmd]spaste.stop[/cmd],
		[cmd]spaste.list[/cmd],
		[cmd]spaste.setdelay[/cmd]
*/
//-------------------------------------------------
// spaste.clipboard
//-------------------------------------------------

static bool spaste_kvs_cmd_clipboard(KviKvsModuleCommandCall * c)
{
	QString szWindow;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("window",KVS_PT_STRING,KVS_PF_OPTIONAL,szWindow)
	KVSM_PARAMETERS_END(c)
	KviWindow * window = spaste_kvs_find_window(szWindow,c);
	if( (!window) || window->console()->isNotConnected())return false;
	
	SPasteController * controller = spaste_find_controller(window);
	if(!controller)controller = new SPasteController(window,++ctrlId);
	controller->pasteClipboardInit();
	return true;
}

/*
	@doc: spaste.stop
	@type:
		command
	@title:
		spaste.stop
	@short:
		Stops one or more slow-paste process.
	@syntax:
		spaste.stop [-a] [id:integer]
	@description:
		This commands stop one or more slow-paste processes. It can operate in three ways. The first, [br]
		without any parameter or switch, stops all slow-paste processes running in the same window [br]
		as the command. The second, using the -a switch, stops all slow paste processes running [br]
		on all windows. The third form, without the switch and specifying a numerical slow paste process ID [br]
		(which you can obtain with the [cmd]spaste.list[/cmd] command), stops only that process ID.
		@switches:
			!sw: -a | --all
			Stops all slow paste processes running
			@seealso:
		[cmd]spaste.clipboard[/cmd],
		[cmd]spaste.file[/cmd],
		[cmd]spaste.list[/cmd],
		[cmd]spaste.setdelay[/cmd]

*/
//--------------------------------------------------
// spaste.stop
//--------------------------------------------------

static bool spaste_kvs_cmd_stop(KviKvsModuleCommandCall * c)
{
	kvs_int_t iId=0;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("delay",KVS_PT_UNSIGNEDINTEGER,KVS_PF_OPTIONAL,iId)
	KVSM_PARAMETERS_END(c)

	if(c->hasSwitch('a',"all")) //Delete all spaste's
	{
		while(g_pControllerList->first()) delete g_pControllerList->first();
		return true;
	} else {
		KviPointerListIterator<SPasteController> it(*g_pControllerList);
		SPasteController *item;
        
		if(!iId) //Delete all spaste's from the current window
		{
			if((c->window()->type() != KVI_WINDOW_TYPE_CHANNEL) && (c->window()->type() != KVI_WINDOW_TYPE_QUERY) && (c->window()->type() != KVI_WINDOW_TYPE_DCCCHAT))
			{
				QString szWinId = c->window()->id();
				c->warning(__tr2qs("The specified window (%Q) is not a channel/query/dcc"),&szWinId);
				return false;
			} else {
				while( (item = it.current()) != 0)
				{
					++it;
					if(kvi_strEqualCS(item->window()->id(),c->window()->id()))delete item;
				}
			}
		} else //Delete the spaste with the given id
		{
			while( (item = it.current()) != 0) 
			{ 
				++it;
				if(item->getId() == iId)delete item;
			}
		}
		return true;
	}
}

/*
	@doc: spaste.list
	@type:
		command
	@title:
		spaste.list
	@short:
		Lists all the running spaste processes.
	@syntax:
		spaste.list
	@description:
		This command will list in the window where it is executed all the current slow-paste [br]
		processes, their identification numbers, and the windows where they are running. [br]
	@seealso:
		[cmd]spaste.clipboard[/cmd],
		[cmd]spaste.file[/cmd],
		[cmd]spaste.stop[/cmd],
		[cmd]spaste.setdelay[/cmd]
*/
//--------------------------------------------------
// spaste.list
//--------------------------------------------------

static bool spaste_kvs_cmd_list(KviKvsModuleCommandCall * c)
{ 
	KviPointerListIterator<SPasteController> it(*g_pControllerList);
	SPasteController *item;

	while( (item = it.current()) != 0)
	{
		++it;
		QString szWinId = item->window()->id();
		c->window()->output(KVI_OUT_NONE,__tr2qs("Slow-paste ID:%d Window:%Q"),item->getId(),&szWinId);
	}
	return true;
}

/*
	@doc: spaste.setdelay
	@type:
		command
	@title:
		spaste.setdelay
	@short:
		Sets the delay time in miliseconds for the spaste module command delay
	@syntax:
		spaste.setdelay <time_in_msecs:integer>
	@description:
		Sets the delay time in milliseconds for the spaste module command delay.
	@seealso:
		[cmd]spaste.clipboard[/cmd],
		[cmd]spaste.file[/cmd],
		[cmd]spaste.stop[/cmd],
		[cmd]spaste.list[/cmd]
*/
//-------------------------------------------------
// spaste.setdelay
//-------------------------------------------------


static bool spaste_kvs_cmd_setdelay(KviKvsModuleCommandCall * c)
{ 
	kvs_int_t delay;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("delay",KVS_PT_INTEGER,0,delay)
	KVSM_PARAMETERS_END(c)
	KVI_OPTION_UINT(KviOption_uintPasteDelay) = delay;
	return true;
}

//-------------------------------------------------    
static bool spaste_module_init(KviModule * m)
{
	g_pControllerList = new KviPointerList<SPasteController>;
	g_pControllerList->setAutoDelete(false);

	KVSM_REGISTER_SIMPLE_COMMAND(m,"file",spaste_kvs_cmd_file);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"clipboard",spaste_kvs_cmd_clipboard);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"setdelay",spaste_kvs_cmd_setdelay);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"stop",spaste_kvs_cmd_stop);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"list",spaste_kvs_cmd_list);
	return true;
}
//-------------------------------------------------
static bool spaste_module_cleanup(KviModule *m)
{
	while(g_pControllerList->first()) delete g_pControllerList->first();
	delete g_pControllerList;
	g_pControllerList = 0;

	return true;
}
//-------------------------------------------------
static bool spaste_module_can_unload(KviModule *m)
{
	return (g_pControllerList->isEmpty());
}
//-------------------------------------------------
KVIRC_MODULE(
	"SPaste",                                                 // module name
	"1.0.0",                                                // module version
	"          (C) 2002 Juanjo Alvarez ([email protected])", // author & (C)
	"Delayed paste commands",
	spaste_module_init,
	spaste_module_can_unload,
	0,
	spaste_module_cleanup
)