Factored out auth string encoding

Rather than re-encoding the auth string for every message, factored it into a
separate method that only gets called when the username or password is changed,
or the plugin is loaded/reloaded.
This commit is contained in:
John Reese 2011-01-13 17:16:43 -05:00
parent 042ade55da
commit 31541dda0a
1 changed files with 19 additions and 4 deletions

View File

@ -43,6 +43,9 @@ class CNotifoMod : public CModule
CString notifo_username; CString notifo_username;
CString notifo_secret; CString notifo_secret;
// BASIC auth string, needs to be encoded each time username/secret is changed
CString notifo_auth;
public: public:
MODCONSTRUCTOR(CNotifoMod) { MODCONSTRUCTOR(CNotifoMod) {
@ -61,6 +64,7 @@ class CNotifoMod : public CModule
notifo_username = ""; notifo_username = "";
notifo_secret = ""; notifo_secret = "";
notifo_auth = "";
} }
virtual ~CNotifoMod() {} virtual ~CNotifoMod() {}
@ -77,6 +81,16 @@ class CNotifoMod : public CModule
return str.Escape_n(CString::EASCII, CString::EURL); return str.Escape_n(CString::EASCII, CString::EURL);
} }
/**
* Re-encode the authentication credentials.
*/
void authencode()
{
// BASIC auth, base64-encoded username:password
CString auth = notifo_username + CString(":") + notifo_secret;
notifo_auth = auth.Base64Encode_n();
}
/** /**
* Send a message to the currently-configured Notifo account. * Send a message to the currently-configured Notifo account.
* Requires (and assumes) that the user has already configured their * Requires (and assumes) that the user has already configured their
@ -87,9 +101,6 @@ 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")
{ {
// BASIC auth style
CString auth = notifo_username + CString(":") + notifo_secret;
// POST body parameters for the request // POST body parameters for the request
CString post = "to=" + urlencode(notifo_username); CString post = "to=" + urlencode(notifo_username);
post += "&msg=" + urlencode(message); post += "&msg=" + urlencode(message);
@ -103,7 +114,7 @@ class CNotifoMod : public CModule
request += "Content-Type: application/x-www-form-urlencoded" + crlf; request += "Content-Type: application/x-www-form-urlencoded" + crlf;
request += "Content-Length: " + CString(post.length()) + crlf; request += "Content-Length: " + CString(post.length()) + crlf;
request += "User-Agent: " + user_agent + crlf; request += "User-Agent: " + user_agent + crlf;
request += "Authorization: Basic " + auth.Base64Encode_n() + crlf; request += "Authorization: Basic " + notifo_auth + crlf;
request += crlf; request += crlf;
request += post + crlf; request += post + crlf;
@ -129,6 +140,8 @@ class CNotifoMod : public CModule
notifo_username = GetNV("notifo_username"); notifo_username = GetNV("notifo_username");
notifo_secret = GetNV("notifo_secret"); notifo_secret = GetNV("notifo_secret");
authencode();
return true; return true;
} }
@ -160,11 +173,13 @@ class CNotifoMod : public CModule
{ {
SetNV("notifo_username", value); SetNV("notifo_username", value);
notifo_username = value; notifo_username = value;
authencode();
} }
else if (option == "secret") else if (option == "secret")
{ {
SetNV("notifo_secret", value); SetNV("notifo_secret", value);
notifo_secret = value; notifo_secret = value;
authencode();
} }
else else
{ {