Browse Source

Add seddy.py, LICENSE, and README

master
Thomas Mannay 7 years ago
parent
commit
b447e9fa78
3 changed files with 144 additions and 1 deletions
  1. +27
    -0
      LICENSE
  2. +5
    -1
      README.md
  3. +112
    -0
      seddy.py

+ 27
- 0
LICENSE View File

@@ -0,0 +1,27 @@
Copyright © 2016 Thomas Mannay
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 5
- 1
README.md View File

@@ -1,2 +1,6 @@
# seddy
A sedbot written for tubes
seddy was written to make use of [tubes](https://github.com/sys-fs/tubes). Two
flags are supported: i and g. Due to using Python's re engine, whatever regex
syntax Python knows about should be understood by seddy.

This project depends on Python 3 and tubes.

+ 112
- 0
seddy.py View File

@@ -0,0 +1,112 @@
#!/usr/bin/python3
# See the LICENSE file for licensing information
import re

nick = 'seddy'
server = 'chat.freenode.net'
channel = '#example'
gecos = 'A text processing bot'

receive = open('/tmp/' + server + '.in', 'r')
send = open('/tmp/' + server + '.out', 'w')

parse_msg = re.compile(' :(.*)')
parse_sed = re.compile('(?<!\\\\)/')
is_sed = re.compile('s/.*/.*')

class Queue:
size = 0
data = []
head = 0
tail = 0
count = 0

def __init__(self, size):
self.size = size
self.data = [None]*size

def enqueue(self, s):
if not self.full():
self.count += 1
self.data[self.tail] = s
self.tail = (self.tail + 1) % self.size

def dequeue(self):
if not self.empty():
self.count -= 1
s = self.data[self.head]
self.head = (self.head + 1) % self.size
return s

def full(self):
return self.size == self.count

def empty(self):
return self.head == self.count

def find(self, s):
i = self.tail-1
while True:
if i == -1:
i = self.size-1
if re.search(s, self.data[i]):
return self.data[i]
i -= 1
if i == self.tail-1:
return False

def seddy(sed, history):
regex = parse_sed.split(sed)
msg = history.find(regex[1])
f = 0

if msg == False:
return False
if 'i' in regex[3]:
f |= re.I
if "g" in regex[3]:
return re.sub(regex[1], regex[2], msg, flags=f)
else:
return re.sub(regex[1], regex[2], msg, 1)

def notice(msg):
send.write('NOTICE ' + channel + ' :' + msg + '\r\n')
send.flush()

def privmsg(msg):
send.write('PRIVMSG ' + channel + ' :' + msg + '\r\n')
send.flush()

if __name__ == "__main__":
history = Queue(48)

send.write('NICK ' + nick + '\r\n')
send.flush()
send.write('USER ' + nick + ' * 8 :' + gecos + '\r\n')
send.flush()
send.write('JOIN ' + channel + '\r\n')
send.flush()

for line in receive:
msg = parse_msg.search(line)
if msg is None:
continue
else:
msg = msg.group(1)

if history.full():
history.dequeue()
if 'PRIVMSG' in line and not is_sed.match(msg):
history.enqueue(msg)

if 'PING' in line:
send.write('PONG\r\n')
send.flush()
if '.bots' in msg or '.bot ' + nick in msg:
notice("I was written to correct your mistakes.")
if '.source ' + nick in msg:
notice('[Python] https://github.com/sys-fs/seddy')
elif is_sed.match(msg):
foo = seddy(msg, history)
if foo != False:
privmsg(re.sub('\\\\/', '/', foo))

Loading…
Cancel
Save