Skip to content

Commit

Permalink
Minor cosmetic changes
Browse files Browse the repository at this point in the history
Refactored difficult-to-grok code involved with displaying alias
Fixed crash on alias error when a user isn't associated with a network
  • Loading branch information
Wuggingston Wugsalot committed Jan 31, 2014
1 parent dbdfa1e commit d2cd657
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions modules/alias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CAlias
CString name;
VCString alias_cmds;

public:
public:
// getters/setters
const CString &GetName() const
{
Expand Down Expand Up @@ -105,9 +105,9 @@ class CAlias
bool subsequent = false;
size_t index = caret + 1;
int token = -1;

skip = 1;

if (alias_data.length() > index && alias_data[index] == '?') { optional = true; ++index; } // try to read optional flag
if (alias_data.length() > index && CString(alias_data.substr(index)).Convert(&token)) // try to read integer
{
Expand All @@ -116,13 +116,13 @@ class CAlias
else return; // token was malformed. leave caret unchanged, and flag first character for skipping
if (alias_data.length() > index && alias_data[index] == '+') { subsequent = true; ++index; } // try to read subsequent flag
if (alias_data.length() > index && alias_data[index] == '%') { ++index; } // try to read end-of-substitution marker
else return;
else return;

CString stok = line.Token(token, subsequent, " "); // if we get here, we're definitely dealing with a token, so get the token's value
if (stok.empty() && !optional)
throw std::invalid_argument(CString("missing required parameter: ") + CString(token)); // blow up if token is required and also empty
output.append(stok); // write token value to output

skip = 0; // since we're moving the cursor after the end of the token, skip no characters
caret = index; // advance the cursor forward by the size of the token
}
Expand Down Expand Up @@ -203,7 +203,7 @@ class CAliasMod : public CModule {
CAlias insert_alias;
int index;
if (CAlias::AliasGet(insert_alias, this, name))
{
{
// if Convert succeeds, then i has been successfully read from user input
if (!sLine.Token(2, false, " ").Convert(&index) || index < 0 || index > (int) insert_alias.AliasCmds().size())
{
Expand All @@ -224,7 +224,7 @@ class CAliasMod : public CModule {
CAlias remove_alias;
int index;
if (CAlias::AliasGet(remove_alias, this, name))
{
{
if (!sLine.Token(2, false, " ").Convert(&index) || index < 0 || index > (int) remove_alias.AliasCmds().size() - 1)
{
PutModule("Invalid index.");
Expand Down Expand Up @@ -274,7 +274,8 @@ class CAliasMod : public CModule {
for (size_t i = 0; i < info_alias.AliasCmds().size(); ++i)
{
CString num(i);
PutModule(CString(i) + (" " + ((num.length() > 3) ? 3 : num.length())) + info_alias.AliasCmds()[i]);
CString padding(4 - (num.length() > 3 ? 3 : num.length()), ' ');
PutModule(num + padding + info_alias.AliasCmds()[i]);
}
PutModule("End of actions for alias " + info_alias.GetName() + ".");
}
Expand Down Expand Up @@ -316,7 +317,9 @@ class CAliasMod : public CModule {
}
catch (std::exception &e)
{
PutUser(CString(":znc.in 461 " + GetNetwork()->GetCurNick() + " " + current_alias.GetName() + " :ZNC alias error: ") + e.what());
CString my_nick = (GetNetwork() == NULL ? "" : GetNetwork()->GetCurNick());
if (my_nick.empty()) my_nick = "*";
PutUser(CString(":znc.in 461 " + my_nick + " " + current_alias.GetName() + " :ZNC alias error: ") + e.what());
return HALTCORE;
}

Expand Down

0 comments on commit d2cd657

Please sign in to comment.