00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "services.h"
00011 #include "anope.h"
00012 #include "bots.h"
00013 #include "servers.h"
00014 #include "protocol.h"
00015 #include "xline.h"
00016 #include "regchannel.h"
00017 #include "channels.h"
00018 #include "config.h"
00019 #include "language.h"
00020 #include "serialize.h"
00021
00022 Serialize::Checker<botinfo_map> BotListByNick("BotInfo"), BotListByUID("BotInfo");
00023
00024 BotInfo *BotServ = NULL, *ChanServ = NULL, *Global = NULL, *HostServ = NULL, *MemoServ = NULL, *NickServ = NULL, *OperServ = NULL;
00025
00026 BotInfo::BotInfo(const Anope::string &nnick, const Anope::string &nuser, const Anope::string &nhost, const Anope::string &nreal, const Anope::string &bmodes) : User(nnick, nuser, nhost, "", "", Me, nreal, Anope::CurTime, "", Servers::TS6_UID_Retrieve()), Serializable("BotInfo"), botmodes(bmodes)
00027 {
00028 this->lastmsg = this->created = Anope::CurTime;
00029 this->introduced = false;
00030 this->oper_only = this->conf = false;
00031
00032 (*BotListByNick)[this->nick] = this;
00033 if (!this->uid.empty())
00034 (*BotListByUID)[this->uid] = this;
00035
00036
00037 if (Me && Me->IsSynced())
00038 {
00039 Anope::string tmodes = !this->botmodes.empty() ? ("+" + this->botmodes) : (IRCD ? IRCD->DefaultPseudoclientModes : "");
00040 if (!tmodes.empty())
00041 this->SetModesInternal(tmodes.c_str());
00042
00043 XLine x(this->nick, "Reserved for services");
00044 IRCD->SendSQLine(NULL, &x);
00045 IRCD->SendClientIntroduction(this);
00046 this->introduced = true;
00047 }
00048 }
00049
00050 BotInfo::~BotInfo()
00051 {
00052
00053 if (Me && Me->IsSynced())
00054 {
00055 IRCD->SendQuit(this, "");
00056 this->introduced = false;
00057 XLine x(this->nick);
00058 IRCD->SendSQLineDel(&x);
00059 }
00060
00061 for (registered_channel_map::const_iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end; ++it)
00062 {
00063 ChannelInfo *ci = it->second;
00064
00065 if (ci->bi == this)
00066 {
00067 ci->QueueUpdate();
00068 ci->bi = NULL;
00069 }
00070 }
00071
00072 BotListByNick->erase(this->nick);
00073 if (!this->uid.empty())
00074 BotListByUID->erase(this->uid);
00075 }
00076
00077 void BotInfo::Serialize(Serialize::Data &data) const
00078 {
00079 data["nick"] << this->nick;
00080 data["user"] << this->ident;
00081 data["host"] << this->host;
00082 data["realname"] << this->realname;
00083 data["created"] << this->created;
00084 data["oper_only"] << this->oper_only;
00085 }
00086
00087 Serializable* BotInfo::Unserialize(Serializable *obj, Serialize::Data &data)
00088 {
00089 Anope::string nick, user, host, realname, flags;
00090
00091 data["nick"] >> nick;
00092 data["user"] >> user;
00093 data["host"] >> host;
00094 data["realname"] >> realname;
00095
00096 BotInfo *bi;
00097 if (obj)
00098 bi = anope_dynamic_static_cast<BotInfo *>(obj);
00099 else if (!(bi = BotInfo::Find(nick)))
00100 bi = new BotInfo(nick, user, host, realname);
00101
00102 data["created"] >> bi->created;
00103 data["oper_only"] >> bi->oper_only;
00104
00105 return bi;
00106 }
00107
00108 void BotInfo::GenerateUID()
00109 {
00110 if (!this->uid.empty())
00111 throw CoreException("Bot already has a uid?");
00112 this->uid = Servers::TS6_UID_Retrieve();
00113 (*BotListByUID)[this->uid] = this;
00114 UserListByUID[this->uid] = this;
00115 }
00116
00117 void BotInfo::SetNewNick(const Anope::string &newnick)
00118 {
00119 UserListByNick.erase(this->nick);
00120 BotListByNick->erase(this->nick);
00121
00122 this->nick = newnick;
00123
00124 UserListByNick[this->nick] = this;
00125 (*BotListByNick)[this->nick] = this;
00126 }
00127
00128 void BotInfo::RejoinAll()
00129 {
00130 for (registered_channel_map::const_iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end; ++it)
00131 {
00132 const ChannelInfo *ci = it->second;
00133
00134 if (ci->bi == this && ci->c && ci->c->users.size() >= Config->BSMinUsers)
00135 this->Join(ci->c);
00136 }
00137 }
00138
00139 void BotInfo::Assign(User *u, ChannelInfo *ci)
00140 {
00141 EventReturn MOD_RESULT = EVENT_CONTINUE;
00142 FOREACH_RESULT(I_OnBotAssign, OnBotAssign(u, ci, this));
00143 if (MOD_RESULT == EVENT_STOP)
00144 return;
00145
00146 if (ci->bi)
00147 ci->bi->UnAssign(u, ci);
00148
00149 ci->bi = this;
00150 if (Me->IsSynced() && ci->c && ci->c->users.size() >= Config->BSMinUsers)
00151 this->Join(ci->c, &ModeManager::DefaultBotModes);
00152 }
00153
00154 void BotInfo::UnAssign(User *u, ChannelInfo *ci)
00155 {
00156 EventReturn MOD_RESULT = EVENT_CONTINUE;
00157 FOREACH_RESULT(I_OnBotUnAssign, OnBotUnAssign(u, ci));
00158 if (MOD_RESULT == EVENT_STOP)
00159 return;
00160
00161 if (ci->c && ci->c->FindUser(ci->bi))
00162 {
00163 if (u)
00164 ci->bi->Part(ci->c, "UNASSIGN from " + u->nick);
00165 else
00166 ci->bi->Part(ci->c);
00167 }
00168
00169 ci->bi = NULL;
00170 }
00171
00172 unsigned BotInfo::GetChannelCount() const
00173 {
00174 unsigned count = 0;
00175 for (registered_channel_map::const_iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end; ++it)
00176 {
00177 const ChannelInfo *ci = it->second;
00178
00179 if (ci->bi == this)
00180 ++count;
00181 }
00182 return count;
00183 }
00184
00185 void BotInfo::Join(Channel *c, ChannelStatus *status)
00186 {
00187 if (c->FindUser(this) != NULL)
00188 return;
00189
00190 if (Config && IRCD && Config->BSSmartJoin)
00191 {
00192 std::pair<Channel::ModeList::iterator, Channel::ModeList::iterator> bans = c->GetModeList("BAN");
00193
00194
00195 for (; bans.first != bans.second; ++bans.first)
00196 {
00197 Entry ban("BAN", bans.first->second);
00198 if (ban.Matches(this))
00199 c->RemoveMode(NULL, "BAN", ban.GetMask());
00200 }
00201
00202 Anope::string Limit;
00203 unsigned limit = 0;
00204 if (c->GetParam("LIMIT", Limit) && Limit.is_pos_number_only())
00205 limit = convertTo<unsigned>(Limit);
00206
00207
00208 if (c->HasMode("INVITE") || (limit && c->users.size() >= limit))
00209 IRCD->SendNotice(this, "@" + c->name, "%s invited %s into the channel.", this->nick.c_str(), this->nick.c_str());
00210
00211 ModeManager::ProcessModes();
00212 }
00213
00214 c->JoinUser(this);
00215 if (IRCD)
00216 IRCD->SendJoin(this, c, status);
00217
00218 FOREACH_MOD(I_OnBotJoin, OnBotJoin(c, this));
00219 }
00220
00221 void BotInfo::Join(const Anope::string &chname, ChannelStatus *status)
00222 {
00223 Channel *c = Channel::Find(chname);
00224 return this->Join(c ? c : new Channel(chname), status);
00225 }
00226
00227 void BotInfo::Part(Channel *c, const Anope::string &reason)
00228 {
00229 if (c->FindUser(this) == NULL)
00230 return;
00231
00232 IRCD->SendPart(this, c, "%s", !reason.empty() ? reason.c_str() : "");
00233 c->DeleteUser(this);
00234 }
00235
00236 void BotInfo::OnMessage(User *u, const Anope::string &message)
00237 {
00238 if (this->commands.empty())
00239 return;
00240
00241 CommandSource source(u->nick, u, u->Account(), u, this);
00242 RunCommand(source, message);
00243 }
00244
00245 CommandInfo& BotInfo::SetCommand(const Anope::string &cname, const Anope::string &sname, const Anope::string &permission)
00246 {
00247 CommandInfo ci;
00248 ci.name = sname;
00249 ci.permission = permission;
00250 this->commands[cname] = ci;
00251 return this->commands[cname];
00252 }
00253
00254 CommandInfo *BotInfo::GetCommand(const Anope::string &cname)
00255 {
00256 CommandInfo::map::iterator it = this->commands.find(cname);
00257 if (it != this->commands.end())
00258 return &it->second;
00259 return NULL;
00260 }
00261
00262 BotInfo* BotInfo::Find(const Anope::string &nick, bool nick_only)
00263 {
00264 BotInfo *bi = NULL;
00265 if (!nick_only && isdigit(nick[0]) && IRCD->RequiresID)
00266 {
00267 botinfo_map::iterator it = BotListByUID->find(nick);
00268 if (it != BotListByUID->end())
00269 bi = it->second;
00270 }
00271 else
00272 {
00273 botinfo_map::iterator it = BotListByNick->find(nick);
00274 if (it != BotListByNick->end())
00275 bi = it->second;
00276 }
00277
00278 if (bi)
00279 bi->QueueUpdate();
00280 return bi;
00281 }
00282