summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_corefunctions_af.cpp
blob: 0fba8d70441187bfc8dab48d099321117934bfb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
//=============================================================================
//
//   File : kvi_kvs_corefunctions_af.cpp
//   Created on Fri 31 Oct 2003 01:52:04 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2003 Szymon Stefanek <pragma at kvirc dot net>
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#define __KVIRC__

#include "kvi_kvs_corefunctions.h"

#include "kvi_kvs_kernel.h"
#include "kvi_kvs_object_controller.h"

#include "kvi_locale.h"
#include "kvi_app.h"
#include "kvi_channel.h"
#include "kvi_console.h"
#include "kvi_ircconnection.h"
#include "kvi_ircconnectionuserinfo.h"
#include "kvi_mirccntrl.h"
#include "kvi_avatar.h"
#include "kvi_ircuserdb.h"
#include "kvi_time.h"

namespace KviKvsCoreFunctions
{
	///////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: active
		@type:
			function
		@title:
			$active
		@short:
			Retrieves the window ID of the active window
		@syntax:
			<integer> $active[(<irc context id:integer>)]
		@description:
			Returns the [b]window ID[/b] of the active window
			bound to the IRC context specified by <irc context id>.
			If no window matches the specified IRC context, and invalid
			window ID is returned (0).[br]
			If no <irc context id> is specified, then
			the application active window is returned (the window
			that currently has the input focus). Note that in this
			case the returned window may also belong to another IRC
			context or be not bound to any IRC context at all.
			In some extreme cases you may even get a window that
			has no output widget and thus has its output redirected.
			Using the "global" active window should be used only
			for communicating something REALLY urgent (and maybe
			unrelated to a specific IRC connection) to the user.
		@seealso:
			[fnc]$window[/fnc]
	*/

	KVSCF(active)
	{
		kvs_uint_t uContextId;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uContextId)
		KVSCF_PARAMETERS_END

		KviWindow * wnd;
		KviConsole * cons;
		if(KVSCF_pParams->count() > 0)
		{
			cons = g_pApp->findConsole(uContextId);
			if(cons)wnd = cons->activeWindow();
			else wnd = 0;
		} else {
			wnd = g_pActiveWindow;
		}

		KVSCF_pRetBuffer->setInteger((kvs_int_t)(wnd ? wnd->numericId() : 0));
		return true;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: ascii
		@type:
			function
		@title:
			$ascii
		@short:
			Returns the UNICODE code of a character
		@syntax:
			<variant> $ascii(<char:string>)
		@description:
			This function has been renamed to $unicode and is present
			only for backward compatibility.
		@seealso:
			[fnc]$cr[/fnc], [fnc]$lf[/fnc], [fnc]$char[/fnc]
	*/

	///////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: asciiToBase64
		@type:
			function
		@title:
			$asciiToBase64
		@short:
			Returns an encoded base64 string
		@syntax:
			$asciiToBase64(<ascii_string>)
		@description:
			Encodes an ASCII string to its base64 encoded rappresentation
			Please note that since KVS is UNICODE based, this function
			will first encode the string in UTF8 and then base64-encode.
			This means that it is substantially only 7bit safe (ASCII codes below 128).
		@examples:
			[cmd]echo[/cmd] $asciiToBase64("Hello!")
		@seealso:
			[fnc]$base64toascii[/fnc]
	*/

