Skip to content
This repository has been archived by the owner on Jun 27, 2018. It is now read-only.

Commit

Permalink
Enable private queries, add default_template parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tilpner committed Sep 27, 2015
1 parent 0c3b5a4 commit 9a4ab5e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 53 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ something like this:
- "#bots"
keys: [null, "hunter2"]
password: abc123abc
triggers:
- template: &template "
templates:
- &template "
#![allow(dead_code, unused_variables)]

static VERSION: &'static str = \"%(version)s\";
Expand All @@ -68,48 +68,53 @@ something like this:
%(input)s
});
}"
- &no_template ""
default_template: *template
triggers:
- template: *template
channel: "stable"
triggers:
- "playbot:(.*)"
- "rusti:(.*)"
- "playbot:(.*)"
- "rusti:(.*)"
- ">>(.*)"
- "s\\s*>>(.*)"
- "stable\\s*>>(.*)"

- template: *template
channel: "beta"
triggers:
- "b\\s*>>(.*)"
- "beta\\s*>>(.*)"

- template: *template
channel: "nightly"
triggers:
- "n\\s*>>(.*)"
- "nightly\\s*>>(.*)"

- template: &no_template ""
- template: *no_template
channel: "stable"
triggers:
- "playbot-mini:(.*)"
- "playbot-mini:(.*)"
- "rusti-mini:(.*)"
- ">(.*)"
- "s\\s*>(.*)"
- "stable\\s*>(.*)"

- template: *no_template
channel: "beta"
triggers:
- "b\\s*>(.*)"
- "beta\\s*>(.*)"

- template: *no_template
channel: "nightly"
triggers:
- "n\\s*>(.*)"
- "nightly\\s*>(.*)"
```
Note that the channel key is `null` for public channels.
Note that the channel key is `null` for public channels.


[playpen]: https://github.com/thestinger/playpen
Expand Down
63 changes: 17 additions & 46 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,6 @@ def bitly(command):
print(response)
return "failed to shorten url"

def gist(version, code):
url = "https://api.github.com/gists"

r = requests.post("https://api.github.com/gists",
params = {"access_token": shorten_key.github},
json = {
"description": "Shared via Rust Playground",
"public": True,
"files": {
"playbot.rs": {
"content": code
}
}
})

if r.status_code == 201:
response = r.json()

gist_id = response["id"]
gist_url = response["html_url"]

play_url = "https://play.rust-lang.org/?gist=" + quote(gist_id) + "&version=" + version
return "output truncated; full output at: " + play_url
else:
return "failed to shorten url"

def pastebin(channel, code):
if channel == "nightly":
return gist(channel, code)
else:
return bitly(code)

def evaluate(code, channel, template):
if "%(version)s" in template and "%(input)s" in template:
version, _ = playpen.execute(channel, "/bin/dash",
Expand All @@ -84,16 +52,16 @@ def evaluate(code, channel, template):

for line in lines:
if len(line) > 150:
return pastebin(channel, code)
return bitly(code)

limit = 3
if len(lines) > limit:
return "\n".join(lines[:limit - 1] + [pastebin(channel, code)])
return "\n".join(lines[:limit - 1] + [bitly(code)])

return out

class RustEvalbot(irc.client.SimpleIRCClient):
def __init__(self, nickname, channels, keys, triggers):
def __init__(self, nickname, channels, keys, triggers, default_template):
irc.client.SimpleIRCClient.__init__(self)
irc.client.ServerConnection.buffer_class = irc.buffer.LenientDecodingLineBuffer
self.nickname = nickname
Expand All @@ -102,6 +70,7 @@ def __init__(self, nickname, channels, keys, triggers):
for t in triggers:
t['triggers'] = [re.compile(s) for s in t['triggers']]
self.triggers = triggers
self.default_template = default_template

def _run(self, irc_channel, code, rust_channel, with_template):
result = evaluate(code, rust_channel, with_template)
Expand Down Expand Up @@ -137,15 +106,16 @@ def on_privmsg(self, connection, event):
print("{} {}: {}".format(event.target, nickname, msg))
# Allow for the same triggers like in channels,
# but fallback to stable with template.
for (trigger, channel, with_template) in self.triggers:
res = trigger.match(msg)
if res:
code = res.group(1)
self._run(nickname, code, channel, with_template)
# Only one match per message
return
for t in self.triggers:
for r in t['triggers']:
res = r.match(msg)
if res:
code = res.group(1)
self._run(nickname, code, t['channel'], t['template'])
# Only one match per message
return

self._run(nickname, msg, "stable", True)
self._run(nickname, msg, "stable", self.default_template)

def on_disconnect(self, connection, event):
sleep(10)
Expand All @@ -159,8 +129,8 @@ def on_kick(self, connection, event):
else:
connection.join(channel, key)

def start(nickname, server, port, channels, keys, triggers):
client = RustEvalbot(nickname, channels, keys, triggers)
def start(nickname, server, port, channels, keys, triggers, default_template):
client = RustEvalbot(nickname, channels, keys, triggers, default_template)
try:
client.connect(server, port, nickname)
client.connection.set_keepalive(30)
Expand All @@ -181,7 +151,8 @@ def main():
cfg["port"],
cfg["channels"],
cfg["keys"],
cfg["triggers"]))
cfg["triggers"],
cfg["default_template"]))
thread.start()

if __name__ == "__main__":
Expand Down

0 comments on commit 9a4ab5e

Please sign in to comment.