Skip to content

Instantly share code, notes, and snippets.

@phedders
Forked from Admicos/config.yml
Created August 16, 2017 22:33
Show Gist options
  • Save phedders/20f52187536a9fa4d78cb4cdaf63c876 to your computer and use it in GitHub Desktop.
Save phedders/20f52187536a9fa4d78cb4cdaf63c876 to your computer and use it in GitHub Desktop.
Join Python receiver thingy
port: 1818
commands:
# Join Push Text : Command to run
"eg=:=shutdown": "systemctl poweroff"
"""
Allows you to run commands on Join pushes
Copyright 2017+ Admicos
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from urllib import parse
import socket
import yaml
import re
import os
def extract_message(raw: str):
"""Extract message from Join's request"""
return parse.unquote(re.search(r".* ?message=(.*) .*", raw.split("\r\n")[0]).group(1))
def main(cfg: dict):
print("..--== By Admicos ==--..")
print("This requires a running Chrome/ium instance with the Join extension.")
print(f"Don't forget to set the 'EventGhost' port to {cfg['port']}")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", cfg["port"]))
s.listen(5)
print("Started listening")
while True:
c, addr = s.accept()
d = c.recv(512).decode("utf-8")
msg = extract_message(d)
print(f"{addr}: {msg}")
cmd = cfg["commands"].get(msg)
if cmd:
print(f"Running '{cmd}'")
os.system(cmd)
else:
print(f"No command for '{msg}', ignoring")
c.close()
if __name__ == '__main__':
with open("config.yml") as f:
config = yaml.load(f)
main(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment