This commit is contained in:
topkecleon 2016-08-15 01:08:55 -04:00
parent f9094a73ca
commit d3b0825fa0

View File

@ -1,13 +1,13 @@
--[[ --[[
drua-tg drua-tg
Based on JuanPotato's lua-tg (https://github.com/juanpotato/lua-tg), Based on JuanPotato's lua-tg (https://github.com/juanpotato/lua-tg),
modified to work more naturally from an API bot. modified to work more naturally from an API bot.
Usage: Usage:
drua = require('drua-tg') drua = require('drua-tg')
drua.IP = 'localhost' -- 'localhost' is default drua.IP = 'localhost' -- 'localhost' is default
drua.PORT = 4567 -- 4567 is default drua.PORT = 4567 -- 4567 is default
drua.message(chat_id, text) drua.message(chat_id, text)
The MIT License (MIT) The MIT License (MIT)
@ -35,150 +35,150 @@ SOFTWARE.
local SOCKET = require('socket') local SOCKET = require('socket')
local comtab = { local comtab = {
add = { 'chat_add_user %s %s', 'channel_invite %s %s' }, add = { 'chat_add_user %s %s', 'channel_invite %s %s' },
kick = { 'chat_del_user %s %s', 'channel_kick %s %s' }, kick = { 'chat_del_user %s %s', 'channel_kick %s %s' },
rename = { 'rename_chat %s "%s"', 'rename_channel %s "%s"' }, rename = { 'rename_chat %s "%s"', 'rename_channel %s "%s"' },
link = { 'export_chat_link %s', 'export_channel_link %s' }, link = { 'export_chat_link %s', 'export_channel_link %s' },
photo_set = { 'chat_set_photo %s %s', 'channel_set_photo %s %s' }, photo_set = { 'chat_set_photo %s %s', 'channel_set_photo %s %s' },
photo_get = { [0] = 'load_user_photo %s', 'load_chat_photo %s', 'load_channel_photo %s' }, photo_get = { [0] = 'load_user_photo %s', 'load_chat_photo %s', 'load_channel_photo %s' },
info = { [0] = 'user_info %s', 'chat_info %s', 'channel_info %s' } info = { [0] = 'user_info %s', 'chat_info %s', 'channel_info %s' }
} }
local function format_target(target) local function format_target(target)
target = tonumber(target) target = tonumber(target)
if target < -1000000000000 then if target < -1000000000000 then
target = 'channel#' .. math.abs(target) - 1000000000000 target = 'channel#' .. math.abs(target) - 1000000000000
return target, 2 return target, 2
elseif target < 0 then elseif target < 0 then
target = 'chat#' .. math.abs(target) target = 'chat#' .. math.abs(target)
return target, 1 return target, 1
else else
target = 'user#' .. target target = 'user#' .. target
return target, 0 return target, 0
end end
end end
local function escape(text) local function escape(text)
text = text:gsub('\\', '\\\\') text = text:gsub('\\', '\\\\')
text = text:gsub('\n', '\\n') text = text:gsub('\n', '\\n')
text = text:gsub('\t', '\\t') text = text:gsub('\t', '\\t')
text = text:gsub('"', '\\"') text = text:gsub('"', '\\"')
return text return text
end end
local drua = { local drua = {
IP = 'localhost', IP = 'localhost',
PORT = 4567 PORT = 4567
} }
function drua.send(command, do_receive) function drua.send(command, do_receive)
local s = SOCKET.connect(drua.IP, drua.PORT) local s = SOCKET.connect(drua.IP, drua.PORT)
assert(s, '\nUnable to connect to tg session.') assert(s, '\nUnable to connect to tg session.')
s:send(command..'\n') s:send(command..'\n')
local output local output
if do_receive then if do_receive then
output = string.match(s:receive('*l'), 'ANSWER (%d+)') output = string.match(s:receive('*l'), 'ANSWER (%d+)')
output = s:receive(tonumber(output)):gsub('\n$', '') output = s:receive(tonumber(output)):gsub('\n$', '')
end end
s:close() s:close()
return output return output
end end
function drua.message(target, text) function drua.message(target, text)
target = format_target(target) target = format_target(target)
text = escape(text) text = escape(text)
local command = 'msg %s "%s"' local command = 'msg %s "%s"'
command = command:format(target, text) command = command:format(target, text)
return drua.send(command) return drua.send(command)
end end
function drua.send_photo(target, photo) function drua.send_photo(target, photo)
target = format_target(target) target = format_target(target)
local command = 'send_photo %s %s' local command = 'send_photo %s %s'
command = command:format(target, photo) command = command:format(target, photo)
return drua.send(command) return drua.send(command)
end end
function drua.add_user(chat, target) function drua.add_user(chat, target)
local a local a
chat, a = format_target(chat) chat, a = format_target(chat)
target = format_target(target) target = format_target(target)
local command = comtab.add[a]:format(chat, target) local command = comtab.add[a]:format(chat, target)
return drua.send(command) return drua.send(command)
end end
function drua.kick_user(chat, target) function drua.kick_user(chat, target)
-- Get the group info so tg will recognize the target. -- Get the group info so tg will recognize the target.
drua.get_info(chat) drua.get_info(chat)
local a local a
chat, a = format_target(chat) chat, a = format_target(chat)
target = format_target(target) target = format_target(target)
local command = comtab.kick[a]:format(chat, target) local command = comtab.kick[a]:format(chat, target)
return drua.send(command) return drua.send(command)
end end
function drua.rename_chat(chat, name) function drua.rename_chat(chat, name)
local a local a
chat, a = format_target(chat) chat, a = format_target(chat)
local command = comtab.rename[a]:format(chat, name) local command = comtab.rename[a]:format(chat, name)
return drua.send(command) return drua.send(command)
end end
function drua.export_link(chat) function drua.export_link(chat)
local a local a
chat, a = format_target(chat) chat, a = format_target(chat)
local command = comtab.link[a]:format(chat) local command = comtab.link[a]:format(chat)
return drua.send(command, true) return drua.send(command, true)
end end
function drua.get_photo(chat) function drua.get_photo(chat)
local a local a
chat, a = format_target(chat) chat, a = format_target(chat)
local command = comtab.photo_get[a]:format(chat) local command = comtab.photo_get[a]:format(chat)
local output = drua.send(command, true) local output = drua.send(command, true)
if output:match('FAIL') then if output:match('FAIL') then
return false return false
else else
return output:match('Saved to (.+)') return output:match('Saved to (.+)')
end end
end end
function drua.set_photo(chat, photo) function drua.set_photo(chat, photo)
local a local a
chat, a = format_target(chat) chat, a = format_target(chat)
local command = comtab.photo_set[a]:format(chat, photo) local command = comtab.photo_set[a]:format(chat, photo)
return drua.send(command) return drua.send(command)
end end
function drua.get_info(target) function drua.get_info(target)
local a local a
target, a = format_target(target) target, a = format_target(target)
local command = comtab.info[a]:format(target) local command = comtab.info[a]:format(target)
return drua.send(command, true) return drua.send(command, true)
end end
function drua.channel_set_admin(chat, user, rank) function drua.channel_set_admin(chat, user, rank)
chat = format_target(chat) chat = format_target(chat)
user = format_target(user) user = format_target(user)
local command = 'channel_set_admin %s %s %s' local command = 'channel_set_admin %s %s %s'
command = command:format(chat, user, rank) command = command:format(chat, user, rank)
return drua.send(command) return drua.send(command)
end end
function drua.channel_set_about(chat, text) function drua.channel_set_about(chat, text)
chat = format_target(chat) chat = format_target(chat)
text = escape(text) text = escape(text)
local command = 'channel_set_about %s "%s"' local command = 'channel_set_about %s "%s"'
command = command:format(chat, text) command = command:format(chat, text)
return drua.send(command) return drua.send(command)
end end
function drua.block(user) function drua.block(user)
return drua.send('block_user user#' .. user) return drua.send('block_user user#' .. user)
end end
function drua.unblock(user) function drua.unblock(user)
return drua.send('unblock_user user#' .. user) return drua.send('unblock_user user#' .. user)
end end
return drua return drua