~ubuntu-branches/ubuntu/trusty/kvirc/trusty

« back to all changes in this revision

Viewing changes to src/kvirc/kvs/KviKvsCoreFunctions_gl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kai Wasserbäch, Kai Wasserbäch, Raúl Sánchez Siles
  • Date: 2011-02-12 10:40:21 UTC
  • mfrom: (14.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110212104021-5mh4f75jlku20mnt
The combined "Twisted Experiment" and "Nocturnal Raid" release.

[ Kai Wasserbäch ]
* Synced to upstream's SVN revision 5467.
* debian/rules:
  - Added .PHONY line.
  - Resurrect -DMANUAL_REVISION, got lost somewhere and we build SVN
    revisions again.
  - Replace "-DWITH_NO_EMBEDDED_CODE=YES" with "-DWANT_CRYPTOPP=YES".
  - Change the remaining -DWITH/-DWITHOUT to the new -DWANT syntax.
* debian/control:
  - Removed DMUA, I'm a DD now.
  - Changed my e-mail address.
  - Removed unneeded relationships (no upgrades over two releases are
    supported).
  - Fix Suggests for kvirc-dbg.
  - kvirc-data: Make the "Suggests: kvirc" a Recommends, doesn't make much
    sense to install the -data package without the program.
* debian/source/local-options: Added with "unapply-patches".
* debian/kvirc.lintian-overrides: Updated to work for 4.1.1.
* debian/patches/21_make_shared-mime-info_B-D_superfluous.patch: Updated.
* debian/kvirc-data.install: Added .notifyrc.

[ Raúl Sánchez Siles ]
* Stating the right version where kvirc-data break and replace should happen.
* Fixing link to license file.
* Added French and Portuguese man pages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=============================================================================
 
2
//
 
3
//   File : KviKvsCoreFunctions_gl.cpp
 
4
//   Creation date : Fri 31 Oct 2003 01:52:04 by Szymon Stefanek
 
5
//
 
6
//   This file is part of the KVIrc IRC client distribution
 
7
//   Copyright (C) 2003-2010 Szymon Stefanek <pragma at kvirc dot net>
 
8
//
 
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.
 
13
//
 
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.
 
18
//
 
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.
 
22
//
 
23
//=============================================================================
 
24
 
 
25
 
 
26
 
 
27
#include "KviKvsCoreFunctions.h"
 
28
#include "KviKvsKernel.h"
 
29
#include "KviKvsTimerManager.h"
 
30
#include "KviKvsArrayCast.h"
 
31
#include "KviLocale.h"
 
32
#include "KviInput.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"
 
43
 
 
44
#include <QRegExp>
 
45
 
 
46
namespace KviKvsCoreFunctions
 
47
{
 
48
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
49
 
 
50
        /*
 
51
                @doc: globals
 
52
                @type:
 
53
                        function
 
54
                @title:
 
55
                        $globals
 
56
                @short:
 
57
                        Dump globals variables
 
58
                @syntax:
 
59
                        <hash> $globals()
 
60
                @description:
 
61
                        Returns an hashtable of all global variables set.
 
62
                @examples:
 
63
                        echo $globals()
 
64
                @seealso:
 
65
                        [cmd]global[/cmd]
 
66
                        [fnc]$hash[/fnc]
 
67
        */
 
68
 
 
69
        KVSCF(globals)
 
70
        {
 
71
                Q_UNUSED(__pContext);
 
72
                Q_UNUSED(__pParams);
 
73
 
 
74
                KVSCF_pRetBuffer->setHash(new KviKvsHash( *(KviKvsKernel::instance()->globalVariables()) ));
 
75
                return true;
 
76
        }
 
77
 
 
78
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
79
 
 
80
        /*
 
81
                @doc: hash
 
82
                @type:
 
83
                        function
 
84
                @title:
 
85
                        $hash
 
86
                @short:
 
87
                        Explicitly creates a hash
 
88
                @syntax:
 
89
                        <hash> $hash([<key:string>,<value:variant>,[<key:string>,<value:variant>]],...)
 
90
                @description:
 
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.
 
94
                @examples:
 
95
                        [example]
 
96
                                [cmd]alias[/cmd](test){ [cmd]return[/cmd] $hash(1,X,2,Y,3,Z); };
 
97
                                %x = $test();
 
98
                                [cmd]foreach[/cmd](%y,%x)
 
99
                                {
 
100
                                        [cmd]echo[/cmd] %y;
 
101
                                }
 
102
                                [cmd]foreach[/cmd](%y,[fnc]$keys[/fnc](%x))
 
103
                                {
 
104
                                        [cmd]echo[/cmd] %y
 
105
                                }
 
106
                        [/example]
 
107
                @seealso:
 
108
                        [fnc]$array[/fnc]
 
109
        */
 
110
 
 
111
        KVSCF(hash)
 
112
        {
 
113
                Q_UNUSED(__pContext);
 
114
 
 
115
                KviKvsHash * a = new KviKvsHash();
 
116
 
 
117
                for(KviKvsVariant * key = KVSCF_pParams->first();key;key = KVSCF_pParams->next())
 
118
                {
 
119
                        KviKvsVariant * val = KVSCF_pParams->next();
 
120
                        if(!val)break;
 
121
                        QString sz;
 
122
                        key->asString(sz);
 
123
                        a->set(sz,new KviKvsVariant(*val));
 
124
                }
 
125
 
 
126
                KVSCF_pRetBuffer->setHash(a);
 
127
                return true;
 
128
        }
 
129
 
 
130
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
131
 
 
132
        /*
 
133
                @doc: hextoAscii
 
134
                @type:
 
135
                        function
 
136
                @title:
 
137
                        $hexToAscii
 
138
                @short:
 
139
                        Returns a decoded hex string
 
140
                @syntax:
 
141
                        $hexToAscii(<hex_encoded_string>)
 
142
                @description:
 
143
                        Decodes the <hex_encoded_string> to its ASCII representation.
 
144
                @examples:
 
145
                        [example]
 
146
                                [cmd]echo[/cmd] $hexToAscii(6B76697263)
 
147
                        [/example]
 
148
                @seealso:
 
149
                        [fnc]$asciiToHex[/fnc]
 
150
        */
 
151
 
 
152
        KVSCF(hexToAscii)
 
153
        {
 
154
                QString szHex;
 
155
                KVSCF_PARAMETERS_BEGIN
 
156
                        KVSCF_PARAMETER("hex_encoded_string",KVS_PT_STRING,0,szHex)
 
157
                KVSCF_PARAMETERS_END
 
158
 
 
159
                KviCString tmp1(szHex);
 
160
                char * buf;
 
161
                int len = tmp1.hexToBuffer(&buf,true);
 
162
                KVSCF_pRetBuffer->setString(QString(QByteArray(buf,len)));
 
163
                KviCString::freeBuffer(buf);
 
164
                return true;
 
165
        }
 
166
 
 
167
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
168
 
 
169
        /*
 
170
                @doc: hostname
 
171
                @type:
 
172
                        function
 
173
                @title:
 
174
                        $hostname
 
175
                @short:
 
176
                        Returns the hostname of the specified user
 
177
                @syntax:
 
178
                        <string> $hostname(<nickname:string>)
 
179
                @description:
 
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
 
190
                        an EMPTY string.[br]
 
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]
 
198
                @seealso:
 
199
                        [fnc]$isWellKnown[/fnc], [fnc]$username[/fnc], [cmd]awhois[/cmd]
 
200
        */
 
201
 
 
202
        KVSCF(hostname)
 
203
        {
 
204
                QString szNick;
 
205
 
 
206
                KVSCF_PARAMETERS_BEGIN
 
207
                        KVSCF_PARAMETER("nick",KVS_PT_NONEMPTYSTRING,0,szNick)
 
208
                KVSCF_PARAMETERS_END
 
209
 
 
210
                if(
 
211
                                KVSCF_pContext->window()->console() &&
 
212
                                KVSCF_pContext->window()->console()->isConnected())
 
213
                {
 
214
                                KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick);
 
215
                                if(e)
 
216
                                {
 
217
                                        KVSCF_pRetBuffer->setString(e->host());
 
218
                                        return true;
 
219
                                }
 
220
                }
 
221
 
 
222
                KVSCF_pRetBuffer->setNothing();
 
223
                return true;
 
224
        }
 
225
 
 
226
        //-------------------------------------------------
 
227
        /*
 
228
        @doc: lag
 
229
        @type:
 
230
                        function
 
231
        @title:
 
232
                        $lag
 
233
        @short:
 
234
                Returns the lag on the current server
 
235
        @syntax:
 
236
                <integer> lag
 
237
        @description:
 
238
                This function returns the lag in the current server, in milliseconds.[br]
 
239
        */
 
240
 
 
241
        KVSCF(lag)
 
242
        {
 
243
                Q_UNUSED(__pParams);
 
244
 
 
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())
 
248
                {
 
249
                        KVSCF_pContext->warning(__tr2qs_ctx("Lag meter was not enabled","kvs"));
 
250
                        return false;
 
251
                }
 
252
 
 
253
                KVSCF_pRetBuffer->setInteger( KVSCF_pContext->window()->console()->connection()->lagMeter()->lag());
 
254
                return true;
 
255
        }
 
