1
//=============================================================================
3
// File : kvi_kvs_corefunctions_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-2008 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 "kvi_kvs_corefunctions.h"
28
#include "kvi_kvs_kernel.h"
29
#include "kvi_kvs_arraycast.h"
30
#include "kvi_kvs_object_controller.h"
31
#include "kvi_locale.h"
32
#include "kvi_ircconnection.h"
33
#include "kvi_mirccntrl.h"
34
#include "kvi_options.h"
35
#include "kvi_console.h"
36
#include "kvi_ircuserdb.h"
37
#include "kvi_ircmask.h"
38
#include "kvi_socket.h"
40
#include "kvi_query.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)
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() == KVI_WINDOW_TYPE_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
Please see the [doc:objects]objects documentation[/doc] for more information.[br]
268
%myobj = $new(widget,0,pippo)
271
[doc:objects]Objects documentation[/doc], [cmd]delete[/cmd]
274
KVSCF(newCKEYWORDWORKAROUND)
276
// prologue: parameter handling
279
kvs_hobject_t hParent;
280
KviKvsVariantList vList;
282
KVSCF_PARAMETERS_BEGIN
283
KVSCF_PARAMETER("className",KVS_PT_NONEMPTYSTRING,0,szClassName)
284
KVSCF_PARAMETER("parentHandle",KVS_PT_HOBJECT,KVS_PF_OPTIONAL,hParent)
285
KVSCF_PARAMETER("name",KVS_PT_STRING,KVS_PF_OPTIONAL,szName)
286
KVSCF_PARAMETER("parameterList",KVS_PT_VARIANTLIST,KVS_PF_OPTIONAL,vList)
289
KviKvsObjectClass * pClass = KviKvsKernel::instance()->objectController()->lookupClass(szClassName);
292
KVSCF_pContext->error(__tr2qs_ctx("Class '%Q' is not defined","kvs"),&szClassName);
296
KviKvsObject * pParent;
297
if(hParent != (kvs_hobject_t)0)
299
pParent = KviKvsKernel::instance()->objectController()->lookupObject(hParent);
302
KVSCF_pContext->error(__tr2qs_ctx("The specified parent object does not exist","kvs"));
309
KviKvsObject * pObject = pClass->allocateInstance(pParent,szName,KVSCF_pContext,&vList);
311
// epilogue: set the return value
312
KVSCF_pRetBuffer->setHObject(pObject ? pObject->handle() : (kvs_hobject_t)0);
316
///////////////////////////////////////////////////////////////////////////////////////////////
325
Evaluates to an empty variable
329
Evaluates to an empty variable. This is a special value somewhat similar
330
to the [fnc]$null[/fnc] object: it is used to denote absence
331
of information. For example, a function might decide to return
332
a meaningful value when the input parameters are correct and
333
$nothing when the input parameters are wrong.
340
Q_UNUSED(__pContext);
343
KVSCF_pRetBuffer->setNothing();
347
///////////////////////////////////////////////////////////////////////////////////////////////
357
Evaluates to the null object handle.
361
Evaluates to the null object handle. This is a special
362
object handle that is returned by some functions.
363
Please note that writing $null is semantically equivalent to writing 0
364
or even [fnc]$nothing[/fnc] in place of the handle, but $null increases code readability.
366
[fnc]$new[/fnc], [fnc]$nothing[/fnc]
369
KVSCF(nullCKEYWORDWORKAROUND)
371
Q_UNUSED(__pContext);
374
KVSCF_pRetBuffer->setHObject(0);
378
///////////////////////////////////////////////////////////////////////////////////////////////
387
Returns the RESET mIRC control character
391
Returns the RESET mIRC control character (CTRL+O).[br]
393
[fnc]$k[/fnc], [fnc]$b[/fnc], [fnc]$u[/fnc], [fnc]$r[/fnc]
398
Q_UNUSED(__pContext);
401
KVSCF_pRetBuffer->setString(QString(QChar(KVI_TEXT_RESET)));
405
///////////////////////////////////////////////////////////////////////////////////////////////
414
Returns the value of an option
416
<string> $option(<optionName:string>)
418
Returns the current value of the internal option named <optionName>.
419
See the [cmd]option[/cmd] command documentation for more info about options.
422
[cmd]echo[/cmd] $option(fontIrcView)
431
// FIXME: This should return a variant in general
432
KVSCF_PARAMETERS_BEGIN
433
KVSCF_PARAMETER("optionName",KVS_PT_NONEMPTYSTRING,0,szOpt)
436
if(g_pApp->getOptionString(szOpt,tmp))KVSCF_pRetBuffer->setString(tmp);
438
KVSCF_pContext->warning(__tr2qs_ctx("There is no option named '%Q'","kvs"),&szOpt);
439
KVSCF_pRetBuffer->setNothing();
444
///////////////////////////////////////////////////////////////////////////////////////////////
453
Retrieves the window ID of a specified query
455
$query[(<target>[,<irc context id>])]
457
Returns the [b]window ID[/b] of the query that has <target>
458
in the list of targets and is bound to the connection specified by
460
If no window matches the specified target or context, and invalid
461
window ID is returned (0).[br]
462
If no <irc context id> is specified, this function looks for
463
the query in the current connection context (if any).[br]
464
If no <target> is specified, this function returns the current
465
query window ID, if executed in a query, else 0.[br]
473
[doc:window_naming_conventions]Window naming conventions[/doc]
479
kvs_uint_t uContextId;
480
KVSCF_PARAMETERS_BEGIN
481
KVSCF_PARAMETER("query_name",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szName)
482
KVSCF_PARAMETER("context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uContextId)
486
if(KVSCF_pParams->count() > 0)
488
if(KVSCF_pParams->count() > 1)
490
KviConsole * cons = g_pApp->findConsole(uContextId);
491
if(!cons)KVSCF_pContext->warning(__tr2qs_ctx("No such IRC context (%u)","kvs"),uContextId);
493
if(cons->connection())
494
wnd = cons->connection()->findQuery(szName);
499
if(KVSCF_pContext->window()->connection())wnd = KVSCF_pContext->window()->connection()->findQuery(szName);
501
if(!KVSCF_pContext->window()->console())
502
KVSCF_pContext->warning(__tr2qs_ctx("This window is not associated to an IRC context","kvs"));
507
if(KVSCF_pContext->window()->type() == KVI_WINDOW_TYPE_QUERY)wnd = KVSCF_pContext->window();
510
KVSCF_pRetBuffer->setInteger((kvs_int_t)(wnd ? wnd->numericId() : 0));
514
///////////////////////////////////////////////////////////////////////////////////////////////
523
Returns the REVERSE mIRC control character
527
Returns the REVERSE mIRC control character (CTRL+R).[br]
529
[fnc]$k[/fnc], [fnc]$b[/fnc], [fnc]$u[/fnc], [fnc]$o[/fnc]
534
Q_UNUSED(__pContext);
537
KVSCF_pRetBuffer->setString(QString(QChar(KVI_TEXT_REVERSE)));
541
///////////////////////////////////////////////////////////////////////////////////////////////
550
Generates a random number
554
Returns a random integer number from 0 to max inclusive.
555
You can repeat sequences of random numbers by calling [cmd]srand[/cmd]
556
with the same seed value. If [cmd]srand[/cmd] has not been called
557
$rand is automatically seeded with value of 1.
558
If no <max> is specified, this function returns an integer between
559
0 and RAND_MAX that is system dependant.
569
// prologue: parameter handling
572
KVSCF_PARAMETERS_BEGIN
573
KVSCF_PARAMETER("max",KVS_PT_UINT,KVS_PF_OPTIONAL,uMax)
575
if(KVSCF_pParams->count() > 0)
577
KVSCF_pRetBuffer->setInteger(::rand() % (uMax+1));
579
KVSCF_pRetBuffer->setInteger(::rand());
584
///////////////////////////////////////////////////////////////////////////////////////////////
593
Casts a variable to a real
595
<real> $real(<data:variant>)
597
Forces <data> to be a "real" data type with the following
600
[li]If <data> is a real, <data> itself is returned.[/li]
601
[li]If <data> is an integer, <data> itself is returned, in its real form.[/li]
602
[li]If <data> is a boolean, its numeric value is returned (either 1.0 or 0.0).[/li]
603
[li]If <data> is an array, the count of its items is returned.[/li]
604
[li]If <data> is a hash, the count of its items is returned.[/li]
605
[li]If <data> is a string, its length is returned.[/li]
606
[li]If <data> is an object, 0.0 is returned if the reference is null (invalid) and 1.0 otherwise[/li]
608
This function is similar to the C (double) cast.[br]
609
Note that since KVIrc does most of the casting work automatically
610
you shouldn't need to use this function.
618
KVSCF_PARAMETERS_BEGIN
619
KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
624
KVSCF_pRetBuffer->setReal(dVal);
627
v->castToInteger(iVal);
628
KVSCF_pRetBuffer->setReal((double)iVal);
640
Returns the realname of the specified user
642
<string> $realname(<nickname:string>)
644
Returns the realname of the specified IRC user IF it is known.[br]
645
The realname is known if [fnc]$isWellKnown[/fnc] returns 1.[br]
646
The realname is generally known if the user is on a channel with you
647
or has an open query with you.[br]
648
Detailed explaination:[br]
649
KVIrc has an internal database of users that are currently
650
visible by *this client*: this includes users on open channels
651
and queries.[br] The other IRC users are NOT in the database:
652
this means that KVIrc knows NOTHING about them and can't return
653
any information immediately. In this case this function will return
655
If a user is in the database, at least his nickname is known.[br]
656
The username and hostname are known only if the server provides that information
657
spontaneously or after a KVIrc request.[br]
658
KVIrc requests user information for all the users in open queries
659
and channels. This information takes some time to be retrieved,
660
in this interval of time KVIrc knows only the user's nickname.
661
This function will return the string "*" in this case.[br]
663
[fnc]$isWellKnown[/fnc], [fnc]$hostname[/fnc], [fnc]$username[/fnc], [cmd]awhois[/cmd]
670
KVSCF_PARAMETERS_BEGIN
671
KVSCF_PARAMETER("nick",KVS_PT_NONEMPTYSTRING,0,szNick)
674
if(KVSCF_pContext->window()->console())
676
if(KVSCF_pContext->window()->console()->isConnected())
678
KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick);
681
KVSCF_pRetBuffer->setString(e->realName());
687
KVSCF_pRetBuffer->setNothing();
699
Returns total received bytes
701
<uint> $receivedBytes()
703
Returns total received bytes
705
[fnc]$sentBytes[/fnc]
710
Q_UNUSED(__pContext);
713
KVSCF_pRetBuffer->setInteger(g_uIncomingTraffic);
724
Sorts an array in reverse order
726
<array> $rsort(<data:array>)
728
Sorts an array in descending order.
737
KVSCF_PARAMETERS_BEGIN
738
KVSCF_PARAMETER("data",KVS_PT_ARRAYCAST,0,a)
743
KviKvsArray * arry = new KviKvsArray(*(a.array()));
745
KVSCF_pRetBuffer->setArray(arry);
747
KVSCF_pRetBuffer->setArray(new KviKvsArray());