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

#include "services.h"
#include "modules.h"
#include "commands.h"

botinfo_map BotList;

BotInfo *BotServ = NULL;
BotInfo *ChanServ = NULL;
BotInfo *Global = NULL;
BotInfo *HostServ = NULL;
BotInfo *MemoServ = NULL;
BotInfo *NickServ = NULL;
BotInfo *OperServ = NULL;

BotInfo::BotInfo(const std::string &nnick, const std::string &nuser, const std::string &nhost, const std::string &nreal)
{
	this->nick = nnick;
	this->user = nuser;
	this->host = nhost;
	this->real = nreal;
	this->lastmsg = this->created = time(NULL);
	this->uid = ts6_uid_retrieve();
	this->chancount = 0;

	ci::string ci_nick(nnick.c_str());
	if (Config.s_ChanServ && ci_nick == Config.s_ChanServ)
	{
		ChanServ = this;
	}
	else if (Config.s_BotServ && ci_nick == Config.s_BotServ)
	{
		BotServ = this;
	}
	else if (Config.s_HostServ && ci_nick == Config.s_HostServ)
	{
		HostServ = this;
	}
	else if (Config.s_OperServ && ci_nick == Config.s_OperServ)
	{
		OperServ = this;
	}
	else if (Config.s_MemoServ && ci_nick == Config.s_MemoServ)
	{
		MemoServ = this;
	}
	else if (Config.s_NickServ && ci_nick == Config.s_NickServ)
	{
		NickServ = this;
	}
	else if (Config.s_GlobalNoticer && ci_nick == Config.s_GlobalNoticer)
	{
		Global = this;
	}

	BotList[this->nick.c_str()] = this;

	// If we're synchronised with the uplink already, call introduce_user() for this bot.
	if (Me && Me->GetUplink()->IsSynced())
	{
		ircdproto->SendClientIntroduction(this->nick, this->user, this->host, this->real, ircd->pseudoclient_mode, this->uid);
		XLine x(this->nick.c_str(), "Reserved for services");
		ircdproto->SendSQLine(&x);
	}
}

BotInfo::~BotInfo()
{
	for (registered_channel_map::const_iterator it = RegisteredChannelList.begin(); it != RegisteredChannelList.end(); ++it)
	{
		ChannelInfo *ci = it->second;

		if (ci->bi == this)
		{
			ci->bi = NULL;
		}
	}

	BotList.erase(this->nick.c_str());
}


void BotInfo::ChangeNick(const char *newnick)
{
	BotList.erase(this->nick.c_str());

	this->nick = newnick;

	BotList[this->nick.c_str()] = this;
}

void BotInfo::RejoinAll()
{
	for (registered_channel_map::const_iterator it = RegisteredChannelList.begin(); it != RegisteredChannelList.end(); ++it)
	{
		ChannelInfo *ci = it->second;

		if (ci->bi == this && ci->c && ci->c->users.size() >= Config.BSMinUsers)
			bot_join(ci);
	}
}

void BotInfo::Assign(User *u, ChannelInfo *ci)
{
	EventReturn MOD_RESULT = EVENT_CONTINUE;
	FOREACH_RESULT(I_OnBotAssign, OnBotAssign(u, ci, this));
	if (MOD_RESULT == EVENT_STOP)
		return;

	if (ci->bi)
		ci->bi->UnAssign(u, ci);

	ci->bi = this;
	++this->chancount;
	if (ci->c && ci->c->users.size() >= Config.BSMinUsers)
		bot_join(ci);
}

void BotInfo::UnAssign(User *u, ChannelInfo *ci)
{
	EventReturn MOD_RESULT = EVENT_CONTINUE;
	FOREACH_RESULT(I_OnBotUnAssign, OnBotUnAssign(u, ci));
	if (MOD_RESULT == EVENT_STOP)
		return;

	if (ci->c && ci->c->users.size() >= Config.BSMinUsers)
	{
		if (u)
			ircdproto->SendPart(ci->bi, ci->c, "UNASSIGN from %s", u->nick.c_str());
		else
			ircdproto->SendPart(ci->bi, ci->c, "");
	}

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