[go: up one dir, main page]

File: tickle.cpp

package info (click to toggle)
sbnc 1.2-20
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,144 kB
  • ctags: 4,866
  • sloc: cpp: 17,557; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 281
file content (457 lines) | stat: -rw-r--r-- 11,760 bytes parent folder | download | duplicates (2)
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
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2007 Gunnar Beutner                                      *
 *                                                                             *
 * 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 option) 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
 *******************************************************************************/

#include "../src/StdAfx.h"
#include "StdAfx.h"

#include "TclClientSocket.h"
#include "TclSocket.h"
#include "tickle.h"
#include "tickleProcs.h"

#define FREEZEFIX

class CTclSupport;

CCore* g_Bouncer;
Tcl_Interp* g_Interp;
CTclSupport* g_Tcl;
bool g_Ret;
bool g_NoticeUser;
Tcl_Encoding g_Encoding;
asprintf_func g_asprintf;
free_func g_free;
int g_ChannelSortValue;

extern tcltimer_t **g_Timers;
extern int g_TimerCount;

int Tcl_AppInit(Tcl_Interp *interp) {
	if (Tcl_Init(interp) == TCL_ERROR)
		return TCL_ERROR;

	if (Tcl_ProcInit(interp) == TCL_ERROR)
		return TCL_ERROR;

	return TCL_OK;
}

class CTclSupport : public CModuleImplementation {
	void Destroy(void) {
		CallBinds(Type_Unload, NULL, NULL, 0, NULL);

		Tcl_FreeEncoding(g_Encoding);

		Tcl_DeleteInterp(g_Interp);

		Tcl_Release(g_Interp);

		g_Interp = NULL;

		Tcl_Finalize();

		int i = 0;

		while (hash_t<CTclSocket*>* p = g_TclListeners->Iterate(i)) {
			static_cast<CSocketEvents*>(p->Value)->Destroy();
		}

		delete g_TclListeners;

		i = 0;

		while (hash_t<CTclClientSocket*>* p = g_TclClientSockets->Iterate(i++)) {
			p->Value->Destroy();
		}

		delete g_TclClientSockets;

		for (int a = 0; a < g_TimerCount; a++) {
			if (g_Timers[a]) {
				g_Timers[a]->timer->Destroy();
				free(g_Timers[a]->proc);
				free(g_Timers[a]->param);
			}
		}

		delete this;
	}

	void Init(CCore* Root) {
		const char * const*argv;

		CModuleImplementation::Init(Root);

		g_Bouncer = Root;

		g_asprintf = GetCore()->GetUtilities()->asprintf;
		g_free = GetCore()->GetUtilities()->Free;

		g_TclListeners = new CHashtable<CTclSocket*, false, 5>;
		g_TclClientSockets = new CHashtable<CTclClientSocket*, false, 5>;

		argv = GetCore()->GetArgV();

		Tcl_FindExecutable(argv[0]);

		Tcl_SetSystemEncoding(NULL, "ISO8859-1");

		g_Encoding = Tcl_GetEncoding(g_Interp, "ISO8859-1");

		g_Interp = Tcl_CreateInterp();

		Tcl_InitMemory(g_Interp);

		Tcl_SetVar(g_Interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);

		Tcl_AppInit(g_Interp);

		Tcl_Preserve(g_Interp);

		Tcl_EvalFile(g_Interp, "/etc/sbnc/sbnc.tcl");
	}

	bool InterceptIRCMessage(CIRCConnection* IRC, int argc, const char** argv) {
		g_Ret = true;

		CallBinds(Type_PreScript, NULL, NULL, 0, NULL);
		CallBinds(Type_Server, IRC->GetOwner()->GetUsername(), NULL, argc, argv);
		CallBinds(Type_PostScript, NULL, NULL, 0, NULL);

		return g_Ret;
	}

	bool InterceptClientMessage(CClientConnection* Client, int argc, const char** argv) {
		g_Ret = true;

		CUser* User = Client->GetOwner();

		CallBinds(Type_PreScript, NULL, NULL, 0, NULL);

		g_CurrentClient = Client;
		CallBinds(Type_Client, User ? User->GetUsername() : "", Client, argc, argv);
		CallBinds(Type_PostScript, NULL, NULL, 0, NULL);

		return g_Ret;
	}

	void AttachClient(CClientConnection *Client) {
		CallBinds(Type_Attach, Client ? Client->GetUser()->GetUsername() : NULL, Client, 0, NULL);
	}

	void DetachClient(CClientConnection *Client) {
		CallBinds(Type_Detach, Client ? Client->GetUser()->GetUsername() : NULL, NULL, 0, NULL);
	}

	void ServerDisconnect(const char* Client) {
		CallBinds(Type_SvrDisconnect, Client, NULL, 0, NULL);
	}

