]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/protocolinterface.cpp
386f2bc318e3322cc5d8a9051d218504e356b3b4
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / protocolinterface.cpp
1 #include "inspircd.h"
2 #include "m_spanningtree/main.h"
3 #include "m_spanningtree/utils.h"
4 #include "m_spanningtree/treeserver.h"
5 #include "m_spanningtree/treesocket.h"
6 #include "m_spanningtree/protocolinterface.h"
7
8 /*
9  * For documentation on this class, see include/protocol.h.
10  */
11
12 void SpanningTreeProtocolInterface::GetServerList(ProtoServerList &sl)
13 {
14         sl.clear();
15         for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
16         {
17                 ProtoServer ps;
18                 ps.servername = i->second->GetName();
19                 TreeServer* s = i->second->GetParent();
20                 ps.parentname = s ? s->GetName() : "";
21                 ps.usercount = i->second->GetUserCount();
22                 ps.opercount = i->second->GetOperCount();
23                 ps.gecos = i->second->GetDesc();
24                 ps.latencyms = i->second->rtt;
25                 sl.push_back(ps);
26         }
27 }
28
29 void SpanningTreeProtocolInterface::SendEncapsulatedData(parameterlist &encap)
30 {
31         Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ENCAP", encap);
32 }
33
34 void SpanningTreeProtocolInterface::SendMetaData(void* target, int type, const std::string &key, const std::string &data)
35 {
36         parameterlist params;
37
38         switch (type)
39         {
40                 case TYPE_USER:
41                         params.push_back(((User*)target)->uuid);
42                 break;
43                 case TYPE_CHANNEL:
44                         params.push_back(((Channel*)target)->name);
45                 break;
46                 case TYPE_SERVER:
47                         params.push_back("*");
48                 break;
49         }
50         params.push_back(key);
51         params.push_back(":" + data);
52
53         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"METADATA",params);
54 }
55
56 void SpanningTreeProtocolInterface::SendTopic(Channel* channel, std::string &topic)
57 {
58         parameterlist params;
59
60         params.push_back(channel->name);
61         params.push_back(ConvToStr(ServerInstance->Time()));
62         params.push_back(ServerInstance->Config->ServerName);
63         params.push_back(":" + topic);
64
65         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FTOPIC", params);
66 }
67
68 void SpanningTreeProtocolInterface::SendMode(const std::string &target, parameterlist &modedata)
69 {
70         if (modedata.empty())
71                 return;
72
73         std::string outdata;
74
75         /* Warning: in-place translation is only safe for type TR_NICK */
76         for (size_t n = 0; n < modedata.size(); n++)
77         {
78                 ServerInstance->Parser->TranslateUIDs(TR_NICK, modedata[n], outdata);
79                 modedata[n] = outdata;
80         }
81
82         std::string uidtarget;
83         ServerInstance->Parser->TranslateUIDs(TR_NICK, target, uidtarget);
84         modedata.insert(modedata.begin(), uidtarget);
85
86         User* a = ServerInstance->FindNick(uidtarget);
87         if (a)
88         {
89                 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODE",modedata);
90                 return;
91         }
92         else
93         {
94                 Channel* c = ServerInstance->FindChan(target);
95                 if (c)
96                 {
97                         modedata.insert(modedata.begin() + 1, ConvToStr(c->age));
98                         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FMODE",modedata);
99                 }
100         }
101 }
102
103 void SpanningTreeProtocolInterface::SendModeNotice(const std::string &modes, const std::string &text)
104 {
105         parameterlist p;
106         p.push_back(modes);
107         p.push_back(":" + text);
108         Utils->DoOneToMany(ServerInstance->Config->GetSID(), "MODENOTICE", p);
109 }
110
111 void SpanningTreeProtocolInterface::SendSNONotice(const std::string &snomask, const std::string &text)
112 {
113         parameterlist p;
114         p.push_back(snomask);
115         p.push_back(":" + text);
116         Utils->DoOneToMany(ServerInstance->Config->GetSID(), "SNONOTICE", p);
117 }
118
119 void SpanningTreeProtocolInterface::PushToClient(User* target, const std::string &rawline)
120 {
121         parameterlist p;
122         p.push_back(target->uuid);
123         p.push_back(rawline);
124         Utils->DoOneToOne(ServerInstance->Config->GetSID(), "PUSH", p, target->server);
125 }
126
127 void SpanningTreeProtocolInterface::SendChannel(Channel* target, char status, const std::string &text)
128 {
129         std::string cname = target->name;
130         if (status)
131                 cname = status + cname;
132         TreeServerList list;
133         CUList exempt_list;
134         Utils->GetListOfServersForChannel(target,list,status,exempt_list);
135         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
136         {
137                 TreeSocket* Sock = i->second->GetSocket();
138                 if (Sock)
139                         Sock->WriteLine(text);
140         }
141 }
142
143
144 void SpanningTreeProtocolInterface::SendChannelPrivmsg(Channel* target, char status, const std::string &text)
145 {
146         SendChannel(target, status, ":" + ServerInstance->Config->GetSID()+" PRIVMSG "+target->name+" :"+text);
147 }
148
149 void SpanningTreeProtocolInterface::SendChannelNotice(Channel* target, char status, const std::string &text)
150 {
151         SendChannel(target, status, ":" + ServerInstance->Config->GetSID()+" NOTICE "+target->name+" :"+text);
152 }
153
154 void SpanningTreeProtocolInterface::SendUserPrivmsg(User* target, const std::string &text)
155 {
156         TreeServer* serv = Utils->FindServer(target->server);
157         if (serv)
158         {
159                 TreeSocket* sock = serv->GetSocket();
160                 if (sock)
161                 {
162                         sock->WriteLine(":" + ServerInstance->Config->GetSID() + " PRIVMSG " + target->nick + " :"+text);
163                 }
164         }
165 }
166
167 void SpanningTreeProtocolInterface::SendUserNotice(User* target, const std::string &text)
168 {
169         TreeServer* serv = Utils->FindServer(target->server);
170         if (serv)
171         {
172                 TreeSocket* sock = serv->GetSocket();
173                 if (sock)
174                 {
175                         sock->WriteLine(":" + ServerInstance->Config->GetSID() + " NOTICE " + target->nick + " :"+text);
176                 }
177         }
178 }
179
180 void SpanningTreeProtocolInterface::Introduce(User* user)
181 {
182         if (IS_LOCAL(user))
183         {
184                 std::deque<std::string> params;
185                 params.push_back(user->uuid);
186                 params.push_back(ConvToStr(user->age));
187                 params.push_back(user->nick);
188                 params.push_back(user->host);
189                 params.push_back(user->dhost);
190                 params.push_back(user->ident);
191                 params.push_back(user->GetIPString());
192                 params.push_back(ConvToStr(user->signon));
193                 params.push_back("+"+std::string(user->FormatModes(true)));
194                 params.push_back(":"+std::string(user->fullname));
195                 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "UID", params);
196         }
197
198         TreeServer* SourceServer = Utils->FindServer(user->server);
199         if (SourceServer)
200         {
201                 SourceServer->SetUserCount(1); // increment by 1
202         }
203 }