256
        /////////////////////////////////////////////////////////////////////////////////////////
 
257
 
 
258
        /*
 
259
                @doc: hptimestamp
 
260
                @type:
 
261
                        function
 
262
                @title:
 
263
                        $hptimestamp
 
264
                @short:
 
265
                        Returns a high precision timestamp
 
266
                @syntax:
 
267
                        <float> $hptimestamp
 
268
                @description:
 
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.
 
276
                @examples:
 
277
                        [example]
 
278
                                %tmp = $hptimestamp
 
279
                                [cmd]echo[/cmd] $($hptimestamp - %tmp)
 
280
                        [/example]
 
281
                @seealso:
 
282
                        [fnc]$unixTime[/fnc], [fnc]$date[/fnc]
 
283
        */
 
284
 
 
285
        KVSCF(hptimestamp)
 
286
        {
 
287
                Q_UNUSED(__pContext);
 
288
                Q_UNUSED(__pParams);
 
289
 
 
290
                struct timeval tv;
 
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);
 
295
                return true;
 
296
        }
 
297
 
 
298
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
299
 
 
300
        /*
 
301
                @doc: icon
 
302
                @type:
 
303
                        function
 
304
                @title:
 
305
                        $icon
 
306
                @short:
 
307
                        Returns the ID of an icon
 
308
                @syntax:
 
309
                        <integer> $icon(<iconname:string>)
 
310
                @description:
 
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]
 
315
                @examples:
 
316
                        [example]
 
317
                                [cmd]echo[/cmd] $icon(linux)
 
318
                        [/example]
 
319
                @seealso:
 
320
                        [fnc]$iconName[/fnc]
 
321
        */
 
