summaryrefslogtreecommitdiff
path: root/src/messages.cpp
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2010-12-06 17:06:57 -0500
committerAdam <Adam@anope.org>2010-12-12 19:37:03 -0500
commitaed53dbb47822a79eb9a6b61095ad04ec3d67818 (patch)
tree72d3210b5609ea2163854d14ec7fb2f48d8b4d12 /src/messages.cpp
parenta507816701d136a1c22d2f6779d811840d61577c (diff)
Cleaned up some things, made the protocol modules use some basic inheritance to cut back on their code duplication. More work can be done in the future to remove even more of it.
Diffstat (limited to 'src/messages.cpp')
-rw-r--r--src/messages.cpp316
1 files changed, 87 insertions, 229 deletions
diff --git a/src/messages.cpp b/src/messages.cpp
index 945414f05..0f3e7f1a1 100644
--- a/src/messages.cpp
+++ b/src/messages.cpp
@@ -12,207 +12,7 @@
#include "services.h"
#include "modules.h"
-/*************************************************************************/
-
-int m_nickcoll(const Anope::string &user)
-{
- introduce_user(user);
- return MOD_CONT;
-}
-
-/*************************************************************************/
-
-int m_away(const Anope::string &source, const Anope::string &msg)
-{
- User *u = finduser(source);
-
- if (u && msg.empty()) /* un-away */
- check_memos(u);
- return MOD_CONT;
-}
-
-/*************************************************************************/
-
-int m_kill(const Anope::string &nick, const Anope::string &msg)
-{
- BotInfo *bi;
-
- /* Recover if someone kills us. */
- if (!Config->s_BotServ.empty() && (bi = findbot(nick)))
- {
- introduce_user(nick);
- bi->RejoinAll();
- }
- else
- do_kill(nick, msg);
-
- return MOD_CONT;
-}
-
-/*************************************************************************/
-
-bool m_time(const Anope::string &source, const std::vector<Anope::string> &)
-{
- if (source.empty())
- return MOD_CONT;
-
- time_t t;
- time(&t);
- struct tm *tm = localtime(&t);
- char buf[64];
- strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y %Z", tm);
- ircdproto->SendNumeric(Config->ServerName, 391, source, "%s :%s", Config->ServerName.c_str(), buf);
- return MOD_CONT;
-}
-
-/*************************************************************************/
-
-int m_motd(const Anope::string &source)
-{
- if (source.empty())
- return MOD_CONT;
-
- FILE *f = fopen(Config->MOTDFilename.c_str(), "r");
- if (f)
- {
- ircdproto->SendNumeric(Config->ServerName, 375, source, ":- %s Message of the Day", Config->ServerName.c_str());
- char buf[BUFSIZE];
- while (fgets(buf, sizeof(buf), f))
- {
- buf[strlen(buf) - 1] = 0;
- ircdproto->SendNumeric(Config->ServerName, 372, source, ":- %s", buf);
- }
- fclose(f);
- ircdproto->SendNumeric(Config->ServerName, 376, source, ":End of /MOTD command.");
- }
- else
- ircdproto->SendNumeric(Config->ServerName, 422, source, ":- MOTD file not found! Please contact your IRC administrator.");
- return MOD_CONT;
-}
-
-/*************************************************************************/
-
-int m_privmsg(const Anope::string &source, const Anope::string &receiver, const Anope::string &message)
-{
- if (source.empty() || receiver.empty() || message.empty())
- return MOD_CONT;
-
- User *u = finduser(source);
-
- if (!u)
- {
- Log() << message << ": user record for " << source << " not found";
-
- BotInfo *bi = findbot(receiver);
- if (bi)
- ircdproto->SendMessage(bi, source, "%s", GetString(USER_RECORD_NOT_FOUND).c_str());
-
- return MOD_CONT;
- }
-
- if (receiver[0] == '#' && !Config->s_BotServ.empty())
- {
- ChannelInfo *ci = cs_findchan(receiver);
- if (ci)
- {
- /* Some paranoia checks */
- if (!ci->HasFlag(CI_FORBIDDEN) && ci->c && ci->bi)
- botchanmsgs(u, ci, message);
- }
- }
- else
- {
- /* Check if we should ignore. Operators always get through. */
- if (allow_ignore && !is_oper(u))
- {
- if (get_ignore(source))
- {
- Anope::string target = myStrGetToken(message, ' ', 0);
- BotInfo *bi = findbot(target);
- if (bi)
- Log(bi) << "Ignored message from " << source << " using command " << target;
- return MOD_CONT;
- }
- }
-
- /* If a server is specified (nick@server format), make sure it matches
- * us, and strip it off. */
- Anope::string botname = receiver;
- size_t s = receiver.find('@');
- if (s != Anope::string::npos)
- {
- Anope::string servername(receiver.begin() + s + 1, receiver.end());
- botname = botname.substr(0, s);
- if (!servername.equals_ci(Config->ServerName))
- return MOD_CONT;
- }
- else if (Config->UseStrictPrivMsg)
- {
- BotInfo *bi = findbot(receiver);
- if (!bi)
- return MOD_CONT;
- Log(LOG_DEBUG) << "Ignored PRIVMSG without @ from " << source;
- u->SendMessage(bi, INVALID_TARGET, receiver.c_str(), receiver.c_str(), Config->ServerName.c_str(), receiver.c_str());
- return MOD_CONT;
- }
-
- BotInfo *bi = findbot(botname);
-
- if (bi)
- {
- if (message[0] == '\1' && message[message.length() - 1] == '\1')
- {
- if (message.substr(0, 6).equals_ci("\1PING "))
- {
- Anope::string buf = message;
- buf.erase(buf.begin());
- buf.erase(buf.end() - 1);
- ircdproto->SendCTCP(bi, u->nick, "%s", buf.c_str());
- }
- else if (message.substr(0, 9).equals_ci("\1VERSION\1"))
- {
- ircdproto->SendCTCP(bi, u->nick, "VERSION Anope-%s %s :%s - (%s) -- %s", Anope::Version().c_str(), Config->ServerName.c_str(), ircd->name, Config->EncModuleList.begin()->c_str(), Anope::Build().c_str());
- }
- }
- if (bi == NickServ || bi == MemoServ || bi == BotServ)
- mod_run_cmd(bi, u, message, false);
- else if (bi == ChanServ)
- {
- if (!is_oper(u) && Config->CSOpersOnly)
- u->SendMessage(ChanServ, ACCESS_DENIED);
- else
- mod_run_cmd(bi, u, message, false);
- }
- else if (bi == HostServ)
- {
- if (!ircd->vhost)
- u->SendMessage(HostServ, SERVICE_OFFLINE, Config->s_HostServ.c_str());
- else
- mod_run_cmd(bi, u, message, false);
- }
- else if (bi == OperServ)
- {
- if (!is_oper(u) && Config->OSOpersOnly)
- {
- u->SendMessage(OperServ, ACCESS_DENIED);
- if (Config->WallBadOS)
- ircdproto->SendGlobops(OperServ, "Denied access to %s from %s!%s@%s (non-oper)", Config->s_OperServ.c_str(), u->nick.c_str(), u->GetIdent().c_str(), u->host.c_str());
- }
- else
- {
- Log(OperServ) << u->nick << ": " << message;
- mod_run_cmd(bi, u, message, false);
- }
- }
- }
- }
-
- return MOD_CONT;
-}
-
-/*************************************************************************/
-
-bool m_stats(const Anope::string &source, const std::vector<Anope::string> &params)
+bool OnStats(const Anope::string &source, const std::vector<Anope::string> &params)
{
if (params.size() < 1)
return true;
@@ -222,7 +22,7 @@ bool m_stats(const Anope::string &source, const std::vector<Anope::string> &para
switch (params[0][0])
{
case 'l':
- if (u && is_oper(u))
+ if (u && u->HasMode(UMODE_OPER))
{
ircdproto->SendNumeric(Config->ServerName, 211, source, "Server SendBuf SentBytes SentMsgs RecvBuf RecvBytes RecvMsgs ConnTime");
ircdproto->SendNumeric(Config->ServerName, 211, source, "%s %d %d %d %d %d %d %ld", uplink_server->host.c_str(), UplinkSock->WriteBufferLen(), TotalWritten, -1, UplinkSock->ReadBufferLen(), TotalRead, -1, static_cast<long>(Anope::CurTime - start_time));
@@ -233,7 +33,7 @@ bool m_stats(const Anope::string &source, const std::vector<Anope::string> &para
case 'o':
case 'O':
/* Check whether the user is an operator */
- if (u && !is_oper(u) && Config->HideStatsO)
+ if (u && !u->HasMode(UMODE_OPER) && Config->HideStatsO)
ircdproto->SendNumeric(Config->ServerName, 219, source, "%c :End of /STATS report.", params[0][0]);
else
{
@@ -269,45 +69,103 @@ bool m_stats(const Anope::string &source, const std::vector<Anope::string> &para
return true;
}
-bool m_version(const Anope::string &source, const std::vector<Anope::string> &)
+bool OnTime(const Anope::string &source, const std::vector<Anope::string> &)
+{
+ if (source.empty())
+ return true;
+
+ time_t t;
+ time(&t);
+ struct tm *tm = localtime(&t);
+ char buf[64];
+ strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y %Z", tm);
+ ircdproto->SendNumeric(Config->ServerName, 391, source, "%s :%s", Config->ServerName.c_str(), buf);
+ return true;
+}
+
+bool OnVersion(const Anope::string &source, const std::vector<Anope::string> &)
{
if (!source.empty())
ircdproto->SendNumeric(Config->ServerName, 351, source, "Anope-%s %s :%s -(%s) -- %s", Anope::Version().c_str(), Config->ServerName.c_str(), ircd->name, Config->EncModuleList.begin()->c_str(), Anope::Build().c_str());
- return MOD_CONT;
+ return true;
}
-/*************************************************************************/
-
-int m_whois(const Anope::string &source, const Anope::string &who)
+/* XXX We should cache the file somewhere not open/read/close it on every request */
+bool OnMotd(const Anope::string &source, const std::vector<Anope::string> &)
{
- if (!source.empty() && !who.empty())
+ if (source.empty())
+ return true;
+
+ FILE *f = fopen(Config->MOTDFilename.c_str(), "r");
+ if (f)
{
- User *u;
- BotInfo *bi = findbot(who);
- if (bi)
- {
- ircdproto->SendNumeric(Config->ServerName, 311, source, "%s %s %s * :%s", bi->nick.c_str(), bi->GetIdent().c_str(), bi->host.c_str(), bi->realname.c_str());
- ircdproto->SendNumeric(Config->ServerName, 307, source, "%s :is a registered nick", bi->nick.c_str());
- ircdproto->SendNumeric(Config->ServerName, 312, source, "%s %s :%s", bi->nick.c_str(), Config->ServerName.c_str(), Config->ServerDesc.c_str());
- ircdproto->SendNumeric(Config->ServerName, 317, source, "%s %ld %ld :seconds idle, signon time", bi->nick.c_str(), static_cast<long>(Anope::CurTime - bi->lastmsg), static_cast<long>(start_time));
- ircdproto->SendNumeric(Config->ServerName, 318, source, "%s :End of /WHOIS list.", who.c_str());
- }
- else if (!ircd->svshold && (u = finduser(who)) && u->server == Me)
+ ircdproto->SendNumeric(Config->ServerName, 375, source, ":- %s Message of the Day", Config->ServerName.c_str());
+ char buf[BUFSIZE];
+ while (fgets(buf, sizeof(buf), f))
{
- ircdproto->SendNumeric(Config->ServerName, 311, source, "%s %s %s * :%s", u->nick.c_str(), u->GetIdent().c_str(), u->host.c_str(), u->realname.c_str());
- ircdproto->SendNumeric(Config->ServerName, 312, source, "%s %s :%s", u->nick.c_str(), Config->ServerName.c_str(), Config->ServerDesc.c_str());
- ircdproto->SendNumeric(Config->ServerName, 318, source, "%s :End of /WHOIS list.", u->nick.c_str());
+ buf[strlen(buf) - 1] = 0;
+ ircdproto->SendNumeric(Config->ServerName, 372, source, ":- %s", buf);
}
- else
- ircdproto->SendNumeric(Config->ServerName, 401, source, "%s :No such service.", who.c_str());
+ fclose(f);
+ ircdproto->SendNumeric(Config->ServerName, 376, source, ":End of /MOTD command.");
}
- return MOD_CONT;
+ else
+ ircdproto->SendNumeric(Config->ServerName, 422, source, ":- MOTD file not found! Please contact your IRC administrator.");
+
+ return true;
}
+#define ProtocolFunc(x) \
+ inline bool x(const Anope::string &source, const std::vector<Anope::string> &params) \
+ { \
+ return ircdmessage->x(source, params); \
+ }
+
+ProtocolFunc(On436)
+ProtocolFunc(OnAway)
+ProtocolFunc(OnJoin)
+ProtocolFunc(OnKick)
+ProtocolFunc(OnKill)
+ProtocolFunc(OnMode)
+ProtocolFunc(OnNick)
+ProtocolFunc(OnUID)
+ProtocolFunc(OnPart)
+ProtocolFunc(OnPing)
+ProtocolFunc(OnPrivmsg)
+ProtocolFunc(OnQuit)
+ProtocolFunc(OnServer)
+ProtocolFunc(OnSQuit)
+ProtocolFunc(OnTopic)
+ProtocolFunc(OnWhois)
+ProtocolFunc(OnCapab)
+ProtocolFunc(OnSJoin)
+ProtocolFunc(OnError)
+
void init_core_messages()
{
- static Message message_stats("STATS", m_stats);
- static Message message_time("TIME", m_time);
- static Message message_verssion("VERSION", m_version);
+ static Message message_stats("STATS", OnStats);
+ static Message message_time("TIME", OnTime);
+ static Message message_verssion("VERSION", OnVersion);
+ static Message message_motd("MOTD", OnMotd);
+
+ static Message message_436("436", On436);
+ static Message message_away("AWAY", OnAway);
+ static Message message_join("JOIN", OnJoin);
+ static Message message_kick("KICK", OnKick);
+ static Message message_kill("KILL", OnKill);
+ static Message message_mode("MODE", OnMode);
+ static Message message_nick("NICK", OnNick);
+ static Message message_uid("UID", OnUID);
+ static Message message_part("PART", OnPart);
+ static Message message_ping("PING", OnPing);
+ static Message message_privmsg("PRIVMSG", OnPrivmsg);
+ static Message message_quit("QUIT", OnQuit);
+ static Message message_server("SERVER", OnServer);
+ static Message message_squit("SQUIT", OnSQuit);
+ static Message message_topic("TOPIC", OnTopic);
+ static Message message_whois("WHOIS", OnWhois);
+ static Message message_capab("CAPAB", OnCapab);
+ static Message message_sjoin("SJOIN", OnSJoin);
+ static Message message_error("ERROR", OnError);
}