Browse Source

initial commit

master
dave 4 years ago
commit
ea9fd28008
  1. 3
      .gitignore
  2. 38
      Makefile
  3. 1
      data/logs/.gitignore
  4. 45
      genconf.py
  5. 16
      plugins/_common.py
  6. 26
      plugins/ping.py
  7. 8
      requirements.txt

3
.gitignore

@ -0,0 +1,3 @@
testenv
__pycache__
circus.ini

38
Makefile

@ -0,0 +1,38 @@
VENV := /opt/extpython/python3.6/bin/virtualenv
SERVER_HOST := 127.0.0.1
SERVER_PORT := 7100
.DEFAULT_GOAL := run
virtualenv: testenv
testenv:
rm -rf testenv
$(VENV) testenv
. testenv/bin/activate && pip3 install -r requirements.txt
.PHONY: circus.ini
circus.ini: virtualenv
. testenv/bin/activate && ./genconf.py $(SERVER_HOST) $(SERVER_PORT) > circus.ini
.PHONY: run
run: circus.ini
. testenv/bin/activate && circusd circus.ini
.PHONY: daemon
daemon: circus.ini
. testenv/bin/activate && circusd --daemon circus.ini
.PHONY: kill
kill:
pkill circusd
clean:
rm -rf testenv circus.ini

1
data/logs/.gitignore

@ -0,0 +1 @@
*

45
genconf.py

@ -0,0 +1,45 @@
#!/usr/bin/env python3
import os
import sys
timefmt = "%Y-%m-%d %H:%M:%S".replace("%", "%%")
def main(host, port):
"""
Generate a circus conf to run all the plugins
"""
os.chdir(os.path.abspath(os.path.dirname(__file__)))
print("[circus]\ncheck_delay = 2\nwarmup_delay = 1\nloglevel = DEBUG\n")
for plugin in os.listdir("plugins"):
if any([plugin.startswith(x) for x in [".", "_"]]):
continue
name = plugin.split(".")[0]
print("[watcher:{name}]\n"
"cmd = ../../plugins/{plugin}\n"
"args = -i {host} -p {port}\n"
"working_dir = data/{name}\n"
"numprocesses = 1\n"
"copy_env = True\n"
"stop_children = True\n"
"stdout_stream.class = FileStream\n"
"stdout_stream.filename = ./data/logs/{name}.log\n"
"stdout_stream.time_format = {timefmt}\n"
"stderr_stream.class = FileStream\n"
"stderr_stream.filename = ./data/logs/{name}.log\n"
"stderr_stream.time_format = {timefmt}\n"
"respawn = True\n".format(host=host, port=port, name=name, plugin=plugin, timefmt=timefmt))
os.makedirs("./data/{}".format(name), exist_ok=True)
if __name__ == '__main__':
try:
host, port = sys.argv[1:]
except ValueError:
print("usage:", __file__, "host port")
sys.exit(2)
main(host, port)

16
plugins/_common.py

@ -0,0 +1,16 @@
import argparse
import logging
def get_args(parser=None):
if not parser:
parser = argparse.ArgumentParser(description="irc bot plugin")
parser.add_argument("-i", "--host", default="127.0.0.1", help="host to connect to")
parser.add_argument("-p", "--port", default=7003, help="port to connect to")
return parser.parse_args()
def get_logger():
logging.basicConfig(level=logging.INFO,
format="%(asctime)-15s %(levelname)-8s %(filename)s:%(lineno)d %(message)s")
return logging.getLogger()

26
plugins/ping.py

@ -0,0 +1,26 @@
#!/usr/bin/env python3
from _common import get_args
from contextlib import closing
from msgbus.client import MsgbusSubClient
from json import dumps, loads
def main():
args = get_args()
with closing(MsgbusSubClient(args.host, args.port)) as client:
client.sub("pyircbot_command_ping") # subscribe to .ping commnad
while True:
channel, msg = client.recv()
_, rest = msg.split(" ", 1)
channel, sender, trailing, extras = loads(rest)
channel = channel[0]
if not channel[0] == "#":
# ignore PMs
continue
print("Pong: {} in {}".format(sender, channel))
client.pub("pyircbot_send", "default privmsg {}".format(dumps([channel, "{}: pong".format(sender)])))
if __name__ == '__main__':
main()

8
requirements.txt

@ -0,0 +1,8 @@
git+http://gitlab.davepedu.com/dave/pymsgbus.git
pyzmq==16.0.2
circus==0.14.0
iowait==0.2
meld3==1.0.2
psutil==5.3.1
tornado==4.5.2
Loading…
Cancel
Save