--[[ --** -------------------------------------------------------------------------- --** /etc/telegram-cli/tg-cmd-exec.sh - exec script --** --** Copyright (c) 2015 Lukas Rose 2015 --** Copyright (c) 2018-2018 The Eisfair Team, team(at)eisfair(dot)org --** --** Creation: 2018-03-23 jed --** Last Update: $Id$ --** --** This program is free software; you can redistribute it and/or modify --** it under the terms of the GNU General Public License as published by --** the Free Software Foundation; either version 2 of the License, or --** (at your option) any later version. --** -------------------------------------------------------------------------- --]] our_id = 0 -- our id: 12345678 receive_active = 0 -- load the string reader library file, which provides the following functions: -- - check(file, string, separator) -- - trim(s) dofile("/etc/telegram-cli/tg-check-string-in-file.lua") ------------------------------------------------------------------------------- -- vardump - dump the content of a variable, independent from its type ------------------------------------------------------------------------------- function vardump(value, depth, key) local linePrefix = "" local spaces = "" if key ~= nil then linePrefix = "["..key.."] = " end if depth == nil then depth = 0 else depth = depth + 1 for i=1, depth do spaces = spaces .. " " end end if type(value) == 'table' then mTable = getmetatable(value) if mTable == nil then print(spaces ..linePrefix.."(table) ") else print(spaces .."(metatable) ") value = mTable end for tableKey, tableValue in pairs(value) do vardump(tableValue, depth, tableKey) end elseif type(value) == 'function' or type(value) == 'thread' or type(value) == 'userdata' or value == nil then print(spaces..tostring(value)) else print(spaces..linePrefix.."("..type(value)..") "..tostring(value)) end end ------------------------------------------------------------------------------- -- on_binlog_replay_end - signal end of message buffer processing ------------------------------------------------------------------------------- function on_binlog_replay_end() print("**BINLOG-REPLAY-END**") receive_active = 1 end ------------------------------------------------------------------------------- -- on_our_id - get our own peer id ------------------------------------------------------------------------------- function on_our_id(id) print("**OUR-ID**") --vardump(id) our_id = id end ------------------------------------------------------------------------------- -- ok_cb - purpose unknown?! ------------------------------------------------------------------------------- function ok_cb(extra, success, result) --print("**OK-CB**") --vardump(extra) --vardump(success) --vardump(result) end ------------------------------------------------------------------------------- -- on_msg_receive - what should be done if a message is received -- -- msg - message table ------------------------------------------------------------------------------- function on_msg_receive(msg) print("**MSG_RECEIVE**") if (receive_active == 1) then -- this code will only be executed for new messages local msg_text = '' local msg_ok = 0 local cmd = '' local cmd_switch = '' local pos = 0 --[[ *** structure of 'msg' table *** (table) [to] = (table) [flags] = (number) 123456 [access_hash] = (number) 1 [peer_type] = (string) user [phone] = (string) 4917299887766 [peer_id] = (number) 123456789 [print_name] = (string) My Messages [first_name] = (string) My Messages [id] = (string) $010000000abcdef01234567890abcdef [date] = (number) 1522574948 [unread] = (boolean) true [flags] = (number) 257 [out] = (boolean) false [text] = (string) Hello [service] = (boolean) false [from] = (table) [last_name] = (string) Test [peer_type] = (string) user [id] = (string) $010000000abcdef09876543210abcdef [flags] = (number) 123456 [access_hash] = (number) 1 [username] = (string) T_TEST [peer_id] = (number) 166947998 [print_name] = (string) Theo_Test [phone] = (string) 4917211223344 [first_name] = (string) Theo [id] = (string) 01000006543210abcdef00000000006543210abcdef ]] --vardump(msg) if (started == 0) then return end if (msg.out) then return end if (msg.text) then mark_read(msg.from.print_name, ok_cb, false) end -- only accept messages where from and to are correctly set if (msg.from ~= nil) then -- only accept commands if from address is set if (msg.text) and (msg.from.peer_type == 'user') then -- msg.text not empty, split it into words ... --[[ *** structure of 'msg_words' table *** (table) [1] = (string) This [2] = (string) is [3] = (string) a [4] = (string) test ]] local msg_words = split(msg.text) --vardump(msg_words) -- extract command if (msg_words[0] ~= nil) then cmd = string.lower(msg_words[0]) else cmd = string.lower(msg_words[1]) end -- check for specific flags -- '+' - return full install result although -- the installation succeeded pos = string.find(cmd, '%+') if (pos ~= nil) then cmd = string.sub(cmd, 1, pos - 1) cmd_switch = "--showresult" end if (tostring(cmd) == 'show-my-sender-details') then -- show sender details to configure tg-cmd-list.txt -- command can be sent by everyone print("INFO: show sender details.") msg_text = "user: " .. msg.from.username .. "\nname: " .. msg.from.print_name .. "\nphone: " .. msg.from.phone .. "\nid: " .. msg.from.id msg_ok = 1 else -- only accept commands from known sender if (check("/etc/telegram-cli/tg-cmd-users.txt", tostring(msg.from.id), '!') == true) then -- known sender -- only accept pre-defined commands if (check("/etc/telegram-cli/tg-cmd-list.txt", tostring(cmd), '!') == true) then -- known command, call external script print("INFO: execute script with command '" .. cmd .. "' ' and parameter '" .. msg.text .. "'") -- send_msg(msg.from.print_name, "executing command ...", ok_cb, false) local handle = io.popen("/etc/telegram-cli/tg-cmd-exec.sh " .. cmd_switch .. " '" .. cmd .. "' '" .. msg.text .. "'" ) local res = handle:read("*a") handle:close() -- build message text msg_text = trim(res) msg_ok = 1 else -- command not defined print("ERROR: command '" .. cmd .. "' unknown!") end else -- invalid sender print("ERROR: sender '" .. tostring(msg.from.id) .. "' unknown!") end end if (msg_ok == 1) then print("INFO: msg receiver: '" .. msg.from.print_name .. "'") print("INFO: msg text : '" .. msg_text .. "'") --os.execute("/usr/bin/send-telegram-message.sh --peer '" .. msg.from.print_name .. "' --msg '" .. msg_text .. "'") send_msg(msg.from.print_name, msg_text, ok_cb, false) else print("INFO: no message sent!") end return end --else -- from not set, secret chat? end end -- the following code will be executed for messages -- which telegram has been missed and all new messages end ------------------------------------------------------------------------------- -- on_user_update ------------------------------------------------------------------------------- function on_user_update(user, what) print("**USER-UPDATE**") --[[ *** structure of 'user' table *** (table) [flags] = (number) 123456 [access_hash] = (number) 1 [peer_type] = (string) user [phone] = (string) 4917299887766 [peer_id] = (number) 1234567890 [print_name] = (string) My Messages [first_name] = (string) My Messages [id] = (string) 01000006543210abcdef0000000000654 ]] --vardump(user) end ------------------------------------------------------------------------------- -- on_chat_update ------------------------------------------------------------------------------- function on_chat_update(chat, what) print("**CHAT-UPDATE**") --[[ *** structure of 'chat' table *** (table) [flags] = (number) 65 [peer_type] = (string) chat [members_num] = (number) 2 [peer_id] = (number) 12345678 [print_name] = (string) My Messages [title] = (string) My Messages [id] = (string) $01000006543210abcdef000000000065 ]] --vardump(chat) end ------------------------------------------------------------------------------- -- on_secret_chat_update ------------------------------------------------------------------------------- function on_secret_chat_update(schat, what) print("**SECRET-CHAT-UPDATE**") --vardump(schat) end