	void ServerConnect(const char* Client) {
		CallBinds(Type_SvrConnect, Client, NULL, 0, NULL);
	}

	void ServerLogon(const char* Client) {
		CallBinds(Type_SvrLogon, Client, NULL, 0, NULL);
	}

	void UserLoad(const char* User) {
		CallBinds(Type_UsrLoad, User, NULL, 0, NULL);
	}

	void UserCreate(const char* User) {
		CallBinds(Type_UsrCreate, User, NULL, 0, NULL);
	}

	void UserDelete(const char* User) {
		CallBinds(Type_UsrDelete, User, NULL, 0, NULL);
	}

	void SingleModeChange(CIRCConnection* IRC, const char* Channel, const char* Source, bool Flip, char Mode, const char* Parameter) {
		char ModeC[3];

		ModeC[0] = Flip ? '+' : '-';
		ModeC[1] = Mode;
		ModeC[2] = '\0';

		const char* argv[4] = { Source, Channel, ModeC, Parameter };

		CallBinds(Type_SingleMode, IRC->GetOwner()->GetUsername(), NULL, Parameter ? 4 : 3, argv);
	}

	const char* Command(const char* Cmd, const char* Parameters) {
		if (strcasecmp(Cmd, "tcl:eval") == 0) {
			Tcl_Eval(g_Interp, const_cast<char *>(Parameters));

			Tcl_Obj* Result = Tcl_GetObjResult(g_Interp);

			const char* strResult = Tcl_GetString(Result);

			return strResult;
		}

		if (strcasecmp(Cmd, "sorthandler") == 0) {
			return (const char *)TclChannelSortHandler;
		}

		return NULL;
	}

	bool InterceptClientCommand(CClientConnection* Client, const char* Subcommand, int argc, const char** argv, bool NoticeUser) {
		CUser* User = Client->GetOwner();
		const utility_t *Utils;

		g_NoticeUser = NoticeUser;

		g_CurrentClient = Client;

		g_Ret = true;

		CallBinds(Type_Command, Client->GetOwner()->GetUsername(), Client, argc, argv);

		if (g_Ret && strcasecmp(Subcommand, "help") == 0 && User && User->IsAdmin()) {
			commandlist_t *Commands = Client->GetCommandList();
			Utils = g_Bouncer->GetUtilities();

			Utils->AddCommand(Commands, "tcl", "Admin", "executes tcl commands", "Syntax: tcl command\nExecutes the specified tcl command.");

			g_Ret = false;
		}

		if (g_Ret && strcasecmp(Subcommand, "tcl") == 0 && User && User->IsAdmin()) {
			if (argc <= 1) {
				if (NoticeUser)
					Client->RealNotice("Syntax: tcl :command");
				else
					Client->Privmsg("Syntax: tcl :command");

				return true;
			}

			setctx(User->GetUsername());

			Tcl_DString dsScript;
			const char **argvdup;

			Utils = g_Bouncer->GetUtilities();

			argvdup = Utils->ArgDupArray(argv);
			Utils->ArgRejoinArray(argvdup, 1);

			g_CurrentClient = Client;
			int Code = Tcl_EvalEx(g_Interp, Tcl_UtfToExternalDString(g_Encoding, argvdup[1], -1, &dsScript), -1, TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);

			Utils->ArgFreeArray(argvdup);

			Tcl_DStringFree(&dsScript);

			Tcl_Obj* Result = Tcl_GetObjResult(g_Interp);

			const char* strResult = Tcl_GetString(Result);

			if (Code == TCL_ERROR) {
				if (NoticeUser)
					Client->RealNotice("An error occured in the tcl script:");
				else
					Client->Privmsg("An error occured in the tcl script:");
			}

			if (strResult && *strResult) {
				Tcl_DString dsResult;

				char* Dup = strdup(Tcl_UtfToExternalDString(g_Encoding, strResult, -1, &dsResult));

				Tcl_DStringFree(&dsResult);

				char* token = strtok(Dup, "\n");

				while (token != NULL) {
					if (NoticeUser)
						Client->RealNotice(*token ? token : "empty string.");
					else
						Client->Privmsg(*token ? token : "empty string.");

					token = strtok(NULL, "\n");
				}
			} else {
					if (NoticeUser)
						Client->RealNotice("<no error>");
					else
						Client->Privmsg("<no error>");
			}

			g_Ret = false;
		}

		return !g_Ret;
	}

	void TagModified(const char *Tag, const char *Value) {
		const char *argv[2];

		argv[0] = Tag;
		argv[1] = Value;
		CallBinds(Type_SetTag, NULL, NULL, 2, argv);
	}