	KVSCF(asciiToBase64)
	{
		QString szAscii;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("ascii_string",KVS_PT_STRING,0,szAscii)
		KVSCF_PARAMETERS_END

		KviStr tmp1(szAscii);
		if(tmp1.len() > 0)
		{
			KviStr tmp2;
			tmp2.bufferToBase64(tmp1.ptr(),tmp1.len());
			KVSCF_pRetBuffer->setString(QString(tmp2.ptr()));
		} else {
			KVSCF_pRetBuffer->setString(QString::null);
		}
		return true;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: asciiToHex
		@type:
			function
		@title:
			$asciiToHex
		@short:
			Returns an encoded hex string
		@syntax:
			<string> $asciiToHex(<ascii_string:string>)
		@description:
			Encodes an ASCII string to its hex encoded rappresentation.
			Please note that since KVS is UNICODE based, this function
			will first encode the string in UTF8 and then hex-encode.
			This means that it is substantially only 7bit safe (ASCII codes below 128).
		@examples:
			[cmd]echo[/cmd] $asciiToHex("Hello!")
		@seealso:
			[fnc]$hextoascii[/fnc]
	*/

	KVSCF(asciiToHex)
	{
		QString szAscii;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("ascii_string",KVS_PT_STRING,0,szAscii)
		KVSCF_PARAMETERS_END

		KviStr tmp1(szAscii);
		if(tmp1.len() > 0)
		{
			KviStr tmp2;
			tmp2.bufferToHex(tmp1.ptr(),tmp1.len());
			KVSCF_pRetBuffer->setString(QString(tmp2.ptr()));
		} else {
			KVSCF_pRetBuffer->setString(QString::null);
		}
		return true;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: array
		@type:
			function
		@title:
			$array
		@short:
			Explicitly creates an array
		@syntax:
			<array> $array(<item:variant>,<item:variant>,<item:variant>,....);
		@description:
			Returns an array with the specified items. The items are indexed starting from 0.
			This is just an explicit way of creating an array with a defined set of items,
			useful for increasing readability.
		@examples:
			[example]
				[cmd]alias[/cmd](test) {
					[cmd]return[/cmd] $array(1,2,3);
				}
				%x = $test();
				[cmd]foreach[/cmd](%y,%x) {
					[cmd]echo[/cmd] %y;
				}
			[/example]
		@seealso:
			[fnc]$hash[/fnc]
	*/

	KVSCF(array)
	{
#ifdef COMPILE_NEW_KVS
		KviKvsArray * a = new KviKvsArray();
		kvs_int_t idx = 0;

		for(KviKvsVariant * v = KVSCF_pParams->first();v;v = KVSCF_pParams->next())
		{
			a->set(idx,new KviKvsVariant(*v));
			idx++;
		}

		KVSCF_pRetBuffer->setArray(a);
#endif
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: away
		@type:
			function
		@title:
			$away
		@short:
			Returns true if the current user is away
		@syntax:
			<boolean> $away
		@description:
			Returns true if the current user is away, else false.
			If the current IRC context is not connected at all, this function returns false.
	*/

	KVSCF(away)
	{
		kvs_uint_t uCntx;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("irc_context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uCntx)
		KVSCF_PARAMETERS_END

		KviConsole * cns;

		if(KVSCF_pParams->count() > 0)
		{
			cns = g_pApp->findConsole(uCntx);
			if(cns)
			{
				if(cns->context()->isConnected())
					KVSCF_pRetBuffer->setBoolean(cns->connection()->userInfo()->isAway());
				else
					KVSCF_pRetBuffer->setNothing();
			} else {
				KVSCF_pRetBuffer->setNothing();
			}
		} else {
			if(KVSCF_pContext->window()->console())
			{
				cns = KVSCF_pContext->window()->console();
				if(cns->context()->isConnected())
					KVSCF_pRetBuffer->setBoolean(cns->connection()->userInfo()->isAway());
				else
					KVSCF_pRetBuffer->setNothing();
			} else {
				KVSCF_pContext->warning(__tr2qs("This window has no associated IRC context"));
				KVSCF_pRetBuffer->setNothing();
			}
		}
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: b
		@type:
			function
		@title:
			$b
		@short:
			Returns the BOLD mIRC control character
		@syntax:
			<string> $b
		@description:
			Returns the BOLD mIRC control character (CTRL+B).[br]
		@seealso:
			[fnc]$k[/fnc], [fnc]$u[/fnc]
	*/

	KVSCF(b)
	{
		KVSCF_pRetBuffer->setString(QString(QChar(KVI_TEXT_BOLD)));
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: base64toAscii
		@type:
			function
		@title:
			$base64ToAscii
		@short:
			Returns a decoded base64 string
		@syntax:
			<string> $base64ToAscii(<base_64_encoded_string:string>)
		@description:
			Decodes a base64 encoded string.
		@seealso:
			[fnc]$asciiToBase64[/fnc]
	*/

	KVSCF(base64ToAscii)
	{
		QString szBase64;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("base64_encoded_string",KVS_PT_STRING,0,szBase64)
		KVSCF_PARAMETERS_END

		KviStr tmp1(szBase64);
		char * buf;
		int len = tmp1.base64ToBuffer(&buf,true);
		KVSCF_pRetBuffer->setString(QString(buf));
		KviStr::freeBuffer(buf);
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////
	
	/*
		@doc: boolean
		@type:
			function
		@title:
			$boolean
		@short:
			Casts a variable to a boolean
		@syntax:
			<integer> $boolean(<data:variant>)
		@description:
			Forces <data> to be a boolean data type by first casting
			to integer (see [fnc]$int[/fnc]()) and then comparing the result against zero.
			A zero integer will result in a false value while a non-zero one
			will result in a true value.
			This function is similar to the C++ (bool) cast and is internally
			aliased to [fnc]$bool[/fnc] too.
			Note that since KVIrc does most of the casting work automatically
			you shouldn't need to use this function.
		@seealso:
			[fnc]$real[/fnc]
			[fnc]$integer[/fnc]
	*/

	/*
		@doc: bool
		@type:
			function
		@title:
			$bool
		@short:
			Casts a variable to a boolean
		@syntax:
			<integer> $bool(<data:variant>)
		@description:
			This is an internal alias to [fnc]$boolean[/fnc]().
		@seealso:
			[fnc]$real[/fnc]
			[fnc]$integer[/fnc]
	*/

	KVSCF(boolean)
	{
		KviKvsVariant * v;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
		KVSCF_PARAMETERS_END

		kvs_int_t iVal;
		v->castToInteger(iVal);
		KVSCF_pRetBuffer->setBoolean(iVal);
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: channel
		@type:
			function
		@title:
			$channel
		@short:
			Retrieves the window ID of a specified channel
		@syntax:
			$channel[(<channel name>[,<irc context id>])]
		@description:
			Returns the [b]window ID[/b] of channel matching the
			<channel name> and bound to the connection specified by
			<irc context id>[br]
			If no window matches the specified name or connection, an invalid
			window ID is returned (0).[br]
			If no <irc context id> is specified, this function looks for
			the channel in the current connection context (if any).[br]
			If no <channel name> is specified, this function returns the current
			channel window ID, if executed in a channel, else 0.[br]
		@seealso:
			[fnc]$window[/fnc],
			[fnc]$query[/fnc],
			[fnc]$console[/fnc],
			[doc:window_naming_conventions]Window naming conventions[/doc]
	*/

	KVSCF(channel)
	{
		QString szName;
		kvs_uint_t uContextId;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("channel_name",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szName)
			KVSCF_PARAMETER("context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uContextId)
		KVSCF_PARAMETERS_END

		KviWindow * wnd = 0;
		if(KVSCF_pParams->count() > 0)
		{
			if(KVSCF_pParams->count() > 1)
			{
				KviConsole * cons = g_pApp->findConsole(uContextId);
				if(!cons)KVSCF_pContext->warning(__tr2qs("No such IRC context (%u)"),uContextId);
				else {
					if(cons->connection())
						wnd = cons->connection()->findChannel(szName);
					else
						wnd = 0;
				}
			} else {
				if(KVSCF_pContext->window()->connection())wnd = KVSCF_pContext->window()->connection()->findChannel(szName);
				else {
					if(!KVSCF_pContext->window()->console())
						KVSCF_pContext->warning(__tr2qs("This window is not associated to an IRC context"));
					wnd = 0;
				}
			}
		} else {
			if(KVSCF_pContext->window()->type() == KVI_WINDOW_TYPE_CHANNEL)wnd = KVSCF_pContext->window();
		}

		KVSCF_pRetBuffer->setInteger((kvs_int_t)(wnd ? wnd->numericId() : 0));
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: char
		@type:
			function
		@title:
			$char
		@short:
			Returns a character specified by unicode
		@syntax:
			<string> $char(<unicode_value:integer>)
		@description:
			Returns a character corresponding to the UNICODE code <unicode_value>.[br]
			This function can not return NUL character (UNICODE 0). Basically
			you should never need it: if you do, drop me a mail.[br]
			If the <unicode_code> is not a valid UNICODE code (or is 0), this function returns
			an empty string.[br]
		@seealso:
			[fnc]$cr[/fnc], [fnc]$lf[/fnc], [fnc]$unicode[/fnc]
	*/

	KVSCF(charCKEYWORDWORKAROUND)
	{
		kvs_uint_t ac;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("unicode_value",KVS_PT_UINT,0,ac)
		KVSCF_PARAMETERS_END

		if(ac != 0 && ac < 65536)
			KVSCF_pRetBuffer->setString(QString(QChar((unsigned short)ac)));
		else
			KVSCF_pRetBuffer->setString(QString::null);
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: classdefined
		@type:
			function
		@title:
			$classdefined
		@short:
			Checks if a class is defined
		@syntax:
			$classdefined(<class_name>)
		@description:
			Returns 1 if the class <class_name> is defined, else 0.
	*/

	KVSCF(classDefined)
	{
		// prologue: parameter handling
		QString szClassName;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("className",KVS_PT_NONEMPTYSTRING,0,szClassName)
		KVSCF_PARAMETERS_END
		KVSCF_pRetBuffer->setBoolean(KviKvsKernel::instance()->objectController()->lookupClass(szClassName) != 0);
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: console
		@type:
			function
		@title:
			$console
		@short:
			Retrieves the window ID of a specified console
		@syntax:
			$console[(<irc context id>)]
		@description:
			Returns the [b]window ID[/b] of the console bound
			to the IRC context specified by <irc context id>.
			If no window matches the specified IRC context, an invalid
			window ID is returned (0).[br]
			If no <irc context id> is specified, this function looks for
			the console in the current IRC context (if any).[br]
		@seealso:
			[fnc]$window[/fnc],
			[fnc]$channel[/fnc],
			[fnc]$query[/fnc],
			[doc:window_naming_conventions]Window naming conventions[/doc]
	*/

	KVSCF(console)
	{
		kvs_uint_t uContextId;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("context_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uContextId)
		KVSCF_PARAMETERS_END

		KviConsole * cons;
		if(KVSCF_pParams->count() > 0)
		{
			cons = g_pApp->findConsole(uContextId);
		} else {
			cons = KVSCF_pContext->window()->console();
			if(!cons)KVSCF_pContext->warning(__tr2qs("This window is not associated to an IRC context"));
		}

		KVSCF_pRetBuffer->setInteger((kvs_int_t)(cons ? cons->numericId() : 0));
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: context
		@type:
			function
		@title:
			$context
		@short:
			Retrieves the ID of the specified IRC context
		@syntax:
			$context[(<server>,<nickname>)]
		@description:
			Returns the [b]IRC context ID[/b] of the IRC context that uses
			the specified <server> and local user's <nickname>.[br] This function can
			find only connected IRC contexts.
			If no context matches the server and nickname, and invalid
			[b]IRC context ID[/b] is returned (0).[br]
			If <server> is an empty string, the first context that matches
			the specified nickname is returned. If <nickname> is an empty string
			the first context that uses the specified server is returned.
			If both parameters are missing this function returns the
			id of the current IRC context, or '0' if the
			window in that this call is executed is not bound to any IRC context.
			Please note that in this last case you may find an [b]IRC context[/b]
			that is 'not connected'.
			This can only happen if the current window is a console that is
			in "idle" state, with no connection established yet.[br]
			It is a good idea to take a look at the
			[doc:window_naming_conventions]window naming conventions[/doc].
			This identifier is equivalent to [fnc]$ic[/fnc].[br]
		@seealso:
			[fnc]$window.context[/fnc]
	*/

	KVSCF(context)
	{
		QString szServer,szNick;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("server",KVS_PT_STRING,KVS_PF_OPTIONAL,szServer)
			KVSCF_PARAMETER("nick",KVS_PT_STRING,KVS_PF_OPTIONAL,szNick)
		KVSCF_PARAMETERS_END

		KviConsole * cons;
		if(!(szServer.isEmpty() && szNick.isEmpty()))
		{
			cons = g_pApp->findConsole(szServer,szNick);
		} else {
			cons = KVSCF_pContext->window()->console();
		}

		KVSCF_pRetBuffer->setInteger(cons ? cons->ircContextId() : 0);
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: cr
		@type:
			function
		@title:
			$cr
		@short:
			Returns a carriage return character
		@syntax:
			<string> $cr
		@description:
			Returns a carriage return character
		@seealso:
			[fnc]$lf[/fnc], [fnc]$ascii[/fnc], [fnc]$char[/fnc]
	*/

	KVSCF(cr)
	{
		KVSCF_pRetBuffer->setString(QString(QChar('\r')));
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: date
		@type:
			function
		@title:
			$date
		@short:
			Returns a date/time string using a specified format
		@syntax:
			<string> $date(<format:string>[,<unixtime:integer>])
		@description:
			Returns the string representation of <unixtime> or
			of the current time if <unixtime> is not given,
			based on <format>.[br]
			The <format string> should contain a set of characters
			that will be transformed according to the following rules:[br]
			[br]
			[table]
			[tr][td][b]a[/b][/td][td]The abbreviated weekday name according to the current locale.[/td][/tr]
			[tr][td][b]A[/b][/td][td]The full weekday name according to the current locale.[/td][/tr]
			[tr][td][b]b[/b][/td][td]The abbreviated month name according to the current locale.[/td][/tr]
			[tr][td][b]B[/b][/td][td]The full month name according to the current locale.[/td][/tr]
			[tr][td][b]c[/b][/td][td]The preferred date and time representation for the current locale.[/td][/tr]
			[tr][td][b]C[/b][/td][td]The century number (year/100) as a 2-digit integer. (SU)[/td][/tr]
			[tr][td][b]d[/b][/td][td]The day of the month as a decimal number (range 01 to 31).[/td][/tr]
			[tr][td][b]D[/b][/td][td]Equivalent to m/d/y.[/td][/tr]
			[tr][td][b]e[/b][/td][td]Like d, the day of the month as a decimal number, but a leading
			zero is replaced by a space. (SU)[/td][/tr]
			[tr][td][b]E[/b][/td][td]Modifier: use alternative format, see below. (SU)[/td][/tr]
			[tr][td][b]F[/b][/td][td]Equivalent to Y-m-d (the ISO 8601 date format). (C99)[/td][/tr]
			[tr][td][b]G[/b][/td][td]The ISO 8601 year with century as a decimal number. The 4-digit
			year corresponding  to the ISO week number (see V). This has the
			same format and value as y, except that if the ISO week number
			belongs to the previous or next year, that year is used instead.[/td][/tr]
			[tr][td][b]g[/b][/td][td]Like G, but without century, i.e., with a 2-digit year (00-99).[/td][/tr]
			[tr][td][b]h[/b][/td][td]Equivalent to b.[/td][/tr]
			[tr][td][b]H[/b][/td][td]The hour as a decimal number using a 24-hour clock (range 00 to 23).[/td][/tr]
			[tr][td][b]I[/b][/td][td]The hour as a decimal number using a 12-hour clock (range 01 to 12).[/td][/tr]
			[tr][td][b]j[/b][/td][td]The day of the year as a decimal number (range 001 to 366).[/td][/tr]
			[tr][td][b]k[/b][/td][td]The hour (24-hour clock) as a decimal number (range 0 to 23); sin-
			gle digits are preceded by a blank. (See also H.)[/td][/tr]
			[tr][td][b]l[/b][/td][td]The hour (12-hour clock) as a decimal number (range 1 to 12); sin-
			gle digits are preceded by a blank. (See also I.)[/td][/tr]
			[tr][td][b]m[/b][/td][td]The month as a decimal number (range 01 to 12).[/td][/tr]
			[tr][td][b]M[/b][/td][td]The minute as a decimal number (range 00 to 59).[/td][/tr]
			[tr][td][b]n[/b][/td][td]A newline character. (SU)[/td][/tr]
			[tr][td][b]p[/b][/td][td]Either  `AM' or `PM' according to the given time value, or the cor-
			responding strings for the current locale.  Noon is treated as `pm' and midnight as `am'.[/td][/tr]
			[tr][td][b]r[/b][/td][td]The time in a.m. or p.m. notation.  In the  POSIX  locale  this  is
			equivalent to `I:M:S p'.[/td][/tr]
			[tr][td][b]s[/b][/td][td]The number of seconds  since  the  Epoch,  i.e.,  since  1970-01-01
			00:00:00 UTC.[/td][/tr]
			[tr][td][b]S[/b][/td][td]The second as a decimal number (range 00 to 60).  (The range is up
			to 60 to allow for occasional leap seconds.)[/td][/tr]
			[tr][td][b]t[/b][/td][td]A tab character.[/td][/tr]
			[tr][td][b]T[/b][/td][td]The time in 24-hour notation (H:M:S). (SU)[/td][/tr]
			[tr][td][b]u[/b][/td][td]The day of the week as a decimal, range 1 to  7,  Monday  being  1.
			See also w.[/td][/tr]
			[tr][td][b]U[/b][/td][td]The week number of the current year as a decimal number, range 00
			to 53, starting with the first Sunday as the first day of week  01. See also V and W.[/td][/tr]
			[tr][td][b]V[/b][/td][td]The ISO 8601:1988 week number of the current year as a decimal num-
			ber, range 01 to 53, where week 1 is the first  week  that  has  at
			least  4 days in the current year, and with Monday as the first day
			of the week. See also U and W.[/td][/tr]
			[tr][td][b]w[/b][/td][td]The day of the week as a decimal, range 0 to  6,  Sunday  being  0.[/td][/tr]
			[tr][td][b]W[/b][/td][td]The week number of the current year as a decimal number, range 00
			to 53, starting with the first Monday as the first day of week  01.[/td][/tr]
			[tr][td][b]x[/b][/td][td]The  preferred  date  representation for the current locale without
			the time.[/td][/tr]
			[tr][td][b]X[/b][/td][td]The preferred time representation for the  current  locale  without
			the date.[/td][/tr]
			[tr][td][b]y[/b][/td][td]The year as a decimal number without a century (range 00 to 99).[/td][/tr]
			[tr][td][b]Y[/b][/td][td]The year as a decimal number including the century.[/td][/tr]
			[tr][td][b]z[/b][/td][td]The   time-zone   as  hour  offset  from  GMT.   Required  to  emit
              RFC822-conformant dates (using "a, d b Y H:M:S z").[/td][/tr]
			[tr][td][b]Z[/b][/td][td]The time zone or name or abbreviation.[/td][/tr]
			[tr][td][b]+[/b][/td][td]The date and time in date(1) format. (TZ)[/td][/tr]
			[/table]
			WARNING: Please note that this list is taken from the unix version of the strftime
			function and not all the escape codes may be supported by other platforms.
		@examples:
			[example]
				[cmd]echo[/cmd] $date("d/m/Y H:M:S")
			[/example]
		@seealso:
			[fnc]$time[/fnc], [fnc]$unixtime[/fnc]
	*/

	KVSCF(date)
	{
		QString szFormat;
		kvs_int_t iTime;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("format",KVS_PT_NONEMPTYSTRING,0,szFormat)
			KVSCF_PARAMETER("unixtime",KVS_PT_INT,KVS_PF_OPTIONAL,iTime)
		KVSCF_PARAMETERS_END

		KviStr tmpFormat("");
		
		#ifdef COMPILE_ON_WINDOWS
			QString szAllowedCharacters;
			//windows version of strftime()
			//kvirc crashes if other then these characters get an % character in front of them
			szAllowedCharacters = "AaBbcdHIjMmpSUWwXxYyZz";
		#endif
		
		const QChar * c = KviQString::nullTerminatedArray(szFormat);
		if(c)
		{
			while(c->unicode())
			{
				//Check for right Characters
				#ifdef COMPILE_ON_WINDOWS
					if (szAllowedCharacters.find((char)(c->unicode()),0,true) >= 0)	tmpFormat += '%';
				#else
					if (c->isLetter()) tmpFormat += '%';
				#endif
				tmpFormat += (char)(c->unicode());
				c++;
			}
		}

		kvi_time_t t;
		if(KVSCF_pParams->count() > 1)
			t = (kvi_time_t)iTime;
		else
			t = kvi_unixTime();

		char buf[256];
		if(strftime(buf,255,tmpFormat.ptr(),localtime(&t))> 0)
		{
			KviStr tmp = buf;
			if(tmp.lastCharIs('\n'))tmp.cutRight(1);
			KVSCF_pRetBuffer->setString(QString(buf));
		} else {
			KVSCF_pContext->warning(__tr2qs("The specified format string wasn't accepted by the underlying system time formatting function"));
		}
		
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: false
		@type:
			function
		@title:
			$false
		@short:
			The boolean false constant
		@syntax:
			<boolean> $false
		@description:
			Evaluates to the false boolean constant. True
			is equivalent to the integer 0 too. This function/constant
			is useful to keep your code readable: when you
			have a variable that can assume boolean values it's
			nicer to use [fnc]$true[/fnc] and $false instead of
			the integer constants 1 and 0. The reader will
			undestand immediately that the variable simply can't
			assume any other value.
		@examples:
			[example]
				%a = $false
				[cmd]echo[/cmd] $typeof(%a)
				[cmd]echo[/cmd] $(%a + 1)
			[/example]
		@seealso:
			[fnc]$true[/fnc]
	*/

	KVSCF(falseCKEYWORDWORKAROUND)
	{
#ifdef COMPILE_NEW_KVS
		KVSCF_pRetBuffer->setBoolean(false);
#endif
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: features
		@type:
			function
		@title:
			$features
		@short:
			Returns the features that KVIrc supports
		@syntax:
			<array> $features()
			<boolean> $features(<test_feature:string>)
		@description:
			The parameterless form returns an array of feature descripton strings that this KVIrc executable supports.[br]
			This function is useful when some part of your script depends on
			an optional KVIrc feature (like SSL support or IPV6 support).[br]
			The returned value may be assigned to a dictionary too: it will be used to simulate an array.[br]
			The form with the [test_feature] parameter returns true if and only if [test_feature] is available.[br]
		@examples:
			[example]
			%myfeats[] = $features
			[cmd]echo[/cmd] %myfeats[]
			%i = %myfeats[]#
			[cmd]while[/cmd](%i > 0)
			{
				[cmd]echo[/cmd] "Supporting feature %myfeats[%i]"
				%i--;
			}
			[/example]
			Nearly the same loop, just really shorter:
			[example]
			[cmd]foreach[/cmd](%f,$features)
				[cmd]echo[/cmd] "Supporting feature %myfeats[%i]"
			[/example]
			You can test for a specific feature in the following way:
			[example]
			[cmd]if[/cmd]($features("SSL"))[cmd]echo[/cmd] "Yes! SSL is available";
			[/example]
			If used in "non-array" context it returns just a comma separated list of entries:[br]
			[example]
			[cmd]echo[/cmd] $features
			[/example]
		@seealso:
			[fnc]$version[/fnc]
	*/

	KVSCF(features)
	{
		QString szFeature;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("test_feature",KVS_PT_STRING,KVS_PF_OPTIONAL,szFeature)
		KVSCF_PARAMETERS_END

		static const char * feature_array[]=
		{
			"IRC",
	#ifdef COMPILE_IPV6_SUPPORT
			"IPv6",
	#endif
	#ifdef COMPILE_CRYPT_SUPPORT
			"Crypt",
	#endif
	#ifdef COMPILE_SSL_SUPPORT
			"SSL",
	#endif
	#ifdef COMPILE_GET_INTERFACE_ADDRESS
			"IfAddr",
	#endif
	#ifndef COMPILE_NO_IPC
			"IPC",
	#endif
	#ifdef COMPILE_KDE_SUPPORT
			"KDE",
	#endif
	#ifdef COMPILE_OSS_SUPPORT
			"OSS",
	#endif
	#ifdef COMPILE_ARTS_SUPPORT
			"ARTS",
	#endif
	#ifdef COMPILE_ESD_SUPPORT
			"ESD",
	#endif
	#ifdef COMPILE_AUDIOFILE_SUPPORT
			"Audiofile",
	#endif
	#ifdef COMPILE_PSEUDO_TRANSPARENCY
			"Transparency",
	#endif
	#ifdef COMPILE_ix86_ASM
			"ix86-ASM",
	#endif
	#ifdef COMPILE_SCRIPTTOOLBAR
			"ScriptToolBar",
	#endif // COMPILE_SCRIPTTOOLBAR
	#ifdef COMPILE_USE_QT4
			"Qt4",
	#else
			"Qt3",
	#endif
	#ifdef COMPILE_NEW_KVS
			"KVS",
	#endif
			0
		};

		if(!szFeature.isEmpty())
		{
			for(int i=0;feature_array[i];i++)
			{
				if(KviQString::equalCI(feature_array[i],szFeature))
				{
					KVSCF_pRetBuffer->setBoolean(true);
					return true;
				}
			}
			KVSCF_pRetBuffer->setBoolean(false);
		} else {
			KviKvsArray * a = new KviKvsArray();
			int id = 0;
			for(int i=0;feature_array[i];i++)a->set(i,new KviKvsVariant(QString(feature_array[i])));
			KVSCF_pRetBuffer->setArray(a);
		}

		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: firstconnectedconsole
		@type:
			function
		@title:
			$firstConnectedConsole
		@short:
			Returns the window id of the first connected console
		@syntax:
			<uint> $firstConnectedConsole()
		@description:
			Returns the window id of the first connected console
			or 0 if no console is actually connected.
	*/

	KVSCF(firstConnectedConsole)
	{
		KviConsole * c = g_pApp->topmostConnectedConsole();
		// FIXME: The window id's should be numeric!!!
		KVSCF_pRetBuffer->setString(c ? c->id() : "0");
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: flatten
		@type:
			function
		@title:
			$flatten
		@short:
			Returns a flattened array of items
		@syntax:
			<array> $flatten(<data1:variant>[,<data2:variant>[,...]])
		@description:
			Returns an array of items built from the passed arguments
			with the following rules:[br]
			[ul]
				[li]If an argument is a scalar value then the argument itself
					is appended to the result.[/li]
				[li]If an argument is an array then each contained item
					is appended to the result.[/li]
				[li]If an argument is a hash then each contained value
					is appended to the result.[/li]
			[/ul]
			A simple example of usage is to merge N arrays into a new one.
			(Please note that for merging one array into another the
			[doc:arrayconcatenation]<+ operator[/doc] is more efficient).
	*/

	KVSCF(flatten)
	{
		KviKvsArray * a = new KviKvsArray();
		KVSCF_pRetBuffer->setArray(a);
		unsigned int uIdx = 0;
		for(KviKvsVariant * v = KVSCF_pParams->first();v;v = KVSCF_pParams->next())
		{
			switch(v->type())
			{
				case KviKvsVariantData::Array:
				{
					KviKvsArray * z = v->array();
					unsigned int uSize = z->size();
					unsigned int uIdx2 = 0;
					while(uIdx2 < uSize)
					{
						KviKvsVariant * pInternal = z->at(uIdx2);
						if(pInternal)
							a->set(uIdx,new KviKvsVariant(*pInternal));
						// else
						//	don't set anything: just leave empty entry (nothing)
						uIdx++;
						uIdx2++;
					}
				}
				break;
				case KviKvsVariantData::Hash:
				{
					KviKvsHash * h = v->hash();
					KviKvsHashIterator it(*(h->dict()));
					while(KviKvsVariant * pInternal = it.current())
					{
						a->set(uIdx,new KviKvsVariant(*pInternal));
						uIdx++;
						++it;
					}
				}
				break;
				default:
					a->set(uIdx,new KviKvsVariant(*v));
					uIdx++;
				break;
			}
		}
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	/*
		@doc: fmtlink
		@type:
			function
		@title:
			$fmtlink
		@short:
			Returns a formatted link buffer
		@syntax:
			<string> $fmtlink(<link_text:string>,<double_click_command:string>[,<tooltip_text:string>])
		@description:
			Returns a link formatted for the [cmd]echo[/cmd] command.[br]
			If you pass the returned string to the echo command, the string will be displayed
			as a link and will be highlighted when the user moves the mouse over it.[br]
			If the user will leave the mouse for a few seconds over the link, the <tooltip_text>
			will be displayed in a small tooltip window. If <tooltip_text> is not given,
			then no tooltip will be shown.[br]
			The <double_click_command> will be executed when the user will double click on the link.[br]
			Please remember that if <double_click_command> contains identifiers
			that must be evaluated at double-click time, you MUST escape them in the $fmtlink() call
			to prevent the evaluation.[br]
			You might also take a look at [doc:escape_sequences]the escape sequences documentation[/doc]
			to learn more about how the links are implemented and how to create more powerful links (add
			right and middle button actions, use predefined kvirc links etc...)
		@seealso:
			[doc:escape_sequences]the escape sequences documentation[/doc]
	*/

	KVSCF(fmtlink)
	{
		QString szLinkText,szCmd,szToolTip;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("link_text",KVS_PT_NONEMPTYSTRING,0,szLinkText)
			KVSCF_PARAMETER("double_click_command",KVS_PT_STRING,0,szCmd)
			KVSCF_PARAMETER("tooltip_text",KVS_PT_STRING,KVS_PF_OPTIONAL,szToolTip)
		KVSCF_PARAMETERS_END

		QString szPart;
		KviQString::sprintf(szPart,"[!dbl]%Q",&szCmd);
		if(!szToolTip.isEmpty())KviQString::appendFormatted(szPart,"[!txt]%Q",&szToolTip);
		QString szLink;
		KviQString::sprintf(szLink,"\r!%Q\r%Q\r",&szPart,&szLinkText);
		KVSCF_pRetBuffer->setString(szLink);
		return true;
	}

};