Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
931d740
Cats
giuseppeM99 Feb 4, 2017
5205823
Fixed config generator
giuseppeM99 Feb 4, 2017
596420b
Merge from @lucentw master
giuseppeM99 Feb 20, 2017
dbab8a0
Merge pull request #2 from giuseppeM99/pr/1
giuseppeM99 Feb 20, 2017
85237e3
Improved stats
giuseppeM99 Mar 2, 2017
94dacf9
Preventing bot flood
giuseppeM99 Apr 7, 2017
c2893ef
Plugins updates. Moving to madeline
giuseppeM99 Jul 8, 2017
98b6773
Real updates
giuseppeM99 Jul 8, 2017
4aba245
Oops
giuseppeM99 Jul 8, 2017
5b01b6e
Don't need this anymore
giuseppeM99 Jul 8, 2017
a3d92a7
Merge
giuseppeM99 Jul 8, 2017
85ef280
Merge branch 'master' of https://github.com/lucentw/s-uzzbot
giuseppeM99 Jul 8, 2017
8f0434e
Merge github.com:giuseppeM99/s-uzzbot into test-madeline
giuseppeM99 Jul 8, 2017
5b381d6
Fixes and styel fixes
giuseppeM99 Jul 8, 2017
d95f4b3
Making plugins work again
giuseppeM99 Jul 10, 2017
d9a8b24
That shouldn't be there
giuseppeM99 Jul 10, 2017
ed595f6
Add basic test suite
CapacitorSet Aug 21, 2017
9556f61
Fix incorrect replacement in earlier commit
CapacitorSet Aug 21, 2017
7dfa644
Implement custom logger
CapacitorSet Aug 21, 2017
ce60157
Add test/, whoops.
CapacitorSet Aug 21, 2017
bce595b
Make utils more readable (early return)
CapacitorSet Aug 22, 2017
e86d503
Format anti-flood plugin, use early return
CapacitorSet Aug 22, 2017
de440ec
Make anti-flood compatible with FakeRedis
CapacitorSet Aug 22, 2017
b9356f5
Make bot.lua more readable (early return)
CapacitorSet Aug 22, 2017
0d4174f
Add basic test suite for antiflood
CapacitorSet Aug 22, 2017
77e18de
Remove useless messages in tests
CapacitorSet Aug 22, 2017
9a8e2cc
Reduce duplication in tests
CapacitorSet Aug 22, 2017
ec72b13
Use correct log level in anti-flood
CapacitorSet Aug 22, 2017
c2d30db
Make tests require Redis
CapacitorSet Aug 22, 2017
3770a09
Fix 'warned' bug in anti-flood.lua
CapacitorSet Aug 22, 2017
06e0216
Add tests for anti-flood.lua
CapacitorSet Aug 22, 2017
8e5316f
Fix off-by-one error in anti-flood.lua
CapacitorSet Aug 22, 2017
2d390c2
Improve error message for plugins with syntax errors
CapacitorSet Aug 22, 2017
880ed63
Test that anti-flood ignores root
CapacitorSet Aug 23, 2017
f0b7023
Fix anti-flood not ignoring root
CapacitorSet Aug 23, 2017
3e3bfbd
Test antiflood addexcept (currently failing)
CapacitorSet Aug 23, 2017
cdc1ddf
Fix tests due to id/peer_id swapping
CapacitorSet Aug 23, 2017
0db1e32
Update
giuseppeM99 Aug 26, 2017
a4974a3
Fixes?
giuseppeM99 Aug 26, 2017
555d172
oauth.....
giuseppeM99 Aug 26, 2017
e453977
oauth...
giuseppeM99 Aug 26, 2017
f2e53e8
oauth....
giuseppeM99 Aug 26, 2017
4de2a6b
This works!!!!
giuseppeM99 Aug 26, 2017
0c453ef
Update submodule
giuseppeM99 Aug 27, 2017
2342c27
update update script
giuseppeM99 Aug 27, 2017
ff358e0
Update launch.sh
giuseppeM99 Aug 31, 2017
d49463e
Update submodule
giuseppeM99 Sep 7, 2017
27e1f31
Merge branch 'test-madeline' of github.com:giuseppeM99/s-uzzbot into …
giuseppeM99 Sep 7, 2017
75a817e
Merge branch 'test-madeline' of https://github.com/giuseppeM99/s-uzzb…
CapacitorSet Sep 19, 2017
44fbbfe
Merge branch 'giuseppeM99-test-madeline'
CapacitorSet Sep 19, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ luac.out
*.hex


