Skip to content

Commit

Permalink
Merge pull request vysheng#9 from koter84/scripted
Browse files Browse the repository at this point in the history
Scripted
  • Loading branch information
koter84 committed May 10, 2014
2 parents 611e9fd + 8134fd7 commit 08a088f
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ config.log
config.status
.idea/
nbproject/
scripts/mysql.conf
16 changes: 16 additions & 0 deletions lua-tg.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,22 @@ void lua_user_update (struct user *U) {
}
}

void lua_user_status_update (struct user *U, int *online) {
if (!have_file) { return; }
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
my_lua_checkstack (luaState, 20);
lua_getglobal (luaState, "on_user_status_update");
push_peer (U->id, (void *)U);
lua_pushnumber(luaState, (lua_Number)*online);
assert (lua_gettop (luaState) == 3);

int r = lua_pcall (luaState, 2, 0, 0);
if (r) {
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
}
}

void lua_chat_update (struct chat *C) {
if (!have_file) { return; }
lua_settop (luaState, 0);
Expand Down
1 change: 1 addition & 0 deletions lua-tg.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void lua_new_msg (struct message *M);
void lua_our_id (int id);
void lua_secret_chat_created (struct secret_chat *U);
void lua_user_update (struct user *U);
void lua_user_status_update (struct user *U, int *online);
void lua_chat_update (struct chat *C);
void lua_binlog_end (void);
void lua_diff_end (void);
Expand Down
8 changes: 7 additions & 1 deletion mtproto-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif
#ifdef USE_LUA
# include "lua-tg.h"
#endif

#define _FILE_OFFSET_BITS 64
Expand Down Expand Up @@ -1002,6 +1005,9 @@ void work_update (struct connection *c UU, long long msg_id UU) {
printf ("%s\n", (U->user.status.online > 0) ? "online" : "offline");
pop_color ();
print_end ();
#ifdef USE_LUA
lua_user_status_update (&U->user, &U->user.status.online);
#endif
}
} else {
struct user_status t;
Expand Down
107 changes: 107 additions & 0 deletions scripts/base_functions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

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

function escapeCSV (value)
if type(value) ~= 'string'
then
value = tostring(value)
end
if string.find(value, '[,"]') then
value = '"' .. string.gsub(value, '"', '""') .. '"'
end
return value
end

function toCSV (value)
local s = ""
for _,p in pairs(value) do
if type(p) == 'table'
then
s = s .. "," .. toCSV(p)
else
s = s .. "," .. escapeCSV(p)
end
end
return string.sub(s, 2) -- remove first comma
end

function fromCSV (value)
value = value .. ',' -- ending comma
local t = {} -- table to collect fields
local fieldstart = 1
repeat
-- next field is quoted? (start with `"'?)
if string.find(value, '^"', fieldstart) then
local a, c
local i = fieldstart
repeat
-- find closing quote
a, i, c = string.find(value, '"("?)', i+1)
until c ~= '"' -- quote not followed by quote?
if not i then error('unmatched "') end
local f = string.sub(value, fieldstart+1, i-1)
table.insert(t, (string.gsub(f, '""', '"')))
fieldstart = string.find(s, ',', i) + 1
else -- unquoted; find next comma
local nexti = string.find(value, ',', fieldstart)
table.insert(t, string.sub(value, fieldstart, nexti-1))
fieldstart = nexti + 1
end
until fieldstart > string.len(value)
return t
end

function file_exists (name)
local f = io.open( name, "r" )
if f ~= nil then
io.close(f)
return true
else
return false
end
end

-- no need to have all the functions in every script
function on_msg_receive (msg) end
function on_our_id (id) end
function on_secret_chat_created (peer) end
function on_user_update (user) end
function on_user_status_update (user, online) end
function on_chat_update (user) end
function on_get_difference_end () end
function on_binlog_replay_end () end
118 changes: 118 additions & 0 deletions scripts/daemonize.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
our_id = 0
inbox = "/tmp/telegram_inbox"
outbox = "/tmp/telegram_outbox"

require "scripts.base_functions";

print ()
print (">---> Telegram-CLI Lua Script!")
print (">---> read from " .. inbox .. " for incoming messages")
print (">---> write to " .. outbox .. " for outgoing messages")
print ()


-- (table)
-- [date] = (number) 1393973625
-- [from] = (table)
-- [id] = (number) 32778144
-- [type] = (string) user
-- [print_name] = (string) user#32778144
-- [unread] = (boolean) true
-- [flags] = (number) 16
-- [service] = (boolean) false
-- [id] = (string) 119
-- [text] = (string) 💋
-- [to] = (table)
-- [id] = (number) 25928291
-- [type] = (string) user
-- [print_name] = (string) user#25928291
-- [out] = (boolean) false
-- Message # 119 (flags 16)
-- date, from_id, from_type, from_print_name, unread, flags, service, id, text, to_id, to_type, to_print_name, out
-- 1393973625, 32778144, user, user#32778144, true, 16, false, 119, bericht, 25928291, user, user#25928291, false
--
--> >---> on_msg_receive
--(table)
--[to] = (table)
-- [phone] = (string) 31629735850
-- [print_name] = (string) Dennis
-- [flags] = (number) 144
-- [first_name] = (string) Dennis
-- [type] = (string) user
-- [id] = (number) 25928291
--[date] = (number) 1394041546
--[flags] = (number) 16
--[from] = (table)
-- [phone] = (string) 31641903990
-- [last_name] = (string) Litchidova
-- [print_name] = (string) Anna_Litchidova
-- [flags] = (number) 528
-- [first_name] = (string) Anna
-- [type] = (string) user
-- [id] = (number) 32778144
--[text] = (string) Ik ben op de terugweg
--[service] = (boolean) false
--[unread] = (boolean) true
--[id] = (string) 120
--[out] = (boolean) false


function on_msg_receive (msg)
if msg.out
then
print (">---> on_msg_send");
--vardump (msg)

else
print (">---> on_msg_receive");
--vardump (msg)

csv =
msg.id .. ";" ..
msg.date .. ";" ..
msg.from.id .. ";" ..
msg.from.phone .. ";" ..
msg.from.print_name .. ";" ..
msg.to.id .. ";" ..
msg.to.phone .. ";" ..
msg.to.print_name .. ";" ..
msg.text .. ";" ..
tostring(msg.out)
print ( csv )
end
end

function on_our_id (id)
print (">---> on_our_id");
vardump (id)
our_id = id
end

function on_secret_chat_created (peer)
print (">---> on_secret_chat_created");
vardump (peer)
end

function on_user_update (user)
print (">---> on_user_update");
vardump (user)
end

function on_user_status_update (user, online)
print (">---> on_user_status_update");
vardump (user)
vardump (online)
end

function on_chat_update (user)
print (">---> on_chat_update");
vardump (user)
end

function on_get_difference_end ()
print (">---> on_get_difference_end");
end

function on_binlog_replay_end ()
print (">---> on_binlog_replay_end");
end
103 changes: 103 additions & 0 deletions scripts/mysql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

require "scripts.base_functions";

print ()
print (">---> Telegram-CLI Lua Script!")
print (">---> read from and write to MySQL database/tables for incoming and outgoing messages")
print ()

-- check for mysql-conf file
if not file_exists("./scripts/mysql.conf") then
f=io.open( "./scripts/mysql.conf", "w" )
f:write( 'db_db = "telegram"\n' )
f:write( 'db_user = "telegram"\n' )
f:write( 'db_pass = "telegram"\n' )
f:write( 'db_host = "127.0.0.1"\n' )
f:write( 'db_port = "3306"\n' )
f:close()
end

-- http://www.keplerproject.org/luasql/examples.html
luasql = require "luasql.mysql"
dofile "./scripts/mysql.conf"
env = assert (luasql.mysql())
con = assert (env:connect( db_db, db_user, db_pass, db_host, db_port ))

res = assert (con:execute[[
CREATE TABLE IF NOT EXISTS `users` (
`uid` int(11) NOT NULL,
`phone` varchar(25) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`print_name` varchar(255) NOT NULL,
PRIMARY KEY (`uid`)
)
]])
res = assert (con:execute[[
CREATE TABLE IF NOT EXISTS `online` (
`online_id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`moment` datetime NOT NULL,
`online` int(11) NOT NULL,
PRIMARY KEY (`online_id`)
)
]])
res = assert (con:execute[[
CREATE TABLE IF NOT EXISTS `inbox` (
`mid` int(11) NOT NULL,
`uid` int(11) NOT NULL,
`moment` datetime NOT NULL,
`msg` text NOT NULL,
PRIMARY KEY (`mid`, `uid`)
)
]])
res = assert (con:execute[[
CREATE TABLE IF NOT EXISTS `outbox` (
`mid` int(11) NOT NULL,
`uid` int(11) NOT NULL,
`moment` datetime NOT NULL,
`msg` text NOT NULL,
PRIMARY KEY (`mid`, `uid`)
)
]])
res = assert (con:execute[[
CREATE TABLE IF NOT EXISTS `sendbox` (
`send_id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`msg` text NOT NULL,
PRIMARY KEY (`send_id`)
)
]])


function on_msg_receive (msg)
if msg.out
then
print (">---> on_msg_send");
--vardump (msg)
res = con:execute("INSERT INTO `outbox` ( mid, uid, moment, msg ) VALUES ( '" .. msg.id .. "', '" .. msg.to.id .. "', FROM_UNIXTIME(" .. msg.date .. "), '" .. msg.text .. "' );");
else
print (">---> on_msg_receive");
--vardump (msg)
res = con:execute("INSERT INTO `inbox` ( mid, uid, moment, msg ) VALUES ( '" .. msg.id .. "', '" .. msg.from.id .. "', FROM_UNIXTIME(" .. msg.date .. "), '" .. msg.text .. "' );");
end
end

function on_user_update (user)
print (">---> on_user_update");
res = con:execute("INSERT INTO `users` ( uid, phone, first_name, last_name, print_name ) VALUES ( '" .. user.id .. "', '" .. user.phone .. "', '" .. user.first_name .. "', '" .. user.last_name .. "', '" .. user.print_name .. "' );");
end

function on_user_status_update (user, online)
print (">---> on_user_status_update");
if online == -1
then
online = 0;
end
res = con:execute("INSERT INTO `online` ( uid, moment, online ) VALUES ( '" .. user.id .. "', NOW(), '" .. online .. "' );");
end

function on_chat_update (user)
print (">---> on_chat_update");
-- vardump (user)
end
Loading

0 comments on commit 08a088f

Please sign in to comment.