big update
too much changed ._.
This commit is contained in:
63
bot/bot.lua
63
bot/bot.lua
@@ -1,3 +1,7 @@
|
||||
package.path = package.path .. ';.luarocks/share/lua/5.2/?.lua'
|
||||
..';.luarocks/share/lua/5.2/?/init.lua'
|
||||
package.cpath = package.cpath .. ';.luarocks/lib/lua/5.2/?.so'
|
||||
|
||||
require("./bot/utils")
|
||||
|
||||
VERSION = '2.5-reloaded'
|
||||
@@ -12,10 +16,13 @@ function on_msg_receive (msg)
|
||||
local receiver = get_receiver(msg)
|
||||
|
||||
-- vardump(msg)
|
||||
msg = pre_process_service_msg(msg)
|
||||
if msg_valid(msg) then
|
||||
msg = pre_process_msg(msg)
|
||||
match_plugins(msg)
|
||||
--mark_read(receiver, ok_cb, false)
|
||||
if msg then
|
||||
match_plugins(msg)
|
||||
--mark_read(receiver, ok_cb, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,7 +32,7 @@ end
|
||||
function on_binlog_replay_end()
|
||||
started = true
|
||||
postpone (cron_plugins, false, 60*5.0)
|
||||
-- See plugins/ping.lua as an example for cron
|
||||
-- See plugins/isup.lua as an example for cron
|
||||
|
||||
_config = load_config()
|
||||
cred_data = load_cred()
|
||||
@@ -36,7 +43,7 @@ function on_binlog_replay_end()
|
||||
end
|
||||
|
||||
function msg_valid(msg)
|
||||
-- Dont process outgoing messages
|
||||
-- Don't process outgoing messages
|
||||
if msg.out then
|
||||
print('\27[36mNicht gültig: Nachricht von mir\27[39m')
|
||||
return false
|
||||
@@ -53,11 +60,6 @@ function msg_valid(msg)
|
||||
return false
|
||||
end
|
||||
|
||||
if msg.service then
|
||||
print('\27[36mNicht gültig: Service\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
if not msg.to.id then
|
||||
print('\27[36mNicht gültig: To id not provided\27[39m')
|
||||
return false
|
||||
@@ -68,13 +70,48 @@ function msg_valid(msg)
|
||||
return false
|
||||
end
|
||||
|
||||
if msg.from.id == our_id then
|
||||
print('\27[36mNicht gültig: Nachricht von unserer ID\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
if msg.to.type == 'encr_chat' then
|
||||
print('\27[36mNicht gültig: Encrypted Chat\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
if msg.from.id == 777000 then
|
||||
print('\27[36mNicht gültig: Telegram Nachricht\27[39m')
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
--
|
||||
function pre_process_service_msg(msg)
|
||||
if msg.service then
|
||||
local action = msg.action or {type=""}
|
||||
-- Double / to discriminate of normal actions
|
||||
msg.text = "//tgservice " .. action.type
|
||||
msg.realservice = true
|
||||
|
||||
-- wipe the data to allow the bot to read service messages
|
||||
if msg.out then
|
||||
msg.out = false
|
||||
end
|
||||
if msg.from.id == our_id then
|
||||
msg.from.id = 0
|
||||
end
|
||||
end
|
||||
return msg
|
||||
end
|
||||
|
||||
|
||||
-- Apply plugin.pre_process function
|
||||
function pre_process_msg(msg)
|
||||
for name,plugin in pairs(plugins) do
|
||||
if plugin.pre_process then
|
||||
if plugin.pre_process and msg then
|
||||
msg = plugin.pre_process(msg)
|
||||
end
|
||||
end
|
||||
@@ -110,7 +147,7 @@ end
|
||||
function match_plugin(plugin, plugin_name, msg)
|
||||
local receiver = get_receiver(msg)
|
||||
|
||||
-- Go over patterns. If one matches is enough.
|
||||
-- Go over patterns. If one matches it's enough.
|
||||
for k, pattern in pairs(plugin.patterns) do
|
||||
local matches = match_pattern(pattern, msg.text)
|
||||
if matches then
|
||||
@@ -154,7 +191,7 @@ function save_config( )
|
||||
end
|
||||
|
||||
-- Returns the config from config.lua file.
|
||||
-- If file doesnt exists, create it.
|
||||
-- If file doesn't exist, create it.
|
||||
function load_config( )
|
||||
local f = io.open('./data/config.lua', "r")
|
||||
-- If config.lua doesnt exists
|
||||
@@ -186,7 +223,7 @@ end
|
||||
|
||||
-- Create a basic config.json file and saves it.
|
||||
function create_config( )
|
||||
-- A simple config with basic plugins and ourserves as priviled user
|
||||
-- A simple config with basic plugins and ourselves as privileged user
|
||||
config = {
|
||||
enabled_plugins = {
|
||||
"help",
|
||||
|
||||
@@ -5,6 +5,7 @@ URL = require "socket.url"
|
||||
json = (loadfile "./libs/JSON.lua")()
|
||||
serpent = (loadfile "./libs/serpent.lua")()
|
||||
mimetype = (loadfile "./libs/mimetype.lua")()
|
||||
redis = (loadfile "./libs/redis.lua")()
|
||||
|
||||
http.TIMEOUT = 10
|
||||
|
||||
@@ -55,9 +56,9 @@ function string:trim()
|
||||
end
|
||||
|
||||
function get_http_file_name(url, headers)
|
||||
-- Eg: fooo.var
|
||||
-- Eg: foo.var
|
||||
local file_name = url:match("[^%w]+([%.%w]+)$")
|
||||
-- Any delimited aphanumeric on the url
|
||||
-- Any delimited alphanumeric on the url
|
||||
file_name = file_name or url:match("[^%w]+(%w+)[^%w]+$")
|
||||
-- Random name, hope content-type works
|
||||
file_name = file_name or str:random(5)
|
||||
@@ -105,7 +106,7 @@ function download_to_file(url, file_name)
|
||||
|
||||
file_name = file_name or get_http_file_name(url, headers)
|
||||
|
||||
local file_path = "/home/pi/Mikubot/tmp/"..file_name
|
||||
local file_path = "tmp/"..file_name
|
||||
print("Gespeichert in: "..file_path)
|
||||
|
||||
file = io.open(file_path, "w+")
|
||||
@@ -138,7 +139,7 @@ function run_command(str)
|
||||
return result
|
||||
end
|
||||
|
||||
-- User has priviledges
|
||||
-- User has privileges
|
||||
function is_sudo(msg)
|
||||
local var = false
|
||||
-- Check users id in config
|
||||
@@ -210,12 +211,12 @@ function serialize_to_file(data, file, uglify)
|
||||
file:close()
|
||||
end
|
||||
|
||||
-- Retruns true if the string is empty
|
||||
-- Returns true if the string is empty
|
||||
function string:isempty()
|
||||
return self == nil or self == ''
|
||||
end
|
||||
|
||||
-- Retruns true if the string is blank
|
||||
-- Returns true if the string is blank
|
||||
function string:isblank()
|
||||
self = self:trim()
|
||||
return self:isempty()
|
||||
@@ -276,7 +277,7 @@ function send_photo_from_url_callback(cb_extra, success, result)
|
||||
end
|
||||
end
|
||||
|
||||
-- Send multimple images asynchronous.
|
||||
-- Send multiple images asynchronous.
|
||||
-- param urls must be a table.
|
||||
function send_photos_from_url(receiver, urls)
|
||||
local cb_extra = {
|
||||
@@ -288,7 +289,7 @@ function send_photos_from_url(receiver, urls)
|
||||
end
|
||||
|
||||
-- Use send_photos_from_url.
|
||||
-- This fuction might be difficult to understand.
|
||||
-- This function might be difficult to understand.
|
||||
function send_photos_from_url_callback(cb_extra, success, result)
|
||||
-- cb_extra is a table containing receiver, urls and remove_path
|
||||
local receiver = cb_extra.receiver
|
||||
@@ -330,7 +331,7 @@ function rmtmp_cb(cb_extra, success, result)
|
||||
os.remove(file_path)
|
||||
print(file_path.." gelöscht!")
|
||||
end
|
||||
-- Finaly call the callback
|
||||
-- Finally call the callback
|
||||
cb_function(cb_extra, success, result)
|
||||
end
|
||||
|
||||
@@ -394,7 +395,7 @@ function user_allowed(plugin, msg)
|
||||
return true
|
||||
end
|
||||
|
||||
-- Same as send_large_msg_callback but frienly params
|
||||
-- Same as send_large_msg_callback but friendly params
|
||||
function send_large_msg(destination, text)
|
||||
local cb_extra = {
|
||||
destination = destination,
|
||||
@@ -450,16 +451,24 @@ function sleep(n)
|
||||
end
|
||||
|
||||
-- Function to read data from files
|
||||
function load_from_file(file)
|
||||
function load_from_file(file, default_data)
|
||||
local f = io.open(file, "r+")
|
||||
-- If file doesn't exists
|
||||
if f == nil then
|
||||
-- Create a new empty table
|
||||
serialize_to_file({}, file)
|
||||
default_data = default_data or {}
|
||||
serialize_to_file(default_data, file)
|
||||
print ('Erstelle Datei', file)
|
||||
else
|
||||
print ('Daten geladen von', file)
|
||||
f:close()
|
||||
end
|
||||
return loadfile (file)()
|
||||
end
|
||||
end
|
||||
|
||||
function run_bash(str)
|
||||
local cmd = io.popen(str)
|
||||
local result = cmd:read('*all')
|
||||
cmd:close()
|
||||
return result
|
||||
end
|
||||
Reference in New Issue
Block a user