-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshim.lua
More file actions
350 lines (328 loc) · 10.3 KB
/
Copy pathshim.lua
File metadata and controls
350 lines (328 loc) · 10.3 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
-- for debug
function vardump(...)
print((serpent or require('serpent')).block({...}, {comment=false}))
end
--https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value#641993
function deepcopy(o, seen)
local seen = seen or {}
if o == nil then return nil end
if seen[o] then return seen[o] end
local no
if type(o) == 'table' then
no = {}
seen[o] = no
for k, v in next, o, nil do
no[deepcopy(k, seen)] = deepcopy(v, seen)
end
setmetatable(no, deepcopy(getmetatable(o), seen))
else -- number, string, boolean, etc
no = o
end
return no
end
-- in Lua 5.3 numbers are passed as double, even if they are integers (using lua_pushnumber form the Lua C API)
function fixfp(data)
if tonumber(_VERSION:match("%d+%.%d+")) < 5.3 then
return data
end
if type(data) == "number" then
return math.tointeger and math.tointeger(data) or data
end
if type(data) == "table" then
for k, v in pairs(data) do
if type(v) == "number" then
data[k] = fixfp(v)
end
if type(v) == "table" then
fixfp(v)
end
end
end
return data
end
-- Pack infos from the madeline function get_info in a tg-cli like object
function packInfo(info, pack)
local pack = pack or {}
if info.type == "channel" or info.type == "supergroup" then
pack.peer_type = "channel"
pack.peer_id = info.channel_id
pack.id = "placeholder"
pack.megagroup = info.Chat.megagroup
pack.title = info.Chat.title
pack.print_name = pack.title:gsub("%s", "_")
pack.flags = 1
pack.username = info.Chat.username
pack.access_hash = info.Chat.access_hash
pack.bot_api_id = info.bot_api_id
pack.participants_count = info.Chat.participants_count
pack.about = info.full and info.full.about
pack.raw = info.Chat
elseif info.type == "chat" then
pack.peer_type = "chat"
pack.peer_id = info.chat_id
pack.id = "placeholder"
pack.title = info.Chat.title
pack.print_name = pack.title:gsub("%s", "_")
pack.flags = 1
pack.access_hash = info.Chat.access_hash
pack.bot_api_id = info.bot_api_id
pack.members_count = info.Chat.participants_count
pack.raw = info.Chat
elseif info.type == "user" or info.type == "bot" then
pack.peer_type = "user"
pack.peer_id = info.user_id
pack.id = "placeholder"
pack.phone = info.User.phone
pack.first_name = info.User.first_name
pack.last_name = info.User.last_name or nil
if pack.first_name then
pack.print_name = pack.last_name and (pack.first_name .. "_".. pack.last_name):gsub("%s", "_") or pack.first_name:gsub("%s", "_")
end
pack.flags = 1
pack.username = info.User.username or nil
pack.access_hash = info.User.access_hash
pack.bot_api_id = info.bot_api_id
pack.raw = info.User
end
return pack
end
function packService(info, pack)
local pack = pack or {}
print("Handling service message", info._)
if info._ == "messageActionChatAddUser" then
pack.type = "chat_add_user"
pack.users = {}
for _, v in pairs(info.users) do
table.insert(pack.users, packInfo(fixfp(get_info(v)), {}))
end
pack.user = deepcopy(pack.users[1])
elseif info._ == "messageActionChatDeleteUser" then
pack.type = "chat_del_user"
pack.user = packInfo(fixfp(get_info(info.user_id)), {})
elseif info._ == "messageActionChatJoinedByLink" then
pack.type = "chat_add_user_link"
pack.link_issuer = packInfo(fixfp(get_info(info.inviter_id)), {})
elseif info._ == "messageActionChatDeletePhoto" then
pack.type = "chat_delete_photo"
elseif info._ == "messageActionChatEditPhoto" then
pack.type = "chat_edit_photo"
elseif info._ == "messageActionChatCreate" then
pack.type = "chat_created"
pack.title = info.title
elseif info._ == "messageActionChannelCreate" then
pack.type = "channel_create"
pack.title = info.title
elseif info._ == "messageActionChatMigrateTo" then
pack.type = "migrated_to"
pack.channel_id = info.channel_id
elseif info._ == "messageActionChannelMigrateFrom" then
pack.type = "migrate_from"
pack.title = info.title
pack.chat_id = info.chat_id
elseif info._ == "messageActionPinMessage" then
pack.type = "pin_message"
elseif info._ == "messageActionChatEditTitle" then
pack.type = "chat_edit_title"
pack.title = info.title
elseif info._ == "messageActionPhoneCall" then
phoneCalls[info.call_id] = nil
end
return pack
end
function packMedia(media, pack)
local pack = pack or {}
if media._ == "messageMediaPhoto" then
pack.type = "photo"
pack.caption = media.caption
elseif media._ == "messageMediaGeo" then
pack.type = "geo"
pack.longiture = media.geo.long
pack.latitude = media.geo.lat
elseif media._ == "messageMediaContact" then
pack.type = "contact"
pack.phone = media.phone_number
pack.first_name = media.first_name
pack.last_name = media.last_name ~= '' and media.last_name or nil
pack.user_id = media.user_id
elseif media._ == "messageMediaUnsupported" then
pack.type = "unsupported"
elseif media._ == "messageMediaDocument" then
pack.type = "document"
pack.caption = media.caption
for _, v in pairs(media.document.attributes) do
if v._ == "documentAttributeVideo" then
pack.type = "video"
elseif v._ == "documentAttributeAudio" then
pack.type = "audio"
elseif v._ == "documentAttributeSticker" then
pack.subtype = "sticker"
elseif v._ == "documentAttributeAnimated" then
pack.subtype = "gif"
elseif v._ == "documentAttributeFilename" then
pack.file_name = v.file_name
pack.document_caption = v.file_name
end
end
elseif media._ == "messageMediaWebPage" then
pack.type = "webpage"
pack.url = media.webpage.url
pack.title = media.webpage.title
pack.description = media.webpage.description
pack.author = media.webpage.author
elseif media._ == "messageMediaVenue" then
pack.type = "venue"
elseif media._ == "messageMediaGame" then
pack.type = "game"
elseif media._ == "messageMediaInvoice" then
pack.type = "invoice"
pack.longiture = media.geo.long
pack.latitude = media.geo.loat
pack.title = media.title
pack.address = media.address
pack.provider = media.provider
pack.venue_id = media.venue_id
end
return pack
end
function parsePwrUser(user, pack)
local pack = pack or {}
if user.type == "user" or user.type == "bot" then
pack.peer_type = "user"
pack.peer_id = user.id
pack.id = "placeholder"
pack.first_name = user.first_name
pack.last_name = user.last_name
pack.username = user.username
pack.bot = user.type == "bot"
pack.print_name = pack.last_name and (pack.first_name .. "_".. pack.last_name):gsub("%s", "_") or pack.first_name:gsub("%s", "_")
pack.access_hash = info.access_hash
pack.flags = 1
pack.phone = info.phone
pack.about = user.about --Bio
pack.raw = user
end
return pack
end
function packMembers(memberlist, users, filter)
local filter = filter or 1
if memberlist.error or not memberlist.participants then
return false
end
for _, v in pairs(memberlist.participants) do
v.user = parsePwrUser(v.user)
v.user.role = v.role
if filter == 1 then
if v.role ~= "banned" then
table.insert(users, v.user)
end
elseif filter == 2 then
if v.role == "admin" or v.role == "creator" then --The method get_pwr_chat returns no role if the user is admin ATM
v.admin = true
v.creator = v.role == "creator"
table.insert(users, v.user)
end
elseif filter == 3 then
if v.user.bot then
table.insert(users, v.user)
end
elseif filter == 4 then
if v.role == "banned" then
table.insert(users, v.user)
end
end
end
return true
end
-- Create the tg-cli style msg object
function tgmsg(message)
local msg = {}
msg.to = {}
msg.from = {}
packInfo(fixfp(get_info(message.to_id)), msg.to)
if message.from_id then
packInfo(fixfp(get_info(message.from_id)), msg.from)
else
msg.from = msg.to
end
if message.fwd_from then
msg.fwd_date = message.fwd_from.date
msg.fwd_from = {}
packInfo(fixfp(get_info(message.fwd_from.channel_id or message.fwd_from.from_id, msg.fwd_from)))
end
msg.text = message.message
msg.out = message.out
msg.message_id = message.id
msg.id = {inputPeer = message.to_id, id = message.id}
msg.service = false
if message.action then
msg.service = true
msg.action = packService(message.action, {})
end
if message.reply_to_msg_id then
msg.reply_id = {}
msg.reply_id.inputPeer = deepcopy(message.to_id)
msg.reply_id.id = message.reply_to_msg_id
end
if message.media then
msg.media = packMedia(message.media, {})
end
msg.date = message.date
msg.flags = 1
return msg
end
function postpone(callback, extra, time)
table.insert(crons, {time = os.time()+time, callback = callback, extra = extra})
end
function doCrons()
local ts = os.time()
for k, v in ipairs(crons) do
if v.time <= ts then
v.callback(v.extra)
table.remove(crons, k)
end
end
end
function madeline_update_callback(data)
if not started or data._ == "init" then
if not phoneCalls then
phoneCalls = {}
end
_methods = loadfile(methodsPath)
on_binlog_replay_end()
on_our_id(fixfp(get_self().id))
end
_methods()
local data = fixfp(data)
print("Got update", data._)
if data._ == "updateChannel" then
--retrive channel info, parse and call on_channel_update
elseif data._ == "updateNewChannelMessage" or data._ == "updateNewMessage" then
local msg = tgmsg(data.message)
if msg then
on_msg_receive(msg)
end
elseif data._ == "updatePhoneCall" then
--[=[
local phoneCall = data.phone_call
if phoneCall.getCallState() == 1 then
phoneCall.configuration.enable_NS = false
phoneCall.configuration.enable_AGC = false
phoneCall.configuration.enable_AEC = false
phoneCall.configuration.shared_config = {
audio_init_bitrate = 70*1000,
audio_max_bitrate = 100*1000,
audio_min_bitrate = 15*1000
}
phoneCall.parseConfig()
phoneCall.accept()
local cid = phoneCall.getCallID()
vardump(cid)
phoneCalls[cid['id']] = phoneCall
phoneCall.play('input.raw')
phoneCall['then']('inputb.raw')
end
--]=]
else
--vardump(data)
end
end