]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
Add ROUTE_TYPE_MESSAGE and use for PRIVMSG/NOTICE routing
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / postcommand.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *        the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Provides a spanning tree server link protocol */
15
16 #include "inspircd.h"
17 #include "socket.h"
18 #include "xline.h"
19 #include "../transport.h"
20
21 #include "main.h"
22 #include "utils.h"
23 #include "treeserver.h"
24 #include "treesocket.h"
25
26 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
27
28 void ModuleSpanningTree::OnPostCommand(const std::string &command, const std::vector<std::string>& parameters, User *user, CmdResult result, const std::string &original_line)
29 {
30         if (result == CMD_SUCCESS)
31                 Utils->RouteCommand(NULL, command, parameters, user);
32 }
33
34 void SpanningTreeUtilities::RouteCommand(TreeServer* origin, const std::string &command, const parameterlist& parameters, User *user)
35 {
36         if (!ServerInstance->IsValidModuleCommand(command, parameters.size(), user))
37                 return;
38
39         /* We know it's non-null because IsValidModuleCommand returned true */
40         Command* thiscmd = ServerInstance->Parser->GetHandler(command);
41
42         RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
43
44         std::string sent_cmd = command;
45         parameterlist params;
46
47         if (routing.type == ROUTE_TYPE_LOCALONLY)
48         {
49                 return;
50         }
51         else if (routing.type == ROUTE_TYPE_OPT_BCAST)
52         {
53                 params.push_back("*");
54                 params.push_back(command);
55                 sent_cmd = "ENCAP";
56         }
57         else if (routing.type == ROUTE_TYPE_OPT_UCAST)
58         {
59                 TreeServer* sdest = FindServer(routing.serverdest);
60                 if (!sdest)
61                 {
62                         ServerInstance->Logs->Log("m_spanningtree",ERROR,"Trying to route ENCAP to nonexistant server %s",
63                                 routing.serverdest.c_str());
64                         return;
65                 }
66                 params.push_back(sdest->GetID());
67                 params.push_back(command);
68                 sent_cmd = "ENCAP";
69         }
70         else
71         {
72                 Module* srcmodule = thiscmd->creator;
73                 Version ver = srcmodule->GetVersion();
74
75                 if (!(ver.Flags & (VF_COMMON | VF_CORE)))
76                 {
77                         ServerInstance->Logs->Log("m_spanningtree",ERROR,"Routed command %s from non-VF_COMMON module %s",
78                                 command.c_str(), srcmodule->ModuleSourceFile.c_str());
79                         return;
80                 }
81         }
82
83         std::string output_text;
84         ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
85
86         params.push_back(output_text);
87
88         if (routing.type == ROUTE_TYPE_MESSAGE)
89         {
90                 char pfx = 0;
91                 std::string dest = routing.serverdest;
92                 if (ServerInstance->Modes->FindPrefix(dest[0]))
93                 {
94                         pfx = dest[0];
95                         dest = dest.substr(1);
96                 }
97                 if (dest[0] == '#')
98                 {
99                         Channel* c = ServerInstance->FindChan(dest);
100                         if (!c)
101                                 return;
102                         TreeServerList list;
103                         // TODO OnBuildExemptList hook was here
104                         GetListOfServersForChannel(c,list,pfx, CUList());
105                         std::string data = ":" + user->uuid + " " + sent_cmd;
106                         for (unsigned int x = 0; x < params.size(); x++)
107                                 data += " " + params[x];
108                         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
109                         {
110                                 TreeSocket* Sock = i->second->GetSocket();
111                                 if (origin && origin->GetSocket() == Sock)
112                                         continue;
113                                 if (Sock)
114                                         Sock->WriteLine(data);
115                         }
116                 }
117                 else if (dest[0] == '$')
118                 {
119                         if (origin)
120                                 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
121                         else
122                                 DoOneToMany(user->uuid, sent_cmd, params);
123                 }
124                 else
125                 {
126                         // user target?
127                         User* d = ServerInstance->FindNick(dest);
128                         if (!d)
129                                 return;
130                         TreeServer* tsd = BestRouteTo(d->server);
131                         if (tsd == origin)
132                                 // huh? no routing stuff around in a circle, please.
133                                 return;
134                         DoOneToOne(user->uuid, sent_cmd, params, d->server);
135                 }
136         }
137         else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
138         {
139                 if (origin)
140                         DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
141                 else
142                         DoOneToMany(user->uuid, sent_cmd, params);
143         }
144         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
145         {
146                 if (origin && routing.serverdest == origin->GetName())
147                         return;
148                 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);
149         }
150 }