322
 
 
323
        KVSCF(icon)
 
324
        {
 
325
                QString szName;
 
326
                KVSCF_PARAMETERS_BEGIN
 
327
                        KVSCF_PARAMETER("iconName",KVS_PT_NONEMPTYSTRING,0,szName)
 
328
                KVSCF_PARAMETERS_END
 
329
 
 
330
                KVSCF_pRetBuffer->setInteger(g_pIconManager->getSmallIconIdFromName(szName));
 
331
                return true;
 
332
        }
 
333
 
 
334
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
335
 
 
336
        /*
 
337
                @doc: iconname
 
338
                @type:
 
339
                        function
 
340
                @title:
 
341
                        $iconname
 
342
                @short:
 
343
                        Returns the name of an icon
 
344
                @syntax:
 
345
                        $iconname(<iconid>)
 
346
                @description:
 
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]
 
351
                        [example]
 
352
                                %i = 0
 
353
                                [cmd]do[/cmd] {
 
354
                                        %name = $iconname(%i)
 
355
                                        echo The icon by ID %i is named %name
 
356
                                        %i++
 
357
                                } [cmd]while[/cmd](%name != "")
 
358
                        [/example]
 
359
                @examples:
 
360
                        [example]
 
361
                                [cmd]echo[/cmd] $iconname(24)
 
362
                                [cmd]echo[/cmd] $iconname([fnc]$icon[/fnc](linux))
 
363
                        [/example]
 
364
                @seealso:
 
365
                        [fnc]$iconName[/fnc]
 
366
        */
 
367
 
 
368
        KVSCF(iconName)
 
