File: sbnc.cpp

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (289 lines) | stat: -rw-r--r-- 6,628 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
/*******************************************************************************
 * 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. *
 *******************************************************************************/

#ifndef SBNC
#define SBNC
#endif

#include "StdAfx.h"

CCore *g_Bouncer = NULL;

int g_ArgC;
char **g_ArgV;
const char *g_ModulePath;

#if defined(IPV6) && defined(__MINGW32__)
const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
#endif

const char *sbncGetBaseName(void) {
	static char *BasePath = NULL;

	if (BasePath != NULL) {
		return BasePath;
	}

#ifndef _WIN32
	size_t Len;

	if (g_ArgV[0][0] == '.' || g_ArgV[0][0] == '/') {
		Len = strlen(g_ArgV[0]) + 1;
		BasePath = (char *)malloc(Len);
		strncpy(BasePath, g_ArgV[0], Len);
	}
	else {
		// This is just a workaround for the Debian bug #512541.
		// *real* patches are welcome!
		fprintf(stderr, "Please use absolute path for starting sbnc.\n");
		exit(EXIT_FAILURE);
	}

	// TODO: look through PATH env

	for (int i = strlen(BasePath) - 1; i >= 0; i--) {
		if (BasePath[i] == '/') {
			BasePath[i] = '\0';

			break;
		}
	}
#else
	BasePath = (char *)malloc(MAXPATHLEN);

	GetModuleFileName(NULL, BasePath, MAXPATHLEN);

	PathRemoveFileSpec(BasePath);
#endif

	return BasePath;
}

bool sbncIsAbsolutePath(const char *Path) {
#ifdef _WIN32
	return !PathIsRelative(Path);
#else
	if (Path[0] == '/') {
		return true;
	}

	return false;
#endif
}

void sbncPathCanonicalize(char *NewPath, const char *Path) {
	int i = 0, o = 0;

	while (true) {
		if ((Path[i] == '\\' || Path[i] == '/') && Path[i + 1] == '.' && Path[i + 1] != '\0' && Path[i + 2] != '.') {
			i += 2;
		}

		if (o >= MAXPATHLEN - 1) {
			NewPath[o] = '\0';

			return;
		} else {
			NewPath[o] = Path[i];
		}

		if (Path[i] == '\0') {
			return;
		}

		i++;
		o++;
	}
}

const char *sbncBuildPath(const char *Filename, const char *BasePath) {
	char NewPath[MAXPATHLEN];
	static char *Path = NULL;
	size_t Len;

	if (sbncIsAbsolutePath(Filename)) {
		return Filename;
	}

	free(Path);

	if (BasePath == NULL) {
		BasePath = sbncGetBaseName();

		// couldn't find the basepath, fall back to relative paths (i.e. Filename)
		if (BasePath == NULL) {
			return Filename;
		}
	}

	Len = strlen(BasePath) + 1 + strlen(Filename) + 1;
	Path = (char *)malloc(Len);
	strncpy(Path, BasePath, Len);
#ifdef _WIN32
	strncat(Path, "\\", Len);
#else
	strncat(Path, "/", Len);
#endif
	strncat(Path, Filename, Len);

#ifdef _WIN32
	for (unsigned int i = 0; i < strlen(Path); i++) {
		if (Path[i] == '/') {
			Path[i] = '\\';
		}
	}
#endif

	sbncPathCanonicalize(NewPath, Path);
	
	strncpy(Path, NewPath, Len);
	Path[Len - 1] = '\0';

	return Path;
}

/**
 * sbncLoad
 *
 * Used by "sbncloader" to start shroudBNC
 */
extern "C" EXPORT int sbncLoad(const char *ModulePath, bool LPC, bool Daemonize, int argc, char **argv) {
	char TclLibrary[512];
	CConfigFile *Config;

	RpcSetLPC(LPC);

#ifdef _WIN32
	_setmode(fileno(stdin), O_BINARY);
	_setmode(fileno(stdout), O_BINARY);
#endif

#if defined(_WIN32) && defined(_DEBUG)
	Sleep(10000);
#endif

	safe_reinit();

	time_t LastResurrect = safe_get_integer(NULL, "ResurrectTimestamp");

	if (LastResurrect > time(NULL) - 30) {
		// We're dying way too often
		safe_exit(6);
	}

	safe_put_integer(NULL, "ResurrectTimestamp", time(NULL));

	int ResurrectCount = safe_get_integer(NULL, "Resurrect");
	ResurrectCount++;
	safe_put_integer(NULL, "Resurrect", ResurrectCount);

	g_ArgC = argc;
	g_ArgV = argv;
	g_ModulePath = ModulePath;

	chdir(sbncBuildPath("."));

#ifdef _WIN32
	if (!GetEnvironmentVariable("TCL_LIBRARY", TclLibrary, sizeof(TclLibrary)) || strlen(TclLibrary) == 0) {
		SetEnvironmentVariable("TCL_LIBRARY", "./tcl8.4");
	}
#endif

	safe_box_t box = safe_put_box(NULL, "hello");

	safe_put_string(box, "hi", "world");

	safe_remove(box, "hi");
	safe_remove(NULL, "hello");

	srand((unsigned int)time(NULL));

#if defined(_DEBUG) && defined(_WIN32)
	SetUnhandledExceptionFilter(GuardPageHandler);

	SymInitialize(GetCurrentProcess(), NULL, TRUE);
#endif

#ifndef _WIN32
	if (getuid() == 0 || geteuid() == 0 || getgid() == 0 || getegid() == 0) {
		safe_printf("You cannot run shroudBNC as 'root' or using an account which has 'wheel' privileges. Use an ordinary user account and remove the suid bit if it is set.\n");
		return EXIT_FAILURE;
	}

	rlimit core_limit = { INT_MAX, INT_MAX };
	setrlimit(RLIMIT_CORE, &core_limit);
#endif

#if !defined(_WIN32 ) || defined(__MINGW32__)
	lt_dlinit();
#endif

	time(&g_CurrentTime);

	Config = new CConfigFile(sbncBuildPath("sbnc.conf", "/etc/sbnc"), NULL);

	if (Config == NULL) {
		safe_printf("Fatal: could not create config object.");

#if !defined(_WIN32 ) || defined(__MINGW32__)
		lt_dlexit();
#endif

		return EXIT_FAILURE;
	}

	// constructor sets g_Bouncer
	new CCore(Config, argc, argv, Daemonize);

#if !defined(_WIN32)
	signal(SIGPIPE, SIG_IGN);
#endif

	g_Bouncer->StartMainLoop();

	if (g_Bouncer != NULL) {
		delete g_Bouncer;
	}

	delete Config;

#if !defined(_WIN32 ) || defined(__MINGW32__)
	lt_dlexit();
#endif

	safe_exit(EXIT_SUCCESS);
	exit(EXIT_SUCCESS);
}

/**
 * sbncSetStatus
 *
 * Used by "sbncloader" to notify shroudBNC of status changes.
 */
extern "C" EXPORT bool sbncSetStatus(int Status) {
	if (g_Bouncer != NULL) {
		g_Bouncer->SetStatus(Status);
	}

	return true;
}

const char *sbncGetModulePath(void) {
	return g_ModulePath;
}