]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/protocolinterface.cpp
9f32963a02c55adb1e645b8e9b32e675a882eff9
[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, TargetTypeFlags 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                 default:
50                         throw CoreException("I don't know how to handle TYPE_OTHER.");
51                         break;
52         }
53         params.push_back(key);
54         params.push_back(":" + data);
55
56         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"METADATA",params);
57 }
58
59 void SpanningTreeProtocolInterface::SendTopic(Channel* channel, std::string &topic)
60 {
61         parameterlist params;
62
63         params.push_back(channel->name);
64         params.push_back(ConvToStr(ServerInstance->Time()));
65         params.push_back(ServerInstance->Config->ServerName);
66         params.push_back(":" + topic);
67
68         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FTOPIC", params);
69 }
70
71 void SpanningTreeProtocolInterface::SendMode(const std::string &target, parameterlist &modedata)
72 {
73         if (modedata.empty())
74                 return;
75
76         std::string outdata;
77
78         /* Warning: in-place translation is only safe for type TR_NICK */
79         for (size_t n = 0; n < modedata.size(); n++)
80         {
81                 ServerInstance->Parser->TranslateUIDs(TR_NICK, modedata[n], outdata);
82                 modedata[n] = outdata;
83         }
84
85         std::string uidtarget;
86         ServerInstance->Parser->TranslateUIDs(TR_NICK, target, uidtarget);
87         modedata.insert(modedata.begin(), uidtarget);
88
89         User* a = ServerInstance->FindNick(uidtarget);
90         if (a)
91         {
92                 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODE",modedata);
93                 return;
94         }
95         else
96         {
97                 Channel* c = ServerInstance->FindChan(target);
98                 if (c)
99                 {
100                         modedata.insert(modedata.begin() + 1, ConvToStr(c->age));
101                         Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FMODE",modedata);
102                 }
103         }
104 }
105
106 void SpanningTreeProtocolInterface::SendModeNotice(const std::string &modes, const std::string &text)
107 {
108         parameterlist p;
109         p.push_back(modes);
110         p.push_back(":" + text);
111         Utils->DoOneToMany(ServerInstance->Config->GetSID(), "MODENOTICE", p);
112 }
113
114 void SpanningTreeProtocolInterface::SendSNONotice(const std::string &snomask, const std::string &text)
115 {
116         parameterlist p;
117         p.push_back(snomask);
118         p.push_back(":" + text);
119         Utils->DoOneToMany(ServerInstance->Config->GetSID(), "SNONOTICE", p);
120 }
121
122 void SpanningTreeProtocolInterface::PushToClient(User* target, const std::string &rawline)
123 {
124         parameterlist p;
125         p.push_back(target->uuid);
126         p.push_back(rawline);
127         Utils->DoOneToOne(ServerInstance->Config->GetSID(), "PUSH", p, target->server);
128 }
129
130 void SpanningTreeProtocolInterface::SendChannel(Channel* target, char status, const std::string &text)
131 {
132         std::string cname = target->name;
133         if (status)
134                 cname = status + cname;
135         TreeServerList list;
136         CUList exempt_list;
137         Utils->GetListOfServersForChannel(target,list,status,exempt_list);
138         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
139         {
140                 TreeSocket* Sock = i->second->GetSocket();
141                 if (Sock)
142                         Sock->WriteLine(text);
143         }
144 }
145
146
147 void SpanningTreeProtocolInterface::SendChannelPrivmsg(Channel* target, char status, const std::string &text)
148 {
149         SendChannel(target, status, ":" + ServerInstance->Config->GetSID()+" PRIVMSG "+target->name+" :"+text);
150 }
151
152 void SpanningTreeProtocolInterface::SendChannelNotice(Channel* target, char status, const std::string &text)
153 {
154         SendChannel(target, status, ":" + ServerInstance->Config->GetSID()+" NOTICE "+target->name+" :"+text);
155 }
156
157 void SpanningTreeProtocolInterface::SendUserPrivmsg(User* target, const std::string &text)
158 {
159         TreeServer* serv = Utils->FindServer(target->server);
160         if (serv)
161         {
162                 TreeSocket* sock = serv->GetSocket();
163                 if (sock)
164                 {
165                         sock->WriteLine(":" + ServerInstance->Config->GetSID() + " PRIVMSG " + target->nick + " :"+text);
166                 }
167         }
168 }
169
170 void SpanningTreeProtocolInterface::SendUserNotice(User* target, const std::string &text)
171 {
172         TreeServer* serv = Utils->FindServer(target->server);
173         if (serv)
174         {
175                 TreeSocket* sock = serv->GetSocket();
176                 if (sock)
177                 {
178                         sock->WriteLine(":" + ServerInstance->Config->GetSID() + " NOTICE " + target->nick + " :"+text);
179                 }
180         }
181 }
182
183 void SpanningTreeProtocolInterface::Introduce(User* user)
184 {
185         if (IS_LOCAL(user))
186         {
187                 std::deque<std::string> params;
188                 params.push_back(user->uuid);
189                 params.push_back(ConvToStr(user->age));
190                 params.push_back(user->nick);
191                 params.push_back(user->host);
192                 params.push_back(user->dhost);
193                 params.push_back(user->ident);
194                 params.push_back(user->GetIPString());
195                 params.push_back(ConvToStr(user->signon));
196                 params.push_back("+"+std::string(user->FormatModes(true)));
197                 params.push_back(":"+std::string(user->fullname));
198                 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "UID", params);
199         }
200
201         TreeServer* SourceServer = Utils->FindServer(user->server);
202         if (SourceServer)
203         {
204                 SourceServer->SetUserCount(1); // increment by 1
205         }
206 }