Browse Source

Parce que Python c'est bien, en faire exécuter par les bots c'est bon esprit.

merge-requests/1/head
Klafyvel 8 years ago
parent
commit
6cc3993f3c
  1. 9
      bots.yaml
  2. BIN
      klafirc/__pycache__/__init__.cpython-36.pyc
  3. BIN
      klafirc/__pycache__/__main__.cpython-36.pyc
  4. BIN
      klafirc/__pycache__/bot.cpython-36.pyc
  5. BIN
      klafirc/__pycache__/irc.cpython-36.pyc
  6. BIN
      klafirc/__pycache__/loader.cpython-36.pyc
  7. BIN
      klafirc/__pycache__/runner.cpython-36.pyc
  8. BIN
      klafirc/__pycache__/settings.cpython-36.pyc
  9. 37
      klafirc/bot.py
  10. BIN
      klafirc/bots/.__init__.py.swp
  11. BIN
      klafirc/bots/.chuck_norris.py.swp
  12. 1
      klafirc/bots/__init__.py
  13. BIN
      klafirc/bots/__pycache__/__init__.cpython-36.pyc
  14. BIN
      klafirc/bots/__pycache__/chuck_norris.cpython-36.pyc
  15. 13
      klafirc/bots/chuck_norris.py
  16. 4
      klafirc/loader.py
  17. 11
      klafirc/settings.py
  18. 5
      requirements.txt

9
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]

BIN
klafirc/__pycache__/__init__.cpython-36.pyc

Binary file not shown.

BIN
klafirc/__pycache__/__main__.cpython-36.pyc

Binary file not shown.

BIN
klafirc/__pycache__/bot.cpython-36.pyc

Binary file not shown.

BIN
klafirc/__pycache__/irc.cpython-36.pyc

Binary file not shown.

BIN
klafirc/__pycache__/loader.cpython-36.pyc

Binary file not shown.

BIN
klafirc/__pycache__/runner.cpython-36.pyc

Binary file not shown.

BIN
klafirc/__pycache__/settings.cpython-36.pyc

Binary file not shown.

37
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

BIN
klafirc/bots/.__init__.py.swp

Binary file not shown.

BIN
klafirc/bots/.chuck_norris.py.swp

Binary file not shown.

1
klafirc/bots/__init__.py

@ -0,0 +1 @@
from . import chuck_norris

BIN
klafirc/bots/__pycache__/__init__.cpython-36.pyc

Binary file not shown.

BIN
klafirc/bots/__pycache__/chuck_norris.cpython-36.pyc

Binary file not shown.

13
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()

4
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

11
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()

5
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

Loading…
Cancel
Save