Skip to content
Browse files

Merge pull request #567 from orchid-hybrid/master

autoop: several hostmasks per user
  • Loading branch information...
2 parents 638b3b5 + b129e20 commit a5cd284ad451f3edc749bb50470e999f86e67c5d @DarthGandalf DarthGandalf committed Jun 8, 2014
Showing with 83 additions and 37 deletions.
  1. +83 −37 modules/autoop.cpp
View
120 modules/autoop.cpp
@@ -50,18 +50,17 @@ class CAutoOpUser {
FromString(sLine);
}
- CAutoOpUser(const CString& sUsername, const CString& sUserKey, const CString& sHostmask, const CString& sChannels) :
+ CAutoOpUser(const CString& sUsername, const CString& sUserKey, const CString& sHostmasks, const CString& sChannels) :
m_sUsername(sUsername),
- m_sUserKey(sUserKey),
- m_sHostmask(sHostmask) {
+ m_sUserKey(sUserKey) {
+ AddHostmasks(sHostmasks);
AddChans(sChannels);
}
virtual ~CAutoOpUser() {}
const CString& GetUsername() const { return m_sUsername; }
const CString& GetUserKey() const { return m_sUserKey; }
- const CString& GetHostmask() const { return m_sHostmask; }
bool ChannelMatches(const CString& sChan) const {
for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
@@ -74,21 +73,40 @@ class CAutoOpUser {
}
bool HostMatches(const CString& sHostmask) {
- return sHostmask.WildCmp(m_sHostmask);
+ for (set<CString>::const_iterator it = m_ssHostmasks.begin(); it != m_ssHostmasks.end(); ++it) {
+ if (sHostmask.WildCmp(*it)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ CString GetHostmasks() const {
+ return CString(",").Join(m_ssHostmasks.begin(), m_ssHostmasks.end());
}
CString GetChannels() const {
- CString sRet;
+ return CString(" ").Join(m_ssChans.begin(), m_ssChans.end());
+ }
- for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
- if (!sRet.empty()) {
- sRet += " ";
- }
+ bool DelHostmasks(const CString& sHostmasks) {
+ VCString vsHostmasks;
+ sHostmasks.Split(",", vsHostmasks);
- sRet += *it;
+ for (unsigned int a = 0; a < vsHostmasks.size(); a++) {
+ m_ssHostmasks.erase(vsHostmasks[a]);
}
- return sRet;
+ return m_ssHostmasks.empty();
+ }
+
+ void AddHostmasks(const CString& sHostmasks) {
+ VCString vsHostmasks;
+ sHostmasks.Split(",", vsHostmasks);
+
+ for (unsigned int a = 0; a < vsHostmasks.size(); a++) {
+ m_ssHostmasks.insert(vsHostmasks[a]);
+ }
}
void DelChans(const CString& sChans) {
@@ -110,22 +128,12 @@ class CAutoOpUser {
}
CString ToString() const {
- CString sChans;
-
- for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
- if (!sChans.empty()) {
- sChans += " ";
- }
-
- sChans += *it;
- }
-
- return m_sUsername + "\t" + m_sHostmask + "\t" + m_sUserKey + "\t" + sChans;
+ return m_sUsername + "\t" + GetHostmasks() + "\t" + m_sUserKey + "\t" + GetChannels();
}
bool FromString(const CString& sLine) {
m_sUsername = sLine.Token(0, false, "\t");
- m_sHostmask = sLine.Token(1, false, "\t");
+ sLine.Token(1, false, "\t").Split(",", m_ssHostmasks);
m_sUserKey = sLine.Token(2, false, "\t");
sLine.Token(3, false, "\t").Split(" ", m_ssChans);
@@ -135,7 +143,7 @@ class CAutoOpUser {
protected:
CString m_sUsername;
CString m_sUserKey;
- CString m_sHostmask;
+ set<CString> m_ssHostmasks;
set<CString> m_ssChans;
};
@@ -165,7 +173,6 @@ class CAutoOpMod : public CModule {
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
delete it->second;
}
-
m_msUsers.clear();
}
@@ -226,7 +233,7 @@ class CAutoOpMod : public CModule {
CString sCommand = sLine.Token(0).AsUpper();
if (sCommand.Equals("HELP")) {
- PutModule("Commands are: ListUsers, AddChans, DelChans, AddUser, DelUser");
+ PutModule("Commands are: ListUsers, AddChans, DelChans, AddMasks, DelMasks, AddUser, DelUser");
} else if (sCommand.Equals("TIMERS")) {
ListTimers();
} else if (sCommand.Equals("ADDUSER") || sCommand.Equals("DELUSER")) {
@@ -236,7 +243,7 @@ class CAutoOpMod : public CModule {
if (sCommand.Equals("ADDUSER")) {
if (sHost.empty()) {
- PutModule("Usage: " + sCommand + " <user> <hostmask> <key> [channels]");
+ PutModule("Usage: " + sCommand + " <user> <hostmask>[,<hostmasks>...] <key> [channels]");
} else {
CAutoOpUser* pUser = AddUser(sUser, sKey, sHost, sLine.Token(4, true));
@@ -257,16 +264,26 @@ class CAutoOpMod : public CModule {
CTable Table;
Table.AddColumn("User");
- Table.AddColumn("Hostmask");
+ Table.AddColumn("Hostmasks");
Table.AddColumn("Key");
Table.AddColumn("Channels");
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
- Table.AddRow();
- Table.SetCell("User", it->second->GetUsername());
- Table.SetCell("Hostmask", it->second->GetHostmask());
- Table.SetCell("Key", it->second->GetUserKey());
- Table.SetCell("Channels", it->second->GetChannels());
+ VCString vsHostmasks;
+ it->second->GetHostmasks().Split(",", vsHostmasks);
+ for (unsigned int a = 0; a < vsHostmasks.size(); a++) {
+ Table.AddRow();
+ if (a == 0) {
+ Table.SetCell("User", it->second->GetUsername());
+ Table.SetCell("Key", it->second->GetUserKey());
+ Table.SetCell("Channels", it->second->GetChannels());
+ } else if (a == vsHostmasks.size()-1) {
+ Table.SetCell("User", "`-");
+ } else {
+ Table.SetCell("User", "|-");
+ }
+ Table.SetCell("Hostmasks", vsHostmasks[a]);
+ }
}
PutModule(Table);
@@ -295,6 +312,35 @@ class CAutoOpMod : public CModule {
}
SetNV(pUser->GetUsername(), pUser->ToString());
+ } else if (sCommand.Equals("ADDMASKS") || sCommand.Equals("DELMASKS")) {
+ CString sUser = sLine.Token(1);
+ CString sHostmasks = sLine.Token(2, true);
+
+ if (sHostmasks.empty()) {
+ PutModule("Usage: " + sCommand + " <user> <mask>,[mask] ...");
+ return;
+ }
+
+ CAutoOpUser* pUser = FindUser(sUser);
+
+ if (!pUser) {
+ PutModule("No such user");
+ return;
+ }
+
+ if (sCommand.Equals("ADDMASKS")) {
+ pUser->AddHostmasks(sHostmasks);
+ PutModule("Hostmasks(s) added to user [" + pUser->GetUsername() + "]");
+ } else {
+ if (pUser->DelHostmasks(sHostmasks)) {
+ PutModule("Removed user [" + pUser->GetUsername() + "] with key [" + pUser->GetUserKey() + "] and channels [" + pUser->GetChannels() + "]");
+ DelUser(sUser);
+ DelNV(sUser);
+ } else {
+ PutModule("Hostmasks(s) Removed from user [" + pUser->GetUsername() + "]");
+ SetNV(pUser->GetUsername(), pUser->ToString());
+ }
+ }
} else {
PutModule("Unknown command, try HELP");
}
@@ -351,15 +397,15 @@ class CAutoOpMod : public CModule {
PutModule("User [" + sUser + "] removed");
}
- CAutoOpUser* AddUser(const CString& sUser, const CString& sKey, const CString& sHost, const CString& sChans) {
+ CAutoOpUser* AddUser(const CString& sUser, const CString& sKey, const CString& sHosts, const CString& sChans) {
if (m_msUsers.find(sUser) != m_msUsers.end()) {
PutModule("That user already exists");
return NULL;
}
- CAutoOpUser* pUser = new CAutoOpUser(sUser, sKey, sHost, sChans);
+ CAutoOpUser* pUser = new CAutoOpUser(sUser, sKey, sHosts, sChans);
m_msUsers[sUser.AsLower()] = pUser;
- PutModule("User [" + sUser + "] added with hostmask [" + sHost + "]");
+ PutModule("User [" + sUser + "] added with hostmask(s) [" + sHosts + "]");
return pUser;
}

0 comments on commit a5cd284

Please sign in to comment.
Something went wrong with that request. Please try again.