composer.lock
*.madeline
*.madeline.lock
data/config.lua

# =========================
# Operating System Files
# =========================
Expand Down
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "tg"]
path = tg
url = https://github.com/LucentW/tg.git
[submodule "Madeline_lua_shim"]
path = Madeline_lua_shim
url = https://github.com/giuseppeM99/Madeline_lua_shim
1 change: 1 addition & 0 deletions Madeline_lua_shim
Submodule Madeline_lua_shim added at f089f1
96 changes: 62 additions & 34 deletions bot/bot.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
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'
local _VERSION_NUM = _VERSION:match('(%d%.%d)$')

package.path = package.path .. ';.luarocks/share/lua/'.._VERSION_NUM..'/?.lua'
..';.luarocks/share/lua/'.._VERSION_NUM..'/?/init.lua'
package.cpath = package.cpath .. ';.luarocks/lib/lua/'.._VERSION_NUM..'/?.so'

require("./bot/utils")
require("./bot/emoji")

VERSION = '0.2'
VERSION = '0.3'

-- Let's leave space for backwards-compatible new levels
LOGLEVEL_DEBUG = 0
LOGLEVEL_INFO = 10
LOGLEVEL_WARN = 20
LOGLEVEL_ERROR = 30
if (not loglevel) then loglevel = LOGLEVEL_INFO end
function log(level, message)
if (level >= loglevel) then print(message) end
end

-- This function is called when tg receive a msg
-- This function is called when tg receives a msg
-- Returns false if the message was ignored, true otherwise
function on_msg_receive (msg)
if not started then
log(LOGLEVEL_DEBUG, "not started")
return
end

Expand All @@ -23,7 +37,7 @@ function on_msg_receive (msg)

-- vardump(msg)
msg = pre_process_service_msg(msg)

if msg_valid(msg) then
msg = pre_process_msg(msg)
if msg then
Expand All @@ -38,8 +52,13 @@ function on_msg_receive (msg)
-- as read for everybody.
-- mark_read(receiver, ok_cb, false)

end
if not (not whitelistmod or (whitelistmod and is_momod(msg))) then
log(LOGLEVEL_INFO, 'Message ignored -- '..chat_id..' has modonly wl enabled')
return false
end

match_plugins(msg)
return true
end

function ok_cb(extra, success, result)
Expand All @@ -51,53 +70,56 @@ function on_binlog_replay_end()

-- See plugins/isup.lua as an example for cron

_config = load_config()
-- You can optionally pass a pre-existing configuration. Used in tests.
if (not _config) then
_config = load_config()
end

-- load plugins
plugins = {}
load_plugins()
return load_plugins()
end

function msg_valid(msg)
-- Don't process outgoing messages
if msg.out then
print('\27[36mNot valid: msg from us\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: msg from us\27[39m')
return false
end

-- Before bot was started
if msg.date < now then
print('\27[36mNot valid: old msg\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: old msg\27[39m')
return false
end

if msg.unread == 0 then
print('\27[36mNot valid: read\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: read\27[39m')
return false
end

if not msg.to.id then
print('\27[36mNot valid: To id not provided\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: To id not provided\27[39m')
return false
end

if not msg.from.id then
print('\27[36mNot valid: From id not provided\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: From id not provided\27[39m')
return false
end

if msg.from.id == our_id then
print('\27[36mNot valid: Msg from our id\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: Msg from our id\27[39m')
return false
end

if msg.to.type == 'encr_chat' then
print('\27[36mNot valid: Encrypted chat\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: Encrypted chat\27[39m')
return false
end

if msg.from.id == 777000 then
print('\27[36mNot valid: Telegram message\27[39m')
log(LOGLEVEL_INFO, '\27[36mNot valid: Telegram message\27[39m')
return false
end

