Skip to content

Instantly share code, notes, and snippets.

@KoolKeith
Forked from Admicos/config.yml
Created November 18, 2018 05:04

Revisions

  1. Admicos revised this gist Aug 20, 2017. 2 changed files with 26 additions and 3 deletions.
    4 changes: 3 additions & 1 deletion config.yml
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    port: 1818
    commands:
    # Join Push Text : Command to run
    "eg=:=shutdown": "systemctl poweroff"
    "eg=:=shutdown": "systemctl poweroff"

    #fallback-cmd: "./fallbacktest.sh"
    25 changes: 23 additions & 2 deletions receiver.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,19 @@
    #!/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
    @@ -47,11 +61,18 @@ def main(cfg: dict):

    print(f"{addr}: {msg}")
    cmd = cfg["commands"].get(msg)
    fallback_cmd = cfg.get("fallback-cmd")

    if cmd:
    print(f"Running '{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")
    print(f" - No command for '{msg}', ignoring")

    c.close()

  2. Admicos created this gist Aug 8, 2017.
    4 changes: 4 additions & 0 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    port: 1818
    commands:
    # Join Push Text : Command to run
    "eg=:=shutdown": "systemctl poweroff"
    63 changes: 63 additions & 0 deletions receiver.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    """
    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)