From bfffde1957a165c4676de0ac3aa780181b225282 Mon Sep 17 00:00:00 2001 From: zgrep Date: Thu, 19 Jul 2018 18:40:41 -0400 Subject: [PATCH] Some initial scribblings. --- bin/connect | 25 +++++++++++++++++++++++++ bin/hateweek/end | 5 +++++ bin/hateweek/isnow | 3 +++ bin/hateweek/start | 5 +++++ readme.md | 40 ++++++++++++++++++++++++++++++++++++++++ sudoers | 1 + xed.py | 25 ++++++++++++++++++++++--- 7 files changed, 101 insertions(+), 3 deletions(-) create mode 100755 bin/connect create mode 100755 bin/hateweek/end create mode 100755 bin/hateweek/isnow create mode 100755 bin/hateweek/start create mode 100644 readme.md create mode 100644 sudoers diff --git a/bin/connect b/bin/connect new file mode 100755 index 0000000..7192a01 --- /dev/null +++ b/bin/connect @@ -0,0 +1,25 @@ +#!/bin/sh + +alias logdate=date +'%Y-%m-%d %H:%M:%S' + +cd "${1:-/home/happybot}" +export PATH="$(pwd)/bin:$PATH" + +if test -e input; then rm input; fi +if test -e /tmp/happybot; then rm -r /tmp/happybot; fi +mkfifo input +mkdir /tmp/happybot + +if hateweek/isnow; then + echo 'hatebot' > /tmp/happybot/nick; +else + echo 'happybot' > /tmp/happybot/nick; +fi + +tail -f input | while read -r line; do + printf '%s :%s %s\n' "$(logdate)" "$(cat /tmp/happybot/nick)" "$line" >> log.txt + printf '%s\n' "$line" +done | openssl s_client -quiet -connect irc.freenode.net:6697 | while read -r line; do + printf '%s %s\n' "$(logdate)" "$line" >> log.txt + callirc "$line" & +done diff --git a/bin/hateweek/end b/bin/hateweek/end new file mode 100755 index 0000000..d815d11 --- /dev/null +++ b/bin/hateweek/end @@ -0,0 +1,5 @@ +#!/bin/sh + +sed -i 's/0/1/' bin/hateweek/isnow +raw 'NICK happybot' +privmsg '#offtopia' 'Hate how hateweek is over!' diff --git a/bin/hateweek/isnow b/bin/hateweek/isnow new file mode 100755 index 0000000..2bb8d86 --- /dev/null +++ b/bin/hateweek/isnow @@ -0,0 +1,3 @@ +#!/bin/sh + +exit 1 diff --git a/bin/hateweek/start b/bin/hateweek/start new file mode 100755 index 0000000..9980652 --- /dev/null +++ b/bin/hateweek/start @@ -0,0 +1,5 @@ +#!/bin/sh + +sed -i 's/1/0/' bin/hateweek/isnow +raw 'NICK hatebot' +privmsg '#offtopia' "Hateweek is here! Hate how it's only once a year!" diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..224811c --- /dev/null +++ b/readme.md @@ -0,0 +1,40 @@ +`input`: +- The named pipe to talk as the bot. + +`bin/`: +- `raw` - Sends `$1` to freenode. +- `reply` - Uses env vars to send message `$@`. +- `result` - Like reply, but does not prepend `nick:`. +- `privmsg` - Sends privmsg to `$1`, with message `shift;$@`. +- `notice` - Sends privmsg to `$1`, with message `shift;$@`. +- `action` - Sends ctcp action to `$1`, with message `shift;$@`. +- `ctcp` - Sends a ctcp query, returns the response. +- `connect` - Connects to IRC, calls commands in `irc/`, listens to `input`, + and writes to log the file. +- `hateweek/start` - Begins hateweek mode. +- `hateweek/end` - Ends hateweek mode. +- `hateweek/isnow` - Check to see if hateweek is now (returns 0 if now, 1 if + not now). + +`log.txt`: +- The single file that has all of the logs. +- Uses the IRC format with a timestamp prepended. + +`irc/`: +- IRC commands are run as shell commands. +- `bin/` is in their `PATH`. +- Set up env vars before calling various functions. +- Add data to log files if needed. + +Env vars: +- `$acct`: NickServ account name, or `\*`. +- `$nick` +- `$cmd` +- `$where` +- `$msg` + +`/tmp/happybot/`: +- directory for temporary information +- `nick` - The bot's nick. +- `acct` - Look up account name by nick. + - Either a directory, or a file. diff --git a/sudoers b/sudoers new file mode 100644 index 0000000..32b56f2 --- /dev/null +++ b/sudoers @@ -0,0 +1 @@ +happybot ALL=(root) NOPASSWD: /sbin/rc-service happybot diff --git a/xed.py b/xed.py index b8e9cec..9d6c584 100644 --- a/xed.py +++ b/xed.py @@ -1,5 +1,4 @@ -#!/usr/bin/env python3 - +#!/usr/bin/env python3 from lark import Lark, Transformer, ParseError, Tree parser = Lark(r''' @@ -13,7 +12,7 @@ parser = Lark(r''' | bracketliteral "-" bracketliteral brackets : "[" range+ "]" -> either - char : /[^\\\[\]\{\}\(\)\|\^~\?]/ + char : /[^\\\[\]\{\}\(\)\|\^~\?!]/ | "\\" /\D/ | // @@ -27,6 +26,9 @@ parser = Lark(r''' | concat_func "~" -> reverse | concat_func "~" NUMBER -> roll | concat_func "~{" NUMBER ["," DIGITS] "}" -> roll + | concat_func "!" -> collapse + | concat_func "!" DIGIT+ -> collapse + | concat_func "!{" numrange ("," numrange)* "}" -> collapse_ranges | concat_func "\\" DIGIT+ -> index | concat_func "\\{" numrange ("," numrange)* "}" -> index_ranges @@ -144,6 +146,23 @@ class Expand(Transformer): result.append(x[i % len(x)]) return result + def collapse(self, args): + result, x = '', args[0] + if len(args) > 1: + for i in args[1:]: + result += x[int(i.value) % len(x)] + else: + result = ''.join(args[0]) + return [result] + + def collapse_ranges(self, args): + result, x = '', args[0] + for arg in args[1:]: + for i in arg: + result += x[i % len(x)] + return [result] + + def concat_repeat(self, args): return self.concat([args[0]] * int(args[1].value)) def either_repeat(self, args):