weechat-relay-info.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |