summaryrefslogtreecommitdiffstats
path: root/src/modules/my/libkvimy.cpp
blob: d98d44af738877f66769f4bf348ebec4495c5ef6 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//
//   File : libkvimy.cpp
//   Creation date : Mon Jul  1 02:46:49 2002 GMT by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2001 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.
//

#include "kvi_module.h"

#include "kvi_console.h"

#include "kvi_app.h"
#include "kvi_locale.h"
#include "kvi_ircconnection.h"
#include "kvi_ircconnectionuserinfo.h"
#include "kvi_ircconnectionserverinfo.h"
#include "kvi_ircserver.h"
#include "idle.h"
Idle* g_pIdle;
#ifdef COMPILE_NEW_KVS
#define GET_KVS_CONSOLE \
	kvs_uint_t  uiWnd; \
	KviConsole *wnd =0; \
	KVSM_PARAMETERS_BEGIN(c) \
		KVSM_PARAMETER("context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uiWnd) \
	KVSM_PARAMETERS_END(c) \
	if(!c->parameterList()->count()) \
	{ \
		if(c->window()->console()) wnd = c->window()->console(); \
			else c->warning(__tr2qs("This window has no associated irc context")); \
	} \
	else \
	{ \
		wnd = g_pApp->findConsole(uiWnd); \
		if(!wnd)c->warning(__tr2qs("No such irc context (%d)"),uiWnd); \
	} 	
#endif
/*
	@doc: my.nick
	@type:
		function
	@title:
		$my.nick
	@short:
		Returns the current nickname
	@syntax:
		<string> $my.nick([irc_context_id:uint])
	@description:
		Returns the nickname of the current irc context.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
		Note that this function is different from [fnc]$me[/fnc] that will work also in a DCC CHAT.[br]
*/


static bool my_kvs_fnc_nick(KviKvsModuleFunctionCall * c)
{
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setString(wnd->connection()->userInfo()->nickName());
	}
	return true;
}

/*
	@doc: my.startIdleTimer
	@type:
		command
	@title:
		my.startIdleTimer
	@short:
		Starts a global idle hook
	@syntax:
		my.startIdleTimer()
	@description:
		Starts a global idle hook. Allows to detect a global user idle time
*/

static bool my_kvs_cmd_startIdleTimer(KviKvsModuleCommandCall * c)
{
	if(!g_pIdle)
		g_pIdle = new Idle();
	g_pIdle->start();
	return true;
}

/*
	@doc: my.stopIdleTimer
	@type:
		command
	@title:
		my.stopIdleTimer
	@short:
		Stops a global idle hook
	@syntax:
		my.stopIdleTimer()
	@description:
		Stops a global idle hook. Allows to detect a global user idle time
*/

static bool my_kvs_cmd_stopIdleTimer(KviKvsModuleCommandCall * c)
{
	if(!g_pIdle) return true;
	g_pIdle->stop();
	//delete g_pIdle;
	return true;
}

/*
	@doc: my.globalIdle
	@type:
		function
	@title:
		$my.globalIdle
	@short:
		Returns the current user global idle time
	@syntax:
		<int> $my.globalIdle()
	@description:
		Returns the current user global idle time
*/

static bool my_kvs_fnc_globalIdle(KviKvsModuleFunctionCall * c)
{
	if(g_pIdle) 
	{
		c->returnValue()->setInteger(g_pIdle->secondsIdle());
	} else {
		c->error(__tr2qs("Global idle timer must be started before using $my.globalIdle function"));
	}
	return true;
}

/*
	@doc: my.umode
	@type:
		function
	@title:
		$my.umode
	@short:
		Returns the current user mode flags
	@syntax:
		<string> $my.umode([irc_context_id:uint])
	@description:
		Returns the user mode flags of the current irc context.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
*/

static bool my_kvs_fnc_umode(KviKvsModuleFunctionCall * c)
{
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setString(wnd->connection()->userInfo()->userMode());
	}
	return true;
}

/*
	@doc: my.user
	@type:
		function
	@title:
		$my.user
	@short:
		Returns the current username
	@syntax:
		<string> $my.user([irc_context_id:uint])
	@description:
		Returns the username of the current irc context.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
*/

static bool my_kvs_fnc_user(KviKvsModuleFunctionCall * c)
{
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setString(wnd->connection()->userInfo()->userName());
	}
	return true;
}

/*
	@doc: my.host
	@type:
		function
	@title:
		$my.host
	@short:
		Returns the current hostname
	@syntax:
		<string> $my.host([irc_context_id:uint])
	@description:
		Returns the hostname of the current irc context as known by the IRC server.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
*/


static bool my_kvs_fnc_host(KviKvsModuleFunctionCall * c)
{
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setString(wnd->connection()->userInfo()->hostName());
	}
	return true;
}

