Switch to using an MCString to track config options

This commit is contained in:
John Reese 2011-01-13 19:50:29 -05:00
parent 5c12abe602
commit 304bef93f0
1 changed files with 41 additions and 33 deletions

View File

@ -39,13 +39,12 @@ class CNotifoMod : public CModule
// User agent to use // User agent to use
CString user_agent; CString user_agent;
// Recipient account's username and API secret
CString notifo_username;
CString notifo_secret;
// BASIC auth string, needs to be encoded each time username/secret is changed // BASIC auth string, needs to be encoded each time username/secret is changed
CString notifo_auth; CString notifo_auth;
// Configuration options
MCString options;
public: public:
MODCONSTRUCTOR(CNotifoMod) { MODCONSTRUCTOR(CNotifoMod) {
@ -60,11 +59,12 @@ class CNotifoMod : public CModule
notifo_url = "/v1/send_notification"; notifo_url = "/v1/send_notification";
#endif #endif
notifo_auth = "";
user_agent = "ZNC To Notifo"; user_agent = "ZNC To Notifo";
notifo_username = ""; // Notifo user account and secret
notifo_secret = ""; options["username"] = "";
notifo_auth = ""; options["secret"] = "";
} }
virtual ~CNotifoMod() {} virtual ~CNotifoMod() {}
@ -87,7 +87,7 @@ class CNotifoMod : public CModule
void authencode() void authencode()
{ {
// BASIC auth, base64-encoded username:password // BASIC auth, base64-encoded username:password
CString auth = notifo_username + CString(":") + notifo_secret; CString auth = options["username"] + CString(":") + options["secret"];
notifo_auth = auth.Base64Encode_n(); notifo_auth = auth.Base64Encode_n();
} }
@ -102,7 +102,7 @@ class CNotifoMod : public CModule
void send_message(const CString& message, const CString& title="New Message") void send_message(const CString& message, const CString& title="New Message")
{ {
// POST body parameters for the request // POST body parameters for the request
CString post = "to=" + urlencode(notifo_username); CString post = "to=" + urlencode(options["username"]);
post += "&msg=" + urlencode(message); post += "&msg=" + urlencode(message);
post += "&label=" + urlencode(app); post += "&label=" + urlencode(app);
post += "&title=" + urlencode(title); post += "&title=" + urlencode(title);
@ -133,6 +133,17 @@ class CNotifoMod : public CModule
#endif #endif
} }
/**
* Determine when to notify the user of a channel message.
*
* @param channel Channel the message was sent to
* @return Notification should be sent
*/
bool notify_channel(const CChan& channel)
{
return true;
}
/** /**
* Determine when to notify the user of a private message. * Determine when to notify the user of a private message.
* *
@ -146,10 +157,18 @@ class CNotifoMod : public CModule
protected: protected:
/**
* Handle the plugin being loaded. Retrieve plugin config values.
*
* @param args Plugin arguments
* @param message Message to show the user after loading
*/
bool OnLoad(const CString& args, CString& message) bool OnLoad(const CString& args, CString& message)
{ {
notifo_username = GetNV("notifo_username"); for (MCString::iterator i = options.begin(); i != options.end(); i++)
notifo_secret = GetNV("notifo_secret"); {
options[i->first] = GetNV(i->first);
}
authencode(); authencode();
@ -219,26 +238,19 @@ class CNotifoMod : public CModule
CString option = tokens[1].AsLower(); CString option = tokens[1].AsLower();
CString value = tokens[2]; CString value = tokens[2];
MCString::iterator pos = options.find(option);
if (option == "username") if (pos == options.end())
{ {
SetNV("notifo_username", value); PutModule("Error: invalid option name");
notifo_username = value;
authencode();
}
else if (option == "secret")
{
SetNV("notifo_secret", value);
notifo_secret = value;
authencode();
} }
else else
{ {
PutModule("Error: invalid option name"); options[option] = value;
return; SetNV(option, value);
}
PutModule("Done"); authencode();
}
} }
// GET command // GET command
else if (action == "get") else if (action == "get")
@ -250,19 +262,15 @@ class CNotifoMod : public CModule
} }
CString option = tokens[1].AsLower(); CString option = tokens[1].AsLower();
MCString::iterator pos = options.find(option);
if (option == "username") if (pos == options.end())
{ {
PutModule(GetNV("notifo_username")); PutModule("Error: invalid option name");
}
else if (option == "secret")
{
PutModule(GetNV("notifo_secret"));
} }
else else
{ {
PutModule("Error: invalid option name"); PutModule(option + CString(": \"") + options[option] + CString("\""));
return;
} }
} }
// SEND command // SEND command