Skip to content

Instantly share code, notes, and snippets.

@meskarune
Last active August 11, 2018 12:01
Show Gist options
  • Save meskarune/e5d47143f77a7240d4333176de10b283 to your computer and use it in GitHub Desktop.
Save meskarune/e5d47143f77a7240d4333176de10b283 to your computer and use it in GitHub Desktop.
get version info from weechat relay
#!/usr/bin/lua
-- libraries: lua-socket, lua-sec, lua-argparse
-- weechat relay documentation: https://weechat.org/files/doc/stable/weechat_relay_protocol.en.html
local socket = require("socket")
local ssl = require("ssl")
local argparse = require "argparse"
-- Setup CLI options - docs: https://argparse.readthedocs.io/en/stable/index.html
local parser = argparse() {
name = "Weechat relay info",
description = "Connect to weechat relay and get the version information.",
epilog = "Author contact: hello@doloresportalatin.info, Licesnse: GPLv3"
}
parser:option "-H --host"
:description "Host to connect to."
:count "1"
:args "1"
parser:option("-P --port")
:description "Port number."
:default "9001"
:count "1"
:args "1"
parser:option("-p --password")
:description "The password for your relay."
:count "1"
:args "1"
local args = parser:parse()
-- TLS/SSL client parameters
local params = {
mode = "client",
protocol = "tlsv1",
-- cafile = "/path/to/downloaded/cert.pem", for weechat you can download at https://host:port/weechat
-- verify = "peer", for self signed cert
verify = "none",
options = "all",
}
-- Create the connection
local conn = socket.tcp()
conn:connect(args.host, args.port)
print(string.format("Connecting to %s:%s", args.host, args.port))
-- TLS/SSL initialization
conn = ssl.wrap(conn, params)
conn:dohandshake()
if conn then
print("Connected via ssl")
end
-- if password has commas they need to be escaped `\,` - write code to do this
-- init password=foo\,bar to send the password "foo,bar"
login = string.format("init password=%s\n", args.password)
conn:send(login)
conn:send("info version\n")
while true do
local line, status = conn:receive(1024)
if status == "closed" then
print(status)
break
else
print(line)
end
end
conn:close()