12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147 |
- /**
- * ZNC Push Module
- *
- * Allows the user to enter a Push user and API token, and sends
- * channel highlights and personal messages to Push.
- *
- * Copyright (c) 2011 John Reese
- * Licensed under the MIT license
- */
- #define REQUIRESSL
- #ifndef PUSHVERSION
- #define PUSHVERSION "dev"
- #endif
- #include <znc/znc.h>
- #include <znc/Chan.h>
- #include <znc/User.h>
- #include <znc/IRCNetwork.h>
- #include <znc/Modules.h>
- #include <znc/FileUtils.h>
- #include <znc/Client.h>
- #include "time.h"
- #include <string.h>
- #ifdef USE_CURL
- #include <curl/curl.h>
- #endif // USE_CURL
- // Forward declaration
- class CPushMod;
- /**
- * Shorthand for encoding a string for a URL.
- *
- * @param str String to be encoded
- * @return Encoded string
- */
- CString urlencode(const CString& str)
- {
- return str.Escape_n(CString::EASCII, CString::EURL);
- }
- #ifndef USE_CURL
- /**
- * Socket class for generating HTTP requests.
- */
- class CPushSocket : public CSocket
- {
- public:
- CPushSocket(CModule *p) : CSocket(p)
- {
- EnableReadLine();
- parent = (CPushMod*) p;
- first = true;
- crlf = "\r\n";
- user_agent = "ZNC Push/" + CString(PUSHVERSION);
- }
- // Implemented after CPushMod
- void Request(bool post, const CString& host, const CString& url, MCString& parameters, const CString& auth="");
- virtual void ReadLine(const CString& data);
- virtual void Disconnected();
- private:
- CPushMod *parent;
- bool first;
- // Too lazy to add CString("\r\n\") everywhere
- CString crlf;
- // User agent to use
- CString user_agent;
- };
- #else
- // forward declaration
- long make_curl_request(const CString& service_host, const CString& service_url,
- const CString& service_auth, MCString& params, int port,
- bool use_ssl, bool use_post,
- const CString& proxy, bool proxy_ssl_verify,
- bool debug);
- #endif // USE_CURL
- /**
- * Push notification module.
- */
- class CPushMod : public CModule
- {
- protected:
- // Application name
- CString app;
- // Time last notification was sent for a given context
- std::map <CString, time_t> last_notification_time;
- // Time of last message by user to a given context
- std::map <CString, time_t> last_reply_time;
- // Time of last activity by user for a given context
- std::map <CString, time_t> last_active_time;
- // Time of last activity by user in any context
- time_t idle_time;
- // User object
- CUser *user;
- // Configuration options
- MCString options;
- MCString defaults;
- public:
- MODCONSTRUCTOR(CPushMod) {
- #ifdef USE_CURL
- curl_global_init(CURL_GLOBAL_DEFAULT);
- #endif
- app = "ZNC";
- idle_time = time(NULL);
- // Current user
- user = GetUser();
- // Push service information
- defaults["service"] = "igloo";
- defaults["username"] = "";
- defaults["secret"] = "";
- defaults["target"] = "";
- // Igloo Devices
- defaults["device1"] = "";
- defaults["device2"] = "";
- defaults["device3"] = "";
- defaults["device4"] = "";
- defaults["device5"] = "";
- // Notification settings
- defaults["message_content"] = "{context}: [{nick}] {message}";
- defaults["message_length"] = "100";
- defaults["message_title"] = "{title}";
- defaults["message_uri"] = "";
- defaults["message_uri_post"] = "no";
- defaults["message_uri_title"] = "";
- defaults["message_priority"] = "0";
- defaults["message_sound"] = "";
- defaults["message_escape"] = "";
- // Notification conditions
- defaults["away_only"] = "no";
- defaults["client_count_less_than"] = "0";
- defaults["highlight"] = "";
- defaults["idle"] = "0";
- defaults["last_active"] = "180";
- defaults["last_notification"] = "300";
- defaults["nick_blacklist"] = "";
- defaults["network_blacklist"] = "";
- defaults["replied"] = "yes";
- defaults["context"] = "*";
- // Proxy, for libcurl
- defaults["proxy"] = "";
- defaults["proxy_ssl_verify"] = "yes";
- // Advanced
- defaults["channel_conditions"] = "all";
- defaults["query_conditions"] = "all";
- defaults["debug"] = "off";
- }
- virtual ~CPushMod() {
- #ifdef USE_CURL
- curl_global_cleanup();
- #endif
- }
- public:
- /**
- * Debugging messages. Prints to *push when the debug option is enabled.
- *
- * @param data Debug message
- */
- void PutDebug(const CString& data)
- {
- if (options["debug"] == "on")
- {
- PutModule(data);
- }
- }
- protected:
- /**
- * Performs string expansion on a set of keywords.
- * Given an initial string and a dictionary of string replacments,
- * iterate over the dictionary, expanding keywords one-by-one.
- *
- * @param content String contents
- * @param replace Dictionary of string replacements
- * @return Result of string replacements
- */
- CString expand(const CString& content, MCString& replace)
- {
- CString result = content.c_str();
- for(MCString::iterator i = replace.begin(); i != replace.end(); i++)
- {
- result.Replace(i->first, i->second);
- }
- return result;
- }
- /**
- * Verifies whether a given string contains only numbers.
- *
- * @param content String to verify
- */
- bool is_number(const CString& content)
- {
- CString::const_iterator it = content.begin();
- while(it != content.end() && std::isdigit(*it)) ++it;
- return !content.empty() && it == content.end();
- }
- /**
- * Send a message to the currently-configured push service.
- * Requires (and assumes) that the user has already configured their
- * username and API secret using the 'set' command.
- *
- * @param message Message to be sent to the user
- * @param title Message title to use
- * @param context Channel or nick context
- */
- void send_message(const CString& message, const CString& title="New Message", const CString& context="*push", const CNick& nick=CString("*push"), const CString& type="")
- {
- // Set the last notification time
- last_notification_time[context] = time(NULL);
- // Shorten message if needed
- unsigned int message_length = options["message_length"].ToUInt();
- CString short_message = message;
- if (message_length > 0)
- {
- short_message = message.Ellipsize(message_length);
- }
- // Generate an ISO8601 date string
- time_t rawtime;
- struct tm * timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- char iso8601 [20];
- strftime(iso8601, 20, "%Y-%m-%d %H:%M:%S", timeinfo);
- // Message string replacements
- MCString replace;
- replace["{context}"] = context;
- replace["{nick}"] = nick.GetNick();
- replace["{datetime}"] = CString(iso8601);
- replace["{unixtime}"] = CString(time(NULL));
- replace["{message}"] = short_message;
- replace["{title}"] = title;
- replace["{username}"] = options["username"];
- replace["{secret}"] = options["secret"];
- replace["{target}"] = options["target"];
- // network is special because it can be nullptr if the user has none set up yet
- CIRCNetwork* network = GetNetwork();
- if (network) {
- replace["{network}"] = network->GetName();
- } else {
- replace["{network}"] = "(No network)";
- }
- if (options["message_escape"] != "")
- {
- CString::EEscape esc = CString::ToEscape(options["message_escape"]);
- for (MCString::iterator i = replace.begin(); i != replace.end(); i++) {
- i->second = i->second.Escape(esc);
- }
- }
- CString message_uri = expand(options["message_uri"], replace);
- CString message_title = expand(options["message_title"], replace);
- CString message_content = expand(options["message_content"], replace);
- // Set up the connection profile
- CString service = options["service"];
- bool use_post = true;
- int use_port = 443;
- bool use_ssl = true;
- CString service_host;
- CString service_url;
- CString service_auth;
- MCString params;
- // Service-specific profiles
- if (service == "pushbullet")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret (api key) not set");
- return;
- }
- service_host = "api.pushbullet.com";
- service_url = "/v2/pushes";
- // BASIC auth, base64-encoded APIKey:
- service_auth = options["secret"] + CString(":");
- if (options["target"] != "")
- {
- params["device_iden"] = options["target"];
- }
- if (message_uri == "")
- {
- params["type"] = "note";
- } else {
- params["type"] = "link";
- params["url"] = message_uri;
- }
- params["title"] = message_title;
- params["body"] = message_content;
- }
- else if (service == "boxcar")
- {
- if (options["username"] == "")
- {
- PutModule("Error: username not set");
- return;
- }
- CString boxcar_api_key = "puSd2qp2gCDZO7nWkvb9";
- CString boxcar_api_secret = "wLQQKSyGybIOkggbiKipefeYGLni9B3FPZabopHp";
- service_host = "boxcar.io";
- service_url = "/devices/providers/" + boxcar_api_key + "/notifications";
- params["email"] = options["username"];
- params["notification[from_screen_name]"] = context;
- params["notification[message]"] = message_content;
- params["notification[source_url]"] = message_uri;
- }
- else if (service == "boxcar2")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret not set to apikey");
- return;
- }
- service_host = "new.boxcar.io";
- service_url = "/api/notifications";
- params["user_credentials"] = options["secret"];
- params["notification[title]"] = message_title;
- params["notification[long_message]"] = message_content;
- if ( options["message_sound"] != "" )
- {
- params["notification[sound]"] = options["message_sound"];
- }
- }
- else if (service == "nma")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret not set");
- return;
- }
- if (options["message_priority"] != "")
- {
- params["priority"] = options["message_priority"];
- }
- service_host = "www.notifymyandroid.com";
- service_url = "/publicapi/notify";
- params["apikey"] = options["secret"];
- params["application"] = app;
- params["event"] = message_title;
- params["description"] = message_content;
- params["url"] = message_uri;
- }
- else if (service == "pushover")
- {
- if (options["username"] == "")
- {
- PutModule("Error: username (user key) not set");
- return;
- }
- if (options["secret"] == "")
- {
- PutModule("Error: secret (application token/key) not set");
- return;
- }
- service_host = "api.pushover.net";
- service_url = "/1/messages.json";
- params["token"] = options["secret"];
- params["user"] = options["username"];
- params["title"] = message_title;
- params["message"] = message_content;
- if (message_uri != "")
- {
- params["url"] = message_uri;
- }
- if ( options["message_uri_title"] != "" )
- {
- params["url_title"] = options["message_uri_title"];
- }
- if (options["target"] != "")
- {
- params["device"] = options["target"];
- }
- if ( options["message_sound"] != "" )
- {
- params["sound"] = options["message_sound"];
- }
- if (options["message_priority"] != "")
- {
- params["priority"] = options["message_priority"];
- }
- }
- else if (service == "pushsafer")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: privatekey (private or alias key) not set");
- return;
- }
- service_host = "pushsafer.com";
- service_url = "/api";
- params["k"] = options["secret"];
- params["t"] = message_title;
- params["m"] = message_content;
- if (message_uri != "")
- {
- params["u"] = message_uri;
- }
- if (options["message_uri_title"] != "" )
- {
- params["ut"] = options["message_uri_title"];
- }
- if (options["target"] != "")
- {
- params["d"] = options["target"];
- }
- if (options["message_sound"] != "" )
- {
- params["s"] = options["message_sound"];
- }
- }
- else if (service == "pushalot")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret (authorization token) not set");
- return;
- }
- service_host = "pushalot.com";
- service_url = "/api/sendmessage";
- params["AuthorizationToken"] = options["secret"];
- params["Title"] = message_title;
- params["Body"] = message_content;
- if (message_uri != "")
- {
- params["Link"] = message_uri;
- }
- if (options["message_uri_title"] != "" )
- {
- params["LinkTitle"] = options["message_uri_title"];
- }
- }
- else if (service == "prowl")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret not set");
- return;
- }
- service_host = "api.prowlapp.com";
- service_url = "/publicapi/add";
- params["apikey"] = options["secret"];
- params["application"] = app;
- params["event"] = message_title;
- params["description"] = message_content;
- params["url"] = message_uri;
- }
- else if (service == "igloo")
- {
- if (options["device1"] == "" && options["device2"] == "" && options["device3"] == "" && options["device4"] == "" && options["device5"] == "")
- {
- PutModule("Error: no devices are set");
- return;
- }
- service_host = "api.iglooirc.com";
- service_url = "/znc/push";
- params["network"] = network->GetName();
- params["channel"] = context;
- params["message"] = message;
- params["nick"] = network->GetNick();
- params["sender"] = nick.GetNick();
- params["type"] = type;
- //params["developer"] = "true";
- params["device1"] = options["device1"];
- params["device2"] = options["device2"];
- params["device3"] = options["device3"];
- params["device4"] = options["device4"];
- params["device5"] = options["device5"];
- }
- else if (service == "supertoasty")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret (device id) not set");
- return;
- }
- use_post = false;
- use_port = 80;
- use_ssl = false;
- service_host = "api.supertoasty.com";
- service_url = "/notify/"+options["secret"];
- params["title"] = message_title;
- params["text"] = message_content;
- params["image"] = "https://raw2.github.com/jreese/znc-push/master/logo.png";
- params["sender"] = "ZNC Push";
- }
- else if (service == "faast")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret not set to apikey");
- return;
- }
- service_host = "www.appnotifications.com";
- service_url = "/account/notifications.json";
- params["user_credentials"] = options["secret"];
- params["notification[title]"] = message_title;
- params["notification[subtitle]"] = context;
- params["notification[message]"] = message_content;
- params["notification[long_message]"] = message_content;
- params["notification[icon_url]"] = "https://raw2.github.com/jreese/znc-push/master/logo.png";
- if ( options["message_sound"] != "" )
- {
- params["notification[sound]"] = options["message_sound"];
- }
- if ( options["message_uri"] != "" )
- {
- params["notification[run_command]"] = options["message_uri"];
- }
- }
- else if (service == "nexmo")
- {
- if (options["username"] == "")
- {
- PutModule("Error: username (api key) not set");
- return;
- }
- if (options["secret"] == "")
- {
- PutModule("Error: secret (api secret) not set");
- return;
- }
- if (options["target"] == "")
- {
- PutModule("Error: destination mobile number (in international format) not set");
- return;
- }
- service_host = "rest.nexmo.com";
- service_url = "/sms/json";
- params["api_secret"] = options["secret"];
- params["api_key"] = options["username"];
- params["from"] = message_title;
- params["to"] = options["target"];
- params["text"] = message_content;
- }
- else if (service == "url")
- {
- if (options["message_uri"] == "")
- {
- PutModule("Error: message_uri not set");
- return;
- }
- CString::size_type count;
- VCString parts;
- CString url = options["message_uri"];
- // Verify that the URL begins with either http:// or https://
- count = url.Split("://", parts, false);
- if (count != 2)
- {
- PutModule("Error: invalid url format");
- return;
- }
- if(options["message_uri_post"] != "yes")
- {
- use_post = false;
- }
- if (parts[0] == "https")
- {
- use_ssl = true;
- use_port = 443;
- }
- else if (parts[0] == "http")
- {
- use_ssl = false;
- use_port = 80;
- }
- else
- {
- PutModule("Error: invalid url schema");
- return;
- }
- // HTTP basic auth
- if(options["username"] != "" || options["secret"] != "")
- {
- service_auth = options["username"] + CString(":") + options["secret"];
- }
- // Process the remaining portion of the URL
- url = parts[1];
- // Split out the host and optional port number; this breaks with raw IPv6 addresses
- CString host = url.Token(0, false, "/");
- count = host.Split(":", parts, false);
- if (count > 1)
- {
- use_port = parts[1].ToInt();
- }
- service_host = parts[0];
- // Split remaining URL into path and query components
- url = "/" + url.Token(1, true, "/");
- service_url = expand(url.Token(0, false, "?"), replace);
- // Parse and expand query parameter values
- url = url.Token(1, true, "?");
- url.URLSplit(params);
- for (MCString::iterator i = params.begin(); i != params.end(); i++) {
- i->second = expand(i->second, replace);
- }
- }
- else if (service == "airgram")
- {
- PutModule("Error: Airgram service shut down. Please configure another notification provider.");
- }
- else if (service == "slack")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret (from webhook, e.g. T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX) not set");
- return;
- }
- if (options["target"] == "")
- {
- PutModule("Error: target (channel or username) not set");
- return;
- }
- service_host = "hooks.slack.com";
- service_url = "/services/" + options["secret"];
- if (options["username"] != "")
- {
- params["username"] = options["username"];
- }
- params["payload"] = expand("{\"channel\": \"{target}\", \"text\": \"*{title}*: {message}\"}", replace);
- PutDebug("payload: " + params["payload"]);
- }
- else if (service == "discord")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret (from webhook, e.g. 111111111111111111/abcdefghijklmopqrstuvwxyz1234567890-ABCDEFGHIJKLMNOPQRSTUVWXYZ123456) not set");
- return;
- }
- service_host = "discordapp.com";
- service_url = "/api/webhooks/" + options["secret"];
- if (options["username"] != "")
- {
- params["username"] = options["username"];
- }
- params["content"] = message_content;
- }
- else if (service == "pushjet")
- {
- if (options["secret"] == "")
- {
- PutModule("Error: secret (service key) not set");
- return;
- }
- service_host = "api.pushjet.io";
- service_url = "/message";
- params["secret"] = options["secret"];
- params["title"] = message_title;
- params["message"] = message_content;
- if (message_uri != "")
- {
- params["link"] = message_uri;
- }
- if (options["message_priority"] != "")
- {
- params["level"] = options["message_priority"];
- }
- }
- else if (service == "telegram")
- {
- if ((options["secret"] == "") || (options["target"] ==""))
- {
- PutModule("Error: secret (API key) or target (chat_id) not set");
- return;
- }
- service_host = "api.telegram.org";
- service_url = "/bot" + options["secret"] + "/sendMessage";
- params["chat_id"] = options["target"];
- params["text"] = message_content;
- if (options["message_escape"] == "HTML") {
- params["parse_mode"] = "HTML";
- }
- }
- else
- {
- PutModule("Error: service type not selected");
- return;
- }
- PutDebug("service: " + service);
- PutDebug("service_host: " + service_host);
- PutDebug("service_url: " + service_url);
- PutDebug("service_auth: " + service_auth);
- PutDebug("use_port: " + CString(use_port));
- PutDebug("use_ssl: " + CString(use_ssl ? 1 : 0));
- PutDebug("use_post: " + CString(use_post ? 1 : 0));
- #ifdef USE_CURL
- PutDebug("using libcurl");
- long http_code = make_curl_request(service_host, service_url, service_auth, params, use_port, use_ssl, use_post, options["proxy"], options["proxy_ssl_verify"] != "no", options["debug"] == "on");
- PutDebug("curl: HTTP status code " + CString(http_code));
- if (!(http_code >= 200 && http_code < 300)) {
- PutModule("Error: HTTP status code " + CString(http_code));
- }
- #else
- PutDebug("NOT using libcurl");
- // Create the socket connection, write to it, and add it to the queue
- CPushSocket *sock = new CPushSocket(this);
- sock->Connect(service_host, use_port, use_ssl);
- sock->Request(use_post, service_host, service_url, params, service_auth);
- AddSocket(sock);
- #endif
- }
- /**
- * Evaluate a boolean expression using condition values.
- * All tokens must be separated by spaces, using "and" and "or" for
- * boolean operators, "(" and ")" to enclose sub-expressions, and
- * condition option names to evaluate each condition.
- *
- * @param expression Boolean expression string
- * @param context Notification context
- * @param nick Sender nick
- * @param message Message contents
- * @return Result of boolean evaluation
- */
- bool eval(const CString& expression, const CString& context=CString(""), const CNick& nick=CNick(""), const CString& message=" ")
- {
- CString padded = expression.Replace_n("(", " ( ");
- padded.Replace(")", " ) ");
- VCString tokens;
- padded.Split(" ", tokens, false);
- PutDebug("Evaluating message: <" + nick.GetNick() + "> " + message);
- bool result = eval_tokens(tokens.begin(), tokens.end(), context, nick, message);
- return result;
- }
- #define expr(x, y) else if (token == x) { \
- bool result = y; \
- dbg += CString(x) + "/" + CString(result ? "true" : "false") + " "; \
- value = oper ? value && result : value || result; \
- }
- /**
- * Evaluate a tokenized boolean expression, or sub-expression.
- *
- * @param pos Token vector iterator current position
- * @param end Token vector iterator end position
- * @param context Notification context
- * @param nick Sender nick
- * @param message Message contents
- * @return Result of boolean expression
- */
- bool eval_tokens(VCString::iterator pos, VCString::iterator end, const CString& context, const CNick& nick, const CString& message)
- {
- bool oper = true;
- bool value = true;
- CString dbg = "";
- for(; pos != end; pos++)
- {
- CString token = pos->AsLower();
- if (token == "(")
- {
- // recursively evaluate sub-expressions
- bool inner = eval_tokens(++pos, end, context, nick, message);
- dbg += "( inner/" + CString(inner ? "true" : "false") + " ) ";
- value = oper ? value && inner : value || inner;
- // search ahead to the matching parenthesis token
- unsigned int parens = 1;
- while(pos != end)
- {
- if (*pos == "(")
- {
- parens++;
- }
- else if (*pos == ")")
- {
- parens--;
- }
- if (parens == 0)
- {
- break;
- }
- pos++;
- }
- }
- else if (token == ")")
- {
- pos++;
- PutDebug(dbg);
- return value;
- }
- else if (token == "and")
- {
- dbg += "and ";
- oper = true;
- }
- else if (token == "or")
- {
- dbg += "or ";
- oper = false;
- }
- expr("true", true)
- expr("false", false)
- expr("away_only", away_only())
- expr("client_count_less_than", client_count_less_than())
- expr("highlight", highlight(message))
- expr("idle", idle())
- expr("last_active", last_active(context))
- expr("last_notification", last_notification(context))
- expr("nick_blacklist", nick_blacklist(nick))
- expr("network_blacklist", network_blacklist())
- expr("replied", replied(context))
- expr("context", context_filter(context))
- else
- {
- PutModule("Error: Unexpected token \"" + token + "\"");
- }
- }
- PutDebug(dbg);
- return value;
- }
- #undef expr
- protected:
- /**
- * Check if the away status condition is met.
- *
- * @return True if away_only is not "yes" or away status is set
- */
- bool away_only()
- {
- CString value = options["away_only"].AsLower();
- return value != "yes" || GetNetwork()->IsIRCAway();
- }
- /**
- * Check how many clients are connected to ZNC.
- *
- * @return Number of connected clients
- */
- size_t client_count()
- {
- return GetNetwork()->GetClients().size();
- }
- /**
- * Check if the client_count condition is met.
- *
- * @return True if client_count is less than client_count_less_than or if client_count_less_than is zero
- */
- bool client_count_less_than()
- {
- unsigned int value = options["client_count_less_than"].ToUInt();
- return value == 0 || client_count() < value;
- }
- /**
- * Determine if the given message matches any highlight rules.
- *
- * @param message Message contents
- * @return True if message matches a highlight
- */
- bool highlight(const CString& message)
- {
- CString msg = " " + message.AsLower() + " ";
- VCString values;
- options["highlight"].Split(" ", values, false);
- values.push_back("%nick%");
- bool matched = false;
- bool negated = false;
- for (VCString::iterator i = values.begin(); i != values.end(); i++)
- {
- CString value = i->AsLower();
- char prefix = value[0];
- bool negate_match = false;
- if (prefix == '-')
- {
- negate_match = true;
- value.LeftChomp(1);
- }
- else if (prefix == '_')
- {
- value = " " + value.LeftChomp_n(1) + " ";
- }
- // Expand substrings like %nick%
- if (m_pNetwork)
- {
- value = m_pNetwork->ExpandString(value);
- }
- else
- {
- value = GetUser()->ExpandString(value);
- }
- value = "*" + value.AsLower() + "*";
- if (msg.WildCmp(value))
- {
- if (negate_match)
- {
- negated = true;
- }
- else
- {
- matched = true;
- }
- }
- }
- return (matched && !negated);
- }
- /**
- * Determine if the given context matches any context rules.
- *
- * @param context The context of a message
- * @return True if context matches the filter
- */
- bool context_filter(const CString& raw_context)
- {
- CString context = raw_context.AsLower();
- if (context == "all" || context == "*")
- return true;
- VCString values;
- options["context"].Split(" ", values, false);
- for (VCString::iterator i = values.begin(); i != values.end(); i++)
- {
- CString value = i->AsLower();
- char prefix = value[0];
- bool push = true;
- if (prefix == '-')
- {
- push = false;
- value.LeftChomp(1);
- }
- if (value != "*")
- {
- value = "*" + value.AsLower() + "*";
- }
- if (context.WildCmp(value))
- {
- return push;
- }
- }
- return false;
- }
- /**
- * Check if the idle condition is met.
- *
- * @return True if idle is less than or equal to zero or elapsed time is greater than idle
- */
- bool idle()
- {
- unsigned int value = options["idle"].ToUInt();
- time_t now = time(NULL);
- return value == 0 || difftime(now, idle_time) >= value;
- }
- /**
- * Check if the last_active condition is met.
- *
- * @param context Channel or nick context
- * @return True if last_active is less than or equal to zero or elapsed time is greater than last_active
- */
- bool last_active(const CString& context)
- {
- unsigned int value = options["last_active"].ToUInt();
- time_t now = time(NULL);
- return value == 0
- || last_active_time.count(context) < 1
- || difftime(now, last_active_time[context]) >= value;
- }
- /**
- * Check if the last_notification condition is met.
- *
- * @param context Channel or nick context
- * @return True if last_notification is less than or equal to zero or elapsed time is greater than last_nofication
- */
- bool last_notification(const CString& context)
- {
- unsigned int value = options["last_notification"].ToUInt();
- time_t now = time(NULL);
- return value == 0
- || last_notification_time.count(context) < 1
- || difftime(now, last_notification_time[context]) >= value;
- }
- /**
- * Check if the nick_blacklist condition is met.
- *
- * @param nick Nick that sent the message
- * @return True if nick is not in the blacklist
- */
- bool nick_blacklist(const CNick& nick)
- {
- VCString blacklist;
- options["nick_blacklist"].Split(" ", blacklist, false);
- CString name = nick.GetNick().AsLower();
- for (VCString::iterator i = blacklist.begin(); i != blacklist.end(); i++)
- {
- CString value;
- // Expand substrings like %nick%
- if (m_pNetwork)
- {
- value = m_pNetwork->ExpandString(*i);
- }
- else
- {
- value = GetUser()->ExpandString(*i);
- }
- if (name.WildCmp(value.AsLower()))
- {
- return false;
- }
- }
- return true;
- }
- /**
- * Check if the network_blacklist condition is met.
- *
- * @param network Network that the message was received on
- * @return True if network is not in the blacklist
- */
- bool network_blacklist()
- {
- VCString blacklist;
- options["network_blacklist"].Split(" ", blacklist, false);
- CString name = (*m_pNetwork).GetName().AsLower();
- for (VCString::iterator i = blacklist.begin(); i != blacklist.end(); i++)
- {
- if (name.WildCmp((*i).AsLower()))
- {
- return false;
- }
- }
- return true;
- }
- /**
- * Check if the replied condition is met.
- *
- * @param context Channel or nick context
- * @return True if last_reply_time > last_notification_time or if replied is not "yes"
- */
- bool replied(const CString& context)
- {
- CString value = options["replied"].AsLower();
- return value != "yes"
- || last_notification_time[context] == 0
- || last_notification_time[context] < last_reply_time[context];
- }
- /**
- * Determine when to notify the user of a channel message.
- *
- * @param nick Nick that sent the message
- * @param channel Channel the message was sent to
- * @param message Message contents
- * @return Notification should be sent
- */
- bool notify_channel(const CNick& nick, const CChan& channel, const CString& message)
- {
- CString context = channel.GetName();
- CString expression = options["channel_conditions"].AsLower();
- if (expression != "all")
- {
- return eval(expression, context, nick, message);
- }
- return away_only()
- && client_count_less_than()
- && highlight(message)
- && idle()
- && last_active(context)
- && last_notification(context)
- && nick_blacklist(nick)
- && network_blacklist()
- && replied(context)
- && context_filter(context)
- && true;
- }
- /**
- * Determine when to notify the user of a private message.
- *
- * @param nick Nick that sent the message
- * @return Notification should be sent
- */
- bool notify_pm(const CNick& nick, const CString& message)
- {
- CString context = nick.GetNick();
- CString expression = options["query_conditions"].AsLower();
- if (expression != "all")
- {
- return eval(expression, context, nick, message);
- }
- return away_only()
- && client_count_less_than()
- && idle()
- && last_active(context)
- && last_notification(context)
- && nick_blacklist(nick)
- && network_blacklist()
- && replied(context)
- && true;
- }
- 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)
- {
- for (MCString::iterator i = defaults.begin(); i != defaults.end(); i++)
- {
- CString value = GetNV(i->first);
- if (value != "")
- {
- options[i->first] = value;
- }
- else
- {
- options[i->first] = defaults[i->first];
- }
- }
- return true;
- }
- /**
- * Handle channel messages.
- *
- * @param nick Nick that sent the message
- * @param channel Channel the message was sent to
- * @param message Message contents
- */
- EModRet OnChanMsg(CNick& nick, CChan& channel, CString& message)
- {
- if (notify_channel(nick, channel, message))
- {
- CString title = "Highlight";
- send_message(message, title, channel.GetName(), nick);
- }
- return CONTINUE;
- }
- /**
- * Handle channel actions.
- *
- * @param nick Nick that sent the action
- * @param channel Channel the message was sent to
- * @param message Message contents
- */
- EModRet OnChanAction(CNick& nick, CChan& channel, CString& message)
- {
- if (notify_channel(nick, channel, message))
- {
- CString title = "Highlight";
- send_message(message, title, channel.GetName(), nick, "action");
- }
- return CONTINUE;
- }
- /**
- * Handle channel notices.
- *
- * @param nick Nick that sent the notice
- * @param channel Channel the notice was sent to
- * @param message Notice contents
- */
- EModRet OnChanNotice(CNick& nick, CChan& channel, CString& message)
- {
- if (notify_channel(nick, channel, message))
- {
- CString title = "Channel Notice";
- send_message(message, title, channel.GetName(), nick, "notice");
- }
- return CONTINUE;
- }
- /**
- * Handle a private message.
- *
- * @param nick Nick that sent the message
- * @param message Message contents
- */
- EModRet OnPrivMsg(CNick& nick, CString& message)
- {
- if (notify_pm(nick, message))
- {
- CString title = "Private Message";
- send_message(message, title, nick.GetNick(), nick);
- }
- return CONTINUE;
- }
- /**
- * Handle a private action.
- *
- * @param nick Nick that sent the action
- * @param message Message contents
- */
- EModRet OnPrivAction(CNick& nick, CString& message)
- {
- if (notify_pm(nick, message))
- {
- CString title = "Private Message";
- send_message(message, title, nick.GetNick(), nick, "action");
- }
- return CONTINUE;
- }
- /**
- * Handle a private notice.
- *
- * @param nick Nick that sent the notice
- * @param message Notice contents
- */
- EModRet OnPrivNotice(CNick& nick, CString& message)
- {
- if (notify_pm(nick, message))
- {
- CString title = "Private Notice";
- send_message(message, title, nick.GetNick(), nick, "notice");
- }
- return CONTINUE;
- }
- /**
- * Handle a message sent by the user.
- *
- * @param target Target channel or nick
- * @param message Message contents
- */
- EModRet OnUserMsg(CString& target, CString& message)
- {
- last_reply_time[target] = last_active_time[target] = idle_time = time(NULL);
- return CONTINUE;
- }
- /**
- * Handle an action sent by the user.
- *
- * @param target Target channel or nick
- * @param message Message contents
- */
- EModRet OnUserAction(CString& target, CString& message)
- {
- last_reply_time[target] = last_active_time[target] = idle_time = time(NULL);
- return CONTINUE;
- }
- /**
- * Handle a notice sent by the user.
- *
- * @param target Target channel or nick
- * @param message Notice contents
- */
- EModRet OnUserNotice(CString& target, CString& message)
- {
- last_reply_time[target] = last_active_time[target] = idle_time = time(NULL);
- return CONTINUE;
- }
- /**
- * Handle the user joining a channel.
- *
- * @param channel Channel name
- * @param key Channel key
- */
- EModRet OnUserJoin(CString& channel, CString& key)
- {
- idle_time = time(NULL);
- return CONTINUE;
- }
- /**
- * Handle the user parting a channel.
- *
- * @param channel Channel name
- * @param message Part message
- */
- EModRet OnUserPart(CString& channel, CString& message)
- {
- idle_time = time(NULL);
- return CONTINUE;
- }
- /**
- * Handle the user setting the channel topic.
- *
- * @param channel Channel name
- * @param topic Topic message
- */
- EModRet OnUserTopic(CString& channel, CString& topic)
- {
- idle_time = time(NULL);
- return CONTINUE;
- }
- /**
- * Handle the user requesting the channel topic.
- *
- * @param channel Channel name
- */
- EModRet OnUserTopicRequest(CString& channel)
- {
- idle_time = time(NULL);
- return CONTINUE;
- }
- /**
- * Handle direct commands to the *push virtual user.
- *
- * @param command Command string
- */
- void OnModCommand(const CString& command)
- {
- VCString tokens;
- CString::size_type token_count = command.Split(" ", tokens, false);
- if (token_count < 1)
- {
- return;
- }
- CString action = tokens[0].AsLower();
- // SET command
- if (action == "set")
- {
- if (token_count < 3)
- {
- PutModule("Usage: set <option> <value>");
- return;
- }
- CString option = tokens[1].AsLower();
- CString value = command.Token(2, true, " ");
- MCString::iterator pos = options.find(option);
- if (pos == options.end())
- {
- PutModule("Error: invalid option name");
- }
- else
- {
- value.Trim();
- if (option == "channel_conditions" || option == "query_conditions")
- {
- if (value != "all")
- {
- eval(value);
- }
- }
- else if (option == "service")
- {
- value.MakeLower();
- if (value == "pushbullet")
- {
- PutModule("Note: Pushbullet requires setting both 'target' (to device id) and 'secret' (to api key) options");
- }
- else if (value == "boxcar")
- {
- PutModule("Note: Boxcar requires setting the 'username' option");
- }
- else if (value == "boxcar2")
- {
- PutModule("Note: Boxcar 2 requires setting the 'secret' option");
- }
- else if (value == "nma")
- {
- PutModule("Note: NMA requires setting the 'secret' option");
- }
- else if (value == "pushover")
- {
- PutModule("Note: Pushover requires setting both the 'username' (to user key) and the 'secret' (to application api key) option");
- }
- else if (value == "pushsafer")
- {
- PutModule("Note: Pushsafer requires setting the 'private or alias key' option");
- }
- else if (value == "pushalot")
- {
- PutModule("Note: Pushalot requires setting the 'secret' (to user key) (to authorization token) option");
- }
- else if (value == "prowl")
- {
- PutModule("Note: Prowl requires setting the 'secret' option");
- }
- else if (value == "igloo")
- {
- PutModule("Note: Igloo requires adding your devices with device1-5");
- }
- else if (value == "supertoasty")
- {
- PutModule("Note: Supertoasty requires setting the 'secret' option with device id");
- }
- else if (value == "url")
- {
- PutModule("Note: URL requires setting the 'message_uri' option with the full URL");
- }
- else if (value == "faast")
- {
- PutModule("Note: Faast requires setting the secret to your apikey");
- }
- else if (value == "nexmo")
- {
- PutModule("Note: Nexmo requires setting the 'username' (to api key), 'secret' (to api secret), 'message_title' (to sender number in international format), and 'target' (to destination number in international format) options");
- }
- else if (value == "slack")
- {
- PutModule("Note: Slack requires setting 'secret' (from webhook) and 'target' (channel or username), optional 'username' (bot name)");
- }
- else if (value == "discord")
- {
- PutModule("Note: Discord requires setting 'secret' (from webhook), optional 'username' (bot name)");
- }
- else if (value == "pushjet")
- {
- PutModule("Note: Pushjet requires setting 'secret' (service key) option");
- }
- else if (value == "telegram")
- {
- PutModule("Note: Telegram requires setting both the 'secret' (api key) and 'target' (chat_id)");
- }
- else
- {
- PutModule("Error: unknown service name");
- return;
- }
- }
- options[option] = value;
- SetNV(option, options[option]);
- PutModule("Ok");
- }
- }
- // APPEND command
- else if (action == "append")
- {
- if (token_count < 3)
- {
- PutModule("Usage: append <option> <value>");
- return;
- }
- CString option = tokens[1].AsLower();
- CString value = command.Token(2, true, " ");
- MCString::iterator pos = options.find(option);
- if (pos == options.end())
- {
- PutModule("Error: invalid option name");
- }
- else if (option == "service")
- {
- PutModule("Error: cannot append to this option");
- }
- else
- {
- options[option] += " " + value;
- options[option].Trim();
- SetNV(option, options[option]);
- PutModule("Ok");
- }
- }
- // PREPEND command
- else if (action == "prepend")
- {
- if (token_count < 3)
- {
- PutModule("Usage: prepend <option> <value>");
- return;
- }
- CString option = tokens[1].AsLower();
- CString value = command.Token(2, true, " ");
- MCString::iterator pos = options.find(option);
- if (pos == options.end())
- {
- PutModule("Error: invalid option name");
- }
- else if (option == "service")
- {
- PutModule("Error: cannot prepend to this option");
- }
- else
- {
- options[option] = value + " " + options[option];
- options[option].Trim();
- SetNV(option, options[option]);
- PutModule("Ok");
- }
- }
- // UNSET command
- else if (action == "unset")
- {
- if (token_count != 2)
- {
- PutModule("Usage: unset <option>");
- return;
- }
- CString option = tokens[1].AsLower();
- MCString::iterator pos = options.find(option);
- if (pos == options.end())
- {
- PutModule("Error: invalid option name");
- }
- else
- {
- options[option] = defaults[option];
- DelNV(option);
- PutModule("Ok");
- }
- }
- // GET command
- else if (action == "get")
- {
- if (token_count > 2)
- {
- PutModule("Usage: get [<option>]");
- return;
- }
- if (token_count < 2)
- {
- CTable table;
- table.AddColumn("Option");
- table.AddColumn("Value");
- for (MCString::iterator i = options.begin(); i != options.end(); i++)
- {
- table.AddRow();
- table.SetCell("Option", i->first);
- table.SetCell("Value", i->second);
- }
- PutModule(table);
- return;
- }
- CString option = tokens[1].AsLower();
- MCString::iterator pos = options.find(option);
- if (pos == options.end())
- {
- PutModule("Error: invalid option name");
- }
- else
- {
- PutModule(option + CString(": ") + options[option]);
- }
- }
- // SAVE command
- else if (action == "save")
- {
- if (token_count < 2)
- {
- PutModule("Usage: save <filepath>");
- }
- CString file_path = command.Token(1, true, " ");
- int status = options.WriteToDisk(file_path);
- if (status == MCString::MCS_SUCCESS)
- {
- PutModule("Options saved to " + file_path);
- }
- else
- {
- switch (status)
- {
- case MCString::MCS_EOPEN:
- case MCString::MCS_EWRITE:
- case MCString::MCS_EWRITEFIL:
- PutModule("Failed to save options to " + file_path);
- break;
- default:
- PutModule("Failure");
- break;
- }
- }
- }
- // LOAD command
- else if (action == "load")
- {
- if (token_count < 2)
- {
- PutModule("Usage: load <filename>");
- }
- CString file_path = command.Token(1, true, " ");
- if (!CFile::Exists(file_path))
- {
- PutModule("File does not exist: " + file_path);
- return;
- }
- int status = options.ReadFromDisk(file_path);
- if (status == MCString::MCS_SUCCESS)
- {
- PutModule("Options loaded from " + file_path);
- // Restore any defaults that aren't in the loaded dictionary,
- // and save loaded options to ZNC's data store
- for (MCString::iterator i = defaults.begin(); i != defaults.end(); i++)
- {
- CString option = i->first;
- MCString::iterator pos = options.find(option);
- if (pos == options.end())
- {
- options[option] = defaults[option];
- DelNV(option);
- }
- else
- {
- SetNV(option, options[option]);
- }
- }
- }
- else
- {
- switch (status)
- {
- case MCString::MCS_EOPEN:
- case MCString::MCS_EREADFIL:
- PutModule("Failed to read options from " + file_path);
- break;
- default:
- PutModule("Failure");
- break;
- }
- }
- }
- // STATUS command
- else if (action == "status")
- {
- CTable table;
- table.AddColumn("Condition");
- table.AddColumn("Status");
- table.AddRow();
- table.SetCell("Condition", "away");
- table.SetCell("Status", GetNetwork()->IsIRCAway() ? "yes" : "no");
- table.AddRow();
- table.SetCell("Condition", "client_count");
- table.SetCell("Status", CString(client_count()));
- time_t now = time(NULL);
- time_t ago = now - idle_time;
- table.AddRow();
- table.SetCell("Condition", "idle");
- table.SetCell("Status", CString(ago) + " seconds");
- table.AddRow();
- table.SetCell("Condition", "network_blacklist");
- // network_blacklist() is True if the network is not in a blacklist
- table.SetCell("Status", network_blacklist() ? "no" : "yes");
- if (token_count > 1)
- {
- CString context = tokens[1];
- table.AddRow();
- table.SetCell("Condition", "last_active");
- if (last_active_time.count(context) < 1)
- {
- table.SetCell("Status", "n/a");
- }
- else
- {
- ago = now - last_active_time[context];
- table.SetCell("Status", CString(ago) + " seconds");
- }
- table.AddRow();
- table.SetCell("Condition", "last_notification");
- if (last_notification_time.count(context) < 1)
- {
- table.SetCell("Status", "n/a");
- }
- else
- {
- ago = now - last_notification_time[context];
- table.SetCell("Status", CString(ago) + " seconds");
- }
- table.AddRow();
- table.SetCell("Condition", "replied");
- table.SetCell("Status", replied(context) ? "yes" : "no");
- }
- PutModule(table);
- }
- // SUBSCRIBE command
- else if (action == "subscribe")
- {
- // Set up the connection profile
- CString service = options["service"];
- bool use_post = true;
- int use_port = 443;
- bool use_ssl = true;
- CString service_host;
- CString service_url;
- CString service_auth;
- MCString params;
- if (service == "boxcar")
- {
- if (options["username"] == "")
- {
- PutModule("Error: username not set");
- return;
- }
- CString boxcar_api_key = "puSd2qp2gCDZO7nWkvb9";
- service_host = "boxcar.io";
- service_url = "/devices/providers/" + boxcar_api_key + "/notifications/subscribe";
- params["email"] = options["username"];
- }
- else if (service == "airgram")
- {
- PutModule("Error: Airgram service shut down. Please configure a different notification provider.");
- }
- else
- {
- PutModule("Error: service does not support subscribe command");
- return;
- }
- #ifdef USE_CURL
- long http_code = make_curl_request(service_host, service_url, service_auth, params, use_port, use_ssl, use_post, options["proxy"], options["proxy_ssl_verify"] != "no", options["debug"] == "on");
- PutDebug("curl: HTTP status code " + CString(http_code));
- if (!(http_code >= 200 && http_code < 300)) {
- PutModule("Error: HTTP status code " + CString(http_code));
- }
- #else
- // Create the socket connection, write to it, and add it to the queue
- CPushSocket *sock = new CPushSocket(this);
- sock->Connect(service_host, use_port, use_ssl);
- sock->Request(use_post, service_host, service_url, params, service_auth);
- AddSocket(sock);
- #endif
- PutModule("Ok");
- }
- // SEND command
- else if (action == "send")
- {
- CString message = command.Token(1, true, " ", true);
- send_message(message);
- PutModule("Ok");
- }
- // HELP command
- else if (action == "help")
- {
- PutModule("View the detailed documentation at https://github.com/jreese/znc-push/blob/master/README.md");
- }
- // VERSION command
- else if (action == "version")
- {
- PutModule("znc-push " + CString(PUSHVERSION));
- }
- // EVAL command
- else if (action == "eval")
- {
- CString value = command.Token(1, true, " ");
- PutModule(eval(value) ? "true" : "false");
- }
- else
- {
- PutModule("Error: invalid command, try `help`");
- }
- }
- };
- /**
- * Build a query string from a dictionary of request parameters.
- *
- * @param params Request parameters
- * @return query string
- */
- CString build_query_string(MCString& params)
- {
- bool more = false;
- CString query;
- CString key;
- CString value;
- for (MCString::iterator param = params.begin(); param != params.end(); param++)
- {
- key = urlencode(param->first);
- value = urlencode(param->second);
- if (more)
- {
- query += "&" + key + "=" + value;
- }
- else
- {
- query += key + "=" + value;
- more = true;
- }
- }
- return query;
- }
- #ifdef USE_CURL
- /**
- * Send an HTTP request using libcurl.
- *
- * @param service_host Host domain
- * @param service_url Host path
- * @param service_auth Basic auth string
- * @param params Request parameters
- * @param port Port number
- * @param use_ssl Use SSL
- * @param use_post Use POST method
- */
- long make_curl_request(const CString& service_host, const CString& service_url,
- const CString& service_auth, MCString& params, int port,
- bool use_ssl, bool use_post,
- const CString& proxy, bool proxy_ssl_verify,
- bool debug)
- {
- CURL *curl;
- CURLcode result;
- long http_code;
- curl = curl_easy_init();
- CString user_agent = "ZNC Push/" + CString(PUSHVERSION);
- CString url = CString(use_ssl ? "https" : "http") + "://" + service_host + service_url;
- CString query = build_query_string(params);
- if (!query.empty())
- {
- url = url + "?" + query;
- }
- if (debug)
- {
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- }
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
- curl_easy_setopt(curl, CURLOPT_URL, url.data());
- curl_easy_setopt(curl, CURLOPT_PORT, port);
- curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent.c_str());
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); // three seconds ought to be good enough for anyone, eh?
- if (service_auth != "")
- {
- curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_easy_setopt(curl, CURLOPT_USERPWD, service_auth.data());
- }
- if (use_post)
- {
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query.data());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, query.length());
- }
- if (proxy != "") {
- curl_easy_setopt(curl, CURLOPT_PROXY, proxy.c_str());
- if (!proxy_ssl_verify) {
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- }
- }
- result = curl_easy_perform(curl);
- if (result != CURLE_OK) {
- curl_easy_cleanup(curl);
- return -1;
- }
- curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
- curl_easy_cleanup(curl);
- return http_code;
- }
- #else
- /**
- * Send an HTTP request.
- *
- * @param post POST command
- * @param host Host domain
- * @param url Resource path
- * @param parameters Query parameters
- * @param auth Basic authentication string
- */
- void CPushSocket::Request(bool post, const CString& host, const CString& url, MCString& parameters, const CString& auth)
- {
- parent->PutDebug("Building notification to " + host + url + "...");
- CString query = build_query_string(parameters);
- // Request headers and POST body
- CString request;
- if (post)
- {
- request += "POST " + url + " HTTP/1.1" + crlf;
- request += "Content-Type: application/x-www-form-urlencoded" + crlf;
- request += "Content-Length: " + CString(query.length()) + crlf;
- }
- else
- {
- request += "GET " + url + "?" + query + " HTTP/1.1" + crlf;
- }
- request += "Host: " + host + crlf;
- request += "Connection: close" + crlf;
- request += "User-Agent: " + user_agent + crlf;
- parent->PutDebug("User-Agent: " + user_agent);
- if (auth != "")
- {
- CString auth_b64 = auth.Base64Encode_n();
- request += "Authorization: Basic " + auth_b64 + crlf;
- parent->PutDebug("Authorization: Basic " + auth_b64);
- }
- request += crlf;
- if (post)
- {
- request += query;
- }
- parent->PutDebug("Query string: " + query);
- Write(request);
- parent->PutDebug("Request sending");
- }
- /**
- * Read each line of data returned from the HTTP request.
- */
- void CPushSocket::ReadLine(const CString& data)
- {
- if (first)
- {
- CString status = data.Token(1);
- CString message = data.Token(2, true);
- parent->PutDebug("Status: " + status);
- parent->PutDebug("Message: " + message);
- first = false;
- }
- else
- {
- parent->PutDebug("Data: " + data);
- }
- }
- void CPushSocket::Disconnected()
- {
- parent->PutDebug("Disconnected.");
- Close(CSocket::CLT_AFTERWRITE);
- }
- #endif // USE_CURL
- template<> void TModInfo<CPushMod>(CModInfo& Info) {
- Info.AddType(CModInfo::UserModule);
- Info.SetWikiPage("push");
- }
- NETWORKMODULEDEFS(CPushMod, "Send highlights and personal messages to a push notification service")
|