Expand Down Expand Up @@ -126,7 +148,7 @@ end
function pre_process_msg(msg)
for name,plugin in pairs(plugins) do
if plugin.pre_process and msg then
print('Preprocess', name)
log(LOGLEVEL_INFO, 'Preprocess ' .. name)
msg = plugin.pre_process(msg)
end
end
Expand All @@ -150,10 +172,10 @@ local function is_plugin_disabled_on_chat(plugin_name, receiver)
for disabled_plugin,disabled in pairs(disabled_chats[receiver]) do
if disabled_plugin == plugin_name and disabled then
if plugins[disabled_plugin].hidden then
print('Plugin '..disabled_plugin..' is disabled on this chat')
log(LOGLEVEL_INFO, 'Plugin '..disabled_plugin..' is disabled on this chat')
else
local warning = 'Plugin '..disabled_plugin..' is disabled on this chat'
print(warning)
log(LOGLEVEL_INFO, warning)
send_msg(receiver, warning, ok_cb, false)
end
return true
Expand All @@ -180,14 +202,14 @@ function match_plugin(plugin, plugin_name, msg)
for k, pattern in pairs(plugin.patterns) do
local matches = match_pattern(pattern, msg.text)
if matches then
print("msg matches: ", pattern)
log(LOGLEVEL_INFO, "msg matches: " .. pattern)

if not is_sudo(msg) then
if is_plugin_disabled_on_chat(plugin_name, receiver) then
return nil
goto continue
end
if plugin.nsfw and is_nsfw_disabled_on_chat(receiver) then
return nil
goto continue
end
end

Expand All @@ -202,8 +224,9 @@ function match_plugin(plugin, plugin_name, msg)
end
end
-- One patterns matches
return
goto continue
end
::continue::
end
end

Expand All @@ -215,7 +238,7 @@ end
-- Save the content of _config to config.lua
function save_config( )
serialize_to_file(_config, './data/config.lua')
print ('saved config into ./data/config.lua')
log(LOGLEVEL_INFO, 'saved config into ./data/config.lua')
end

-- Returns the config from config.lua file.
Expand All @@ -224,14 +247,14 @@ function load_config( )
local f = io.open('./data/config.lua', "r")
-- If config.lua doesn't exist
if not f then
print ("Created new config file: data/config.lua")
log(LOGLEVEL_INFO, "Created new config file: data/config.lua")
create_config()
else
f:close()
end
local config = loadfile ("./data/config.lua")()
for v,user in pairs(config.sudo_users) do
print("Allowed user: " .. user)
log(LOGLEVEL_INFO, "Allowed user: " .. user)
end
return config
end
Expand Down Expand Up @@ -288,8 +311,8 @@ function create_config( )
moderation = {data = 'data/moderation.json'}
}
serialize_to_file(config, './data/config.lua')
print ('Saved clean configuration into ./data/config.lua')
print ('Make sure to edit sudo_users and add your ID.')
log(LOGLEVEL_INFO, 'Saved clean configuration into ./data/config.lua')
log(LOGLEVEL_INFO, 'Make sure to edit sudo_users and add your ID.')
end

function on_our_id (id)
Expand All @@ -316,21 +339,25 @@ function on_get_difference_end ()
end

-- Enable plugins in config.json
-- Returns true if all the plugins were loaded correctly, false otherwise
function load_plugins()
local success = true
for k, v in pairs(_config.enabled_plugins) do
print("Loading plugin", v)
log(LOGLEVEL_INFO, "Loading plugin " .. v)

local ok, err = pcall(function()
local t = loadfile("plugins/"..v..'.lua')()
local t = assert(loadfile("plugins/"..v..'.lua'))()
plugins[v] = t
end)

if not ok then
print('\27[31mError loading plugin '..v..'\27[39m')
print('\27[31m'..err..'\27[39m')
success = false
log(LOGLEVEL_WARN, '\27[31mError loading plugin '..v..'\27[39m')
log(LOGLEVEL_WARN, '\27[31m'..err..'\27[39m')
end

end

return success
end

-- custom add
Expand Down Expand Up @@ -371,6 +398,7 @@ function cron_plugins()
postpone (cron_plugins, false, 5*60.0)
end


-- Start and load values
our_id = 0
now = os.time()
Expand Down
Loading