summaryrefslogtreecommitdiff
path: root/src/hostserv.c
blob: 553f2e74175ef763658402c73ae9dc54dde0e8f6 (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
/* HostServ functions
 *
 * (C) 2003-2009 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 *
 * $Id$
 *
 */

/*************************************************************************/
#include "services.h"
#include "pseudo.h"

#define HASH(nick)	((tolower((nick)[0])&31)<<5 | (tolower((nick)[1])&31))

HostCore *head = NULL;		  /* head of the HostCore list */

E int do_hs_sync(NickCore * nc, char *vIdent, char *hostmask,
				 char *creator, time_t time);

E void moduleAddHostServCmds();

/*************************************************************************/

void moduleAddHostServCmds()
{
	ModuleManager::LoadModuleList(Config.HostServCoreModules);
}

/*************************************************************************/

/**
 * Return information on memory use.
 * Assumes pointers are valid.
 **/

void get_hostserv_stats(long *nrec, long *memuse)
{
	long count = 0, mem = 0;
	HostCore *hc;

	for (hc = head; hc; hc = hc->next) {
		count++;
		mem += sizeof(*hc);
		if (hc->nick)
			mem += strlen(hc->nick) + 1;
		if (hc->vIdent)
			mem += strlen(hc->vIdent) + 1;
		if (hc->vHost)
			mem += strlen(hc->vHost) + 1;
		if (hc->creator)
			mem += strlen(hc->creator) + 1;
	}

	*nrec = count;
	*memuse = mem;
}

/*************************************************************************/

/**
 * HostServ initialization.
 * @return void
 */
void hostserv_init()
{
	if (Config.s_HostServ) {
		moduleAddHostServCmds();
	}
}

/*************************************************************************/

/**
 * Main HostServ routine.
 * @param u User Struct
 * @param buf Buffer holding the message
 * @return void
 */
void hostserv(User * u, char *buf)
{
	const char *cmd, *s;

	cmd = strtok(buf, " ");

	if (!cmd) {
		return;
	} else if (stricmp(cmd, "\1PING") == 0) {
		if (!(s = strtok(NULL, ""))) {
			s = "";
		}
		ircdproto->SendCTCP(findbot(Config.s_HostServ), u->nick, "PING %s", s);
	} else {
		if (ircd->vhost) {
			mod_run_cmd(Config.s_HostServ, u, HOSTSERV, cmd);
		} else {
			notice_lang(Config.s_HostServ, u, SERVICE_OFFLINE, Config.s_HostServ);
		}
	}
}

/*************************************************************************/
/*	Start of Linked List routines										 */
/*************************************************************************/

HostCore *hostCoreListHead()
{
	return head;
}

/**
 * Create HostCore list member
 * @param next HostCore next slot
 * @param nick Nick to add
 * @param vIdent Virtual Ident
 * @param vHost  Virtual Host
 * @param creator Person whom set the vhost
 * @param time Time the vhost was Set
 * @return HostCore
 */
HostCore *createHostCorelist(HostCore * next, const char *nick, char *vIdent,
							 char *vHost, const char *creator, int32 tmp_time)
{

	next = new HostCore;
	if (next == NULL) {
		ircdproto->SendGlobops(Config.s_HostServ,
						 "Unable to allocate memory to create the vHost LL, problems i sense..");
	} else {
		next->nick = new char[strlen(nick) + 1];
		next->vHost = new char[strlen(vHost) + 1];
		next->creator = new char[strlen(creator) + 1];
		if (vIdent)
			next->vIdent = new char[strlen(vIdent) + 1];
		if ((next->nick == NULL) || (next->vHost == NULL)
			|| (next->creator == NULL)) {
			ircdproto->SendGlobops(Config.s_HostServ,
							 "Unable to allocate memory to create the vHost LL, problems i sense..");
			return NULL;
		}
		strlcpy(next->nick, nick, strlen(nick) + 1);
		strlcpy(next->vHost, vHost, strlen(vHost) + 1);
		strlcpy(next->creator, creator, strlen(creator) + 1);
		if (vIdent) {
			if ((next->vIdent == NULL)) {
				ircdproto->SendGlobops(Config.s_HostServ,
								 "Unable to allocate memory to create the vHost LL, problems i sense..");
				return NULL;
			}
			strlcpy(next->vIdent, vIdent, strlen(vIdent) + 1);
		} else {
			next->vIdent = NULL;
		}
		next->time = tmp_time;
		next->next = NULL;
		return next;
	}
	return NULL;
}

/*************************************************************************/
/**
 * Returns either NULL for the head, or the location of the *PREVIOUS*
 * record, this is where we need to insert etc..
 * @param head HostCore head
 * @param nick Nick to find
 * @param found If found
 * @return HostCore
 */
HostCore *findHostCore(HostCore * phead, const char *nick, bool* found)
{
	HostCore *previous, *current;

	*found = false;
	current = phead;
	previous = current;

	if (!nick) {
		return NULL;
	}
	FOREACH_MOD(I_OnFindHostCore, OnFindHostCore(nick));
	while (current != NULL) {
		if (stricmp(nick, current->nick) == 0) {
			*found = true;
			break;
		} else if (stricmp(nick, current->nick) < 0) {
			/* we know were not gonna find it now.... */
			break;
		} else {
			previous = current;
			current = current->next;
		}
	}
	if (current == phead) {
		return NULL;
	} else {
		return previous;
	}
}

/*************************************************************************/
HostCore *insertHostCore(HostCore * phead, HostCore * prev, const char *nick,
						 char *vIdent, char *vHost, const char *creator,
						 int32 tmp_time)
{

	HostCore *newCore, *tmp;

	if (!nick || !vHost || !creator) {
		return NULL;
	}

	newCore = new HostCore;
	if (newCore == NULL) {
		ircdproto->SendGlobops(Config.s_HostServ,
						 "Unable to allocate memory to insert into the vHost LL, problems i sense..");
		return NULL;
	} else {
		newCore->nick = new char[strlen(nick) + 1];
		newCore->vHost = new char[strlen(vHost) + 1];
		newCore->creator = new char[strlen(creator) + 1];
		if (vIdent)
			newCore->vIdent = new char[strlen(vIdent) + 1];
		if ((newCore->nick == NULL) || (newCore->vHost == NULL)
			|| (newCore->creator == NULL)) {
			ircdproto->SendGlobops(Config.s_HostServ,
							 "Unable to allocate memory to create the vHost LL, problems i sense..");
			return NULL;
		}
		strlcpy(newCore->nick, nick, strlen(nick) + 1);
		strlcpy(newCore->vHost, vHost, strlen(vHost) + 1);
		strlcpy(newCore->creator, creator, strlen(creator) + 1);
		if (vIdent) {
			if ((newCore->vIdent == NULL)) {
				ircdproto->SendGlobops(Config.s_HostServ,
								 "Unable to allocate memory to create the vHost LL, problems i sense..");
				return NULL;
			}
			strlcpy(newCore->vIdent, vIdent, strlen(vIdent) + 1);
		} else {
			newCore->vIdent = NULL;
		}
		newCore->time = tmp_time;
		if (prev == NULL) {
			tmp = phead;
			phead = newCore;
			newCore->next = tmp;
		} else {
			tmp = prev->next;
			prev->next = newCore;
			newCore->next = tmp;
		}
	}
	FOREACH_MOD(I_OnInsertHostCore, OnInsertHostCore(newCore));
	return phead;
}

/*************************************************************************/
HostCore *deleteHostCore(HostCore * phead, HostCore * prev)
{

	HostCore *tmp;

	if (prev == NULL) {
		tmp = phead;
		phead = phead->next;
	} else {
		tmp = prev->next;
		prev->next = tmp->next;
	}
	FOREACH_MOD(I_OnDeleteHostCore, OnDeleteHostCore(tmp));
	delete [] tmp->vHost;
	delete [] tmp->nick;
	delete [] tmp->creator;
	if (tmp->vIdent) {
		delete [] tmp->vIdent;
	}
	delete tmp;
	return phead;
}

/*************************************************************************/
void addHostCore(const char *nick, char *vIdent, char *vhost, const char *creator,
				 int32 tmp_time)
{
	HostCore *tmp;
	bool found = false;

	if (head == NULL) {
		head =
			createHostCorelist(head, nick, vIdent, vhost, creator,
							   tmp_time);
	} else {
		tmp = findHostCore(head, nick, &found);
		if (!found) {
			head =
				insertHostCore(head, tmp, nick, vIdent, vhost, creator,
							   tmp_time);
		} else {
			head = deleteHostCore(head, tmp);   /* delete the old entry */
			addHostCore(nick, vIdent, vhost, creator, tmp_time);		/* recursive call to add new entry */
		}
	}
}

/*************************************************************************/
char *getvHost(char *nick)
{
	HostCore *tmp;
	bool found = false;
	tmp = findHostCore(head, nick, &found);
	if (found) {
		if (tmp == NULL)
			return head->vHost;
		else
			return tmp->next->vHost;
	}
	return NULL;
}

/*************************************************************************/
char *getvIdent(char *nick)
{
	HostCore *tmp;
	bool found = false;
	tmp = findHostCore(head, nick, &found);
	if (found) {
		if (tmp == NULL)
			return head->vIdent;
		else
			return tmp->next->vIdent;
	}
	return NULL;
}

/*************************************************************************/
void delHostCore(const char *nick)
{
	HostCore *tmp;
	bool found = false;
	tmp = findHostCore(head, nick, &found);
	if (found) {
		head = deleteHostCore(head, tmp);
	}

}

/*************************************************************************/
/*	End of Linked List routines					 */
/*************************************************************************/

/*************************************************************************/
/*	Start of Generic Functions					 */
/*************************************************************************/

int do_hs_sync(NickCore * nc, char *vIdent, char *hostmask, char *creator,
			   time_t time)
{
	int i;
	NickAlias *na;

	for (i = 0; i < nc->aliases.count; i++) {
		na = static_cast<NickAlias *>(nc->aliases.list[i]);
		addHostCore(na->nick, vIdent, hostmask, creator, time);
	}
	return MOD_CONT;
}

/*************************************************************************/
int do_on_id(User * u)
{							   /* we've assumed that the user exists etc.. */
	char *vHost;				/* as were only gonna call this from nsid routine */
	char *vIdent;
	vHost = getvHost(u->nick);
	vIdent = getvIdent(u->nick);
	if (vHost != NULL) {
		if (vIdent) {
			notice_lang(Config.s_HostServ, u, HOST_IDENT_ACTIVATED, vIdent,
						vHost);
		} else {
			notice_lang(Config.s_HostServ, u, HOST_ACTIVATED, vHost);
		}
		ircdproto->SendVhost(u->nick, vIdent, vHost);
		if (ircd->vhost)
		{
			if (u->vhost)
				delete [] u->vhost;
			u->vhost = sstrdup(vHost);
		}
		if (ircd->vident) {
			if (vIdent)
				u->SetVIdent(vIdent);
		}
		set_lastmask(u);
	}
	return MOD_CONT;
}

/*************************************************************************/

/*
 * Sets the last_usermak properly. Using virtual ident and/or host
 */
void set_lastmask(User * u)
{
	NickAlias *na = findnick(u->nick);
	if (na->last_usermask)
		delete [] na->last_usermask;

	std::string last_usermask = u->GetIdent() + "@" + u->GetDisplayedHost();
	na->last_usermask = sstrdup(last_usermask.c_str());
}