From 8d262f2fb12e2e0a6e995c0dc11a968bd28290f2 Mon Sep 17 00:00:00 2001 From: mio Date: Sun, 25 Oct 2020 22:04:16 +0000 Subject: [PATCH] Add cli flag for specifying config file - Add cli flag for specifying config file (This allows running multiple running instances) - Fix channel joins by waiting for server reply (thanks benharri) - Remove toot commands from help message (unannounced feature) - Update readme --- .gitignore | 4 ++-- README.md | 8 ++++---- itte.py | 35 ++++++++++++++++++++++++++++------- ramenkan.py | 22 ++++++++++++---------- ramenkan/config.sample.yml | 9 ++++++++- ramenkan/misc.yml | 2 +- 6 files changed, 55 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index c2d6616..ae67476 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ __pycache__/ *.swp nohup.out -*/config.yml -ramenkan/debug.log +*.config.yml +*.log diff --git a/README.md b/README.md index fd77d92..3d8c380 100644 --- a/README.md +++ b/README.md @@ -5,16 +5,16 @@ A very basic Python IRC bot script. ## Example: ramenkan -- Install dependencies: Mastodon.py pyyaml +- Install dependencies: `pip install Mastodon.py pyyaml` -- Copy the `ramenkan/config.sample.yml` as `ramenkan/comfig.yml` and change - settings. +- Copy the `ramenkan/config.sample.yml` as `ramenkan/default.config.yml` and + change the settings as applicable. - Run: ``` chmod +x ramenkan.py - nohup python3 ramenkan.py & + nohup python3 ramenkan.py >/dev/null 2>&1 & ``` diff --git a/itte.py b/itte.py index e603fdb..6ec095e 100644 --- a/itte.py +++ b/itte.py @@ -1,3 +1,4 @@ +import argparse import socket import yaml from time import sleep @@ -9,25 +10,40 @@ class Util: """Utility functions.""" def yml(self, yml_file): - "Open a YAML file and return a dictionary of values." - fh = open(yml_file, "r") - data = yaml.safe_load(fh) - fh.close() + """Open a YAML file and return a dictionary of values.""" + try: + fh = open(yml_file, "r") + data = yaml.safe_load(fh) + fh.close() + except TypeError: + exit("[debug][err] Cannot load YML file. Please check it exists.") return data def rand(self, lst): """Return a random item from a given list.""" return lst[randint(0, len(lst)-1)] + def cli_flags(self): + """Parse command line flags.""" + self.argp = argparse.ArgumentParser() + self.argp.add_argument("-c", "--config", help="Config file") + return self.argp.parse_args() + class IRC: """Methods for basic IRC communication.""" - def config(self, conf): + def config(self, def_conf): """Load runtime settings from a YAML config file, and returns a - dictionary of config values.""" + dictionary of config values. Looks for the file in a runtime path or in + the default location.""" self.util = Util() - cfg = self.util.yml(conf) + # Check for runtime config locatiion + flags = self.util.cli_flags() + if flags.config != "": + cfg = self.util.yml(flags.config) + else: + cfg = self.util.yml(def_conf) self.server = (cfg["server"]["host"], cfg["server"]["port"]) self.channels = cfg["channels"] self.bot_nick = cfg["bot_nick"] @@ -41,6 +57,11 @@ class IRC: """A routine that connects to a server, joins channels, and attaches the request listener hook to a loop.""" self.connect(self.server, self.bot_nick) + # Wait for server to reply before joining channels + svr_greet = self.receive() + while ("001 " + self.bot_nick) not in svr_greet: + sleep(1) + svr_greet = self.receive() self.join_channels(self.channels) while 1: data = self.receive() diff --git a/ramenkan.py b/ramenkan.py index 23d438f..e9ade03 100755 --- a/ramenkan.py +++ b/ramenkan.py @@ -16,16 +16,17 @@ class Ramen: self.links = self.util.yml("ramenkan/links.yml") self.photos = self.util.yml("ramenkan/photos.yml") self.dishes = self.util.yml("ramenkan/dishes.yml") - # Init irc object + # Init irc object and load config self.irc = IRC() - self.cfg = self.irc.config("ramenkan/config.yml") + self.cfg = self.irc.config("ramenkan/default.config.yml") # Init mastodon object - self.masto = Mastodon( - api_base_url=self.cfg["mastodon"]["base_url"], - access_token=self.cfg["mastodon"]["access_token"], - client_id=self.cfg["mastodon"]["client_id"], - client_secret=self.cfg["mastodon"]["client_secret"] - ) + if "mastodon" in self.cfg: + self.masto = Mastodon( + api_base_url=self.cfg["mastodon"]["base_url"], + access_token=self.cfg["mastodon"]["access_token"], + client_id=self.cfg["mastodon"]["client_id"], + client_secret=self.cfg["mastodon"]["client_secret"] + ) # Init request listeners self.irc.run(self.add_listeners) @@ -43,8 +44,9 @@ class Ramen: self.irc.listen(cxt, "rkveg", self.ramen_veggie) self.irc.listen(cxt, "rklink", self.link) self.irc.listen(cxt, "rkselfie", self.selfie) - self.irc.listen(cxt, "rktoot", self.toot) - self.irc.listen(cxt, "rkvtoot", self.toot_veggie) + if "mastodon" in self.cfg: + self.irc.listen(cxt, "rktoot", self.toot) + self.irc.listen(cxt, "rkvtoot", self.toot_veggie) def quit(self, cxt): """Disconnect from the server and quit.""" diff --git a/ramenkan/config.sample.yml b/ramenkan/config.sample.yml index 4025831..32a429b 100644 --- a/ramenkan/config.sample.yml +++ b/ramenkan/config.sample.yml @@ -4,7 +4,7 @@ server: port: 6667 channels: - - "#rktest" + - "#bots" bot_nick: "ramenkan" @@ -18,3 +18,10 @@ req_prefix: "!" # Print messages to stdout debug: False + +# Mastodon account +# mastodon: +# base_url: "https://example.com" +# access_token: "" +# client_secret: "" +# client_id: "" diff --git a/ramenkan/misc.yml b/ramenkan/misc.yml index 32d43fe..bd00cb9 100644 --- a/ramenkan/misc.yml +++ b/ramenkan/misc.yml @@ -1,7 +1,7 @@ rollcall: "一、二、三、らーめん缶! Hello, I am a ramen vending machine. Please type a code for service: - !help !ramen !vramen !rklink !rkselfie !rktoot !rkvtoot + !help !ramen !vramen !rklink !rkselfie - Support: +81 012-700-1MIO どうぞめしあがれ。" water: