Anope IRC Services  Version 2.0
process.cpp
Go to the documentation of this file.
1 /* Main processing code for Services.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  */
11 
12 #include "services.h"
13 #include "modules.h"
14 #include "protocol.h"
15 #include "servers.h"
16 #include "users.h"
17 #include "regchannel.h"
18 
19 void Anope::Process(const Anope::string &buffer)
20 {
21  /* If debugging, log the buffer */
22  Log(LOG_RAWIO) << "Received: " << buffer;
23 
24  if (buffer.empty())
25  return;
26 
27  Anope::string source, command;
28  std::vector<Anope::string> params;
29 
30  IRCD->Parse(buffer, source, command, params);
31 
33  {
34  Log() << "Source : " << (source.empty() ? "No source" : source);
35  Log() << "Command: " << command;
36 
37  if (params.empty())
38  Log() << "No params";
39  else
40  for (unsigned i = 0; i < params.size(); ++i)
41  Log() << "params " << i << ": " << params[i];
42  }
43 
44  if (command.empty())
45  {
46  Log(LOG_DEBUG) << "No command? " << buffer;
47  return;
48  }
49 
51 
52  MessageSource src(source);
53 
54  EventReturn MOD_RESULT;
55  FOREACH_RESULT(OnMessage, MOD_RESULT, (src, command, params));
56  if (MOD_RESULT == EVENT_STOP)
57  return;
58 
59  ServiceReference<IRCDMessage> m("IRCDMessage", proto_name + "/" + command.lower());
60  if (!m)
61  {
62  Log(LOG_DEBUG) << "unknown message from server (" << buffer << ")";
63  return;
64  }
65 
66  if (m->HasFlag(IRCDMESSAGE_SOFT_LIMIT) ? (params.size() < m->GetParamCount()) : (params.size() != m->GetParamCount()))
67  Log(LOG_DEBUG) << "invalid parameters for " << command << ": " << params.size() << " != " << m->GetParamCount();
68  else if (m->HasFlag(IRCDMESSAGE_REQUIRE_USER) && !src.GetUser())
69  Log(LOG_DEBUG) << "unexpected non-user source " << source << " for " << command;
70  else if (m->HasFlag(IRCDMESSAGE_REQUIRE_SERVER) && !source.empty() && !src.GetServer())
71  Log(LOG_DEBUG) << "unexpected non-server source " << source << " for " << command;
72  else
73  m->Run(src, params);
74 }
75 
76 void IRCDProto::Parse(const Anope::string &buffer, Anope::string &source, Anope::string &command, std::vector<Anope::string> &params)
77 {
78  spacesepstream sep(buffer);
79 
80  if (buffer[0] == ':')
81  {
82  sep.GetToken(source);
83  source.erase(0, 1);
84  }
85 
86  sep.GetToken(command);
87 
88  for (Anope::string token; sep.GetToken(token);)
89  {
90  if (token[0] == ':')
91  {
92  if (!sep.StreamEnd())
93  params.push_back(token.substr(1) + " " + sep.GetRemaining());
94  else
95  params.push_back(token.substr(1));
96  break;
97  }
98  else
99  params.push_back(token);
100  }
101 }
102 
104 {
105  if (!source.empty())
106  return ":" + source + " " + message;
107  else
108  return message;
109 }
110 
Anope::string name
Definition: modules.h:221
#define FOREACH_RESULT(ename, ret, args)
Definition: modules.h:95
string lower() const
Definition: anope.h:255
iterator erase(const iterator &i)
Definition: anope.h:155
Server * GetServer() const
Definition: protocol.cpp:488
User * GetUser() const
Definition: protocol.cpp:478
bool empty() const
Definition: anope.h:126
CoreExport IRCDProto * IRCD
Definition: protocol.cpp:23
const Anope::string GetRemaining()
Definition: hashcomp.cpp:156
EventReturn
Definition: modules.h:129
CoreExport bool ProtocolDebug
Definition: main.cpp:28
static Module * FindFirstOf(ModType type)
virtual Anope::string Format(const Anope::string &source, const Anope::string &message)
Definition: process.cpp:103
bool GetToken(Anope::string &token)
Definition: hashcomp.cpp:99
virtual void Parse(const Anope::string &, Anope::string &, Anope::string &, std::vector< Anope::string > &)
Definition: process.cpp:76
Definition: logger.h:53
void Process(const Anope::string &)
Definition: process.cpp:19
bool StreamEnd()
Definition: hashcomp.cpp:161