1
//=============================================================================
3
// File : KviKvsCoreFunctions_mr.cpp
4
// Creation date : Fri 31 Oct 2003 01:52:04 by Szymon Stefanek
6
// This file is part of the KVIrc IRC client distribution
7
// Copyright (C) 2003-2010 Szymon Stefanek <pragma at kvirc dot net>
9
// This program is FREE software. You can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your opinion) any later version.
14
// This program is distributed in the HOPE that it will be USEFUL,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
// See the GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program. If not, write to the Free Software Foundation,
21
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
//=============================================================================
27
#include "KviKvsCoreFunctions.h"
28
#include "KviKvsKernel.h"
29
#include "KviKvsArrayCast.h"
30
#include "KviKvsObjectController.h"
31
#include "KviLocale.h"
32
#include "KviIrcConnection.h"
33
#include "KviControlCodes.h"
34
#include "KviOptions.h"
35
#include "KviConsoleWindow.h"
36
#include "KviIrcUserDataBase.h"
37
#include "KviIrcMask.h"
38
#include "kvi_socket.h"
39
#include "KviApplication.h"
40
#include "KviQueryWindow.h"
42
#include <stdlib.h> // rand & srand
44
namespace KviKvsCoreFunctions
46
///////////////////////////////////////////////////////////////////////////////////////////////
55
Retrieves the host mask of a user
57
$mask[(<nickname>[,<mask_type>])]
59
Returns the specified type of mask for the user with <nickname>.[br]
60
If the host or username are not known, the mask may contain less information
62
If the <nickname> is not given it is assumed to be the current nickname.[br]
63
If <mask_type> is not given or is invalid, it is assumed to be 0.[br]
64
Available mask types:[br]
65
0 : nick!user@machine.host.top (nick!user@XXX.XXX.XXX.XXX) (default)[br]
66
1 : nick!user@*.abc.host.top (nick!user@XXX.XXX.XXX.*)[br]
68
3 : nick!*@machine.host.top (nick!user@XXX.XXX.XXX.XXX)[br]
69
4 : nick!*@*.abc.host.top (nick!user@XXX.XXX.XXX.*)[br]
71
6 : *!user@machine.host.top (*!user@XXX.XXX.XXX.XX)[br]
72
7 : *!user@*.abc.host.top (*!user@XXX.XXX.XXX.*)[br]
74
9 : *!*@machine.host.top (*!*@XXX.XXX.XXX.XXX)[br]
75
10: *!*@*.abc.host.top (*!*@XXX.XXX.XXX.*)[br]
76
11: nick!*user@machine.host.top (nick!*user@XXX.XXX.XXX.XXX)[br]
77
12: nick!*user@*.abc.host.top (nick!*user@XXX.XXX.XXX.*)[br]
79
14: *!*user@machine.host.top (*!*user@XXX.XXX.XXX.XXX)[br]
80
15: *!*user@*.abc.host.top (*!*user@XXX.XXX.XXX.*)[br]
82
17: nick!~user@*.host.top (nick!~user@XXX.XXX.*)[br]
83
18: nick!*@*.host.top (nick!*@XXX.XXX.*)[br]
84
19: *!~user@*.host.top (*!~user@XXX.XXX.*)[br]
85
20: nick!*user@*.host.top (nick!*user@XXX.XXX.*)[br]
86
21: *!*user@*.host.top (*!user@*XXX.XXX.*)[br]
87
22: nick!~user@*.host.top (nick!~user@XXX.XXX.*)[br]
88
23: nick!*@*.host.top (nick!*@XXX.XXX.*)[br]
89
24: *!~user@*.host.top (*!~user@XXX.XXX.*)[br]
90
25: nick!*user@*.host.top (nick!*user@XXX.XXX.*)[br]
91
26: *!*user@*.host.top (*!user@*XXX.XXX.*)[br]
92
If some data is missing, these types may change:[br]
93
For example, if the hostname is missing, the mask type 3 or 4 may be reduced to type 5.[br]
94
If the user with <nickname> is not found in the current IRC context user database,
95
an empty string is returned.[br]
96
The masks 22-26 are the smart versions of the masks 17-21 that try take care of masked ip addresses
97
in the form xxx.xxx.INVALID-TOP-MASK. If a masked ip address is found then
98
the XXX.XXX.* or XXX.* host mask is returned instead of the (wrong) *.INVALID-TOP-MASK
108
KVSCF_PARAMETERS_BEGIN
109
KVSCF_PARAMETER("nickname",KVS_PT_STRING,KVS_PF_OPTIONAL,szNick)
110
KVSCF_PARAMETER("maskType",KVS_PT_UINT,KVS_PF_OPTIONAL,maskType)
113
if(maskType > 26) maskType = 0;
115
if(KVSCF_pContext->window()->console())
117
if(KVSCF_pContext->window()->console()->isConnected())
119
KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick.isEmpty() ? KVSCF_pContext->window()->connection()->currentNickName() : szNick);
124
u.setUsername(e->user());
125
u.setHost(e->host());
128
u.mask(tmp,(KviIrcMask::MaskType)maskType);
129
KVSCF_pRetBuffer->setString(tmp);
134
KVSCF_pRetBuffer->setNothing();
138
///////////////////////////////////////////////////////////////////////////////////////////////
147
Returns the current nickname
149
<string> $me[(<irc_context_id:uint>)]
151
Returns the current nickname used in the specified IRC context.[br]
152
If no <irc_context_id> is specified, the current IRC context is used.[br]
153
If you are not connected to a server, this function will return a null string.[br]
154
If the current window is a DCC chat and no irc_context is specified,
155
the local nickname is returned.[br]
156
If the current window does not belong to any IRC context, no irc_context_id
157
is specified, and the current window is not a DCC chat, a warning is printed.[br]
164
KVSCF_PARAMETERS_BEGIN
165
KVSCF_PARAMETER("irc_context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uCntx)
168
KviConsoleWindow * cns;
170
if(KVSCF_pParams->count() > 0)
172
cns = g_pApp->findConsole(uCntx);
175
if(cns->isConnected())
176
KVSCF_pRetBuffer->setString(cns->connection()->currentNickName());
178
KVSCF_pRetBuffer->setNothing();
180
KVSCF_pRetBuffer->setNothing();
183
if(KVSCF_pContext->window()->console())
185
cns = KVSCF_pContext->window()->console();
186
if(cns->isConnected())
187
KVSCF_pRetBuffer->setString(cns->connection()->currentNickName());
189
KVSCF_pRetBuffer->setNothing();
191
if(KVSCF_pContext->window()->type() == KviWindow::DccChat)
193
KVSCF_pRetBuffer->setString(KVSCF_pContext->window()->localNick());
195
KVSCF_pContext->warning(__tr2qs_ctx("This window has no associated IRC context and is not a DCC chat","kvs"));
196
KVSCF_pRetBuffer->setNothing();
203
///////////////////////////////////////////////////////////////////////////////////////////////
212
Returns the id of a message type color set
214
$msgtype(<message type color set name>)
216
Returns the id of a message type color set used by the [cmd]echo[/cmd] command.
217
You can take a look at the options dialog section related to the
218
message output to see the list of available message type names.
221
[cmd]echo[/cmd] -i=$msgtype(Highlight) Highlighted text!
230
KVSCF_PARAMETERS_BEGIN
231
KVSCF_PARAMETER("msg_type_color_set_name",KVS_PT_NONEMPTYSTRING,0,szName)
234
QString tmp = "msgtype";
236
for(int i=0;i< KVI_NUM_MSGTYPE_OPTIONS;i++)
238
if(KviQString::equalCI(tmp,g_msgtypeOptionsTable[i].name))
240
KVSCF_pRetBuffer->setInteger(i);
244
KVSCF_pRetBuffer->setInteger(0);
248
///////////////////////////////////////////////////////////////////////////////////////////////
259
$new(<className>,[<parentHandle> [,<name>[,<param>[,<param>[...]]]])
261
Creates a new instance of the object <class> with
262
the parent object <parent_id> and the specified <name>.[br]
263
<name> and <parent_id> are optional: if not specified, <name>
264
is assumed to be an empty string and <parent_id> default to 0 (parentless object).[br]
265
This function accepts a list of optional parameters that will be passed to the <class>'s constructor.
266
Please see the [doc:objects]objects documentation[/doc] for more information.[br]
269
%myobj = $new(widget,0,pippo)
272
[doc:objects]Objects documentation[/doc], [cmd]delete[/cmd]
275
KVSCF(newCKEYWORDWORKAROUND)
277
// prologue: parameter handling
280
kvs_hobject_t hParent = 0;
281
KviKvsVariantList vList;
283
KVSCF_PARAMETERS_BEGIN
284
KVSCF_PARAMETER("className",KVS_PT_NONEMPTYSTRING,0,szClassName)
285
KVSCF_PARAMETER("parentHandle",KVS_PT_HOBJECT,KVS_PF_OPTIONAL,hParent)
286
KVSCF_PARAMETER("name",KVS_PT_STRING,KVS_PF_OPTIONAL,szName)
287
KVSCF_PARAMETER("parameterList",KVS_PT_VARIANTLIST,KVS_PF_OPTIONAL,vList)
290
KviKvsObjectClass * pClass = KviKvsKernel::instance()->objectController()->lookupClass(szClassName);
293
KVSCF_pContext->error(__tr2qs_ctx("Class '%Q' is not defined","kvs"),&szClassName);
297
KviKvsObject * pParent;
298
if(hParent != (kvs_hobject_t)0)
300
pParent = KviKvsKernel::instance()->objectController()->lookupObject(hParent);
303
KVSCF_pContext->error(__tr2qs_ctx("The specified parent object does not exist","kvs"));
310
KviKvsObject * pObject = pClass->allocateInstance(pParent,szName,KVSCF_pContext,&vList);
312
// epilogue: set the return value
313
KVSCF_pRetBuffer->setHObject(pObject ? pObject->handle() : (kvs_hobject_t)0);
317
///////////////////////////////////////////////////////////////////////////////////////////////
326
Evaluates to an empty variable
330
Evaluates to an empty variable. This is a special value somewhat similar
331
to the [fnc]$null[/fnc] object: it is used to denote absence
332
of information. For example, a function might decide to return
333
a meaningful value when the input parameters are correct and
334
$nothing when the input parameters are wrong.
341
Q_UNUSED(__pContext);
344
KVSCF_pRetBuffer->setNothing();
348
///////////////////////////////////////////////////////////////////////////////////////////////
358
Evaluates to the null object handle.
362
Evaluates to the null object handle. This is a special
363
object handle that is returned by some functions.
364
Please note that writing $null is semantically equivalent to writing 0
365
or even [fnc]$nothing[/fnc] in place of the handle, but $null increases code readability.
367
[fnc]$new[/fnc], [fnc]$nothing[/fnc]
370
KVSCF(nullCKEYWORDWORKAROUND)
372
Q_UNUSED(__pContext);
375
KVSCF_pRetBuffer->setHObject(0);
379
///////////////////////////////////////////////////////////////////////////////////////////////
388
Returns the RESET mIRC control character
392
Returns the RESET mIRC control character (CTRL+O).[br]
394
[fnc]$k[/fnc], [fnc]$b[/fnc], [fnc]$u[/fnc], [fnc]$r[/fnc]
399
Q_UNUSED(__pContext);
402
KVSCF_pRetBuffer->setString(QString(QChar(KviControlCodes::Reset)));
406
///////////////////////////////////////////////////////////////////////////////////////////////
415
Returns the value of an option
417
<string> $option(<optionName:string>)
419
Returns the current value of the internal option named <optionName>.
420
See the [cmd]option[/cmd] command documentation for more info about options.
423
[cmd]echo[/cmd] $option(fontIrcView)
432
// FIXME: This should return a variant in general
433
KVSCF_PARAMETERS_BEGIN
434
KVSCF_PARAMETER("optionName",KVS_PT_NONEMPTYSTRING,0,szOpt)
437
if(g_pApp->getOptionString(szOpt,tmp))KVSCF_pRetBuffer->setString(tmp);
439
KVSCF_pContext->warning(__tr2qs_ctx("There is no option named '%Q'","kvs"),&szOpt);
440
KVSCF_pRetBuffer->setNothing();
445
///////////////////////////////////////////////////////////////////////////////////////////////
454
Retrieves the window ID of a specified query
456
$query[(<target>[,<irc context id>])]
458
Returns the [b]window ID[/b] of the query that has <target>
459
in the list of targets and is bound to the connection specified by
461
If no window matches the specified target or context, and invalid
462
window ID is returned (0).[br]
463
If no <irc context id> is specified, this function looks for
464
the query in the current connection context (if any).[br]
465
If no <target> is specified, this function returns the current
466
query window ID, if executed in a query, else 0.[br]
474
[doc:window_naming_conventions]Window naming conventions[/doc]
480
kvs_uint_t uContextId;
481
KVSCF_PARAMETERS_BEGIN
482
KVSCF_PARAMETER("query_name",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szName)
483
KVSCF_PARAMETER("context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uContextId)
487
if(KVSCF_pParams->count() > 0)
489
if(KVSCF_pParams->count() > 1)
491
KviConsoleWindow * cons = g_pApp->findConsole(uContextId);
492
if(!cons)KVSCF_pContext->warning(__tr2qs_ctx("No such IRC context (%u)","kvs"),uContextId);
494
if(cons->connection())
495
wnd = cons->connection()->findQuery(szName);
500
if(KVSCF_pContext->window()->connection())wnd = KVSCF_pContext->window()->connection()->findQuery(szName);
502
if(!KVSCF_pContext->window()->console())
503
KVSCF_pContext->warning(__tr2qs_ctx("This window is not associated to an IRC context","kvs"));
508
if(KVSCF_pContext->window()->type() == KviWindow::Query)wnd = KVSCF_pContext->window();
511
KVSCF_pRetBuffer->setInteger((kvs_int_t)(wnd ? wnd->numericId() : 0));
515
///////////////////////////////////////////////////////////////////////////////////////////////
524
Returns the REVERSE mIRC control character
528
Returns the REVERSE mIRC control character (CTRL+R).[br]
530
[fnc]$k[/fnc], [fnc]$b[/fnc], [fnc]$u[/fnc], [fnc]$o[/fnc]
535
Q_UNUSED(__pContext);
538
KVSCF_pRetBuffer->setString(QString(QChar(KviControlCodes::Reverse)));
542
///////////////////////////////////////////////////////////////////////////////////////////////
551
Generates a random number
555
Returns a random integer number from 0 to max inclusive.
556
You can repeat sequences of random numbers by calling [cmd]srand[/cmd]
557
with the same seed value. If [cmd]srand[/cmd] has not been called
558
$rand is automatically seeded with value of 1.
559
If no <max> is specified, this function returns an integer between
560
0 and RAND_MAX that is system dependant.
570
// prologue: parameter handling
573
KVSCF_PARAMETERS_BEGIN
574
KVSCF_PARAMETER("max",KVS_PT_UINT,KVS_PF_OPTIONAL,uMax)
576
if(KVSCF_pParams->count() > 0)
578
KVSCF_pRetBuffer->setInteger(::rand() % (uMax+1));
580
KVSCF_pRetBuffer->setInteger(::rand());
585
///////////////////////////////////////////////////////////////////////////////////////////////
594
Casts a variable to a real
596
<real> $real(<data:variant>)
598
Forces <data> to be a "real" data type with the following
601
[li]If <data> is a real, <data> itself is returned.[/li]
602
[li]If <data> is an integer, <data> itself is returned, in its real form.[/li]
603
[li]If <data> is a boolean, its numeric value is returned (either 1.0 or 0.0).[/li]
604
[li]If <data> is an array, the count of its items is returned.[/li]
605
[li]If <data> is a hash, the count of its items is returned.[/li]
606
[li]If <data> is a string, its length is returned.[/li]
607
[li]If <data> is an object, 0.0 is returned if the reference is null (invalid) and 1.0 otherwise[/li]
609
This function is similar to the C (double) cast.[br]
610
Note that since KVIrc does most of the casting work automatically
611
you shouldn't need to use this function.
619
KVSCF_PARAMETERS_BEGIN
620
KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
625
KVSCF_pRetBuffer->setReal(dVal);
628
v->castToInteger(iVal);
629
KVSCF_pRetBuffer->setReal((double)iVal);
641
Returns the realname of the specified user
643
<string> $realname(<nickname:string>)
645
Returns the realname of the specified IRC user IF it is known.[br]
646
The realname is known if [fnc]$isWellKnown[/fnc] returns 1.[br]
647
The realname is generally known if the user is on a channel with you
648
or has an open query with you.[br]
649
Detailed explaination:[br]
650
KVIrc has an internal database of users that are currently
651
visible by *this client*: this includes users on open channels
652
and queries.[br] The other IRC users are NOT in the database:
653
this means that KVIrc knows NOTHING about them and can't return
654
any information immediately. In this case this function will return
656
If a user is in the database, at least his nickname is known.[br]
657
The username and hostname are known only if the server provides that information
658
spontaneously or after a KVIrc request.[br]
659
KVIrc requests user information for all the users in open queries
660
and channels. This information takes some time to be retrieved,
661
in this interval of time KVIrc knows only the user's nickname.
662
This function will return the string "*" in this case.[br]
664
[fnc]$isWellKnown[/fnc], [fnc]$hostname[/fnc], [fnc]$username[/fnc], [cmd]awhois[/cmd]
671
KVSCF_PARAMETERS_BEGIN
672
KVSCF_PARAMETER("nick",KVS_PT_NONEMPTYSTRING,0,szNick)
675
if(KVSCF_pContext->window()->console())
677
if(KVSCF_pContext->window()->console()->isConnected())
679
KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick);
682
KVSCF_pRetBuffer->setString(e->realName());
688
KVSCF_pRetBuffer->setNothing();
700
Returns total received bytes
702
<uint> $receivedBytes()
704
Returns total received bytes
706
[fnc]$sentBytes[/fnc]
711
Q_UNUSED(__pContext);
714
KVSCF_pRetBuffer->setInteger(g_uIncomingTraffic);
725
Sorts an array in reverse order
727
<array> $rsort(<data:array>)
729
Sorts an array in descending order.
738
KVSCF_PARAMETERS_BEGIN
739
KVSCF_PARAMETER("data",KVS_PT_ARRAYCAST,0,a)
744
KviKvsArray * arry = new KviKvsArray(*(a.array()));
746
KVSCF_pRetBuffer->setArray(arry);
748
KVSCF_pRetBuffer->setArray(new KviKvsArray());