Last active
August 11, 2018 12:01
-
-
Save meskarune/e5d47143f77a7240d4333176de10b283 to your computer and use it in GitHub Desktop.
get version info from weechat relay
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://lipp.github.io/lua-websockets/
http://w3.impa.br/~diego/software/luasocket/tcp.html#tcp
https://www.lua.org/pil/2.4.html
https://github.com/LuaDist/lpack