summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_corefunctions_sz.cpp
blob: 5b1c60d34dd9dc0505f1133472f5d1b9f271111f (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
//=============================================================================
//
//   File : kvi_kvs_corefunctions_sz.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_arraycast.h"
#include "kvi_kvs_object_controller.h"

#include "kvi_window.h"
#include "kvi_out.h"
#include "kvi_locale.h"
#include "kvi_app.h"
#include "kvi_options.h"
#include "kvi_channel.h"
#include "kvi_console.h"
#include "kvi_time.h"
#include "kvi_ircuserdb.h"
#include "kvi_modulemanager.h"
#include "kvi_mirccntrl.h"
#include "kvi_sourcesdate.h"
#include "kvi_socket.h"

#include <stdlib.h> // rand & srand

/*
	Data types:

	<variant>
		<nothing>
		<scalar>
			<boolean>
			<string>
			<numeric>
				<integer>
				<real>
		<array>
		<hash>
		<object>

	$isEmpty(<nothing>) == $true

	<nothing> == <empty string>
	<null> == <null object>


*/

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

	// FIXME: #warning "THIS HAS TO WORK FOR QUERIES TOO!"
	/*
		@doc: selected
		@type:
			function
		@title:
			$selected
		@short:
			Returns the list of selected nicknames in the channel
		@syntax:
			<array> $selected
			<array> $selected(<window id:string>)
		@description:
			The form with the <window id> parameter returns an array of the selected
			nicknames in the channel designated by <window id>.
			The form without parameters returns an array of the selected nicknames
			in the current window (assuming that it is a channel),
			thus it is equivalent to calling $selected([fnc]$window[/fnc])
			The returned value may be assigned to a dictionary too: it will be used to simulate an array.[br]
			In a non-array/dictionary context it returns the selected nicknames as a comma separated list.
		@examples:
			[example]
				[cmd]echo[/cmd] $selected
				[cmd]foreach[/cmd](%i,$selected)[cmd]echo[/cmd] %i
			[/example]
		@seealso:
			[fnc]$window[/fnc],
			[fnc]$channel[/fnc],
			[doc:window_naming_conventions]Window naming conventions[/doc]
	*/

	KVSCF(selected)
	{
		TQString winId;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("winId",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,winId)
		KVSCF_PARAMETERS_END

		KviWindow * wnd;
		if(KVSCF_pParams->count() > 0)
		{
			wnd = g_pApp->findWindow(winId.utf8().data());
			if(!wnd)
			{
				KVSCF_pContext->warning(__tr2qs("Window with ID '%s' not found, returning empty string"),winId.utf8().data());
				KVSCF_pRetBuffer->setNothing();
				return true;
			}
		} else {
			wnd = KVSCF_pContext->window();
		}

		if(wnd->type() != KVI_WINDOW_TYPE_CHANNEL)
		{
			KVSCF_pContext->warning(__tr2qs("The specified window is not a channel"));
			KVSCF_pRetBuffer->setNothing();
			return true;
		}

		KviKvsArray * a = new KviKvsArray();

		kvs_int_t i = 0;
		for(TQString * s = ((KviChannel *)wnd)->firstSelectedNickname();s;s = ((KviChannel *)wnd)->nextSelectedNickname())
		{
			a->set(i,new KviKvsVariant(*s));
			i++;
		}

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


		/*
		@doc: sentBytes
		@type:
			function
		@title:
			$sentBytes
		@short:
			Returns total sent bytes
		@syntax:
			<uint> $sentBytes()
		@description:
			Returns total sent bytes
		@seealso:
			[fnc]$receivedBytes[/fnc]
	*/

	KVSCF(sentBytes)
	{
		KVSCF_pRetBuffer->setInteger(g_uOutgoingTraffic);
		return true;
	}

	/*
		@doc: serialize
		@type:
			function
		@title:
			<string> $serialize(<data:mixed>)
		@short:
			Encodes variable to JSON string
		@syntax:
			<string> $serialize(<data:mixed>)
		@description:
			Decodes JSON-encoded string
			$serialize() returns a string containing a byte-stream representation of value that can be stored anywhere.
		@seealso:
			[fnc]$serialize[/fnc]
	*/
	KVSCF(serialize)
	{
		KviKvsVariant *pVar = 0;
		TQString szBuffer;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,pVar)
		KVSCF_PARAMETERS_END

		if(!pVar) return false;
		pVar->serialize(szBuffer);
		KVSCF_pRetBuffer->setString(szBuffer);
		return true;
	}

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

	/*
		@doc: server
		@type:
			function
		@title:
			$server
		@short:
			Returns the current server name (if any)
		@syntax:
			<string> $server[(irc_context_id:uint)]
		@description:
			Returns the current server name of the specified IRC context.[br]
			If no <irc_context_id> is specified, the current IRC context is used.[br]
			If you are not connected to a server, this function will return an empty string.[br]
			If the current window does not belong to any IRC context and no irc_context_id
			is specified, this function prints a warning and also returns an empty string.[br]
	*/

	KVSCF(server)
	{
		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() || cns->context()->isLoggingIn())
					KVSCF_pRetBuffer->setString(cns->connection()->currentServerName());
				else
					KVSCF_pRetBuffer->setNothing();
			} else {
				KVSCF_pRetBuffer->setNothing();
			}
		} else {
			if(KVSCF_pContext->window()->console())
			{
				cns = KVSCF_pContext->window()->console();
				if(cns->context()->isConnected() || cns->context()->isLoggingIn())
					KVSCF_pRetBuffer->setString(cns->connection()->currentServerName());
				else
					KVSCF_pRetBuffer->setNothing();
			} else {
				KVSCF_pContext->warning(__tr2qs("This window has no associated IRC context"));
				KVSCF_pRetBuffer->setNothing();
			}
		}
		return true;
	}

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

	/*
		@doc: sort
		@type:
			function
		@title:
			$sort
		@short:
			Sorts an array
		@syntax:
			<array> $sort(<data:array>)
		@description:
			Sorts an array in ascending order.
		@seealso:
			[fnc]$rsort[/fnc]
	*/

	KVSCF(sort)
	{
		KviKvsArrayCast a;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("data",KVS_PT_ARRAYCAST,0,a)
		KVSCF_PARAMETERS_END

		if(a.array())
		{
			KviKvsArray * arry = new KviKvsArray(*(a.array()));
			arry->sort();
			KVSCF_pRetBuffer->setArray(arry);
		} else {
			KVSCF_pRetBuffer->setArray(new KviKvsArray());
		}
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////
	
	/*
		@doc: string
		@type:
			function
		@title:
			$string
		@short:
			Casts a variable to a string
		@syntax:
			<integer> $string(<data:variant>)
		@description:
			Forces <data> to be a string data type with the following semantics:
			[ul]
				[li]If <data> is a string then <data> itself is returned.[/li]
				[li]If <data> is an integer then its decimal rappresentation is returned.[/li]
				[li]If <data> is a real then its decimal floating-point rappresentation is returned.[/li]
				[li]If <data> is a boolean then the string "1" is returned for a true value and the string "0" for a false value.[/li]
				[li]If <data> is nothing (unset) then an empty string is returned[/li]
				[li]If <data> is an array then a string with all the items converted to strings and separated by commas is returned[/li]
				[li]If <data> is a hash then a string with all the values converted to strings and separated by commas is returned[/li]
				[li]If <data> is a hobject then the string "object" is returned[/li]
			[/ul]
			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]
	*/

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

		TQString szVal;
		v->asString(szVal);
		KVSCF_pRetBuffer->setString(szVal);
		return true;
	}

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

	/*
		@doc: sw
		@type:
			function
		@title:
			$sw
		@short:
			Returns the value of a switch for an alias
		@syntax:
			<variant> $sw(<switch_name:string>[,<long_switch_name:string>])
		@description:
			This function is valid and useful only in aliases.
			It allows an alias to handle switches just like any other
			KVIrc command. If a switch in the form -<letter> was
			passed to the current alias then $sw(<letter>)
			returns 1 (true). If a switch in the form -<letter>=<value>
			was passed to the current alias then <value> is returned.
			If the switch was not present at all then this function
			returns an empty string (that evaluates to false in an expression).
			A warning is printed if this function is used non-alias code.
		@examples:
			[example]
				[cmd]alias[/cmd](test){
					if($sw(a,append)) [cmd]echo[/cmd] "Switch -a was passed"
					%x = $sw(x);
					if(%x) [cmd]echo[/cmd] "Switch -x=%x was passed"
				}
				test -a
				test -x
				test --append -x
				test -a -x
				test -a -x=test
				test -a=10 -x=test
			[/example]
	*/

	KVSCF(sw)
	{
		TQString szSwitch;
		TQString szLongSwitch;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("switch_name",KVS_PT_STRING,0,szSwitch)
			KVSCF_PARAMETER("long_name",KVS_PT_STRING | KVS_PF_OPTIONAL ,0,szLongSwitch)
		KVSCF_PARAMETERS_END

		KviKvsSwitchList * sl = KVSCF_pContext->aliasSwitchList();
		if(!sl)
		{
			KVSCF_pContext->warning(__tr2qs("The $sw() function can be used only in aliases"));
			return true;
		}

		KviKvsVariant * v;
		

		if(szSwitch.length() > 1)
		{
			if(szLongSwitch.isEmpty())
				v = sl->find(szSwitch);
			else
				v = sl->find(szSwitch[0].unicode(),szLongSwitch);
		}else {
			if(szLongSwitch.isEmpty())
				v = sl->find(szSwitch[0]);
			else
				v = sl->find(szSwitch[0].unicode(),szLongSwitch);
		}

		if(v)KVSCF_pRetBuffer->copyFrom(*v);
		else KVSCF_pRetBuffer->setNothing();
		return true;
	}

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

	/*
		@doc: target
		@type:
			function
		@title:
			$target
		@short:
			Returns the target of the current window
		@syntax:
			<string> $target
			<string> $target(<window id>)
		@description:
			The form with the <window id> parameter returns the target
			of the channel,query or dcc that has the specified ID.
			The form without parameters returns the target of the current window,
			thus it is equivalent to calling $target([fnc]$window[/fnc]).
			For channel windows the target is the channel name,
			for query windows it is the list of the "queried" users, for the
			dcc windows it is the remote end of the connection.
			The other windows have an empty target.
		@examples:
			[example]
				[cmd]echo[/cmd] $target
			[/example]
		@seealso:
			[fnc]$window[/fnc],
			[fnc]$console[/fnc],
			[fnc]$channel[/fnc],
			[fnc]$query[/fnc],
			[doc:window_naming_conventions]Window naming conventions[/doc]
	*/

	KVSCF(target)
	{
		TQString winId;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("winId",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,winId)
		KVSCF_PARAMETERS_END

		KviWindow * wnd;
		if(KVSCF_pParams->count() > 0)
		{
			wnd = g_pApp->findWindow(winId.utf8().data());
			if(!wnd)
			{
				KVSCF_pContext->warning(__tr2qs("Window with ID '%s' not found, returning empty string"),winId.utf8().data());
				KVSCF_pRetBuffer->setNothing();
				return true;
			}
		} else {
			wnd = KVSCF_pContext->window();
		}

		//debug("CALLING $target on window %s",wnd->name());
		TQString szTa = wnd->target();


		KVSCF_pRetBuffer->setString(wnd->target());
		return true;
	}

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

	/*
		@doc: this
		@type:
			function
		@title:
			$this
		@short:
			Retrieves the ID of the current object
		@syntax:
			$this
		@description:
			Returns the ID of the current object or ('0') if there is
			none. This function has a "quick" version with syntax:
			[b]$$[/b][br]
	*/

	/*
		@doc: $
		@type:
			function
		@title:
			$$
		@short:
			Retrieves the ID of the current object
		@syntax:
			$$
		@description:
			Returns the ID of the current object or ('0') if there is
			none. This function has equivalent to [fnc]$this[/fnc]
	*/

	KVSCF(thisCKEYWORDWORKAROUND)
	{
		// prologue: parameter handling
		KviKvsObject * o = KVSCF_pContext->thisObject();
		KVSCF_pRetBuffer->setHObject(o ? o->handle() : ((kvs_hobject_t)0));
		return true;
	}

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

	/*
		@doc: script_localization
		@type:
			generic
		@title:
			Localization of scripts
		@short:
			Explains how to add translation capabilities to your scripts
		@body:
			[p]
			[big]Introduction[/big]
			[/p]
			[p]
			Adding the translated versions of the strings adds a great
			value to your scripts. The process of translating a part of
			software is called localization. KVIrc offers some commands
			and functions for this purpose and this document explains
			briefly how to use them.
			[/p]

			[p]
			[big]The big picture[/big]
			[/p]
			[p]
			All of the strings in your script are written in a "primary language".
			The most common "primary language" is english, but theoretically
			it can be any language of your choice.
			[/p]
			[p]
			By the means of the gettext package programs you extract
			the strings from your script and produce a translation file.
			[/p]
			[p]
			The translation file is then effectively translated in another
			language and later compiled in a binary form.
			[/p]
			[p]
			The binary form translations are then loaded in the KVIrc
			executable at runtime and a function is used to look up the
			translations.
			[/p]

			[p]
			[big]How to translate scripts[/big]
			[/p]
			[p]
			Your strings should be surrounded by the [fnc]$tr[/fnc]() function in the following way:
			[example]
				[fnc]$tr[/fnc]("your default language text")
			[/example]
			[/p]
			[p]
			Then you should run the xgettext command on your script files.
			This is done by a shell commandline similar to the following:[br]
			[pre]xgettext -o myscript.pot -ktr mykvsfile1.kvs mykvsfile2.kvs ...[/pre]
			[/p]
			[p]
			Copy the translation file obtained in the following way:[br]
			[pre]cp myscript.pot myscript_XX.pot[/pre]
			Where the XX is your country/language code. For example, for Italian
			it would be:[br]
			[pre]cp myscript.pot myscript_it.po[/pre]
			[/p]
			[p]
			Translate mytranslation_it.po. The format of the po file is straightforward.
			There are msgid lines with the original english text and immediately
			following msgstr lines that must be filled with the corresponding translation.
			For example in Italian you would translate:[br]
			msgid "your default language text"
			msgstr "il tuo testo in linguaggio predefinito"
			[/p]
			[p]
			Compile your translation to binary form with the following command:[br]
			[pre]msgfmt -o myscript_it.mo myscript_it.po[/pre]
			[/p]
			[p]
			Copy the generated *.mo file to the "locale" subdirectory
			in the KVIrc's local directory (usually $HOME/.kvirc/locale/).
			[/p]
			[p]
			Set the system language to the XX above with the following command:[br]
			[pre]export LANG="XX"[/pre][br]
			For Italian it would be:[br]
			[pre]export LANG="it"[/pre][br]
			[/p]
			[p]
			Start KVIrc and type in the commandline:
			[example]
			[cmd]echo[/cmd] [fnc]$tr[/fnc]("your default language text","myscript")
			[/example]
			If you did everything well, you should see the translated
			text echoed in the window :)[br]
			[/p]
			[p]
			Obviously if you don't set LANG="XX", the same command will
			output the original string unchanged.
			[/p]
			[p]
			You can manage translations in several languages by producing
			several *.mo files all with the proper language/country code appended.
			The right *.mo file will be magically loaded by KVIrc that
			will look up the user's LANG variable. (If you don't want
			to use LANG, you can use KVIRC_LANG instead, it will still work).
			[/p]

			[big]Caveats[/big]
			[/p]
			[p]
			You should NEVER use variables or identifiers inside the $tr() function.
			This because the translation files are generated offline,
			when the string is not evaluated yet (i.e variables ad identifiers
			are not substituted by their actual return values).
			The translation process, instead, happens at runtime, when
			the variables and identifiers have been substituted by their
			actual values. This would lead to a mismatch between the
			string you look up in the translation catalogue and the
			effectively translated one. If you need to include variables
			in your strings you should compose the string with smaller pieces
			[example]
				[cmd]echo[/cmd] [fnc]$tr[/fnc]("On this channel") %number [fnc]$tr[/fnc]("users are operators")
			[/example]
			[/p]
			[p]
			The translation process can be realized only if your
			scripts are written in external files. This makes sense since
			if you're translating the script then you will probably want to
			distribute it and the only way to distribute it is on files.
			But well.. this is a caveat.
			[/p]
	*/

	/*
		@doc: tr
		@type:
			function
		@title:
			$tr
		@short:
			Translates an english string to the current language
		@syntax:
			<string> $tr(<default_language_string:string>[,<catalogue:string>])
		@description:
			This function searches for the translation of <default_language_string>
			in the specified translation <catalogue> or in the main
			KVIrc translation file if <catalogue> is omitted.[br]
			If no translation is found then <english_string> is returned.[br]
			<default_language_string> is a string in your script default
			language (which should probably be english since it is the
			most common language spoken by the translators...).[br]
			If the <catalogue> is not loaded yet, KVIrc will attempt to load it,
			but only the first time that the catalogue is accessed (i.e. a load
			failure will cause the catalogue to be ignored completly until [cmd]trunload[/cmd]
			is explicitly used.[br]
			KVIrc will search the catalogue only in [fnc]$file.localdir[/fnc]/locale/
			and in [fnc]$file.globaldir[/fnc]/locale/. If your catalogues are
			in some other place then you must load them explicitly by the
			means of [cmd]trload[/cmd].
			For more informations see the documentation about [doc:script_localization]script localization[/doc].
		@examples:
			[example]
				[cmd]echo[/cmd] $tr("Hello World!")
			[/example]
		@seealso:
			[cmd]trload[/cmd], [cmd]trunload[/cmd]
	*/

	KVSCF(tr)
	{
		// prologue: parameter handling
		TQString szString;
		TQString szCatalogue;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("default_language_string",KVS_PT_STRING,0,szString)
			KVSCF_PARAMETER("catalogue",KVS_PT_STRING,KVS_PF_OPTIONAL,szCatalogue)
		KVSCF_PARAMETERS_END
		if(szString.isEmpty()) return true;
		// body: the real job
		TQString translation;

		if(!szCatalogue.isEmpty())
		{
			KviMessageCatalogue * pCat = KviLocale::getLoadedCatalogue(szCatalogue);
			if(pCat)
			{
				translation = pCat->translateToTQString(szString);
			} else {
				// attempt to load it automatically
				TQString szDir;
				g_pApp->getLocalKvircDirectory(szDir,KviApp::Locale);
				if(!KviLocale::loadCatalogue(szCatalogue,szDir))
				{
					g_pApp->getGlobalKvircDirectory(szDir,KviApp::Locale);
					KviLocale::loadCatalogue(szCatalogue,szDir);
				}
				// If the code above fails to load the catalogue
				// then __tr2qs_ctx_no_xgettext will place
				// a dummy catalogue in its place
				// This means that the next call to getLoadedCatalogue will
				// not fail unless /trunload is explicitly used
				// This will avoid trashing the user's disk too much
				// when a catalogue for a given language is not available
				translation = __tr2qs_ctx_no_xgettext(szString,szCatalogue);
			}
		} else {
			translation = __tr2qs_no_xgettext(szString);
		}

		// epilogue: set the return value
		KVSCF_pRetBuffer->setString(translation);
		return true;
	}

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

	/*
		@doc: true
		@type:
			function
		@title:
			$true
		@short:
			The boolean true constant
		@syntax:
			<boolean> $true
		@description:
			Evaluates to the true boolean constant. True
			is equivalent to the integer 1 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 $true and [fnc]$false[/fnc] 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 = $true
				[cmd]echo[/cmd] $typeof(%a)
				[cmd]echo[/cmd] $(%a + 1)
			[/example]
		@seealso:
			[fnc]$false[/fnc]
	*/

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

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

	/*
		@doc: typeof
		@type:
			function
		@title:
			$typeof
		@short:
			Returns the internal data type of a variable
		@syntax:
			<string> $typeof(<data:variant>)
		@description:
			Returns the internal data type of the <data>.
		@examples:
			[example]
				[cmd]echo[/cmd] $typeof("test")
				%a = "test"
				[cmd]echo[/cmd] $typeof(%a)
				%a = 1
				[cmd]echo[/cmd] $typeof(%a)
				%a = $(1 + 2)
				[cmd]echo[/cmd] $typeof(%a)
				[cmd]echo[/cmd] $typeof($typeof(%a))
			[/example]
	*/

	KVSCF(typeofCKEYWORDWORKAROUND)
	{
#ifdef COMPILE_NEW_KVS
		KviKvsVariant * v;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("data",KVS_PT_VARIANT,0,v)
		KVSCF_PARAMETERS_END

		TQString szType;
		v->getTypeName(szType);
		KVSCF_pRetBuffer->setString(szType);
#endif
		return true;
	}

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

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

	KVSCF(u)
	{
		KVSCF_pRetBuffer->setString(TQString(TQChar(KVI_TEXT_UNDERLINE)));
		return true;
	}

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

	/*
		@doc: unicode
		@type:
			function
		@title:
			$unicode
		@short:
			Returns the UNICODE code of a sets of characters
		@syntax:
			<variant> $unicode(<char:string>)
		@description:
			If <char> is composed only of a single character
			then returns its UNICODE code point as an integer.
			If <char> is composed of more than one character
			then an array of UNICODE code points is returned.
		@seealso:
			[fnc]$cr[/fnc], [fnc]$lf[/fnc], [fnc]$char[/fnc]
	*/

	KVSCF(unicode)
	{
		TQString sz;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("char",KVS_PT_NONEMPTYSTRING,0,sz)
		KVSCF_PARAMETERS_END

		if(sz.length() > 1)
		{
			KviKvsArray * a = new KviKvsArray();
			for(kvs_int_t i=0;i<sz.length();i++)
				a->set(i,new KviKvsVariant((kvs_int_t)(sz[(int)i].unicode())));
			KVSCF_pRetBuffer->setArray(a);
		} else {
			KVSCF_pRetBuffer->setInteger((kvs_int_t)(sz[0].unicode()));
		}
		return true;
	}

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

	/*
		@doc: unixtime
		@type:
			function
		@title:
			$unixTime
		@short:
			Returns the current UNIX time
		@syntax:
			<integer> $unixtime
		@description:
			Returns the time since the Epoch (00:00:00 UTC, January 1, 1970),
			measured in seconds.
		@seealso:
			[fnc]$date[/fnc], [fnc]$hpTimeStamp[/fnc]
	*/

	KVSCF(unixtime)
	{
		KVSCF_pRetBuffer->setInteger((kvs_int_t)(time(0)));
		return true;
	}

	/*
		@doc: unserialize
		@type:
			function
		@title:
			<mixed> $unserialize(<data:string>)
		@short:
			Decodes JSON-encoded string
		@syntax:
			<mixed> $unserialize(<data:string>)
		@description:
			Decodes JSON-encoded string
		@seealso:
			[fnc]$serialize[/fnc]
	*/

	KVSCF(unserialize)
	{
		TQString szData;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("data",KVS_PT_NONEMPTYSTRING,0,szData)
		KVSCF_PARAMETERS_END

		KviKvsVariant *pVar = KviKvsVariant::unserialize(szData);
		if(pVar)
			KVSCF_pRetBuffer->copyFrom(pVar);
		return true;
	}

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

	/*
		@doc: username
		@type:
			function
		@title:
			$username
		@short:
			Returns the username of the specified user
		@syntax:
			<string> $username(<nickname:string>)
		@description:
			Returns the username of the specified IRC user IF it is known.[br]
			The username is known if [fnc]$isWellKnown[/fnc] returns 1.[br]
			The username is generally known if the user is on a channel with you
			or has an open query with you.[br]
			Detailed explaination:[br]
			KVIrc has an internal database of users that are currently
			visible by *this client*: this includes users on open channels
			and queries.[br] The other IRC users are NOT in the database:
			this means that KVIrc knows NOTHING about them and can't return
			any information immediately. In this case this function will return
			an EMPTY string.[br]
			If a user is in the database, at least his nickname is known.[br]
			The username and hostname are known only if the server provides that information
			spontaneously or after a KVIrc request.[br]
			KVIrc requests user information for all the users in open queries
			and channels. This information takes some time to be retrieved,
			in this interval of time KVIrc knows only the user's nickname.
			This function will return the string "*" in this case.[br]
		@seealso:
			[fnc]$isWellKnown[/fnc], [$fnc]$hostname[/fnc], [cmd]awhois[/cmd]
	*/

	KVSCF(username)
	{
		TQString szNick;

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("nick",KVS_PT_NONEMPTYSTRING,0,szNick)
		KVSCF_PARAMETERS_END

		if(KVSCF_pContext->window()->console())
		{
			if(KVSCF_pContext->window()->console()->isConnected())
			{
				KviIrcUserEntry * e = KVSCF_pContext->window()->connection()->userDataBase()->find(szNick);
				if(e)
				{
					KVSCF_pRetBuffer->setString(e->user());
					return true;
				}
			}
		}

		KVSCF_pRetBuffer->setNothing();
		return true;
	}

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

	/*
		@doc: version
		@type:
			function
		@title:
			$version
		@short:
			Returns informations about the version of KVIrc.
		@syntax:
			<string> $version()
			<string> $version(<type:string>)
		@description:
			Returns informations about the version of the currently running KVIrc.[br]
			Type can be one of:[br]
			[ul]
			[li]v: return the current numeric version[/li]
			[li]s: return the current sources date[/li]
			[li]r: return the release name[/li]
			[li]b: return the build date in human readable form[/li]
			[/ul]
			If <type> is omitted then v is assumed.[br]
		@examples:
			[example]
				[cmd]echo[/cmd] $version $version(r)
			[/example]
		@seealso:
			[fnc]$features[/fnc]
	*/

	KVSCF(version)
	{
		TQString szType;
		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("type",KVS_PT_STRING,KVS_PF_OPTIONAL,szType)
		KVSCF_PARAMETERS_END

		if(szType.isEmpty())
			KVSCF_pRetBuffer->setString(KVI_VERSION);
		else {
			if(szType.find('r') != -1)KVSCF_pRetBuffer->setString(KVI_RELEASE_NAME);
			else if(szType.find('s') != -1)KVSCF_pRetBuffer->setString(KVI_SOURCES_DATE);
			else if(szType.find('b') != -1)KVSCF_pRetBuffer->setString(KVI_BUILD_DATE);
			else KVSCF_pRetBuffer->setString(KVI_VERSION);
		}
		return true;
	}

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

	/*
 		@doc: window
 		@type:
 			function
		@title:
			$window
		@short:
			Retrieve the id of a specified window.
		@syntax:
			<string> $window
			<string> $window([<caption_text:hash>[,<context_id:uint>]])
		@description:
			Returns the [b]window id[/b] of the first window that
			has the specified <caption text>.[br]
			If no window matches the specified <caption text>, and invalid
			window id is returned (0).[br]
			If no <caption text> is specified , this function returns the id
			of the current window.[br]
			<context_id> restricts search in only one irc context.
		@examples:
 			[example]
 				[cmd]echo[/cmd] This is the window with id $window
			[/example]
		@seealso:
			[fnc]$channel[/fnc],
 			[fnc]$query[/fnc],
			[fnc]$console[/fnc],
			[doc:window_naming_conventions]Window naming conventions[/doc]
	*/

 	KVSCF(window)
 	{
		//#warning "FIXME: the window identifiers could be numbers!"
		TQString szCaption;
		kvs_int_t iContextId; // kvs_int_t is 64bit while int is 32 (and thus KVSCF_PARAMETER() crashes)

		KVSCF_PARAMETERS_BEGIN
			KVSCF_PARAMETER("caption_text",KVS_PT_STRING,KVS_PF_OPTIONAL,szCaption)
			KVSCF_PARAMETER("context_id",KVS_PT_INTEGER,KVS_PF_OPTIONAL,iContextId)
		KVSCF_PARAMETERS_END

		if(KVSCF_pParams->count() < 2)
			iContextId = -1;

		KviWindow * pWnd;
		if(szCaption.isEmpty())
		{
			pWnd = KVSCF_pContext->window();
		} else {
			pWnd = g_pApp->findWindowByCaption(szCaption,iContextId);
			if(!pWnd)
			{
				//follow the documented behaviour
				KVSCF_pRetBuffer->setInteger(0);
				return true;
			}
		}
		KVSCF_pRetBuffer->setInteger(pWnd->numericId());
		return true;
	}
};