Skip to content

Instantly share code, notes, and snippets.

@Admicos
Last active November 18, 2018 05:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Admicos/2a43681a1f71d97d0671cc084dc94ffb to your computer and use it in GitHub Desktop.
Save Admicos/2a43681a1f71d97d0671cc084dc94ffb 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"
#fallback-cmd: "./fallbacktest.sh"
#!/usr/bin/python3.6
"""
Allows you to run commands on Join pushes
--
Changelog:
1.1.0:
- Fallback commands
- Pass join pushes to commands as "JOIN_PUSH" env. value
(accessible as "$JOIN_PUSH" on (Ba)sh, "%JOIN_PUSH%" on cmd (i assume))
1.0.0:
- Initial release
--
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)
fallback_cmd = cfg.get("fallback-cmd")
if cmd:
print(f" - Running '{cmd}'")
os.environ["JOIN_PUSH"] = msg
os.system(cmd)
elif fallback_cmd:
print(f" - Running '{fallback_cmd}'")
os.environ["JOIN_PUSH"] = msg
os.system(fallback_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)
@phedders
Copy link

Thats fab. I think it needs to pass the full auto string to the process maybe as a environment variable so scripts can pick it up and use it.

@Admicos
Copy link
Author

Admicos commented Aug 19, 2017

@phedders Good idea, I'll try to add it after the weekend.

@Admicos
Copy link
Author

Admicos commented Aug 20, 2017

@phedders your suggestion has been added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment