1
//=============================================================================
3
// File : KviKvsCoreFunctions_gl.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 "KviKvsTimerManager.h"
30
#include "KviKvsArrayCast.h"
31
#include "KviLocale.h"
33
#include "KviApplication.h"
34
#include "KviChannelWindow.h"
35
#include "KviConsoleWindow.h"
36
#include "KviIconManager.h"
37
#include "KviControlCodes.h"
38
#include "KviMainWindow.h"
39
#include "KviTimeUtils.h"
40
#include "KviKvsEventManager.h"
41
#include "KviKvsEventHandler.h"
42
#include "KviLagMeter.h"
46
namespace KviKvsCoreFunctions
48
///////////////////////////////////////////////////////////////////////////////////////////////
57
Dump globals variables
61
Returns an hashtable of all global variables set.
74
KVSCF_pRetBuffer->setHash(new KviKvsHash( *(KviKvsKernel::instance()->globalVariables()) ));
78
///////////////////////////////////////////////////////////////////////////////////////////////
87
Explicitly creates a hash
89
<hash> $hash([<key:string>,<value:variant>,[<key:string>,<value:variant>]],...)
91
Returns a hash with the specified items. Each <value> is indexed by the preceeding <key>.
92
This is just an explicit way of creating a hash with a defined set of items,
93
useful for increasing readability.
96
[cmd]alias[/cmd](test){ [cmd]return[/cmd] $hash(1,X,2,Y,3,Z); };
98
[cmd]foreach[/cmd](%y,%x)
102
[cmd]foreach[/cmd](%y,[fnc]$keys[/fnc](%x))
113
Q_UNUSED(__pContext);
115
KviKvsHash * a = new KviKvsHash();
117
for(KviKvsVariant * key = KVSCF_pParams->first();key;key = KVSCF_pParams->next())
119
KviKvsVariant * val = KVSCF_pParams->next();
123
a->set(sz,new KviKvsVariant(*val));
126
KVSCF_pRetBuffer->setHash(a);
130
///////////////////////////////////////////////////////////////////////////////////////////////
139
Returns a decoded hex string
141
$hexToAscii(<hex_encoded_string>)
143
Decodes the <hex_encoded_string> to its ASCII representation.
146
[cmd]echo[/cmd] $hexToAscii(6B76697263)
149
[fnc]$asciiToHex[/fnc]
155
KVSCF_PARAMETERS_BEGIN
156
KVSCF_PARAMETER("hex_encoded_string",KVS_PT_STRING,0,szHex)
159
KviCString tmp1(szHex);
161
int len = tmp1.hexToBuffer(&buf,true);
162
KVSCF_pRetBuffer->setString(QString(QByteArray(buf,len)));
163
KviCString::freeBuffer(buf);
167
///////////////////////////////////////////////////////////////////////////////////////////////
176
Returns the hostname of the specified user
178
<string> $hostname(<nickname:string>)
180
Returns the hostname of the specified IRC user IF it is known.[br]
181
The hostname is known if [fnc]$isWellKnown[/fnc] returns 1.[br]
182
The hostname is generally known if the user is on a channel with you
183
or has an open query with you.[br]
184
Detailed explaination:[br]
185
KVIrc has an internal database of users that are currently
186
visible by *this client*: this includes users on open channels
187
and queries.[br] The other IRC users are NOT in the database:
188
this means that KVIrc knows NOTHING about them and can't return
189
any information immediately. In this case this function will return
191
If a user is in the database, at least his nickname is known.[br]
192
The username and hostname are known only if the server provides that information
193
spontaneously or after a KVIrc request.[br]
194
KVIrc requests user information for all the users in open queries
195
and channels. This information takes some time to be retrieved,
196
in this interval of time KVIrc knows only the user's nickname.
197
This function will return the string "*" in this case.[br]
199
[fnc]$isWellKnown[/fnc], [fnc]$username[/fnc], [cmd]awhois[/cmd]
206
KVSCF_PARAMETERS_BEGIN
207
KVSCF_PARAMETER("nick",KVS_PT_NONEMPTYSTRING,0,szNick)
211
KVSCF_pContext->window()->console() &&
212
KVSCF_pContext->window()->console()->isConnected())
214
KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick);
217
KVSCF_pRetBuffer->setString(e->host());
222
KVSCF_pRetBuffer->setNothing();
226
//-------------------------------------------------
234
Returns the lag on the current server
238
This function returns the lag in the current server, in milliseconds.[br]
245
if(!KVSCF_pContext->window()->console()) return KVSCF_pContext->errorNoIrcContext();
246
if(!KVSCF_pContext->window()->console()->connection()) return KVSCF_pContext->warningNoIrcConnection();
247
if(!KVSCF_pContext->window()->console()->connection()->lagMeter())
249
KVSCF_pContext->warning(__tr2qs_ctx("Lag meter was not enabled","kvs"));
253
KVSCF_pRetBuffer->setInteger( KVSCF_pContext->window()->console()->connection()->lagMeter()->lag());
256
/////////////////////////////////////////////////////////////////////////////////////////
265
Returns a high precision timestamp
269
Returns a high precision timestamp as a floaint point value.
270
The timestamp rappresent the number of seconds elapsed since
271
a certain moment in the past. The number of seconds
272
contains a "high precision" fractional part. The "certain moment" definition
273
depends on the platform KVIrc is executed. This means that
274
this timestamp is totally useless to rappresent a time value
275
but is useful to compute time intervals with sub-second precision.
279
[cmd]echo[/cmd] $($hptimestamp - %tmp)
282
[fnc]$unixTime[/fnc], [fnc]$date[/fnc]
287
Q_UNUSED(__pContext);
291
kvi_gettimeofday(&tv,0);
292
kvs_real_t dTimestamp = (kvs_real_t)(tv.tv_sec);
293
dTimestamp += (((kvs_real_t)(tv.tv_usec)) / 1000000.0);
294
KVSCF_pRetBuffer->setReal(dTimestamp);
298
///////////////////////////////////////////////////////////////////////////////////////////////
307
Returns the ID of an icon
309
<integer> $icon(<iconname:string>)
311
Returns the ID of the icon <iconname>.
312
You should always use this function where an <image_id> is required
313
and you want to use an internal icon.
314
See [fnc]$iconname[/fnc] for a list of the icon names supported by kvirc.[br]
317
[cmd]echo[/cmd] $icon(linux)
326
KVSCF_PARAMETERS_BEGIN
327
KVSCF_PARAMETER("iconName",KVS_PT_NONEMPTYSTRING,0,szName)
330
KVSCF_pRetBuffer->setInteger(g_pIconManager->getSmallIconIdFromName(szName));
334
///////////////////////////////////////////////////////////////////////////////////////////////
343
Returns the name of an icon
347
Returns the name of a builtin icon given an <iconid>.
348
The <iconid> is returned by the function [fnc]$icon[/fnc].
349
If the <iconid> is not valid, an empty name is returned.[br]
350
The following code will list all the available icon names:[br]
354
%name = $iconname(%i)
355
echo The icon by ID %i is named %name
357
} [cmd]while[/cmd](%name != "")
361
[cmd]echo[/cmd] $iconname(24)
362
[cmd]echo[/cmd] $iconname([fnc]$icon[/fnc](linux))
371
KVSCF_PARAMETERS_BEGIN
372
KVSCF_PARAMETER("iconid",KVS_PT_UINT,0,uIco)
375
if(uIco < KviIconManager::IconCount)
377
KVSCF_pRetBuffer->setString(g_pIconManager->getSmallIconName(uIco));
379
KVSCF_pRetBuffer->setNothing();
385
///////////////////////////////////////////////////////////////////////////////////////////////
394
Casts a variable to an integer
396
<integer> $integer(<data:variant>)
398
Forces <data> to be an integer data type with the following
401
[li]If <data> is an integer, <data> itself is returned.[/li]
402
[li]If <data> is a boolean, its numeric value is returned (either 1 or 0).[/li]
403
[li]If <data> is a real, its integer part is returned.[/li]
404
[li]If <data> is an array, the count of its items is returned.[/li]
405
[li]If <data> is a hash, the count of its items is returned.[/li]
406
[li]If <data> is a string, its length is returned.[/li]
407
[li]If <data> is an object, 0 is returned if the reference is null (invalid) and 1 otherwise[/li]
409
This function is similar to the C (int) cast and is internally
410
aliased to [fnc]$int[/fnc]() too.
411
Note that since KVIrc does most of the casting work automatically
412
you shouldn't need to use this function.
425
Casts a variable to an integer
427
<integer> $int(<data:variant>)
429
This is an internal alias to [fnc]$integer[/fnc]().
438
KVSCF_PARAMETERS_BEGIN
439
KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
443
v->castToInteger(iVal);
444
KVSCF_pRetBuffer->setInteger(iVal);
448
///////////////////////////////////////////////////////////////////////////////////////////////
451
@doc: isMainWindowActive
457
Checks if main KVirc window is active
459
<boolean> $isMainWindowActive()
461
Returns true if the KVIrc's window is currently
462
the active window on the current desktop.
465
KVSCF(isMainWindowActive)
467
Q_UNUSED(__pContext);
470
KVSCF_pRetBuffer->setBoolean(g_pMainWindow->isActiveWindow());
474
///////////////////////////////////////////////////////////////////////////////////////////////
477
@doc: isMainWindowMinimized
481
$isMainWindowMinimized
483
Checks if main KVirc window is minimized
485
<boolean> $isMainWindowMinimized()
487
Returns true if main KVirc window is minimized and false otherwise.
490
KVSCF(isMainWindowMinimized)
492
Q_UNUSED(__pContext);
495
KVSCF_pRetBuffer->setBoolean(g_pMainWindow->isMinimized());
499
///////////////////////////////////////////////////////////////////////////////////////////////
508
Checks if a variable is set (empty or non empty)
510
<boolean> $isEmpty(<data:variant>)
512
Returns 0 if <data> is actually set to some non-empty value
513
and 1 otherwise. Since KVIrc treats unset variables as empty
514
ones then this function is the exact opposite of [fnc]$isSet[/fnc].
522
KVSCF_PARAMETERS_BEGIN
523
KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
526
KVSCF_pRetBuffer->setBoolean(v->isEmpty());
529
//FIXME: documentation
531
///////////////////////////////////////////////////////////////////////////////////////////////
540
Checks if an event enabled
542
<boolean> $isEventEnabled(<event_name:string>,<handler_name:string>)
544
Returns 1 if the event handler enabled
547
///////////////////////////////////////////////////////////////////////////////////////////////
549
KVSCF(isEventEnabled)
551
QString szEventName,szHandlerName;
552
KviKvsScriptEventHandler *h=0;
554
KVSCF_PARAMETERS_BEGIN
555
KVSCF_PARAMETER("event_name",KVS_PT_NONEMPTYSTRING,0,szEventName)
556
KVSCF_PARAMETER("handler_name",KVS_PT_NONEMPTYSTRING,0,szHandlerName)
560
int iNumber = szEventName.toInt(&bOk);
561
bool bIsRaw = (bOk && (iNumber >= 0) && (iNumber < 1000));
565
if(!KviKvsEventManager::instance()->isValidRawEvent(iNumber))
567
KVSCF_pContext->warning(__tr2qs_ctx("No such event (%Q)","kvs"),&szEventName);
569
h=KviKvsEventManager::instance()->findScriptRawHandler(iNumber,szHandlerName);
572
iNumber = KviKvsEventManager::instance()->findAppEventIndexByName(szEventName);
573
if(!KviKvsEventManager::instance()->isValidAppEvent(iNumber))
575
KVSCF_pContext->warning(__tr2qs_ctx("No such event (%Q)","kvs"),&szEventName);
577
h=KviKvsEventManager::instance()->findScriptAppHandler(iNumber,szHandlerName);
581
KVSCF_pRetBuffer->setBoolean(h->isEnabled());
583
KVSCF_pContext->warning(__tr2qs_ctx("No such event handler (%Q) for event %Q","kvs"),&szHandlerName,&szEventName);
588
///////////////////////////////////////////////////////////////////////////////////////////////
597
<boolean> $isNumeric(<data:variant>)
599
Finds whether a variable contains a rappresentation of a number
601
Returns 1 if the <data> is an integer or a real number, 0 otherwise.
607
KVSCF_PARAMETERS_BEGIN
608
KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
612
KVSCF_pRetBuffer->setBoolean(v->asNumber(n));
616
///////////////////////////////////////////////////////////////////////////////////////////////
625
Checks if a variable is set (non-empty)
627
<boolean> $isSet(<data:variant>)
629
Returns 1 if <data> is actually set to some non-empty value
630
and 0 otherwise. If <data> is a variable, then this function
631
simply checks if the variable is set. If <data> is a constant
632
then this function checks if the constant is non empty.
633
Since KVIrc treats empty strings as "unset" values then
634
this function could be also called "isNonEmpty" and it is
635
the perfect opposite of [fnc]$isEmpty[/fnc]
637
[fnc]$isEmpty[/fnc], [cmd]unset[/cmd]
643
KVSCF_PARAMETERS_BEGIN
644
KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
647
KVSCF_pRetBuffer->setBoolean(!v->isEmpty());
651
///////////////////////////////////////////////////////////////////////////////////////////////
660
Checks for a timer existence
662
<boolean> $istimer(<name:string>)
664
Returns 1 if the timer named <name> is actually running, else 0
666
[cmd]timer[/cmd], [cmd]killtimer[/cmd]
673
KVSCF_PARAMETERS_BEGIN
674
KVSCF_PARAMETER("timerName",KVS_PT_NONEMPTYSTRING,0,szName)
676
KVSCF_pRetBuffer->setBoolean(KviKvsTimerManager::instance()->timerExists(szName));
680
///////////////////////////////////////////////////////////////////////////////////////////////
689
Returns $true if the specified user is well known
691
<bool> $isWellKnown(<nickname:string>)
693
Returns 1 if KVIrc has the basic user information about the specified <nickname>.[br]
694
The basic information include the username and hostname.[br]
695
This is almost always true if the user is on a channel with you or
696
you have an open query with him.[br]
697
If $isWellKnown returns 0, [fnc]$username[/fnc] and [fnc]$hostname[/fnc]
698
will return empty strings.[br]
699
In this case you must use [cmd]awhois[/cmd] to obtain the user basic information.[br]
706
KVSCF_PARAMETERS_BEGIN
707
KVSCF_PARAMETER("nick",KVS_PT_NONEMPTYSTRING,0,szNick)
710
if(KVSCF_pContext->window()->console())
712
if(KVSCF_pContext->window()->console()->isConnected())
714
KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick);
717
KVSCF_pRetBuffer->setBoolean(e->hasHost() && e->hasUser());
723
KVSCF_pRetBuffer->setBoolean(false);
727
///////////////////////////////////////////////////////////////////////////////////////////////
736
Returns the COLOR mIRC control character
738
<string> $k(<foreground:integer>[,<background:integer>])
741
Returns the COLOR mIRC control character (Qt::CTRL+K).[br]
742
If <foreground> and <background> are passed, a standard mIRC
743
color escape is returned.[br]
750
kvs_uint_t iFore,iBack;
751
KVSCF_PARAMETERS_BEGIN
752
KVSCF_PARAMETER("foreground",KVS_PT_UINT,KVS_PF_OPTIONAL,iFore)
753
KVSCF_PARAMETER("background",KVS_PT_UINT,KVS_PF_OPTIONAL,iBack)
756
QString szRet = QChar(KviControlCodes::Color);
757
if(KVSCF_pParams->count() > 0)
759
KviQString::appendFormatted(szRet,"%u",iFore);
760
if(KVSCF_pParams->count() > 1)
761
KviQString::appendFormatted(szRet,",%u",iBack);
763
KVSCF_pRetBuffer->setString(szRet);
767
///////////////////////////////////////////////////////////////////////////////////////////////
776
Returns the array of keys of a hash
778
<array> $keys(<hash_value:hash>)
780
Returns an array with the keys of the <hash> parameter.
781
<hash> must be obviously a hash (or eventually an empty variable
782
that is treated as an empty hash).
789
KviKvsHash * pHash = 0;
790
KVSCF_PARAMETERS_BEGIN
791
KVSCF_PARAMETER("hash",KVS_PT_HASH,KVS_PF_OPTIONAL,pHash)
794
KviKvsArray * a = new KviKvsArray();
796
// we have to support an empty hash, returning an empty array (ticket #940)
800
KviKvsHashIterator it(*(pHash->dict()));
803
a->set(idx,new KviKvsVariant(it.currentKey()));
808
KVSCF_pRetBuffer->setArray(a);
811
///////////////////////////////////////////////////////////////////////////////////////////////
820
Returns name of currently used language
822
<string> $lang([<type:string>])
824
Returns the short name of currently used language
825
Type <type> should be one of: [br]
826
"full" - returns full locale name, such as ru_RU.UTF-8 (default)
827
"lang" - return language name, such as "ru_RU"
828
"short" - returns only language group such as "ru"
835
KVSCF_PARAMETERS_BEGIN
836
KVSCF_PARAMETER("type",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szType)
839
QString szLocale(KviLocale::localeName().ptr());
840
if(szType=="lang") KVSCF_pRetBuffer->setString(szLocale.left(5));
841
else if(szType=="short") KVSCF_pRetBuffer->setString(szLocale.left(2));
842
else KVSCF_pRetBuffer->setString(szLocale);
849
///////////////////////////////////////////////////////////////////////////////////////////////
858
Returns the length of a variable
860
<integer> $length(<value:variant>)
862
When <value> is an array or a hash, it returns the number
863
of its elements. When <value> is an object reference it returns 0.
864
In all the other cases <value> is interpreted
865
as a string and its length is returned.
872
KviKvsVariant * pVar;
873
KVSCF_PARAMETERS_BEGIN
874
KVSCF_PARAMETER("value",KVS_PT_VARIANT,0,pVar)
879
case KviKvsVariantData::Array:
880
KVSCF_pRetBuffer->setInteger(pVar->array()->size());
882
case KviKvsVariantData::Hash:
883
KVSCF_pRetBuffer->setInteger(pVar->hash()->size());
885
case KviKvsVariantData::HObject:
886
KVSCF_pRetBuffer->setInteger(0);
892
KVSCF_pRetBuffer->setInteger(tmp.length());
899
///////////////////////////////////////////////////////////////////////////////////////////////
908
Returns a line feed character
912
Returns a line feed character
914
[fnc]$cr[/fnc], [fnc]$ascii[/fnc], [fnc]$char[/fnc]
919
Q_UNUSED(__pContext);
922
KVSCF_pRetBuffer->setString(QString(QChar('\n')));
926
///////////////////////////////////////////////////////////////////////////////////////////////
935
Returns the text specified as a link
937
<string> $link(<text:string>[,<type:string>])
939
Gets the text you pass as the first parameter and formats it
940
so that KVIrc will display it as a link. You can specify the
941
type of the link as the second parameter. Valid values for the
942
second parameter are:[br]
944
[li]nick link to a nickname[/li]
945
[li]channel link to a channel name[/li]
946
[li]host link to an host[/li]
947
[li]url (default) link to an url[/li]
949
Please note that the text you get as the output is valid only
950
when interpreted locally. Sending this text to the server you can
951
get unpredictable results, depending on the irc client other people
953
The returned string format is described by [doc:escape_sequences]the escape sequences documentation[/doc]
954
and you might also take a look at [fnc]$fmtlink[/fnc] which has related functionality.
956
echo $link(pragma,nick)
958
[cmd]echo[/cmd], [fnc]$fmtlink[/fnc].
963
QString szData, szType;
964
KVSCF_PARAMETERS_BEGIN
965
KVSCF_PARAMETER("text",KVS_PT_NONEMPTYSTRING,0,szData)
966
KVSCF_PARAMETER("type",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szType)
969
QString szRet("\r!");
973
} else if(szType=="channel") {
975
} else if(szType=="host") {
980
szRet.append(szData);
982
KVSCF_pRetBuffer->setString(szRet);
993
Returns a list of the active timers
995
<array> $listtimers([<name:string>][,<flags:string>])
997
Returns a list of all the active timers.[br]
998
If you specify a <name>, only timers matching that name are returned;
999
You can use the * and ? wildcards in <name>.[br]
1000
<flags> can be any combination of the characters 's' and 'r'[br]
1001
If the flag 'r' is specified then <name> is assumed to be a standard regular expression.
1002
If the flag 's' is specified the matches are case sensitive.[br]
1005
[cmd]timer[/cmd], [fnc]$isTimer[/fnc], [cmd]killtimer[/cmd], [cmd]listtimers[/cmd]
1010
QString szText, szFlags;
1011
KVSCF_PARAMETERS_BEGIN
1012
KVSCF_PARAMETER("text",KVS_PT_STRING,KVS_PF_OPTIONAL,szText)
1013
KVSM_PARAMETER("flags",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags)
1014
KVSCF_PARAMETERS_END
1016
bool bCaseSensitive = szFlags.indexOf('s',0,Qt::CaseInsensitive) != -1;
1017
bool bRegexp = szFlags.indexOf('r',0,Qt::CaseInsensitive) != -1;
1018
bool bMatch = !szText.isEmpty();
1020
KviPointerHashTable<QString,KviKvsTimer> * pTimerDict = KviKvsTimerManager::instance()->timerDict();
1021
KviKvsArray * a = new KviKvsArray();
1026
QRegExp re(szText,bCaseSensitive?Qt::CaseSensitive:Qt::CaseInsensitive,bRegexp?QRegExp::RegExp:QRegExp::Wildcard);
1027
KviPointerHashTableIterator<QString,KviKvsTimer> it(*pTimerDict);
1030
while(KviKvsTimer * pTimer = it.current())
1034
if(!re.exactMatch(pTimer->name()))
1041
a->set(idx,new KviKvsVariant(pTimer->name()));
1046
KVSCF_pRetBuffer->setArray(a);