369
        {
 
370
                kvs_uint_t uIco;
 
371
                KVSCF_PARAMETERS_BEGIN
 
372
                        KVSCF_PARAMETER("iconid",KVS_PT_UINT,0,uIco)
 
373
                KVSCF_PARAMETERS_END
 
374
 
 
375
                if(uIco < KviIconManager::IconCount)
 
376
                {
 
377
                        KVSCF_pRetBuffer->setString(g_pIconManager->getSmallIconName(uIco));
 
378
                } else {
 
379
                        KVSCF_pRetBuffer->setNothing();
 
380
                }
 
381
                return true;
 
382
        }
 
383
 
 
384
 
 
385
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
386
 
 
387
        /*
 
388
                @doc: integer
 
389
                @type:
 
390
                        function
 
391
                @title:
 
392
                        $integer
 
393
                @short:
 
394
                        Casts a variable to an integer
 
395
                @syntax:
 
396
                        <integer> $integer(<data:variant>)
 
397
                @description:
 
398
                        Forces <data> to be an integer data type with the following
 
399
                        semantics:[br]
 
400
                        [ul]
 
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]
 
408
                        [/ul]
 
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.
 
413
                @seealso:
 
414
                        [fnc]$real[/fnc]
 
415
                        [fnc]$boolean[/fnc]
 
416
        */
 
417
 
 
418
        /*
 
419
                @doc: int
 
420
                @type:
 
421
                        function
 
422
                @title:
 
423
                        $int
 
424
                @short:
 
425
                        Casts a variable to an integer
 
426
                @syntax:
 
427
                        <integer> $int(<data:variant>)
 
428
                @description:
 
429
                        This is an internal alias to [fnc]$integer[/fnc]().
 
430
                @seealso:
 
431
                        [fnc]$real[/fnc]
 
432
                        [fnc]$boolean[/fnc]
 
433
        */
 
434
 
 
435
        KVSCF(integer)
 
436
        {
 
437
                KviKvsVariant * v;
 
438
                KVSCF_PARAMETERS_BEGIN
 
439
                        KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
 
440
                KVSCF_PARAMETERS_END
 
441
 
 
442
                kvs_int_t iVal;
 
443
                v->castToInteger(iVal);
 
444
                KVSCF_pRetBuffer->setInteger(iVal);
 
445
                return true;
 
446
        }
 
447
 
 
448
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
449
 
 
450
        /*
 
451
                @doc: isMainWindowActive
 
452
                @type:
 
453
                        function
 
454
                @title:
 
455
                        $isMainWindowActive
 
456
                @short:
 
457
                        Checks if main KVirc window is active
 
458
                @syntax:
 
459
                        <boolean> $isMainWindowActive()
 
460
                @description:
 
461
                        Returns true if the KVIrc's window is currently
 
462
                        the active window on the current desktop.
 
463
        */
 
464
 
 
465
        KVSCF(isMainWindowActive)
 
466
        {
 
467
                Q_UNUSED(__pContext);
 
468
                Q_UNUSED(__pParams);
 
469
 
 
470
                KVSCF_pRetBuffer->setBoolean(g_pMainWindow->isActiveWindow());
 
471
                return true;
 
472
        }
 
473
 
 
474
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
475
 
 
476
        /*
 
477
                @doc: isMainWindowMinimized
 
478
                @type:
 
479
                        function
 
480
                @title:
 
481
                        $isMainWindowMinimized
 
482
                @short:
 
483
                        Checks if main KVirc window is minimized
 
484
                @syntax:
 
485
                        <boolean> $isMainWindowMinimized()
 
486
                @description:
 
487
                        Returns true if main KVirc window is minimized and false otherwise.
 
488
        */
 
489
 
 
490
        KVSCF(isMainWindowMinimized)
 
491
        {
 
492
                Q_UNUSED(__pContext);
 
493
                Q_UNUSED(__pParams);
 
494
 
 
495
                KVSCF_pRetBuffer->setBoolean(g_pMainWindow->isMinimized());
 
496
                return true;
 
497
        }
 
498
 
 
499
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
500
 
 
501
        /*
 
502
                @doc: isempty
 
503
                @type:
 
504
                        function
 
505
                @title:
 
506
                        $isEmpty
 
507
                @short:
 
508
                        Checks if a variable is set (empty or non empty)
 
509
                @syntax:
 
510
                        <boolean> $isEmpty(<data:variant>)
 
511
                @description:
 
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].
 
515
                @seealso:
 
516
                        [fnc]$isSet[/fnc]
 
517
        */
 
