summaryrefslogtreecommitdiff
path: root/src/bots.cpp
blob: 0ba119ed928bd913e4bedae456d17343a93c3f07 (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
/*
 * Copyright (C) 2008-2009 Robin Burchell <w00t@inspircd.org>
 * Copyright (C) 2008-2009 Anope Team <team@anope.org>
 *
 * Please read COPYING and README for further details.
 *
 *
 * $Id$
 *
 */

#include "services.h"

BotInfo::BotInfo(const char *nnick)
{
	this->nick = sstrdup(nnick);
	this->lastmsg = time(NULL);
	this->uid = ts6_uid_retrieve(); // XXX is this safe? has ts6 been setup yet?
	insert_bot(this); // XXX, this is ugly, but it needs to stay until hashing of bots is redone in STL.
	nbots++;
	this->cmdTable = NULL;
}

BotInfo::BotInfo(const char *nnick, const char *nuser, const char *nhost, const char *nreal)
{
	this->nick = sstrdup(nnick);
	this->user = sstrdup(nuser);
	this->host = sstrdup(nhost);
	this->real = sstrdup(nreal);
	this->lastmsg = time(NULL);
	this->uid = ts6_uid_retrieve(); // XXX is this safe? has ts6 been setup yet?
	insert_bot(this); // XXX, this is ugly, but it needs to stay until hashing of bots is redone in STL.
	nbots++;
	this->cmdTable = NULL;
}

BotInfo::~BotInfo()
{
	int i;
	ChannelInfo *ci;

	for (i = 0; i < 256; i++)
		for (ci = chanlists[i]; ci; ci = ci->next)
			if (ci->bi == this)
				ci->bi = NULL;

	if (this->next)
		this->next->prev = this->prev;
	if (this->prev)
		this->prev->next = this->next;
	else
		botlists[tolower(*this->nick)] = this->next;

	nbots--;

	delete [] this->nick;
	delete [] this->user;
	delete [] this->host;
	delete [] this->real;
}


void BotInfo::ChangeNick(const char *newnick)
{
	if (this->next)
		this->next->prev = this->prev;
	if (this->prev)
		this->prev->next = this->next;
	else
		botlists[tolower(*this->nick)] = this->next;

	if (this->nick)
		delete [] this->nick;
	this->nick = sstrdup(newnick);

	insert_bot(this);
}

void BotInfo::RejoinAll()
{
	int i;
	ChannelInfo *ci;

	for (i = 0; i < 256; i++)
		for (ci = chanlists[i]; ci; ci = ci->next)
			if (ci->bi == this && ci->c && (ci->c->usercount >= BSMinUsers))
				bot_join(ci);
}

void BotInfo::Assign(User *u, ChannelInfo *ci)
{
	if (ci->bi)
	{
		if (u)
			ci->bi->UnAssign(u, ci);
		else
			ci->bi->UnAssign(NULL, ci);
	}

	ci->bi = this;
	this->chancount++;
	if (ci->c && ci->c->usercount >= BSMinUsers)
		bot_join(ci);

	send_event(EVENT_BOT_ASSIGN, 2, ci->name, this->nick);
}

void BotInfo::UnAssign(User *u, ChannelInfo *ci)
{
	send_event(EVENT_BOT_UNASSIGN, 2, ci->name, ci->bi->nick);

	if (u && ci->c && ci->c->usercount >= BSMinUsers)
		ircdproto->SendPart(ci->bi, ci->name, "UNASSIGN from %s", u->nick);

	ci->bi->chancount--;
	ci->bi = NULL;
}