	void UserTagModified(const char *Tag, const char *Value) {
		const char *argv[2];

		argv[0] = Tag;
		argv[1] = Value;
		CallBinds(Type_SetUserTag, NULL, NULL, 2, argv);
	}
public:
	void RehashInterpreter(void) {
		CallBinds(Type_PreRehash, NULL, NULL, 0, NULL);
		Tcl_EvalFile(g_Interp, "/etc/sbnc/sbnc.tcl");
		CallBinds(Type_PostRehash, NULL, NULL, 0, NULL);
	}
};

extern "C" EXPORT CModuleFar* bncGetObject(void) {
	g_Tcl = new CTclSupport();
	return (CModuleFar*)g_Tcl;
}

extern "C" EXPORT int bncGetInterfaceVersion(void) {
	return INTERFACEVERSION;
}

void RehashInterpreter(void) {
	g_Tcl->RehashInterpreter();
}

void CallBinds(binding_type_e type, const char* user, CClientConnection *client, int argc, const char** argv) {
	Tcl_Obj** listv;
	CUser *User = NULL;

	int objc = 3;
	int idx = 1;
	Tcl_Obj* objv[3];
	bool lazyConversionDone = false;

	for (int i = 0; i < g_BindCount; i++) {
		if (g_Binds[i].valid && g_Binds[i].type == type) {
			Tcl_DString dsProc;

			if (user && strcasecmp(g_Binds[i].user, user) != 0 && !strcasecmp(g_Binds[i].user, "*") == 0)
				continue;

			bool Match = false;

			if (g_Binds[i].pattern == NULL || strcmp(g_Binds[i].pattern, "*") == 0)
				Match = true;

			if (!Match) {
				for (int a = 0; a < argc; a++) {
					if (strcasecmp(g_Binds[i].pattern, argv[a]) == 0) {
					//if (g_Bouncer->Match(g_Binds[i].pattern, argv[a])) {
						Match = true;

						break;
					}
				}
			}

			if (Match) {
				if (!lazyConversionDone) {
					if (user) {
						Tcl_DString dsUser;

						Tcl_ExternalToUtfDString(g_Encoding, user ? user : "", -1, &dsUser);
						objv[idx++] = Tcl_NewStringObj(Tcl_DStringValue(&dsUser), Tcl_DStringLength(&dsUser));
						Tcl_DStringFree(&dsUser);

						Tcl_IncrRefCount(objv[idx - 1]);
					}

					if (argc) {
						listv = (Tcl_Obj**)malloc(sizeof(Tcl_Obj*) * argc);

						for (int a = 0; a < argc; a++) {
							Tcl_DString dsString;

							Tcl_ExternalToUtfDString(g_Encoding, argv[a], -1, &dsString);
							listv[a] = Tcl_NewStringObj(Tcl_DStringValue(&dsString), Tcl_DStringLength(&dsString));
							Tcl_DStringFree(&dsString);

							Tcl_IncrRefCount(listv[a]);
						}

						objv[idx++] = Tcl_NewListObj(argc, listv);
						Tcl_IncrRefCount(objv[idx - 1]);

						for (int a = 0; a < argc; a++) {
							Tcl_DecrRefCount(listv[a]);
						}

						free(listv);
					}

					lazyConversionDone = true;
				}

				Tcl_ExternalToUtfDString(g_Encoding, g_Binds[i].proc, -1, &dsProc);
				objv[0] = Tcl_NewStringObj(Tcl_DStringValue(&dsProc), Tcl_DStringLength(&dsProc));
				Tcl_DStringFree(&dsProc);

				Tcl_IncrRefCount(objv[0]);

				if (User == NULL) {
					User = g_Bouncer->GetUser(user);
				}

				if (User != NULL) {
					setctx(user);
				}

				g_CurrentClient = client;

				Tcl_EvalObjv(g_Interp, idx, objv, TCL_EVAL_GLOBAL);

				Tcl_DecrRefCount(objv[0]);
			}
		}
	}

	if (lazyConversionDone) {
		for (int i = 1; i < idx; i++) {
			if (objv[i])
				Tcl_DecrRefCount(objv[i]);
		}
	}
}

void SetLatchedReturnValue(bool Ret) {
	g_Ret = Ret;
}

int TclChannelSortHandler(const void *p1, const void *p2) {
	const char *Channels[2];

	g_ChannelSortValue = 0;

	Channels[0] = (*(const CChannel **)p1)->GetName();
	Channels[1] = (*(const CChannel **)p2)->GetName();

	CallBinds(Type_ChannelSort, (*(const CChannel **)p1)->GetOwner()->GetOwner()->GetUsername(), (*(const CChannel **)p1)->GetOwner()->GetOwner()->GetPrimaryClientConnection(), 2, Channels);

	return g_ChannelSortValue;
}