518
 
 
519
        KVSCF(isEmpty)
 
520
        {
 
521
                KviKvsVariant * v;
 
522
                KVSCF_PARAMETERS_BEGIN
 
523
                        KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
 
524
                KVSCF_PARAMETERS_END
 
525
 
 
526
                KVSCF_pRetBuffer->setBoolean(v->isEmpty());
 
527
                return true;
 
528
        }
 
529
        //FIXME: documentation
 
530
 
 
531
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
532
 
 
533
        /*
 
534
                @doc: iseventenabled
 
535
                @type:
 
536
                        function
 
537
                @title:
 
538
                        $isEventEnabled
 
539
                @short:
 
540
                        Checks if an event enabled
 
541
                @syntax:
 
542
                        <boolean> $isEventEnabled(<event_name:string>,<handler_name:string>)
 
543
                @description:
 
544
                        Returns 1 if the event handler enabled
 
545
        */
 
546
 
 
547
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
548
 
 
549
        KVSCF(isEventEnabled)
 
550
        {
 
551
                QString szEventName,szHandlerName;
 
552
                KviKvsScriptEventHandler *h=0;
 
553
 
 
554
                KVSCF_PARAMETERS_BEGIN
 
555
                        KVSCF_PARAMETER("event_name",KVS_PT_NONEMPTYSTRING,0,szEventName)
 
556
                        KVSCF_PARAMETER("handler_name",KVS_PT_NONEMPTYSTRING,0,szHandlerName)
 
557
                KVSCF_PARAMETERS_END
 
558
 
 
559
                bool bOk;
 
560
                int iNumber = szEventName.toInt(&bOk);
 
561
                bool bIsRaw = (bOk && (iNumber >= 0) && (iNumber < 1000));
 
562
 
 
563
                if(bIsRaw)
 
564
                {
 
565
                        if(!KviKvsEventManager::instance()->isValidRawEvent(iNumber))
 
566
                        {
 
567
                                        KVSCF_pContext->warning(__tr2qs_ctx("No such event (%Q)","kvs"),&szEventName);
 
568
                        } else {
 
569
                                h=KviKvsEventManager::instance()->findScriptRawHandler(iNumber,szHandlerName);
 
570
                        }
 
571
                } else {
 
572
                        iNumber = KviKvsEventManager::instance()->findAppEventIndexByName(szEventName);
 
573
                        if(!KviKvsEventManager::instance()->isValidAppEvent(iNumber))
 
574
                        {
 
575
                                        KVSCF_pContext->warning(__tr2qs_ctx("No such event (%Q)","kvs"),&szEventName);
 
576
                        } else {
 
577
                                h=KviKvsEventManager::instance()->findScriptAppHandler(iNumber,szHandlerName);
 
578
                        }
 
579
                }
 
580
                if(h)
 
581
                        KVSCF_pRetBuffer->setBoolean(h->isEnabled());
 
582
                else
 
583
                        KVSCF_pContext->warning(__tr2qs_ctx("No such event handler (%Q) for event %Q","kvs"),&szHandlerName,&szEventName);
 
584
 
 
585
                return true;
 
586
        }
 
587
 
 
588
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
589
 
 
590
        /*
 
591
                @doc: isnumeric
 
592
                @type:
 
593
                        function
 
594
                @title:
 
595
                        $isNumeric
 
596
                @syntax:
 
597
                        <boolean> $isNumeric(<data:variant>)
 
598
                @short:
 
599
                        Finds whether a variable contains a rappresentation of a number
 
600
                @description:
 
601
                        Returns 1 if the <data> is an integer or a real number, 0 otherwise.
 
602
        */
 
603
 
 
604
        KVSCF(isNumeric)
 
605
        {
 
606
                KviKvsVariant * v;
 
607
                KVSCF_PARAMETERS_BEGIN
 
608
                        KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
 
609
                KVSCF_PARAMETERS_END
 
610
 
 
611
                KviKvsNumber n;
 
612
                KVSCF_pRetBuffer->setBoolean(v->asNumber(n));
 
613
                return true;
 
614
        }
 
