1
0
mirror of https://github.com/jiriks74/presence.nvim synced 2024-11-27 14:19:00 +01:00
presence.nvim/lua/presence/discord.lua

201 lines
5.9 KiB
Lua
Raw Normal View History

2021-01-03 01:08:17 -08:00
local Discord = {}
Discord.opcodes = {
auth = 0,
frame = 1,
closed = 2,
}
-- Discord RPC Subscription events
-- https://discord.com/developers/docs/topics/rpc#commands-and-events-rpc-events
-- Ready: https://discord.com/developers/docs/topics/rpc#ready
-- Error: https://discord.com/developers/docs/topics/rpc#error
Discord.events = {
READY = "READY",
ERROR = "ERROR",
}
local struct = require("deps.struct")
2021-01-03 01:08:17 -08:00
-- Initialize a new Discord RPC client
function Discord:init(options)
2021-01-03 01:08:17 -08:00
self.log = options.logger
self.client_id = options.client_id
self.ipc_socket = options.ipc_socket
2021-01-03 01:08:17 -08:00
self.pipe = vim.loop.new_pipe(false)
2021-01-03 01:08:17 -08:00
return self
end
-- Connect to the local Discord RPC socket
-- TODO Might need to check for pipes ranging from discord-ipc-0 to discord-ipc-9:
-- https://github.com/discord/discord-rpc/blob/master/documentation/hard-mode.md#notes
function Discord:connect(on_connect)
if self.pipe:is_closing() then
self.pipe = vim.loop.new_pipe(false)
2021-01-03 01:08:17 -08:00
end
self.pipe:connect(self.ipc_socket, on_connect)
2021-01-03 01:08:17 -08:00
end
function Discord:is_connected()
return self.pipe:is_active()
end
-- Disconnect from the local Discord RPC socket
function Discord:disconnect(on_close)
self.pipe:shutdown()
if not self.pipe:is_closing() then
self.pipe:close(on_close)
end
end
-- Make a remote procedure call to Discord
-- Callback argument in format: on_response(error[, response_table])
function Discord:call(opcode, payload, on_response)
self.encode_json(payload, function(success, body)
if not success then
self.log:warn(string.format("Failed to encode payload: %s", vim.inspect(body)))
return
end
2021-01-03 01:08:17 -08:00
-- Start reading for the response
self.pipe:read_start(function(...)
self:read_message(payload.nonce, on_response, ...)
end)
-- Construct message denoting little endian, auth opcode, msg length
local message = struct.pack("<ii", opcode, #body)..body
-- Write the message to the pipe
self.pipe:write(message, function(err)
if err then
local err_format = "Pipe write error - %s"
local err_message = string.format(err_format, err)
on_response(err_message)
else
self.log:debug("Wrote message to pipe")
2021-01-03 01:08:17 -08:00
end
end)
end)
end
-- Read and handle socket messages
function Discord:read_message(nonce, on_response, err, chunk)
if err then
local err_format = "Pipe read error - %s"
local err_message = string.format(err_format, err)
on_response(err_message)
elseif chunk then
-- Strip header from the chunk
local message = chunk:match("({.+)")
2021-01-03 01:08:17 -08:00
local response_opcode = struct.unpack("<ii", chunk)
self.decode_json(message, function(success, response)
2021-01-03 01:08:17 -08:00
-- Check for a non-frame opcode in the response
if response_opcode ~= self.opcodes.frame then
local err_format = "Received unexpected opcode - %s (code %s)"
local err_message = string.format(err_format, response.message, response.code)
return on_response(err_message)
end
-- Unable to decode the response
if not success then
-- Indetermine state at this point, no choice but to simply warn on the parse failure
-- but invoke empty response callback as request may still have succeeded
self.log:warn(string.format("Failed to decode payload: %s", vim.inspect(message)))
return on_response()
end
2021-01-03 01:08:17 -08:00
-- Check for an error event response
if response.evt == self.events.ERROR then
local data = response.data
local err_format = "Received error event - %s (code %s)"
local err_message = string.format(err_format, data.message, data.code)
return on_response(err_message)
end
-- Check for a valid nonce value
if response.nonce and response.nonce ~= vim.NIL and response.nonce ~= nonce then
local err_format = "Received unexpected nonce - %s (expected %s)"
local err_message = string.format(err_format, response.nonce, nonce)
return on_response(err_message)
end
on_response(nil, response)
end)
else
-- TODO: Handle when pipe is closed
self.log:warn("Pipe was closed")
end
end
-- Call to authorize the client connection with Discord
-- Callback argument in format: on_authorize(error[, response_table])
function Discord:authorize(on_authorize)
local payload = {
client_id = self.client_id,
v = 1,
}
self:call(self.opcodes.auth, payload, on_authorize)
end
-- Call to set the Neovim activity to Discord
function Discord:set_activity(activity, on_response)
local payload = {
cmd = "SET_ACTIVITY",
nonce = self.generate_uuid(),
args = {
activity = activity,
pid = vim.loop:os_getpid(),
2021-01-03 01:08:17 -08:00
},
}
self:call(self.opcodes.frame, payload, on_response)
end
function Discord.generate_uuid(seed)
local index = 0
local template ="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
2021-01-03 01:08:17 -08:00
local uuid = template:gsub("[xy]", function(char)
-- Increment an index to seed per char
index = index + 1
math.randomseed((seed or os.clock()) / index)
local n = char == "x"
2021-01-03 01:08:17 -08:00
and math.random(0, 0xf)
or math.random(8, 0xb)
return string.format("%x", n)
2021-01-03 01:08:17 -08:00
end)
return uuid
end
function Discord.decode_json(t, on_done)
vim.schedule(function()
on_done(pcall(function()
return vim.fn.json_decode(t)
end))
2021-01-03 01:08:17 -08:00
end)
end
function Discord.encode_json(t, on_done)
vim.schedule(function()
on_done(pcall(function()
return vim.fn.json_encode(t)
end))
2021-01-03 01:08:17 -08:00
end)
end
return Discord