diff --git a/bots.yaml b/bots.yaml index 9ff7ea3..c7fc988 100644 --- a/bots.yaml +++ b/bots.yaml @@ -45,8 +45,15 @@ bots: - "Tu sais {user}, si j'suis toujours si bien accompagné, c'est pour mon argent hein, c'est pas pour mon odeur... Et ça ça fait mal" - "Ou tu sors, ou j'te sors, hein, mais faudra prendre une décision." - "Et à propos de vieille truie toi comment ça va ?" + Chuck: + on_ping: + - "No, thanks." + on_ping_python: + - 'klafirc.bots.chuck_norris.on_ping' + channels: - server: irc.rezometz.org port: 6667 channel: "#campus" - bots: [sel, Macron, Patou, Claudy] + bots: [sel, Macron, Patou, Claudy, Chuck] + diff --git a/klafirc/__pycache__/__init__.cpython-36.pyc b/klafirc/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index c4c15dc..0000000 Binary files a/klafirc/__pycache__/__init__.cpython-36.pyc and /dev/null differ diff --git a/klafirc/__pycache__/__main__.cpython-36.pyc b/klafirc/__pycache__/__main__.cpython-36.pyc deleted file mode 100644 index 4ae9bb2..0000000 Binary files a/klafirc/__pycache__/__main__.cpython-36.pyc and /dev/null differ diff --git a/klafirc/__pycache__/bot.cpython-36.pyc b/klafirc/__pycache__/bot.cpython-36.pyc deleted file mode 100644 index d8e5529..0000000 Binary files a/klafirc/__pycache__/bot.cpython-36.pyc and /dev/null differ diff --git a/klafirc/__pycache__/irc.cpython-36.pyc b/klafirc/__pycache__/irc.cpython-36.pyc deleted file mode 100644 index 12c69a0..0000000 Binary files a/klafirc/__pycache__/irc.cpython-36.pyc and /dev/null differ diff --git a/klafirc/__pycache__/loader.cpython-36.pyc b/klafirc/__pycache__/loader.cpython-36.pyc deleted file mode 100644 index e361f08..0000000 Binary files a/klafirc/__pycache__/loader.cpython-36.pyc and /dev/null differ diff --git a/klafirc/__pycache__/runner.cpython-36.pyc b/klafirc/__pycache__/runner.cpython-36.pyc deleted file mode 100644 index 5df96e3..0000000 Binary files a/klafirc/__pycache__/runner.cpython-36.pyc and /dev/null differ diff --git a/klafirc/__pycache__/settings.cpython-36.pyc b/klafirc/__pycache__/settings.cpython-36.pyc deleted file mode 100644 index a534a13..0000000 Binary files a/klafirc/__pycache__/settings.cpython-36.pyc and /dev/null differ diff --git a/klafirc/bot.py b/klafirc/bot.py index 964cbd7..2802047 100644 --- a/klafirc/bot.py +++ b/klafirc/bot.py @@ -1,5 +1,6 @@ import re import random +import importlib class Bot: def __init__(self, nickname): @@ -31,11 +32,30 @@ class Bot: } self.reactions[re.compile(match.format(**context))] = reaction + def add_python_reaction(self, match, reaction): + """ Add a Python callback to the reactions. + + Args: + match: The string which, if matched will trigger the answer. + reaction: The path to the callback + """ + self.add_reaction(match, self.fetch_callback(reaction)) + def add_ping(self, reaction): """Add a reaction to a ping""" - self.pings.append(reaction) + def add_python_ping(self, reaction): + """Fetch a Python callable and add it to the pings""" + self.add_ping(self.fetch_callback(reaction)) + + def fetch_callback(self, path): + """Fetch a Python callable""" + s = path.split('.') + module, callback = '.'.join(s[:-1]), s[-1] + module = importlib.import_module(module) + return getattr(module, callback) + def get_reaction(self, user, channel, message): """Get a reaction to a message. @@ -62,11 +82,20 @@ class Bot: result = [] for m in self.reactions.keys(): if m.search(message): - result.append(self.reactions[m].format(**context)) + r = self.reactions[m] + if callable(r): + r = r(self, username, channel, message) + else: + r = r.format(**context) + result.append(r) if not result and self.ping_match.search(message): - sentence = random.choice(self.pings).format(**context) - result.append(' : '.join([username, sentence])) + r = random.choice(self.pings) + if callable(r): + r = r(self, username, channel, message) + else: + r = r.format(**context) + result.append(' : '.join([username, r])) return result diff --git a/klafirc/bots/.__init__.py.swp b/klafirc/bots/.__init__.py.swp new file mode 100644 index 0000000..ea1c7e5 Binary files /dev/null and b/klafirc/bots/.__init__.py.swp differ diff --git a/klafirc/bots/.chuck_norris.py.swp b/klafirc/bots/.chuck_norris.py.swp new file mode 100644 index 0000000..d6363dc Binary files /dev/null and b/klafirc/bots/.chuck_norris.py.swp differ diff --git a/klafirc/bots/__init__.py b/klafirc/bots/__init__.py new file mode 100644 index 0000000..a243a98 --- /dev/null +++ b/klafirc/bots/__init__.py @@ -0,0 +1 @@ +from . import chuck_norris diff --git a/klafirc/bots/__pycache__/__init__.cpython-36.pyc b/klafirc/bots/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..96a844c Binary files /dev/null and b/klafirc/bots/__pycache__/__init__.cpython-36.pyc differ diff --git a/klafirc/bots/__pycache__/chuck_norris.cpython-36.pyc b/klafirc/bots/__pycache__/chuck_norris.cpython-36.pyc new file mode 100644 index 0000000..8da7df6 Binary files /dev/null and b/klafirc/bots/__pycache__/chuck_norris.cpython-36.pyc differ diff --git a/klafirc/bots/chuck_norris.py b/klafirc/bots/chuck_norris.py new file mode 100644 index 0000000..458c6fd --- /dev/null +++ b/klafirc/bots/chuck_norris.py @@ -0,0 +1,13 @@ +""" +Fetch a random chuck norris fact and dislays it. +""" +import requests +import json + + +def get_content(): + r = requests.get('https://api.chucknorris.io/jokes/random') + return json.loads(r.content)['value'] + +def on_ping(bot, user, channel, message): + return get_content() diff --git a/klafirc/loader.py b/klafirc/loader.py index 9d8e355..81f9135 100644 --- a/klafirc/loader.py +++ b/klafirc/loader.py @@ -19,10 +19,14 @@ class Loader: b.port = port for ping in template.get('on_ping', []): b.add_ping(ping) + for ping in template.get('on_ping_python', []): + b.add_python_ping(ping) matches = template.get('on_match', []) for match in matches: b.add_reaction(match, matches[match]) + for match in template.get('on_match_python', []): + b.add_python_reaction(match, matches[match]) return b diff --git a/klafirc/settings.py b/klafirc/settings.py index 1d3ab7c..e5beb14 100644 --- a/klafirc/settings.py +++ b/klafirc/settings.py @@ -1,9 +1,14 @@ import logging from logging.handlers import RotatingFileHandler -DEBUG = True -BOT_FILE = '/etc/klafirc/bots.yaml' -LOG_FILE = '/var/log/klafirc/klafirc.log' +DEBUG = False + +if not DEBUG: + BOT_FILE = '/etc/klafirc/bots.yaml' + LOG_FILE = '/var/log/klafirc/klafirc.log' +else: + BOT_FILE = './bots.yaml' + LOG_FILE = './klafirc.log' logger = logging.getLogger() diff --git a/requirements.txt b/requirements.txt index ca9cb3b..3d0284a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,16 @@ attrs==18.1.0 Automat==0.7.0 +certifi==2018.4.16 +chardet==3.0.4 constantly==15.1.0 hyperlink==18.0.0 idna==2.7 incremental==17.5.0 +Klafirc==0.1 PyHamcrest==1.9.0 PyYAML==3.13 +requests==2.19.1 six==1.11.0 Twisted==18.7.0 +urllib3==1.23 zope.interface==4.5.0