615
 
 
616
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
617
 
 
618
        /*
 
619
                @doc: isset
 
620
                @type:
 
621
                        function
 
622
                @title:
 
623
                        $isSet
 
624
                @short:
 
625
                        Checks if a variable is set (non-empty)
 
626
                @syntax:
 
627
                        <boolean> $isSet(<data:variant>)
 
628
                @description:
 
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]
 
636
                @seealso:
 
637
                        [fnc]$isEmpty[/fnc], [cmd]unset[/cmd]
 
638
        */
 
639
 
 
640
        KVSCF(isSet)
 
641
        {
 
642
                KviKvsVariant * v;
 
643
                KVSCF_PARAMETERS_BEGIN
 
644
                        KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
 
645
                KVSCF_PARAMETERS_END
 
646
 
 
647
                KVSCF_pRetBuffer->setBoolean(!v->isEmpty());
 
648
                return true;
 
649
        }
 
650
 
 
651
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
652
 
 
653
        /*
 
654
                @doc: istimer
 
655
                @type:
 
656
                        function
 
657
                @title:
 
658
                        $isTimer
 
659
                @short:
 
660
                        Checks for a timer existence
 
661
                @syntax:
 
662
                        <boolean> $istimer(<name:string>)
 
663
                @description:
 
664
                        Returns 1 if the timer named <name> is actually running, else 0
 
665
                @seealso:
 
666
                        [cmd]timer[/cmd], [cmd]killtimer[/cmd]
 
667
        */
 
668
 
 
669
        KVSCF(isTimer)
 
670
        {
 
671
                QString szName;
 
672
 
 
673
                KVSCF_PARAMETERS_BEGIN
 
674
                        KVSCF_PARAMETER("timerName",KVS_PT_NONEMPTYSTRING,0,szName)
 
675
                KVSCF_PARAMETERS_END
 
676
                KVSCF_pRetBuffer->setBoolean(KviKvsTimerManager::instance()->timerExists(szName));
 
677
                return true;
 
678
        }
 
679
 
 
680
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
681
 
 
682
        /*
 
683
                @doc: iswellknown
 
684
                @type:
 
685
                        function
 
686
                @title:
 
687
                        $isWellKnown
 
688
                @short:
 
689
                        Returns $true if the specified user is well known
 
690
                @syntax:
 
691
                        <bool> $isWellKnown(<nickname:string>)
 
692
                @description:
 
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]
 
700
        */
 
701
 
 
702
        KVSCF(isWellKnown)
 
703
        {
 
704
                QString szNick;
 
705
 
 
706
                KVSCF_PARAMETERS_BEGIN
 
707
                        KVSCF_PARAMETER("nick",KVS_PT_NONEMPTYSTRING,0,szNick)
 
708
                KVSCF_PARAMETERS_END
 
709
 
 
710
                if(KVSCF_pContext->window()->console())
 
711
                {
 
712
                        if(KVSCF_pContext->window()->console()->isConnected())
 
713
                        {
 
714
                                KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick);
 
715
                                if(e)
 
716
                                {
 
717
                                        KVSCF_pRetBuffer->setBoolean(e->hasHost() && e->hasUser());
 
718
                                        return true;
 
719
                                }
 
720
                        }
 
721
                }
 
722
 
 
723
                KVSCF_pRetBuffer->setBoolean(false);
 
724
                return true;
 
725
        }
 
726
 
 
727
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
728
 
 
729
        /*
 
730
                @doc: k
 
731
                @type:
 
732
                        function
 
733
                @title:
 
734
                        $k
 
735
                @short:
 
736
                        Returns the COLOR mIRC control character
 
737
                @syntax:
 
738
                        <string> $k(<foreground:integer>[,<background:integer>])
 
739
                        <string> $k
 
740
                @description:
 
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]
 
744
                @seealso:
 
745
                        [fnc]$b[/fnc]
 
746
        */
 
747
 
 
748
        KVSCF(k)
 
749
        {
 
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)
 
754
                KVSCF_PARAMETERS_END
 
755
 
 
756
                QString szRet = QChar(KviControlCodes::Color);
 
757
                if(KVSCF_pParams->count() > 0)
 
758
                {
 
759
                        KviQString::appendFormatted(szRet,"%u",iFore);
 
760
                        if(KVSCF_pParams->count() > 1)
 
761
                                KviQString::appendFormatted(szRet,",%u",iBack);
 
762
                }
 
