From c7ae3e39563d1ef65c7ebf28bbbbaf2b2a33dedd Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Tue, 5 Jun 2018 00:01:48 +0200 Subject: [PATCH] Created module for sending telegram.org messages --- include/Utils.h | 1 + scripts/lua/modules/telegram.lua | 52 ++++++++++++++++++++++++++++++++ src/Lua.cpp | 26 +++++++++++++++- src/Utils.cpp | 51 +++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 scripts/lua/modules/telegram.lua diff --git a/include/Utils.h b/include/Utils.h index d7b066aa10..26bf0756b6 100755 --- a/include/Utils.h +++ b/include/Utils.h @@ -66,6 +66,7 @@ class Utils { static void purifyHTTPparam(char *param, bool strict, bool allowURL, bool allowDots); static char* stripHTML(const char * const str); static bool postHTTPJsonData(char *username, char *password, char *url, char *json, HTTPTranferStats *stats); + static bool postHTTPForm(char *username, char *password, char *url, char *form_data); static bool sendMail(char *from, char *to, char *message, char *smtp_server); static bool postHTTPTextFile(char *username, char *password, char *url, char *path, HTTPTranferStats *stats); static bool httpGet(lua_State* vm, char *url, char *username, diff --git a/scripts/lua/modules/telegram.lua b/scripts/lua/modules/telegram.lua new file mode 100644 index 0000000000..67f9264853 --- /dev/null +++ b/scripts/lua/modules/telegram.lua @@ -0,0 +1,52 @@ +-- +-- (C) 2018 - ntop.org +-- + +dirs = ntop.getDirs() +package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path + +local telegram = {} + +-- ######################################################## + +local char_to_hex = function(c) + return string.format("%%%02X", string.byte(c)) +end + +local function urlencode(url) + if url == nil then + return + end + url = url:gsub("\n", "\r\n") + url = url:gsub("([^%w ])", char_to_hex) + url = url:gsub(" ", "+") + return url +end + +-- ######################################################## +-- # +-- # Steps +-- # +-- # 1. Create a new bot +-- # - connect to @BotFather +-- # - type /newbot +-- # - Write down the bot_token_id +-- # - Optional "turn on privacy" +-- # +-- # 2. Get the chat_id +-- # - Do curl -s -X POST https://api.telegram.org/bot/getUpdates +-- # and you will the chat id in the JSON response code +-- # as ..."chat":{"id":XXXXXXXX, +-- # +-- ######################################################## + +function telegram.sendMessage(bot_token_id, chat_id, message) + local url = "https://api.telegram.org/bot"..bot_token_id.."/sendMessage" + local postfields = "chat_id="..chat_id.."&text="..urlencode(message) + + ntop.postHTTPform("", "", url, postfields) +end + +-- ######################################################## + +return telegram diff --git a/src/Lua.cpp b/src/Lua.cpp index f02c0f8e5e..36be106e15 100644 --- a/src/Lua.cpp +++ b/src/Lua.cpp @@ -4564,6 +4564,29 @@ static int ntop_post_http_json_data(lua_State* vm) { /* ****************************************** */ +static int ntop_post_http_form(lua_State* vm) { + char *username, *password, *url, *form_data; + + if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING) != CONST_LUA_OK) return(CONST_LUA_PARAM_ERROR); + if((username = (char*)lua_tostring(vm, 1)) == NULL) return(CONST_LUA_PARAM_ERROR); + + if(ntop_lua_check(vm, __FUNCTION__, 2, LUA_TSTRING) != CONST_LUA_OK) return(CONST_LUA_PARAM_ERROR); + if((password = (char*)lua_tostring(vm, 2)) == NULL) return(CONST_LUA_PARAM_ERROR); + + if(ntop_lua_check(vm, __FUNCTION__, 3, LUA_TSTRING) != CONST_LUA_OK) return(CONST_LUA_PARAM_ERROR); + if((url = (char*)lua_tostring(vm, 3)) == NULL) return(CONST_LUA_PARAM_ERROR); + + if(ntop_lua_check(vm, __FUNCTION__, 4, LUA_TSTRING) != CONST_LUA_OK) return(CONST_LUA_PARAM_ERROR); + if((form_data = (char*)lua_tostring(vm, 4)) == NULL) return(CONST_LUA_PARAM_ERROR); + + bool rv = Utils::postHTTPForm(username, password, url, form_data); + + lua_pushboolean(vm, rv); + return(CONST_LUA_OK); +} + +/* ****************************************** */ + static int ntop_post_http_text_file(lua_State* vm) { char *username, *password, *url, *path; bool delete_file_after_post = false; @@ -7426,8 +7449,9 @@ static const luaL_Reg ntop_reg[] = { /* HTTP */ { "postHTTPJsonData", ntop_post_http_json_data }, + { "postHTTPform", ntop_post_http_form }, { "postHTTPTextFile", ntop_post_http_text_file }, - + #ifdef HAVE_CURL_SMTP /* SMTP */ { "sendMail", ntop_send_mail }, diff --git a/src/Utils.cpp b/src/Utils.cpp index c2fd9124ae..f5bf8441c6 100755 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -1067,6 +1067,57 @@ static void readCurlStats(CURL *curl, HTTPTranferStats *stats, lua_State* vm) { /* **************************************** */ +/* form_data is in format param=value¶m1=&value1... */ +bool Utils::postHTTPForm(char *username, char *password, char *url, char *form_data) { + CURL *curl; + bool ret = true; + + curl = curl_easy_init(); + if(curl) { + CURLcode res; + + curl_easy_setopt(curl, CURLOPT_URL, url); + + if((username && (username[0] != '\0')) + || (password && (password[0] != '\0'))) { + char auth[64]; + + snprintf(auth, sizeof(auth), "%s:%s", + username ? username : "", + password ? password : ""); + curl_easy_setopt(curl, CURLOPT_USERPWD, auth); + curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC); + } + + if(!strncmp(url, "https", 5)) { + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); + } + + curl_easy_setopt(curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, form_data); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(form_data)); + + res = curl_easy_perform(curl); + + if(res != CURLE_OK) { + ntop->getTrace()->traceEvent(TRACE_WARNING, + "Unable to post data to (%s): %s", + url, curl_easy_strerror(res)); + ret = false; + } else { + ntop->getTrace()->traceEvent(TRACE_INFO, "Posted data to %s", url); + } + + /* always cleanup */ + curl_easy_cleanup(curl); + } + + return(ret); +} + +/* **************************************** */ + bool Utils::postHTTPJsonData(char *username, char *password, char *url, char *json, HTTPTranferStats *stats) { CURL *curl;