]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/protocolinterface.cpp
Replace copyright headers with headers granting specific authors copyright
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / protocolinterface.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "main.h"
23 #include "utils.h"
24 #include "treeserver.h"
25 #include "treesocket.h"
26 #include "protocolinterface.h"
27
28 /*
29  * For documentation on this class, see include/protocol.h.
30  */
31
32 void SpanningTreeProtocolInterface::GetServerList(ProtoServerList &sl)
33 {
34         sl.clear();
35         for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
36         {
37                 ProtoServer ps;
38                 ps.servername = i->second->GetName();
39                 TreeServer* s = i->second->GetParent();
40                 ps.parentname = s ? s->GetName() : "";
41                 ps.usercount = i->second->GetUserCount();
42                 ps.opercount = i->second->GetOperCount();
43                 ps.gecos = i->second->GetDesc();
44                 ps.latencyms = i->second->rtt;
45                 sl.push_back(ps);
46         }
47 }
48
49 bool SpanningTreeProtocolInterface::SendEncapsulatedData(const parameterlist &encap)
50 {
51         if (encap[0].find('*') != std::string::npos)
52         {
53                 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ENCAP", encap);
54                 return true;
55         }
56         return Utils->DoOneToOne(ServerInstance->Config->GetSID(), "ENCAP", encap, encap[0]);
57 }
58
59 void SpanningTreeProtocolInterface::SendMetaData(Extensible* target, const std::string &key, const std::string &data)
60 {
61         parameterlist params;
62
63         User* u = dynamic_cast<User*>(target);
64         Channel* c = dynamic_cast<Channel*>(target);
65         if (u)
66                 params.push_back(u->uuid);
67         else if (c)
68                 params.push_back(c->name);
69         else
70                 params.push_back("*");
71
72         params.push_back(key);
73         params.push_back(":" + data);
74
75         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"METADATA",params);
76 }
77
78 void SpanningTreeProtocolInterface::SendTopic(Channel* channel, std::string &topic)
79 {
80         parameterlist params;
81
82         params.push_back(channel->name);
83         params.push_back(ConvToStr(ServerInstance->Time()));
84         params.push_back(ServerInstance->Config->ServerName);
85         params.push_back(":" + topic);
86
87         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FTOPIC", params);
88 }
89
90 void SpanningTreeProtocolInterface::SendMode(const std::string &target, const parameterlist &modedata, const std::vector<TranslateType> &translate)
91 {
92         if (modedata.empty())
93                 return;
94
95         std::string outdata;
96         ServerInstance->Parser->TranslateUIDs(translate, modedata, outdata);
97
98         std::string uidtarget;
99         ServerInstance->Parser->TranslateUIDs(TR_NICK, target, uidtarget);
100
101         parameterlist outlist;
102         outlist.push_back(uidtarget);
103         outlist.push_back(outdata);
104
105         User* a = ServerInstance->FindNick(uidtarget);
106         if (a)
107         {
108                 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODE",outlist);
109                 return;
110         }
111         else
112         {
113                 Channel* c = ServerInstance->FindChan(target);
114                 if (c)
115                 {
116                         outlist.insert(outlist.begin() + 1, ConvToStr(c->age));
117                         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FMODE",outlist);
118                 }
119         }
120 }
121
122 void SpanningTreeProtocolInterface::SendSNONotice(const std::string &snomask, const std::string &text)
123 {
124         parameterlist p;
125         p.push_back(snomask);
126         p.push_back(":" + text);
127         Utils->DoOneToMany(ServerInstance->Config->GetSID(), "SNONOTICE", p);
128 }
129
130 void SpanningTreeProtocolInterface::PushToClient(User* target, const std::string &rawline)
131 {
132         parameterlist p;
133         p.push_back(target->uuid);
134         p.push_back(":" + rawline);
135         Utils->DoOneToOne(ServerInstance->Config->GetSID(), "PUSH", p, target->server);
136 }
137
138 void SpanningTreeProtocolInterface::SendChannel(Channel* target, char status, const std::string &text)
139 {
140         std::string cname = target->name;
141         if (status)
142                 cname = status + cname;
143         TreeServerList list;
144         CUList exempt_list;
145         Utils->GetListOfServersForChannel(target,list,status,exempt_list);
146         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
147         {
148                 TreeSocket* Sock = i->second->GetSocket();
149                 if (Sock)
150                         Sock->WriteLine(text);
151         }
152 }
153
154
155 void SpanningTreeProtocolInterface::SendChannelPrivmsg(Channel* target, char status, const std::string &text)
156 {
157         SendChannel(target, status, ":" + ServerInstance->Config->GetSID()+" PRIVMSG "+target->name+" :"+text);
158 }
159
160 void SpanningTreeProtocolInterface::SendChannelNotice(Channel* target, char status, const std::string &text)
161 {
162         SendChannel(target, status, ":" + ServerInstance->Config->GetSID()+" NOTICE "+target->name+" :"+text);
163 }
164
165 void SpanningTreeProtocolInterface::SendUserPrivmsg(User* target, const std::string &text)
166 {
167         TreeServer* serv = Utils->FindServer(target->server);
168         if (serv)
169         {
170                 TreeSocket* sock = serv->GetSocket();
171                 if (sock)
172                 {
173                         sock->WriteLine(":" + ServerInstance->Config->GetSID() + " PRIVMSG " + target->nick + " :"+text);
174                 }
175         }
176 }
177
178 void SpanningTreeProtocolInterface::SendUserNotice(User* target, const std::string &text)
179 {
180         TreeServer* serv = Utils->FindServer(target->server);
181         if (serv)
182         {
183                 TreeSocket* sock = serv->GetSocket();
184                 if (sock)
185                 {
186                         sock->WriteLine(":" + ServerInstance->Config->GetSID() + " NOTICE " + target->nick + " :"+text);
187                 }
188         }
189 }