763
                KVSCF_pRetBuffer->setString(szRet);
 
764
                return true;
 
765
        }
 
766
 
 
767
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
768
 
 
769
        /*
 
770
                @doc: keys
 
771
                @type:
 
772
                        function
 
773
                @title:
 
774
                        $keys
 
775
                @short:
 
776
                        Returns the array of keys of a hash
 
777
                @syntax:
 
778
                        <array> $keys(<hash_value:hash>)
 
779
                @description:
 
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).
 
783
                @seealso:
 
784
                        [cmd]foreach[/cmd]
 
785
        */
 
786
 
 
787
        KVSCF(keys)
 
788
        {
 
789
                KviKvsHash * pHash = 0;
 
790
                KVSCF_PARAMETERS_BEGIN
 
791
                        KVSCF_PARAMETER("hash",KVS_PT_HASH,KVS_PF_OPTIONAL,pHash)
 
792
                KVSCF_PARAMETERS_END
 
793
 
 
794
                KviKvsArray * a = new KviKvsArray();
 
795
 
 
796
                // we have to support an empty hash, returning an empty array (ticket #940)
 
797
                if(pHash)
 
798
                {
 
799
                        kvs_int_t idx = 0;
 
800
                        KviKvsHashIterator it(*(pHash->dict()));
 
801
                        while(it.current())
 
802
                        {
 
803
                                a->set(idx,new KviKvsVariant(it.currentKey()));
 
804
                                idx++;
 
805
                                ++it;
 
806
                        }
 
807
                }
 
808
                KVSCF_pRetBuffer->setArray(a);
 
809
                return true;
 
810
        }
 
811
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
812
 
 
813
        /*
 
814
                @doc: lang
 
815
                @type:
 
816
                        function
 
817
                @title:
 
818
                        $lang
 
819
                @short:
 
820
                        Returns name of currently used language
 
821
                @syntax:
 
822
                        <string> $lang([<type:string>])
 
823
                @description:
 
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"
 
829
        */
 
830
 
 
831
        KVSCF(lang)
 
832
        {
 
833
                QString szType;
 
834
 
 
835
                KVSCF_PARAMETERS_BEGIN
 
836
                        KVSCF_PARAMETER("type",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szType)
 
837
                KVSCF_PARAMETERS_END
 
838
 
 
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);
 
843
                return true;
 
844
        }
 
845
 
 
846
 
 
847
 
 
848
 
 
849
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
850
 
 
851
        /*
 
852
                @doc: length
 
853
                @type:
 
854
                        function
 
855
                @title:
 
856
                        $length
 
857
                @short:
 
858
                        Returns the length of a variable
 
859
                @syntax:
 
860
                        <integer> $length(<value:variant>)
 
861
                @description:
 
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.
 
866
                @seealso:
 
867
                        [fnc]$str.len[/fnc]
 
868
        */
 
869
 
 
870
        KVSCF(length)
 
871
        {
 
872
                KviKvsVariant * pVar;
 
873
                KVSCF_PARAMETERS_BEGIN
 
874
                        KVSCF_PARAMETER("value",KVS_PT_VARIANT,0,pVar)
 
875
                KVSCF_PARAMETERS_END
 
876
 
 
877
                switch(pVar->type())
 
878
                {
 
879
                        case KviKvsVariantData::Array:
 
880
                                KVSCF_pRetBuffer->setInteger(pVar->array()->size());
 
881
                        break;
 
882
                        case KviKvsVariantData::Hash:
 
883
                                KVSCF_pRetBuffer->setInteger(pVar->hash()->size());
 
884
                        break;
 
885
                        case KviKvsVariantData::HObject:
 
886
                                KVSCF_pRetBuffer->setInteger(0);
 
887
                        break;
 
888
                        default:
 
889
                        {
 
890
                                QString tmp;
 
891
                                pVar->asString(tmp);
 
892
                                KVSCF_pRetBuffer->setInteger(tmp.length());
 
893
                        }
 
894
                        break;
 
895
                }
 
896
                return true;
 
897
        }
 
