summaryrefslogtreecommitdiffstats
path: root/src/modules/regchan/libkviregchan.cpp
blob: 51fb91d1955b14e0cccbe45b91af7bd9ba8ddffe (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
//======================================================================================
//
//   File : libkviregchan.cpp
//   Creation date : Sat Jun 29 02:55:18 2002 GMT by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2002-2004 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.
//
//======================================================================================
// incomplete
#include "kvi_module.h"


#include "kvi_regchan.h"
#include "kvi_locale.h"
#include "kvi_out.h"
#include "kvi_mirccntrl.h"
#include "kvi_window.h"


// kvi_app.cpp
extern KVIRC_API KviRegisteredChannelDataBase * g_pRegisteredChannelDataBase;

/*
	@doc: regchan.add
	@type:
		command
	@title:
		regchan.add
	@keyterms:
		registering channels
	@short:
		Registers a channel
	@syntax:
		regchan.add <channel:string> [nettqmask:string]
	@description:
		Registers the <channel> with the specified [nettqmask].
		[nettqmask] can be a full network name or a wildcard string
		tht will be matched against network names.
		For example, if you want to register #kvirc on IRCNet
		you can use regchan.add #kvirc IRCNet.
		If you want to handle #kvirc on all the IRC networks then
		you can use regchan.add #kvirc *[br]
		Obviously if you register both #kvirc/IRCNet and #kvirc/*
		then #kvirc/IRCNet will be looked first.
		If nettqmask is not passed a nettqmask of * is assumed.[br]
	@examples:
		[example]
			regchan.add #kvirc openprojects
		[/example]
	@seealso:
*/

static bool regchan_kvs_cmd_add(KviKvsModuleCommandCall * c)
{ 
	TQString szChan,szNettqmask;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("channel name",KVS_PT_NONEMPTYSTRING,0,szChan)
		KVSM_PARAMETER("nettqmask",KVS_PT_STRING,KVS_PF_OPTIONAL,szNettqmask)
	KVSM_PARAMETERS_END(c)
	if(szNettqmask.isEmpty())szNettqmask="*";
	g_pRegisteredChannelDataBase->add(new KviRegisteredChannel(szChan,szNettqmask));
	return true;
}

/*
	@doc: regchan.remove
	@type:
		command
	@title:
		regchan.remove
	@keyterms:
		unregistering channels
	@short:
		Unregisters a channel
	@syntax:
		regchan.remove [-q|--quiet] [-e|--exactly] <channel:string> <network:string>
	@description:
		Unregisters the channel named <channel> that best matches <network>
		with its nettqmask.[br]
		If no match for <channel>/<network> is found in the database then
		a warning is printed unless -q is used.[br]
		If -e is specified then <network> is not matched against the
		netmasks in the database but is compared directly. This means
		that <network> must be exactly the nettqmask used to register a channel.
	@examples:
		[example]
			regchan.remove #kvirc openprojects
		[/example]
	@seealso:
		[cmd]regchan.add[/cmd]
*/
static bool regchan_kvs_cmd_remove(KviKvsModuleCommandCall * c)
{ 
	TQString szChan,szNetwork;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("channel name",KVS_PT_NONEMPTYSTRING,0,szChan)
		KVSM_PARAMETER("network",KVS_PT_NONEMPTYSTRING,0,szNetwork)
	KVSM_PARAMETERS_END(c)
	KviRegisteredChannel * ch;
	if(c->hasSwitch('e',"exactly"))ch = g_pRegisteredChannelDataBase->findExact(szChan,szNetwork);
	else ch = g_pRegisteredChannelDataBase->tqfind(szChan,szNetwork);
	if(ch)
	{
		g_pRegisteredChannelDataBase->remove(ch);
	} else {
		if(!c->hasSwitch('q',"quiet"))c->warning(__tr2qs("No such channel/nettqmask entry in the database"));
	}
	return true;
}

/*
	@doc: regchan.setproperty
	@type:
		command
	@title:
		regchan.setproperty
	@short:
		Sets a property for a registered channel
	@syntax:
		regchan.setproperty [-e|--exactly] [-q|--quiet] <channel:string> <network:string> <property:string> <value:string>
	@description:
		Sets the <property> for the registered <channel> that best
		matches <network> to <value>.[br]
		If <value> is an empty string the <property> is removed.[br]
		If no match for <channel>/<network> is found in the database then
		a warning is printed unless -q is used.[br]
	@switches:	
		!fn: -q|--quiet
		No warning on screen.
		!fn: -e|--exactly
		If -e is specified then <network> is not matched against the
		netmasks in the database but is compared directly. This means
		that <network> must be exactly the nettqmask used to register a channel.	
	@examples:
		[example]
			regchan.setproperty #kvirc openprojects autojoin true
		[/example]
	@seealso:
		[cmd]regchan.add[/cmd]
*/

static bool regchan_kvs_cmd_setProperty(KviKvsModuleCommandCall * c)
{ 
	TQString szChan,szNetwork,szProperty,szValue;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("channel name",KVS_PT_NONEMPTYSTRING,0,szChan)
		KVSM_PARAMETER("network",KVS_PT_NONEMPTYSTRING,0,szNetwork)
		KVSM_PARAMETER("property name",KVS_PT_NONEMPTYSTRING,0,szProperty)
		KVSM_PARAMETER("value",KVS_PT_STRING,0,szValue)
	KVSM_PARAMETERS_END(c)
	KviRegisteredChannel * ch;
	if(c->hasSwitch('e',"exactly"))ch = g_pRegisteredChannelDataBase->findExact(szChan,szNetwork);
	else ch = g_pRegisteredChannelDataBase->tqfind(szChan,szNetwork);
	if(ch)
	{
		if(!szValue.isEmpty())
		{
			ch->setProperty(szProperty.utf8().data(),new KviStr(szValue));
		} else {
			ch->removeProperty(szProperty);
		}
	} else {
		if(!c->hasSwitch('q',"quiet")) c->warning(__tr2qs("No such channel/nettqmask entry in the database"));
	}
	return true;
}
/*
	@doc: regchan.showlist
	@type:
		command
	@title:
		regchan.showlist
	@short:
		Lists the registered channels
	@syntax:
		regchan.showlist
	@description:
		Lists the registered channels with their description
*/

static bool regchan_kvs_cmd_showlist(KviKvsModuleCommandCall * c)
{ 
	c->window()->output(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Registered channel list:"));

	int tot = 0;

	KviPointerHashTableIterator<const char *,KviRegisteredChannelList> it(*(g_pRegisteredChannelDataBase->channelDict()));
	while(KviRegisteredChannelList * l = it.current())
	{
		for(KviRegisteredChannel * ch = l->first();ch;ch = l->next())
		{
			tot++;
			c->window()->output(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Channel: %c%s@%s"),
				KVI_TEXT_BOLD,ch->name().ptr(),ch->netMask().ptr());
			KviPointerHashTableIterator<const char *,KviStr> pit(*(ch->propertyDict()));
			while(KviStr * s = pit.current())
			{
				c->window()->output(KVI_OUT_SYSTEMMESSAGE,__tr2qs("   Property: %s=%s"),pit.currentKey(),s->ptr());
				++pit;
			}
		}
		++it;
	}

	c->window()->output(KVI_OUT_SYSTEMMESSAGE,__tr2qs("Total %d channels"),tot);
	return true;
}

/*
	@doc: regchan.list
	@type:
		function
	@title:
		$regchan.list
	@short:
		Get a list of registered channels
	@syntax:
		<array> $regchan.list([<channel_tqmask:string>],[<network_tqmask:string>])
	@description:
		Returns an array of the channel@nettqmask entries in the registered 
		channels database.[br]
		Please note that <network_tqmask> is a wildcard string that will match
		wildcard strings.[br]
	@examples:
		[example]
			echo $regchan.list(#kvirc*,[fnc]$my.network[/fnc])
		[/example]
*/

static bool regchan_kvs_fnc_list(KviKvsModuleFunctionCall * c)
{ 
	TQString szChan,szNettqmask;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("channel name",KVS_PT_STRING,KVS_PF_OPTIONAL,szChan)
		KVSM_PARAMETER("nettqmask",KVS_PT_STRING,KVS_PF_OPTIONAL,szNettqmask)
	KVSM_PARAMETERS_END(c)

	KviKvsArray* pArray = new KviKvsArray();
//	KviKvsArray* pArrayCN = new KviKvsArray();
//	pArrayCN->set(0,new KviKvsVariant(TQString("")));
//	pArrayCN->set(1,new KviKvsVariant(TQString("")));
	int aid=0;

	if(szChan.isEmpty())szChan="*";
	if(szNettqmask.isEmpty())szNettqmask="*";

	KviPointerHashTable<const char *,KviRegisteredChannelList> * d = g_pRegisteredChannelDataBase->channelDict();
	KviPointerHashTableIterator<const char *,KviRegisteredChannelList> it(*d);

	while(KviRegisteredChannelList * l = it.current())
	{
		for(KviRegisteredChannel * ch = l->first();ch;ch = l->next())
		if(KviTQString::matchWildExpressionsCI(ch->name().ptr(),szChan) &&
			KviTQString::matchWildExpressionsCI(ch->netMask().ptr(),szNettqmask))
		{
//		FIXME: WE NEED TO RETURN AN ARRAY OF 2-ELEMENT ARRAYS (chan name, nettqmask)
			pArray->set(aid,new KviKvsVariant(TQString(ch->name()+"@"+ch->netMask())));
//			pArray->set(aid,new KviKvsVariant(TQString(ch->name()));
//			pArrayCN->set(0,new KviKvsVariant(TQString(ch->name())));
//			pArrayCN->set(1,new KviKvsVariant(TQString(ch->netMask())));
//			pArray->set(aid,new KviKvsVariant(pArrayCN));
			aid++;
		}
		++it;
	}
	c->returnValue()->setArray(pArray);
	return true;
}

/*
	@doc: regchan.property
	@type:
		function
	@title:
		$regchan.property
	@short:
		Returns a property of an entry in the registered channels database
	@syntax:
		<string> $regchan.property(<channel_name:string>,<network:string>,<property_name:string>)
	@description:
		Returns the value of the property identified by <property_name> and
		bound to the channel identified by <channel_name> and best
		matched by <network>.[br]
		If the property is not set or if there is no matching channel entry an empty string is returned.[br]
	@examples:
		[example]
			echo $regchan.property(#kvirc,[fnc]$my.network[/fnc],autojoin)
		[/example]
*/

static bool regchan_kvs_fnc_property(KviKvsModuleFunctionCall * c)
{ 
	TQString szChan,szNetwork,szPropertyName;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("channel name",KVS_PT_STRING,0,szChan)
		KVSM_PARAMETER("network",KVS_PT_STRING,0,szNetwork)
		KVSM_PARAMETER("property name",KVS_PT_NONEMPTYSTRING,0,szPropertyName)
	KVSM_PARAMETERS_END(c)
	KviRegisteredChannel * ch = g_pRegisteredChannelDataBase->tqfind(szChan,szNetwork);
	if(ch)
	{
		KviStr * p = ch->property(szPropertyName);
		if(p)c->returnValue()->setString(p->ptr());
	} //else c->warning(__tr("User %s not found"),parms->safeFirstParam());
	return true;
}
/*
	@doc: regchan.match
	@type:
		function
	@title:
		$regchan.match
	@short:
		Checks if a channel is registered
	@syntax:
		<boolean> $regchan.match(<channel_name:string>,<network:string>)
	@description:
		Returns 1 if <channel_name> and <network> match a registered channel entry
		and 0 otherwise.
	@examples:
		[example]
			echo $regchan.match(#kvirc,IRCNet)
			echo $regchan.match(#kvirc,$my.network)
		[/example]
	@seealso:
		[fnc]$regchan.tqfind[/fnc]
*/


static bool regchan_kvs_fnc_match(KviKvsModuleFunctionCall * c)
{
	TQString szChan,szNetwork;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("channel name",KVS_PT_STRING,0,szChan)
		KVSM_PARAMETER("network",KVS_PT_STRING,0,szNetwork)
	KVSM_PARAMETERS_END(c)
	KviRegisteredChannel * ch = g_pRegisteredChannelDataBase->tqfind(szChan,szNetwork);
	c->returnValue()->setBoolean(ch);
	return true;
}

/*
	@doc: regchan.tqfind
	@type:
		function
	@title:
		$regchan.tqfind
	@short:
		Checks if a channel is registered
	@syntax:
		<boolean> $regchan.tqfind(<channel_name:string>,<nettqmask:string>)
	@description:
		Returns 1 if the channel identified by <channel_name> and <nettqmask>
		is registered and 0 otherwise. This function is similar to $regchan.match
		but for $regchan.find the second parameter is the exact nettqmask
		of the registered channel entry.
	@examples:
		[example]
			echo $regchan.tqfind(#kvirc,IRCNet)
			echo $regchan.tqfind(#kvirc,*)
		[/example]
	@seealso:
		[fnc]$regchan.match[/fnc]
*/

static bool regchan_kvs_fnc_tqfind(KviKvsModuleFunctionCall * c)
{ 
	TQString szChan,szNettqmask;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("channel name",KVS_PT_STRING,0,szChan)
		KVSM_PARAMETER("nettqmask",KVS_PT_STRING,0,szNettqmask)
	KVSM_PARAMETERS_END(c)
	KviRegisteredChannel * ch = g_pRegisteredChannelDataBase->tqfind(szChan,szNettqmask);
	c->returnValue()->setBoolean(ch);
	return true;
}

static bool regchan_module_init(KviModule * m)
{
	KVSM_REGISTER_SIMPLE_COMMAND(m,"add",regchan_kvs_cmd_add);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"showlist",regchan_kvs_cmd_showlist);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"remove",regchan_kvs_cmd_remove);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"setProperty",regchan_kvs_cmd_setProperty);
	KVSM_REGISTER_FUNCTION(m,"property",regchan_kvs_fnc_property);
	KVSM_REGISTER_FUNCTION(m,"match",regchan_kvs_fnc_match);
	KVSM_REGISTER_FUNCTION(m,"tqfind",regchan_kvs_fnc_tqfind);
	KVSM_REGISTER_FUNCTION(m,"list",regchan_kvs_fnc_list);
	
	return true;
}

static bool regchan_module_cleanup(KviModule *m)
{
	return true;
}

static bool regchan_module_can_unload(KviModule *)
{
	return true;
}

KVIRC_MODULE(
	"RegChan",                                              // module name
	"1.0.0",                                                // module version
	"Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot net)", // author & (C)
	"Script interface to the registered channels database",
	regchan_module_init,
	regchan_module_can_unload,
	0,
	regchan_module_cleanup
)