-
Notifications
You must be signed in to change notification settings - Fork 14
/
backup.lua
191 lines (173 loc) · 5.73 KB
/
backup.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
MODE = 'history' -- 'history' or 'users'
CONTACT_NAME = 'Raff' -- It can be a part of contacts name
MESSAGE_COUNT = 500
TYPE = 'csv' -- 'csv' or 'sqlite'
DATABASE_FILE = 'db.sqlite3'
CSV_FILE_CONTACTS = 'contacts.csv'
CSV_FILE_MESSAGES = 'messages.csv'
function writeCSV(path, data, sep)
sep = sep or ','
local file = assert(io.open(path, "a"))
for i=1,#data do
for j=1,#data[i] do
if j>1 then file:write(sep) end
if data[i][j] == nil then
file:write("")
else
file:write(data[i][j])
end
end
file:write('\n')
end
file:close()
end
function history_cb(extra, success, history)
if success then
for _,m in pairs(history) do
if not m.service then -- Ignore Telegram service messages
local out = m.out and 1 or 0 -- Cast boolean to integer
if m.text == nil then -- No nil value
m.text = ''
end
local fromName = 'DELETED_ACCOUNT'
if (m.from ~= nil) then
fromName = m.from.print_name
end
local toName = ''
if (m.to ~= nil) then
toName = m.to.print_name
end
if TYPE == 'csv' then
if (m.media == nil or m.media == '') then
writeCSV(CSV_FILE_MESSAGES, {{fromName, toName, m.text, out, m.date, m.id,'','',''}})
elseif m.media.type == 'webpage' and m.media.url ~= nil then
writeCSV(CSV_FILE_MESSAGES, {{fromName, toName, m.text, out, m.date, m.id,'1',m.media.type,m.media.url}})
else
writeCSV(CSV_FILE_MESSAGES, {{fromName, toName, m.text, out, m.date, m.id,'1',m.media.type,''}})
end
elseif TYPE == 'sqlite' then
local sql = [[
INSERT INTO messages
(`from`, `to`, `text`, `out`, `date`, `message_id`, `media`, `media_type`,`url`)
VALUES (
']] .. db:escape(fromName) .. [[',
']] .. db:escape(toName) .. [[',
']] .. db:escape(m.text) .. [[',
']] .. out .. [[',
']] .. m.date .. [[',
']] .. m.id .. [[',
]]
if (m.media == nil or m.media == '') then
sql = sql .. "NULL, NULL, NULL)"
elseif m.media.type == 'webpage' and m.media.url ~= nil then
sql = sql .. "'1','".. m.media.type .. "', '" .. m.media.url .. "')"
else
sql = sql .. "'1','".. m.media.type .. "', NULL)"
end
print(m.id)
--require 'pl.pretty'.dump(m)
-- print(sql)
res = db:execute(sql)
-- print(heh)
end
end
end
print("done")
end
end
function user_cb(extra, success, user)
if success and user.print_name ~= 'deleted' then
if user.phone == nil then
user.phone = ''
end
if user.username == nil then
user.username = ''
end
if TYPE == 'csv' then
writeCSV(CSV_FILE_CONTACTS, {{user.id, user.print_name, user.username, user.phone}})
elseif TYPE == 'sqlite' then
local sql = [[
INSERT INTO users
(`id`, `name`, `username`, `phone`)
VALUES (
']] .. user.id .. [[',
']] .. db:escape(user.print_name) .. [[',
']] .. user.username .. [[',
']] .. user.phone .. [['
);
]]
-- print(sql)
res = db:execute(sql)
-- print(res)
end
end
end
function dialogs_cb(extra, success, dialog)
if success then
for _,d in pairs(dialog) do
v = d.peer
print(v.print_name)
if v.print_name ~= nil and string.find(v.print_name, CONTACT_NAME) then
print(v.print_name)
if (v.type == 'user') then
user_info(v.print_name, user_cb, history_extra)
else
chat_info(v.print_name, user_cb, history_extra)
end
if MODE == 'history' then
get_history(v.print_name, MESSAGE_COUNT, history_cb, history_extra)
end
end
end
end
end
function on_binlog_replay_end ()
if TYPE == 'csv' then
data = {{'id', 'name', 'username', 'phone'}}
writeCSV(CSV_FILE_CONTACTS, data)
data = {{'id', 'from', 'to', 'text', 'out', 'date', 'message_id', 'media', 'media_type', 'url'}}
writeCSV(CSV_FILE_MESSAGES, data)
elseif TYPE == 'sqlite' then
sqlite3 = require("luasql.sqlite3")
--db = sqlite3.open(DATABASE_FILE)
driver = sqlite3.sqlite3()
db = driver:connect(DATABASE_FILE)
res = db:execute[[
CREATE TABLE messages (
`id` INTEGER PRIMARY KEY,
`from` TEXT,
`to` TEXT,
`text` TEXT,
`out` INTEGER,
`date` INTEGER,
`message_id` INTEGER,
`media` TEXT,
`media_type` TEXT,
`url` TEXT
);
]]
res = db:execute[[
CREATE TABLE users (
`id` INTEGER PRIMARY KEY,
`name` TEXT,
`username` TEXT,
`phone` TEXT
);
]]
else
error("Please choose type csv or sqlite")
end
res = get_dialog_list(dialogs_cb, contacts_extra)
end
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_chat_update (user)
end
function on_get_difference_end ()
end