Skip to content

Commit

Permalink
Per-network bind hosts.
Browse files Browse the repository at this point in the history
Fix #147
  • Loading branch information
DarthGandalf committed Jul 18, 2012
1 parent 7951a50 commit cebc093
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 9 deletions.
3 changes: 3 additions & 0 deletions include/znc/IRCNetwork.h
Expand Up @@ -131,11 +131,13 @@ class CIRCNetwork {
const CString& GetAltNick(const bool bAllowDefault = true) const;
const CString& GetIdent(const bool bAllowDefault = true) const;
const CString& GetRealName() const;
const CString& GetBindHost() const;

void SetNick(const CString& s);
void SetAltNick(const CString& s);
void SetIdent(const CString& s);
void SetRealName(const CString& s);
void SetBindHost(const CString& s);

double GetFloodRate() const { return m_fFloodRate; }
unsigned short int GetFloodBurst() const { return m_uFloodBurst; }
Expand All @@ -155,6 +157,7 @@ class CIRCNetwork {
CString m_sAltNick;
CString m_sIdent;
CString m_sRealName;
CString m_sBindHost;

CModules* m_pModules;

Expand Down
17 changes: 16 additions & 1 deletion modules/data/webadmin/tmpl/add_edit_network.tmpl
Expand Up @@ -15,7 +15,7 @@
<? IF Edit ?><input type="hidden" name="network" value="<? VAR Name ?>" /><? ENDIF ?>

<h3>Network Info</h3>
<span class="info">Nick, AltNick, Ident, RealName can be left empty to use the value from the user.</span>
<span class="info">Nick, AltNick, Ident, RealName, BindHost can be left empty to use the value from the user.</span>
<div class="sectionbg">
<div class="sectionbody">
<? IF !Edit ?>
Expand Down Expand Up @@ -48,6 +48,21 @@
title="Your real name." />
</div>

<? IF BindHostEdit ?>
<div class="subsection">
<div class="inputlabel">BindHost:</div>
<? IF BindHostLoop ?>
<select name="bindhost">
<option value="">- Default -</option>
<? LOOP BindHostLoop ?><option value="<? VAR BindHost ?>"<? IF Checked ?> selected="selected"<? ENDIF ?>><? VAR BindHost ?></option><? ENDLOOP ?>
</select>
<? ELSE ?>
<input type="text" name="bindhost" value="<? VAR BindHost ?>"/>
<? ENDIF ?>
</div>
<div style="clear: both;"></div>
<? ENDIF ?>

<div class="subsection">
<div class="inputlabel">Active:</div>
<div class="checkbox"><input type="checkbox" name="doconnect" id="doconnect_checkbox"<? IF IRCConnectEnabled ?> checked="checked"<? ENDIF ?> />
Expand Down
38 changes: 38 additions & 0 deletions modules/webadmin.cpp
Expand Up @@ -684,6 +684,38 @@ class CWebAdminMod : public CModule {
}
}

// To change BindHosts be admin or don't have DenySetBindHost
if (spSession->IsAdmin() || !spSession->GetUser()->DenySetBindHost()) {
Tmpl["BindHostEdit"] = "true";
const VCString& vsBindHosts = CZNC::Get().GetBindHosts();
if (vsBindHosts.empty()) {
if (pNetwork) {
Tmpl["BindHost"] = pNetwork->GetBindHost();
}
} else {
bool bFoundBindHost = false;
for (unsigned int b = 0; b < vsBindHosts.size(); b++) {
const CString& sBindHost = vsBindHosts[b];
CTemplate& l = Tmpl.AddRow("BindHostLoop");

l["BindHost"] = sBindHost;

if (pNetwork && pNetwork->GetBindHost() == sBindHost) {
l["Checked"] = "true";
bFoundBindHost = true;
}
}

// If our current bindhost is not in the global list...
if (pNetwork && !bFoundBindHost && !pNetwork->GetBindHost().empty()) {
CTemplate& l = Tmpl.AddRow("BindHostLoop");

l["BindHost"] = pNetwork->GetBindHost();
l["Checked"] = "true";
}
}
}

if (pNetwork) {
Tmpl["Action"] = "editnetwork";
Tmpl["Edit"] = "true";
Expand Down Expand Up @@ -767,6 +799,12 @@ class CWebAdminMod : public CModule {

pNetwork->SetIRCConnectEnabled(WebSock.GetParam("doconnect").ToBool());

sArg = WebSock.GetParam("bindhost");
// To change BindHosts be admin or don't have DenySetBindHost
if (spSession->IsAdmin() || !spSession->GetUser()->DenySetBindHost()) {
pNetwork->SetBindHost(WebSock.GetParam("bindhost"));
}

if (WebSock.GetParam("floodprotection").ToBool()) {
pNetwork->SetFloodRate(WebSock.GetParam("floodrate").ToDouble());
pNetwork->SetFloodBurst(WebSock.GetParam("floodburst").ToUInt());
Expand Down
4 changes: 2 additions & 2 deletions src/Client.cpp
Expand Up @@ -783,12 +783,12 @@ CString CClient::GetNickMask() const {
return GetIRCSock()->GetNickMask();
}

CString sHost = m_pUser->GetBindHost();
CString sHost = m_pNetwork ? m_pNetwork->GetBindHost() : m_pUser->GetBindHost();
if (sHost.empty()) {
sHost = "irc.znc.in";
}

return GetNick() + "!" + m_pUser->GetIdent() + "@" + sHost;
return GetNick() + "!" + (m_pNetwork ? m_pNetwork->GetBindHost() : m_pUser->GetIdent()) + "@" + sHost;
}

void CClient::RespondCap(const CString& sResponse)
Expand Down
55 changes: 54 additions & 1 deletion src/ClientCommand.cpp
Expand Up @@ -1004,13 +1004,50 @@ void CClient::UserCommand(CString& sLine) {
}
PutStatus(Table);
} else if ((sCommand.Equals("SETBINDHOST") || sCommand.Equals("SETVHOST")) && (m_pUser->IsAdmin() || !m_pUser->DenySetBindHost())) {
if (!m_pNetwork) {
PutStatus("You must be connected with a network to use this command. Try SetUserBindHost instead");
return;
}
CString sHost = sLine.Token(1);

if (sHost.empty()) {
PutStatus("Usage: SetBindHost <host>");
return;
}

if (sHost.Equals(m_pNetwork->GetBindHost())) {
PutStatus("You already have this bind host!");
return;
}

const VCString& vsHosts = CZNC::Get().GetBindHosts();
if (!m_pUser->IsAdmin() && !vsHosts.empty()) {
VCString::const_iterator it;
bool bFound = false;

for (it = vsHosts.begin(); it != vsHosts.end(); ++it) {
if (sHost.Equals(*it)) {
bFound = true;
break;
}
}

if (!bFound) {
PutStatus("You may not use this bind host. See [ListBindHosts] for a list");
return;
}
}

m_pNetwork->SetBindHost(sHost);
PutStatus("Set bind host for network [" + m_pNetwork->GetName() + "] to [" + m_pNetwork->GetBindHost() + "]");
} else if (sCommand.Equals("SETUSERBINDHOST") && (m_pUser->IsAdmin() || !m_pUser->DenySetBindHost())) {
CString sHost = sLine.Token(1);

if (sHost.empty()) {
PutStatus("Usage: SetUserBindHost <host>");
return;
}

if (sHost.Equals(m_pUser->GetBindHost())) {
PutStatus("You already have this bind host!");
return;
Expand All @@ -1037,8 +1074,15 @@ void CClient::UserCommand(CString& sLine) {
m_pUser->SetBindHost(sHost);
PutStatus("Set bind host to [" + m_pUser->GetBindHost() + "]");
} else if ((sCommand.Equals("CLEARBINDHOST") || sCommand.Equals("CLEARVHOST")) && (m_pUser->IsAdmin() || !m_pUser->DenySetBindHost())) {
if (!m_pNetwork) {
PutStatus("You must be connected with a network to use this command. Try ClearUserBindHost instead");
return;
}
m_pNetwork->SetBindHost("");
PutStatus("Bind host cleared");
} else if (sCommand.Equals("CLEARUSERBINDHOST") && (m_pUser->IsAdmin() || !m_pUser->DenySetBindHost())) {
m_pUser->SetBindHost("");
PutStatus("Bind Host Cleared");
PutStatus("Bind host cleared");
} else if (sCommand.Equals("PLAYBUFFER")) {
if (!m_pNetwork) {
PutStatus("You must be connected with a network to use this command");
Expand Down Expand Up @@ -1419,9 +1463,18 @@ void CClient::HelpUser() {
Table.SetCell("Arguments", "<host (IP preferred)>");
Table.SetCell("Description", "Set the bind host for this connection");

Table.AddRow();
Table.SetCell("Command", "SetUserBindHost");
Table.SetCell("Arguments", "<host (IP preferred)>");
Table.SetCell("Description", "Set the default bind host for this user");

Table.AddRow();
Table.SetCell("Command", "ClearBindHost");
Table.SetCell("Description", "Clear the bind host for this connection");

Table.AddRow();
Table.SetCell("Command", "ClearUserBindHost");
Table.SetCell("Description", "Clear the default bind host for this user");
}

Table.AddRow();
Expand Down
26 changes: 24 additions & 2 deletions src/IRCNetwork.cpp
Expand Up @@ -88,6 +88,7 @@ void CIRCNetwork::Clone(const CIRCNetwork& Network) {
SetAltNick(Network.GetAltNick());
SetIdent(Network.GetIdent());
SetRealName(Network.GetRealName());
SetBindHost(Network.GetBindHost());

// Servers
const vector<CServer*>& vServers = Network.GetServers();
Expand Down Expand Up @@ -246,7 +247,8 @@ bool CIRCNetwork::ParseConfig(CConfig *pConfig, CString& sError, bool bUpgrade)
{ "nick", &CIRCNetwork::SetNick },
{ "altnick", &CIRCNetwork::SetAltNick },
{ "ident", &CIRCNetwork::SetIdent },
{ "realname", &CIRCNetwork::SetRealName }
{ "realname", &CIRCNetwork::SetRealName },
{ "bindhost", &CIRCNetwork::SetBindHost },
};
size_t numStringOptions = sizeof(StringOptions) / sizeof(StringOptions[0]);
TOption<bool> BoolOptions[] = {
Expand Down Expand Up @@ -380,6 +382,9 @@ CConfig CIRCNetwork::ToConfig() {
if (!m_sRealName.empty()) {
config.AddKeyValuePair("RealName", m_sRealName);
}
if (!m_sBindHost.empty()) {
config.AddKeyValuePair("BindHost", m_sBindHost);
}

config.AddKeyValuePair("IRCConnectEnabled", CString(GetIRCConnectEnabled()));
config.AddKeyValuePair("FloodRate", CString(GetFloodRate()));
Expand Down Expand Up @@ -947,7 +952,7 @@ bool CIRCNetwork::Connect() {
);

CString sSockName = "IRC::" + m_pUser->GetUserName() + "::" + m_sName;
CZNC::Get().GetManager().Connect(pServer->GetName(), pServer->GetPort(), sSockName, 120, bSSL, m_pUser->GetBindHost(), pIRCSock);
CZNC::Get().GetManager().Connect(pServer->GetName(), pServer->GetPort(), sSockName, 120, bSSL, GetBindHost(), pIRCSock);

return true;
}
Expand Down Expand Up @@ -1034,6 +1039,14 @@ const CString& CIRCNetwork::GetRealName() const {
return m_sRealName;
}

const CString& CIRCNetwork::GetBindHost() const {
if (m_sBindHost.empty()) {
return m_pUser->GetBindHost();
}

return m_sBindHost;
}

void CIRCNetwork::SetNick(const CString& s) {
if (m_pUser->GetNick().Equals(s)) {
m_sNick = "";
Expand Down Expand Up @@ -1066,6 +1079,14 @@ void CIRCNetwork::SetRealName(const CString& s) {
}
}

void CIRCNetwork::SetBindHost(const CString& s) {
if (m_pUser->GetBindHost().Equals(s)) {
m_sBindHost = "";
} else {
m_sBindHost = s;
}
}

CString CIRCNetwork::ExpandString(const CString& sStr) const {
CString sRet;
return ExpandString(sStr, sRet);
Expand All @@ -1078,6 +1099,7 @@ CString& CIRCNetwork::ExpandString(const CString& sStr, CString& sRet) const {
sRet.Replace("%altnick%", GetAltNick());
sRet.Replace("%ident%", GetIdent());
sRet.Replace("%realname%", GetRealName());
sRet.Replace("%bindhost%", GetBindHost());

return m_pUser->ExpandString(sRet, sRet);
}
2 changes: 1 addition & 1 deletion src/IRCSock.cpp
Expand Up @@ -54,7 +54,7 @@ CIRCSock::CIRCSock(CIRCNetwork* pNetwork) : CZNCSock() {
m_iSendsAllowed = m_uFloodBurst;
EnableReadLine();
m_Nick.SetIdent(m_pNetwork->GetIdent());
m_Nick.SetHost(m_pNetwork->GetUser()->GetBindHost());
m_Nick.SetHost(m_pNetwork->GetBindHost());

m_uMaxNickLen = 9;
m_uCapPaused = 0;
Expand Down
10 changes: 8 additions & 2 deletions src/Socket.cpp
Expand Up @@ -9,6 +9,7 @@
#include <znc/Socket.h>
#include <znc/Modules.h>
#include <znc/User.h>
#include <znc/IRCNetwork.h>
#include <znc/znc.h>
#include <signal.h>

Expand Down Expand Up @@ -248,7 +249,7 @@ void CSockManager::SetTDNSThreadFinished(TDNSTask* task, bool bBind, addrinfo* a
aiTarget = aiTarget4;
#endif
} else if (!aiBind4 && !aiBind6) {
throw "Can't resolve bind hostname. Try /znc clearbindhost";
throw "Can't resolve bind hostname. Try /znc clearbindhost and /znc clearuserbindhost";
} else if (aiBind6 && aiTarget6) {
aiTarget = aiTarget6;
aiBind = aiBind6;
Expand Down Expand Up @@ -457,7 +458,12 @@ bool CSocket::Connect(const CString& sHostname, unsigned short uPort, bool bSSL,

if (pUser) {
sSockName += "::" + pUser->GetUserName();
sBindHost = m_pModule->GetUser()->GetBindHost();
sBindHost = pUser->GetBindHost();
CIRCNetwork* pNetwork = m_pModule->GetNetwork();
if (pNetwork) {
sSockName += "::" + pNetwork->GetName();
sBindHost = pNetwork->GetBindHost();
}
}

// Don't overwrite the socket name if one is already set
Expand Down

0 comments on commit cebc093

Please sign in to comment.