CmdResult Handle (const std::vector<std::string> ¶meters, User *user) { if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName.c_str()) return CMD_SUCCESS; User *targuser; Channel *targchan; std::string checkstr; std::string chliststr; checkstr = ":" + ServerInstance->Config->ServerName + " 304 " + user->nick + " :CHECK"; targuser = ServerInstance->FindNick(parameters[0]); targchan = ServerInstance->FindChan(parameters[0]); /* * Syntax of a /check reply: * :server.name 304 target :CHECK START <target> * :server.name 304 target :CHECK <field> <value> * :server.name 304 target :CHECK END */ user->SendText(checkstr + " START " + parameters[0]); if (targuser) { LocalUser* loctarg = IS_LOCAL(targuser); /* /check on a user */ user->SendText(checkstr + " nuh " + targuser->GetFullHost()); user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost()); user->SendText(checkstr + " realname " + targuser->fullname); user->SendText(checkstr + " modes +" + targuser->FormatModes()); user->SendText(checkstr + " snomasks " + GetSnomasks(targuser)); user->SendText(checkstr + " server " + targuser->server->GetName()); user->SendText(checkstr + " uid " + targuser->uuid); user->SendText(checkstr + " signon " + timestring(targuser->signon)); user->SendText(checkstr + " nickts " + timestring(targuser->age)); if (loctarg) user->SendText(checkstr + " lastmsg " + timestring(loctarg->idle_lastmsg)); if (targuser->IsAway()) { /* user is away */ user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime)); user->SendText(checkstr + " awaymsg " + targuser->awaymsg); } if (targuser->IsOper()) { OperInfo* oper = targuser->oper; /* user is an oper of type ____ */ user->SendText(checkstr + " opertype " + oper->name); if (loctarg) { std::string umodes; std::string cmodes; for(char c='A'; c < 'z'; c++) { ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER); if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER)) umodes.push_back(c); mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL); if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL)) cmodes.push_back(c); } user->SendText(checkstr + " modeperms user="******" channel=" + cmodes); std::string opcmds; for(std::set<std::string>::iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); i++) { opcmds.push_back(' '); opcmds.append(*i); } std::stringstream opcmddump(opcmds); user->SendText(checkstr + " commandperms", opcmddump); std::string privs; for(std::set<std::string>::iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); i++) { privs.push_back(' '); privs.append(*i); } std::stringstream privdump(privs); user->SendText(checkstr + " permissions", privdump); } } if (loctarg) { user->SendText(checkstr + " clientaddr " + loctarg->client_sa.str()); user->SendText(checkstr + " serveraddr " + loctarg->server_sa.str()); std::string classname = loctarg->GetClass()->name; if (!classname.empty()) user->SendText(checkstr + " connectclass " + classname); } else user->SendText(checkstr + " onip " + targuser->GetIPString()); for (UCListIter i = targuser->chans.begin(); i != targuser->chans.end(); i++) { Channel* c = (*i)->chan; chliststr.append(c->GetPrefixChar(targuser)).append(c->name).append(" "); } std::stringstream dump(chliststr); user->SendText(checkstr + " onchans", dump); dumpExt(user, checkstr, targuser); } else if (targchan) { /* /check on a channel */ user->SendText(checkstr + " timestamp " + timestring(targchan->age)); if (targchan->topic[0] != 0) { /* there is a topic, assume topic related information exists */ user->SendText(checkstr + " topic " + targchan->topic); user->SendText(checkstr + " topic_setby " + targchan->setby); user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset)); } user->SendText(checkstr + " modes " + targchan->ChanModes(true)); user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter())); /* now the ugly bit, spool current members of a channel. :| */ const UserMembList *ulist= targchan->GetUsers(); /* note that unlike /names, we do NOT check +i vs in the channel */ for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++) { /* * Unlike Asuka, I define a clone as coming from the same host. --w00t */ user->SendText("%s member %-3lu %s%s (%s@%s) %s ", checkstr.c_str(), ServerInstance->Users->GlobalCloneCount(i->first), targchan->GetAllPrefixChars(i->first), i->first->nick.c_str(), i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str()); } const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes(); for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i) dumpListMode(user, checkstr, (*i)->GetList(targchan)); dumpExt(user, checkstr, targchan); } else { /* /check on an IP address, or something that doesn't exist */ long x = 0; /* hostname or other */ for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++) { if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map)) { /* host or vhost matches mask */ user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname); } /* IP address */ else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0])) { /* same IP. */ user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname); } } user->SendText(checkstr + " matches " + ConvToStr(x)); } user->SendText(checkstr + " END " + parameters[0]); return CMD_SUCCESS; }
static bool WriteDatabase(PermChannel& permchanmode, Module* mod, bool save_listmodes) { ChanModeReference ban(mod, "ban"); /* * We need to perform an atomic write so as not to f**k things up. * So, let's write to a temporary file, flush it, then rename the file.. * -- w00t */ // If the user has not specified a configuration file then we don't write one. if (permchannelsconf.empty()) return true; std::string permchannelsnewconf = permchannelsconf + ".tmp"; std::ofstream stream(permchannelsnewconf.c_str()); if (!stream.is_open()) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno); return false; } stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl << "<config format=\"xml\">" << std::endl; for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++) { Channel* chan = i->second; if (!chan->IsModeSet(permchanmode)) continue; std::string chanmodes = chan->ChanModes(true); if (save_listmodes) { std::string modes; std::string params; const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes(); for (ModeParser::ListModeList::const_iterator j = listmodes.begin(); j != listmodes.end(); ++j) { ListModeBase* lm = *j; ListModeBase::ModeList* list = lm->GetList(chan); if (!list || list->empty()) continue; size_t n = 0; // Append the parameters for (ListModeBase::ModeList::const_iterator k = list->begin(); k != list->end(); ++k, n++) { params += k->mask; params += ' '; } // Append the mode letters (for example "IIII", "gg") modes.append(n, lm->GetModeChar()); } if (!params.empty()) { // Remove the last space params.erase(params.end()-1); // If there is at least a space in chanmodes (that is, a non-listmode has a parameter) // insert the listmode mode letters before the space. Otherwise just append them. std::string::size_type p = chanmodes.find(' '); if (p == std::string::npos) chanmodes += modes; else chanmodes.insert(p, modes); // Append the listmode parameters (the masks themselves) chanmodes += ' '; chanmodes += params; } } stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name) << "\" ts=\"" << chan->age << "\" topic=\"" << ServerConfig::Escape(chan->topic) << "\" topicts=\"" << chan->topicset << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby) << "\" modes=\"" << ServerConfig::Escape(chanmodes) << "\">" << std::endl; } if (stream.fail()) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno); return false; } stream.close(); #ifdef _WIN32 if (remove(permchannelsconf.c_str())) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno); return false; } #endif // Use rename to move temporary to new db - this is guarenteed not to f**k up, even in case of a crash. if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno); return false; } return true; }
static bool WriteDatabase() { /* * We need to perform an atomic write so as not to f**k things up. * So, let's write to a temporary file, flush it, then rename the file.. * -- w00t */ // If the user has not specified a configuration file then we don't write one. if (permchannelsconf.empty()) return true; std::string permchannelsnewconf = permchannelsconf + ".tmp"; std::ofstream stream(permchannelsnewconf.c_str()); if (!stream.is_open()) { ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot create database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno); return false; } stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl << "<config format=\"xml\">" << std::endl; for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++) { Channel* chan = i->second; if (!chan->IsModeSet('P')) continue; stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name) << "\" topic=\"" << ServerConfig::Escape(chan->topic) << "\" modes=\"" << ServerConfig::Escape(chan->ChanModes(true)) << "\">" << std::endl; } if (stream.fail()) { ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno); return false; } stream.close(); #ifdef _WIN32 if (remove(permchannelsconf.c_str())) { ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot remove old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno); return false; } #endif // Use rename to move temporary to new db - this is guarenteed not to f**k up, even in case of a crash. if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0) { ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno); return false; } return true; }
static bool WriteDatabase(PermChannel& permchanmode, Module* mod, bool save_listmodes) { ChanModeReference ban(mod, "ban"); /* * We need to perform an atomic write so as not to f**k things up. * So, let's write to a temporary file, flush it, then rename the file.. * -- w00t */ // If the user has not specified a configuration file then we don't write one. if (permchannelsconf.empty()) return true; std::string permchannelsnewconf = permchannelsconf + ".tmp"; std::ofstream stream(permchannelsnewconf.c_str()); if (!stream.is_open()) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno); return false; } stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl << "<config format=\"xml\">" << std::endl; for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++) { Channel* chan = i->second; if (!chan->IsModeSet(permchanmode)) continue; std::string chanmodes = chan->ChanModes(true); if (save_listmodes) { ListModeData lm; // Bans are managed by the core, so we have to process them separately static_cast<ListModeBase*>(*ban)->DoSyncChannel(chan, mod, &lm); // All other listmodes are managed by modules, so we need to ask them (call their // OnSyncChannel() handler) to give our ProtoSendMode() a list of modes that are // set on the channel. The ListModeData struct is passed as an opaque pointer // that will be passed back to us by the module handling the mode. FOREACH_MOD(OnSyncChannel, (chan, mod, &lm)); if (!lm.modes.empty()) { // Remove the last space lm.params.erase(lm.params.end()-1); // If there is at least a space in chanmodes (that is, a non-listmode has a parameter) // insert the listmode mode letters before the space. Otherwise just append them. std::string::size_type p = chanmodes.find(' '); if (p == std::string::npos) chanmodes += lm.modes; else chanmodes.insert(p, lm.modes); // Append the listmode parameters (the masks themselves) chanmodes += ' '; chanmodes += lm.params; } } stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name) << "\" ts=\"" << chan->age << "\" topic=\"" << ServerConfig::Escape(chan->topic) << "\" topicts=\"" << chan->topicset << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby) << "\" modes=\"" << ServerConfig::Escape(chanmodes) << "\">" << std::endl; } if (stream.fail()) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno); return false; } stream.close(); #ifdef _WIN32 if (remove(permchannelsconf.c_str())) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno); return false; } #endif // Use rename to move temporary to new db - this is guarenteed not to f**k up, even in case of a crash. if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0) { ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno); return false; } return true; }
static bool WriteDatabase() { FILE *f; if (permchannelsconf.empty()) { // Fake success. return true; } std::string tempname = permchannelsconf + ".tmp"; /* * We need to perform an atomic write so as not to f**k things up. * So, let's write to a temporary file, flush and sync the FD, then rename the file.. * -- w00t */ f = fopen(tempname.c_str(), "w"); if (!f) { ServerInstance->Logs->Log("m_permchannels",LOG_DEFAULT, "permchannels: Cannot create database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno); return false; } fputs("# Permchannels DB\n# This file is autogenerated; any changes will be overwritten!\n<config format=\"compat\">\n", f); // Now, let's write. for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++) { Channel* chan = i->second; if (!chan->IsModeSet('P')) continue; char line[1024]; const char* items[] = { "<permchannels channel=", chan->name.c_str(), " topic=", chan->topic.c_str(), " modes=", chan->ChanModes(true), ">\n" }; int lpos = 0, item = 0, ipos = 0; while (lpos < 1022 && item < 7) { char c = items[item][ipos++]; if (c == 0) { // end of this string; hop to next string, insert a quote item++; ipos = 0; c = '"'; } else if (c == '\\' || c == '"') { line[lpos++] = '\\'; } line[lpos++] = c; } line[--lpos] = 0; fputs(line, f); } int write_error = 0; write_error = ferror(f); write_error |= fclose(f); if (write_error) { ServerInstance->Logs->Log("m_permchannels",LOG_DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno); return false; } #ifdef _WIN32 if (remove(permchannelsconf.c_str())) { ServerInstance->Logs->Log("m_permchannels",LOG_DEFAULT, "permchannels: Cannot remove old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno); return false; } #endif // Use rename to move temporary to new db - this is guarenteed not to f**k up, even in case of a crash. if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0) { ServerInstance->Logs->Log("m_permchannels",LOG_DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno); ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno); return false; } return true; }