898
 
 
899
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
900
 
 
901
        /*
 
902
                @doc: lf
 
903
                @type:
 
904
                        function
 
905
                @title:
 
906
                        $lf
 
907
                @short:
 
908
                        Returns a line feed character
 
909
                @syntax:
 
910
                        <string> $lf
 
911
                @description:
 
912
                        Returns a line feed character
 
913
                @seealso:
 
914
                        [fnc]$cr[/fnc], [fnc]$ascii[/fnc], [fnc]$char[/fnc]
 
915
        */
 
916
 
 
917
        KVSCF(lf)
 
918
        {
 
919
                Q_UNUSED(__pContext);
 
920
                Q_UNUSED(__pParams);
 
921
 
 
922
                KVSCF_pRetBuffer->setString(QString(QChar('\n')));
 
923
                return true;
 
924
        }
 
925
 
 
926
        ///////////////////////////////////////////////////////////////////////////////////////////////
 
927
 
 
928
        /*
 
929
                @doc: link
 
930
                @type:
 
931
                        function
 
932
                @title:
 
933
                        $link
 
934
                @short:
 
935
                        Returns the text specified as a link
 
936
                @syntax:
 
937
                        <string> $link(<text:string>[,<type:string>])
 
938
                @description:
 
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]
 
943
                        [ul]
 
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]
 
948
                        [/ul]
 
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
 
952
                        is using.
 
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.
 
955
                @examples:
 
956
                        echo $link(pragma,nick)
 
957
                @seealso:
 
958
                        [cmd]echo[/cmd], [fnc]$fmtlink[/fnc].
 
959
        */
 
960
 
 
961
        KVSCF(link)
 
962
        {
 
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)
 
967
                KVSCF_PARAMETERS_END
 
968
 
 
969
                QString szRet("\r!");
 
970
                if(szType=="nick")
 
971
                {
 
972
                        szRet.append("n\r");
 
973
                } else if(szType=="channel") {
 
974
                        szRet.append("c\r");
 
975
                } else if(szType=="host") {
 
976
                        szRet.append("h\r");
 
977
                } else {
 
978
                        szRet.append("u\r");
 
979
                }
 
980
                szRet.append(szData);
 
981
                szRet.append("\r");
 
982
                KVSCF_pRetBuffer->setString(szRet);
 
983
                return true;
 
984
        }
 
985
 
 
986
        /*
 
987
                @doc: listtimers
 
988
                @title:
 
989
                        $listtimers
 
990
                @type:
 
991
                        function
 
992
                @short:
 
993
                        Returns a list of the active timers
 
994
                @syntax:
 
995
                        <array> $listtimers([<name:string>][,<flags:string>])
 
996
                @description:
 
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]
 
1003
 
 
1004
                @seealso:
 
1005
                        [cmd]timer[/cmd], [fnc]$isTimer[/fnc], [cmd]killtimer[/cmd], [cmd]listtimers[/cmd]
 
1006
        */
 
1007
 
 
1008
        KVSCF(listtimers)
 
1009
        {
 
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
 
1015
 
 
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();
 
1019
 
 
1020
                KviPointerHashTable<QString,KviKvsTimer> * pTimerDict = KviKvsTimerManager::instance()->timerDict();
 
1021
                KviKvsArray * a = new KviKvsArray();
 
1022
 
 
1023
                if(!pTimerDict)
 
1024
                        return true;
 
1025
 
 
1026
                QRegExp re(szText,bCaseSensitive?Qt::CaseSensitive:Qt::CaseInsensitive,bRegexp?QRegExp::RegExp:QRegExp::Wildcard);
 
1027
                KviPointerHashTableIterator<QString,KviKvsTimer> it(*pTimerDict);
 
1028
                kvs_int_t idx=0;
 
1029
 
 
1030
                while(KviKvsTimer * pTimer = it.current())
 
1031
                {
 
1032
                        if(bMatch)
 
1033
                        {
 
1034
                                if(!re.exactMatch(pTimer->name()))
 
1035
                                {
 
1036
                                        ++it;
 
1037
                                        continue;
 
1038
                                }
 
1039
                        }
 
1040
 
 
1041
                        a->set(idx,new KviKvsVariant(pTimer->name()));
 
1042
                        idx++;
 
1043
                        ++it;
 
1044
                }
 
1045
 
 
1046
                KVSCF_pRetBuffer->setArray(a);
 
1047
                return true;
 
1048
        }
 
1049
 
 
1050
};