1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* Copyright (C) 2004-2010 See the AUTHORS file for details.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "znc.h"
#include "User.h"
#include "IRCSock.h"
struct reply {
const char *szReply;
bool bLastResponse;
};
// TODO this list is far from complete, no errors are handled
static const struct {
const char *szRequest;
struct reply vReplies[10];
} vRouteReplies[] = {
{"WHO", {
{"352", false},
{"403", true}, // No such chan
{"315", true},
{NULL, true}
}},
{"LIST", {
{"321", false},
{"322", false},
{"323", true},
{NULL, true}
}},
{"NAMES", {
{"353", false},
{"366", true},
// No such nick/channel
{"401", true},
{NULL, true},
}},
{"LUSERS", {
{"251", false},
{"252", false},
{"253", false},
{"254", false},
{"255", false},
{"265", false},
{"266", true},
// We don't handle 250 here since some IRCds don't sent it
//{"250", true},
{NULL, true}
}},
{"WHOIS", {
{"311", false},
{"319", false},
{"312", false},
// "<ip> :actually using host"
{"338", false},
{"318", true},
// No such nick/channel
{"401", true},
// No such server
{"402", true},
{NULL, true}
}},
{"PING", {
{"PONG", true},
{NULL, true}
}},
{"USERHOST", {
{"302", true},
{NULL, true}
}},
{"TIME", {
{"391", true},
{NULL, true}
}},
{"WHOWAS", {
{"312", false},
{"314", false},
{"369", true},
{NULL, true}
}},
{"ISON", {
{"303", true},
{NULL, true}
}},
{"LINKS", {
{"364", false},
{"365", true},
{NULL, true}
}},
{"MAP", {
{"006", false},
// inspircd
{"270", false},
// SilverLeo wants this two added
{"015", false},
{"017", true},
{"007", true},
{NULL, true}
}},
{"TRACE", {
{"200", false},
{"205", false},
{"262", true},
{NULL, true}
}},
{"USERS", {
{"265", false},
{"266", true},
{NULL, true},
}},
// END (last item!)
{NULL, {{NULL, true}}}
};
class CRouteTimeout : public CTimer {
public:
CRouteTimeout(CModule* pModule, unsigned int uInterval, unsigned int uCycles,
const CString& sLabel, const CString& sDescription)
: CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {}
virtual ~CRouteTimeout() {}
protected:
virtual void RunJob();
};
struct queued_req {
CString sLine;
const struct reply *reply;
};
typedef std::map<CClient *, std::vector<struct queued_req> > requestQueue;
class CRouteRepliesMod : public CModule
{
public:
MODCONSTRUCTOR(CRouteRepliesMod)
{
m_pDoing = NULL;
m_pReplies = NULL;
}
virtual ~CRouteRepliesMod() {
requestQueue::iterator it;
while (!m_vsPending.empty()) {
it = m_vsPending.begin();
while (!it->second.empty()) {
PutIRC(it->second[0].sLine);
it->second.erase(it->second.begin());
}
m_vsPending.erase(it);
}
}
virtual void OnIRCConnected()
{
m_pDoing = NULL;
m_pReplies = NULL;
m_vsPending.clear();
// No way we get a reply, so stop the timer (If it's running)
RemTimer("RouteTimeout");
}
virtual void OnIRCDisconnected()
{
OnIRCConnected(); // Let's keep it in one place
}
virtual void OnClientDisconnect()
{
requestQueue::iterator it;
if (m_pClient == m_pDoing) {
// The replies which aren't received yet will be
// broadcasted to everyone, but at least nothing breaks
RemTimer("RouteTimeout");
m_pDoing = NULL;
m_pReplies = NULL;
}
it = m_vsPending.find(m_pClient);
if (it != m_vsPending.end())
m_vsPending.erase(it);
SendRequest();
}
virtual EModRet OnRaw(CString& sLine)
{
CString sCmd = sLine.Token(1).AsUpper();
size_t i = 0;
if (!m_pReplies)
return CONTINUE;
// Is this a "not enough arguments" error?
if (sCmd == "461") {
// :server 461 nick WHO :Not enough parameters
CString sOrigCmd = sLine.Token(3);
if (m_sLastRequest.Token(0).Equals(sOrigCmd)) {
// This is the reply to the last request
if (RouteReply(sLine, true))
return HALTCORE;
return CONTINUE;
}
}
while (m_pReplies[i].szReply != NULL) {
if (m_pReplies[i].szReply == sCmd) {
if (RouteReply(sLine, m_pReplies[i].bLastResponse, sCmd == "353"))
return HALTCORE;
return CONTINUE;
}
i++;
}
// TODO HALTCORE is wrong, it should not be passed to
// the clients, but the core itself should still handle it!
return CONTINUE;
}
virtual EModRet OnUserRaw(CString& sLine)
{
CString sCmd = sLine.Token(0).AsUpper();
size_t i = 0;
if (!m_pUser->GetIRCSock())
return CONTINUE;
while (vRouteReplies[i].szRequest != NULL) {
if (vRouteReplies[i].szRequest == sCmd) {
struct queued_req req = {
sLine, vRouteReplies[i].vReplies
};
m_vsPending[m_pClient].push_back(req);
SendRequest();
return HALTCORE;
}
i++;
}
return CONTINUE;
}
void Timeout()
{
// The timer will be deleted after this by the event loop
if (GetNV("silent_timeouts") != "yes") {
PutModule("This module hit a timeout which is possibly a bug.");
PutModule("Use \"silent yes\" to disable this message.");
PutModule("Last request: " + m_sLastRequest);
PutModule("Expected replies: ");
for (size_t i = 0; m_pReplies[i].szReply != NULL; i++) {
if (m_pReplies[i].bLastResponse)
PutModule(m_pReplies[i].szReply +
CString(" (last)"));
else
PutModule(m_pReplies[i].szReply);
}
}
m_pDoing = NULL;
m_pReplies = NULL;
SendRequest();
}
private:
bool RouteReply(const CString& sLine, bool bFinished = false, bool bIsRaw353 = false)
{
if (!m_pDoing)
return false;
// 353 needs special treatment due to NAMESX and UHNAMES
if (bIsRaw353)
GetUser()->GetIRCSock()->ForwardRaw353(sLine, m_pDoing);
else
m_pDoing->PutClient(sLine);
if (bFinished) {
// Stop the timeout
RemTimer("RouteTimeout");
m_pDoing = NULL;
m_pReplies = NULL;
SendRequest();
}
return true;
}
void SendRequest()
{
requestQueue::iterator it;
if (m_pDoing || m_pReplies)
return;
if (m_vsPending.empty())
return;
it = m_vsPending.begin();
if (it->second.empty()) {
m_vsPending.erase(it);
SendRequest();
return;
}
// When we are called from the timer, we need to remove it.
// We can't delete it (segfault on return), thus we
// just stop it. The main loop will delete it.
CTimer *pTimer = FindTimer("RouteTimeout");
if (pTimer) {
pTimer->Stop();
UnlinkTimer(pTimer);
}
AddTimer(new CRouteTimeout(this, 60, 1, "RouteTimeout",
"Recover from missing / wrong server replies"));
m_pDoing = it->first;
m_pReplies = it->second[0].reply;
m_sLastRequest = it->second[0].sLine;
PutIRC(it->second[0].sLine);
it->second.erase(it->second.begin());
}
virtual void OnModCommand(const CString& sCommand) {
const CString sCmd = sCommand.Token(0);
const CString sArgs = sCommand.Token(1, true);
if (sCmd.Equals("silent")) {
if (sArgs.Equals("yes")) {
SetNV("silent_timeouts", "yes");
PutModule("Disabled timeout messages");
} else if (sArgs.Equals("no")) {
DelNV("silent_timeouts");
PutModule("Enabled timeout messages");
} else if (sArgs.empty()) {
if (GetNV("silent_timeouts") == "yes")
PutModule("Timeout messages are disabled");
else
PutModule("Timeout message are enabled");
} else
PutModule("Invalid argument");
} else {
PutModule("Available commands: silent [yes/no], silent");
}
}
CClient *m_pDoing;
const struct reply *m_pReplies;
requestQueue m_vsPending;
// This field is only used for display purpose.
CString m_sLastRequest;
};
void CRouteTimeout::RunJob()
{
CRouteRepliesMod *pMod = (CRouteRepliesMod *) m_pModule;
pMod->Timeout();
}
MODULEDEFS(CRouteRepliesMod, "Send replies (e.g. to /who) to the right client only")
|