-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
discord.lua
489 lines (456 loc) · 13.6 KB
/
discord.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
---Discord rich presence
---@author ObserverOfTime
---@license 0BSD
local utils = require 'mp.utils'
local msg = require 'mp.msg'
---@class DiscordOptions
---@field timeout integer
---@field keybind string
---@field enabled boolean
---@field invidious string
---@field piped string
---@field nitter string
---@field libreddit string
---@field client_id string
local o = {
timeout = 2,
keybind = 'D',
enabled = false,
invidious = 'yewtu%.be',
piped = 'piped%.kavin%.rocks',
nitter = 'nitter%.net',
libreddit = 'libredd%.it',
client_id = '700723249889149038'
}
require('mp.options').read_options(o, 'discord')
---@return string
local function uuid()
math.randomseed(mp.get_time() * 1e4)
local tpl = 'XXXXXXXX-XXXX-4XXX-%xXXX-XXXXXXXXXXXX'
return tpl:format(math.random(8, 0xb)):gsub('X', function(_)
return ('%x'):format(math.random(0, 0xf))
end)
end
---@param str string
---@return string
local function tohex(str)
return str:gsub('.', function(c)
return ('\\x%02x'):format(c:byte())
end)
end
---@type string
local VERSION = mp.get_property('mpv-version')
---@enum OP
local OP = {AUTHENTICATE = 0, FRAME = 1, CLOSE = 2}
---@class unixstream
---@field connect fun()
---@field receive fun()
---@field send fun()
---@class RPC
---@field socket integer|file*|unixstream?
---@field pid integer
---@field unix boolean
---@field path string
---@field _last string?
local RPC = {
socket = nil,
pid = utils.getpid(),
unix = package.config:sub(1, 1) == '/'
}
if RPC.unix then
local temp = os.getenv('XDG_RUNTIME_DIR')
or os.getenv('TMPDIR')
or os.getenv('TMP')
or os.getenv('TEMP')
or '/tmp'
RPC.path = temp..'/discord-ipc-0'
if _G.jit then
msg.verbose('using', _G.jit.version)
local ffi = require 'ffi'
ffi.cdef[[
struct sockaddr {
unsigned short int sa_family;
char sa_data[14];
};
struct sockaddr_un {
unsigned short int sun_family;
char sun_path[108];
};
int socket(int domain, int type, int protocol);
int connect(int fd, const struct sockaddr *addr, unsigned int len);
int recv(int fd, void *buf, unsigned int len, int flags);
int send(int fd, const void *buf, unsigned int n, int flags);
int close(int fd);
char *strerror(int errnum);
]]
---@return string
function RPC._strerror()
return ffi.string(ffi.C.strerror(ffi.errno()))
end
---@param fd integer
---@param addr ffi.cdata*
---@return integer
function RPC._connect(fd, addr)
local cast = ffi.cast('const struct sockaddr *', addr)
return ffi.C.connect(fd, cast, ffi.sizeof(addr[0]))
end
---@param fd integer
---@param len integer
---@return boolean, string
function RPC._recv(fd, len)
local buff = ffi.new('unsigned char[?]', len)
local status = ffi.C.recv(fd, buff, len, 0) ~= -1
return status, status and ffi.string(buff, len) or RPC._strerror()
end
else
local socket = assert(require 'socket')
msg.verbose('using', socket._VERSION)
end
else
RPC.path = [[\\.\pipe\discord-ipc-0]]
end
---@class Assets
---@field large_image string
---@field large_text string?
---@field small_image string?
---@field small_text string?
---@class Timestamps
---@field start integer
---@field end integer?
---@class Button
---@field label string
---@field url string
---@class Activity
---@field details string
---@field state string?
---@field timestamps Timestamps?
---@field buttons Button[]?
---@field assets Assets
RPC.activity = {
details = 'No file',
state = nil,
timestamps = nil,
buttons = nil,
assets = {
large_image = 'mpv',
large_text = VERSION,
small_image = 'stop',
small_text = 'Idle'
}
}
---@return integer
function RPC.get_time()
local pos = mp.get_property_number('time-pos', 0)
return math.floor(os.time() - pos)
end
---@param op OP
---@param body string?
---@return string
function RPC.pack(op, body)
local bytes = {}
assert(body, 'empty body')
local len = body:len()
for _ = 1, 4 do
table.insert(bytes, string.char(op % (2 ^ 8)))
op = math.floor(op / (2 ^ 8))
end
for _ = 1, 4 do
table.insert(bytes, string.char(len % (2 ^ 8)))
len = math.floor(len / (2 ^ 8))
end
return table.concat(bytes, '')..body
end
---@param body string?
---@return number, number
function RPC.unpack(body)
local byte
local op = 0
local len = 0
local iter = 1
assert(body, 'empty body')
for j = 1, 4 do
byte = body:sub(iter, iter):byte()
op = op + byte * (2 ^ ((j - 1) * 8))
iter = iter + 1
end
for j = 1, 4 do
byte = body:sub(iter, iter):byte()
len = len + byte * (2 ^ ((j - 1) * 8))
iter = iter + 1
end
return math.floor(op), math.floor(len)
end
---@return boolean
function RPC:connect()
local status, data
if _G.jit then
local ffi = package.loaded.ffi
local addr = ffi.new('struct sockaddr_un[1]', {{
sun_family = 1, -- AF_UNIX
sun_path = self.path
}})
self.socket = ffi.C.socket(1, 1, 0) -- AF_UNIX, SOCK_STREAM, 0
if self.socket ~= -1 then
status = self._connect(self.socket, addr)
if status ~= -1 then return true end
end
data = self._strerror()
elseif self.unix then
self.socket = require 'socket.unix' ()
status, data = pcall(function()
assert(self.socket:connect(self.path))
end)
if status then return true end
else
status, data = pcall(function()
return assert(io.open(self.path, 'r+b'))
end)
self.socket = data
if status then return true end
end
self.socket = nil
msg.fatal(data, '('..self.path..')')
return false
end
---@param len integer
---@return string?
function RPC:recv(len)
if not self.socket then
assert(self:connect(), 'failed to connect')
end
local status, data
if _G.jit then
status, data = self._recv(self.socket, len)
elseif self.unix then
status, data = pcall(function()
return assert(self.socket:receive(len))
end)
else
status, data = pcall(function()
return assert(self.socket:read(len))
end)
end
if not status then
msg.error(data)
return nil
end
msg.debug('received', tohex(data))
assert(data:len() == len, 'incorrect data length')
return data
end
---@param op OP
---@param body string?
function RPC:send(op, body)
if not self.socket then
assert(self:connect(), 'failed to connect')
end
local data = self.pack(op, body)
msg.debug('sending', tohex(data))
if _G.jit then
local status = package.loaded.ffi.C
.send(self.socket, data, #data, 0)
assert(status ~= -1, self._strerror())
elseif self.unix then
assert(self.socket:send(data))
else
assert(self.socket:write(data))
self.socket:flush()
end
end
---@param version? integer
function RPC:handshake(version)
local body = utils.format_json {
v = version or 1,
client_id = o.client_id
}
self:send(OP.AUTHENTICATE, body)
local op, len = self.unpack(self:recv(8))
local res = utils.parse_json(self:recv(len))
assert(op == OP.FRAME, res.message)
assert(res.evt == 'READY', res.message)
msg.verbose('performed handshake')
end
---@return string?
function RPC:set_activity()
if self.activity.details:len() > 127 then
self.activity.details = self.activity.details:sub(1, 126)..'…'
end
local nonce = uuid()
local body = utils.format_json {
cmd = 'SET_ACTIVITY', nonce = nonce,
args = {activity = self.activity, pid = self.pid}
}
self:send(OP.FRAME, body)
local res = self:recv(8)
if not res then
msg.info('reattempting to set activity')
return self:set_activity()
end
local _, len = self.unpack(res)
res = utils.parse_json(self:recv(len))
if not res then
msg.info('reattempting to set activity')
return self:set_activity()
end
assert(res.cmd == 'SET_ACTIVITY', 'incorrect cmd')
assert(res.nonce == nonce, 'incorrect nonce')
if res.evt == 'ERROR' then
msg.error(res.data.message)
return nil
end
return body
end
function RPC:disconnect()
if self.socket then
self:send(OP.CLOSE, '')
if _G.jit then
local status = package.loaded.ffi.C.close(self.socket)
assert(status ~= -1, self._strerror())
else
self.socket:close()
end
self.socket = nil
end
end
mp.register_event('idle-active', function()
RPC.activity = {
details = 'No file',
state = nil,
buttons = nil,
timestamps = nil,
assets = {
small_image = 'stop',
small_text = 'Idle',
large_image = 'mpv',
large_text = VERSION
}
}
end)
mp.register_event('file-loaded', function()
local title = mp.get_property('media-title') or 'Untitled'
local artist = mp.get_property('metadata/by-key/Artist')
title = artist and title..' - '..artist or title
local time = mp.get_property_number('duration')
time = time and 'Duration: '..os.date('!%T', time) or ''
local plist = mp.get_property_number('playlist-count')
if plist > 1 then
local item = mp.get_property('playlist-pos')
plist = (' [%d/%d]'):format(item + 1, plist)
item = mp.get_property(('playlist/%d/title'):format(item))
if item then title = item end
else
plist = ''
end
local path = mp.get_property('path')
if path and path:find('^https?://') then
if path:find('youtube%.com') or
path:find('youtu%.be') then
RPC.activity.assets.large_image = 'youtube'
elseif path:find('twitch%.tv') then
RPC.activity.assets.large_image = 'twitch'
elseif path:find('cdn%.discordapp%.com') or
path:find('media%.discordapp%.net') then
RPC.activity.assets.large_image = 'discord'
elseif path:find('drive%.google%.') then
RPC.activity.assets.large_image = 'drive'
elseif path:find('twitter%.com') then
RPC.activity.assets.large_image = 'twitter'
elseif path:find('reddit%.com') or
path:find('redd%.it') then
RPC.activity.assets.large_image = 'reddit'
elseif path:find(o.invidious) then
RPC.activity.assets.large_image = 'invidious'
elseif path:find(o.piped) then
RPC.activity.assets.large_image = 'piped'
elseif path:find(o.nitter) then
RPC.activity.assets.large_image = 'nitter'
elseif path:find(o.libreddit) then
RPC.activity.assets.large_image = 'libreddit'
else
RPC.activity.assets.large_image = 'stream'
end
RPC.activity.buttons = {
{label = 'Open URL', url = path}
}
else
RPC.activity.buttons = nil
RPC.activity.assets.large_image = 'mpv'
end
RPC.activity.details = title
RPC.activity.state = time..plist
RPC.activity.assets.small_image = 'pause'
RPC.activity.assets.small_text = 'Paused'
RPC.activity.timestamps = nil
end)
mp.register_event('shutdown', function()
RPC:disconnect()
end)
mp.register_event('seek', function()
if not mp.get_property_bool('pause') then
RPC.activity.timestamps = {start = RPC.get_time()}
end
end)
mp.observe_property('paused-for-cache', 'bool', function(_, value)
if value then
RPC.activity.timestamps = nil
RPC.activity.assets.small_image = 'play'
RPC.activity.assets.small_text = 'Playing'
else
RPC.activity.timestamps = {start = RPC.get_time()}
RPC.activity.assets.small_image = 'play'
RPC.activity.assets.small_text = 'Playing'
end
end)
mp.observe_property('core-idle', 'bool', function(_, value)
if value then
RPC.activity.timestamps = nil
RPC.activity.assets.small_image = 'pause'
RPC.activity.assets.small_text = 'Loading'
else
RPC.activity.timestamps = {start = RPC.get_time()}
RPC.activity.assets.small_image = 'play'
RPC.activity.assets.small_text = 'Playing'
end
end)
mp.observe_property('pause', 'bool', function(_, value)
if value then
RPC.activity.timestamps = nil
RPC.activity.assets.small_image = 'pause'
RPC.activity.assets.small_text = 'Paused'
else
RPC.activity.timestamps = {start = RPC.get_time()}
RPC.activity.assets.small_image = 'play'
RPC.activity.assets.small_text = 'Playing'
end
end)
mp.observe_property('eof-reached', 'bool', function(_, value)
if value then
RPC.activity.timestamps = nil
RPC.activity.assets.small_image = 'stop'
RPC.activity.assets.small_text = 'Idle'
end
end)
local timer = mp.add_periodic_timer(o.timeout, function()
local curr = utils.format_json(RPC.activity)
if RPC._last ~= curr then
RPC:set_activity()
RPC._last = curr
end
end)
mp.add_key_binding(o.keybind, 'toggle-discord-rpc', function()
o.enabled = not o.enabled
if o.enabled then
RPC:handshake()
timer:resume()
else
RPC._last = nil
RPC:disconnect()
timer:kill()
end
end)
if o.enabled then
RPC:handshake()
RPC:set_activity()
else
timer:kill()
end