/*
	@doc: my.ip
	@type:
		function
	@title:
		$my.ip
	@short:
		Returns the current ip address
	@syntax:
		<string> $my.ip([irc_context_id:uint])
	@description:
		Returns the ip address of the current irc context as known by the IRC server.[br]
		The ip address is resolved as soon as the hostname is received from the server.
		If the hostname is masked or there is an error in the DNS lookup then
		you may get the real local host ip address as determined at connection startup.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
*/


static bool my_kvs_fnc_ip(KviKvsModuleFunctionCall * c)
{
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setString(wnd->connection()->userInfo()->hostIp());
	}
	return true;
}

/*
	@doc: my.serverIsIPV6
	@type:
		function
	@title:
		$my.serverIsSSL
	@short:
		Returns 1 if the current server connection use IPV6.
	@syntax:
		<boolean> $my.serverIsIPV6([irc_context_id:uint])
	@description:
		Returns 1 if the server connection use IPV6.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
*/

static bool my_kvs_fnc_serverIsIPV6(KviKvsModuleFunctionCall * c)
{
	
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setBoolean(wnd->connection()->server()->isIpV6());
	}
	return true;
}


/*
	@doc: my.serverIsSSL
	@type:
		function
	@title:
		$my.serverIsSSL
	@short:
		Returns 1 if the current server connection use SSL.
	@syntax:
		<boolean> $my.serverIsSSL([irc_context_id:uint])
	@description:
		Returns 1 if the server connection use SSL.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
*/

static bool my_kvs_fnc_serverIsSSL(KviKvsModuleFunctionCall * c)
{
	
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setBoolean(wnd->connection()->socket()->usingSSL());
	}
	return true;
}

/*
	@doc: my.server
	@type:
		function
	@title:
		$my.server
	@short:
		Returns the current server name
	@syntax:
		<string> $my.server([irc_context_id:uint])
	@description:
		Returns the server name of the current irc context.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
		Please note that this function returns the name of the server as reported
		by the server itself. Some servers report a bogus value for this field.
		You should take a look at $context.serverIpAddress or $context.serverHostName
		if you want a value that can be used to really reconnect to this server.
*/

static bool my_kvs_fnc_server(KviKvsModuleFunctionCall * c)
{
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setString(wnd->connection()->serverInfo()->name());
	}
	return true;
}

/*
	@doc: my.network
	@type:
		function
	@title:
		$my.network
	@short:
		Returns the current network name
	@syntax:
		<string> $my.network([irc_context_id:uint])
	@description:
		Returns the network name of the current irc context.[br]
		If the irc context is not connected then an empty string is returned.[br]
		If <irc_context_id> is specified this function returns acts as it was called
		in that irc_context.[br]
*/

static bool my_kvs_fnc_network(KviKvsModuleFunctionCall * c)
{
	GET_KVS_CONSOLE
	if(wnd)
	{
		if(wnd->connection())
			c->returnValue()->setString(wnd->currentNetworkName().utf8().data());
	}
	return true;
}

static bool my_module_init(KviModule * m)
{
	g_pIdle = 0;
	KVSM_REGISTER_FUNCTION(m,"nick",my_kvs_fnc_nick);
	KVSM_REGISTER_FUNCTION(m,"user",my_kvs_fnc_user);
	KVSM_REGISTER_FUNCTION(m,"host",my_kvs_fnc_host);
	KVSM_REGISTER_FUNCTION(m,"ip",my_kvs_fnc_ip);
	KVSM_REGISTER_FUNCTION(m,"server",my_kvs_fnc_server);
	KVSM_REGISTER_FUNCTION(m,"network",my_kvs_fnc_network);
	KVSM_REGISTER_FUNCTION(m,"umode",my_kvs_fnc_umode);
	KVSM_REGISTER_FUNCTION(m,"serverIsSSL",my_kvs_fnc_serverIsSSL);
	KVSM_REGISTER_FUNCTION(m,"serverIsIPV6",my_kvs_fnc_serverIsIPV6);

	KVSM_REGISTER_SIMPLE_COMMAND(m,"stopIdleTimer",my_kvs_cmd_stopIdleTimer);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"startIdleTimer",my_kvs_cmd_startIdleTimer);
	return true;
}

static bool my_module_cleanup(KviModule *m)
{
	if(g_pIdle)
		delete g_pIdle;
	g_pIdle = 0;
	return true;
}

static bool my_module_can_unload(KviModule *)
{
	return !g_pIdle;
}

KVIRC_MODULE(
	"My",                                              // module name
	"1.0.0",                                                // module version
	"Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot net)" \
	"		   (C) 2005 Tonino Imbesi (grifisx at barmes dot net)" \
	"          (C) 2005 Alessandro Carbone (noldor at barmes dot net)", // author & (C)
	"Scripting irc-context related functions",
	my_module_init,
	my_module_can_unload,
	0,
	my_module_cleanup
)