master
Michael Murtaugh 4 years ago
commit b26490f108

1
.gitignore vendored

@ -0,0 +1 @@
venv/

@ -0,0 +1,11 @@
A small selection of IRC bots
=================================
simplebot.py is a really minimal bot (meant as a proof of concept and starting point) written in standard library python 3. The source is from IRC Hacks, Paul Mutton, 2004.
ircpipebot and reversebot make use of the irc module, installable with:
pip3 install irc

@ -0,0 +1,77 @@
from __future__ import print_function
import irc.bot
import sys, time
from _thread import start_new_thread
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in range(0, len(l), n):
yield l[i:i+n]
class Bot (irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, input, port=6667, idle=False):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.input = input
self.channel = channel
if idle:
start_new_thread(self.idle, ())
def on_welcome(self, c, e):
c.join(self.channel)
print ("join", file=sys.stderr)
start_new_thread(self.receive, (c,))
def idle(self):
while True:
tstr = time.strftime("%H:%M:%S", time.localtime())
out = u"({0}) {1}: {2}".format(tstr, "_system", "idle")
sys.stdout.write(out.encode("utf-8")+"\n")
sys.stdout.flush()
time.sleep(1)
def receive (self, c):
if self.input == sys.stdin:
# print ("reading from stdin", file=sys.stderr)
_in = sys.stdin
else:
# print ("reading from {0}".format(self.input), file=sys.stderr)
_in = open(self.input)
while True:
line = _in.readline()
if line == '':
break
# line = line.rstrip().decode("utf-8")
line = line.rstrip()
if line:
# print ("read line: {0}".format(line), file=sys.stderr)
# CHUNK IT
for chunk in chunks(line, 400):
c.privmsg(self.channel, chunk)
def on_pubmsg(self, c, e):
# e.target, e.source, e.arguments, e.type
msg = e.arguments[0]
tstr = time.strftime("%H:%M:%S", time.localtime())
nick = e.source.split("!", 1)[0]
out = u"({0}) {1}: {2}".format(tstr, nick, msg)
# sys.stdout.write(out.encode("utf-8")+"\n")
sys.stdout.write(out+"\n")
sys.stdout.flush()
# print ("pubmsg: {0}".format(msg), file=sys.stderr)
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--server', default='irc.freenode.net', help='server hostname (default: localhost)')
parser.add_argument('--port', default=6667, type=int, help='server port (default: 6667)')
parser.add_argument('--channel', default='#botopera', help='channel to join (default: #botopera)')
parser.add_argument('--nickname', default='pipebot', help='bot nickname (default: pipebot)')
parser.add_argument('--input', default=sys.stdin, help='input')
parser.add_argument('--idle', default=False, action="store_true", help='output idle messages')
args = parser.parse_args()
bot = Bot(args.channel, args.nickname, args.server, args.input, port=args.port, idle=args.idle)
bot.start()

@ -0,0 +1,37 @@
import irc.bot
class ReverseBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
def on_welcome(self, c, e):
c.join(self.channel)
print ("join")
def on_privmsg(self, c, e):
pass
def on_pubmsg(self, c, e):
# print e.arguments, e.target, e.source, e.arguments, e.type
msg = e.arguments[0]
if "bot" in msg:
words = msg.split()
words.reverse()
msg = " ".join(words)
c.privmsg(self.channel, msg)
if __name__ == "__main__":
import sys, argparse
parser = argparse.ArgumentParser(description='I am a bot!')
parser.add_argument('--server', default='irc.freenode.net', help='server hostname (default: irc.freenode.net)')
parser.add_argument('--port', default=6667, type=int, help='server port (default: 6667)')
parser.add_argument('--channel', default='#botopera', help='channel to join (default: #botopera)')
parser.add_argument('--nickname', default='reverseb0t', help='bot nickname (default: botoperahost)')
args = parser.parse_args()
bot = ReverseBot(args.channel, args.nickname, args.server, args.port)
bot.start()

@ -0,0 +1,23 @@
import sys
import socket
import string
HOST="irc.freenode.net"
PORT=6667
NICK="MauBot"
IDENT="maubot"
REALNAME="MauritsBot"
readbuffer=""
s=socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
while 1:
readbuffer=readbuffer+s.recv(1024)
temp=string.split(readbuffer, "\n")
readbuffer=temp.pop( )
for line in temp:
line=string.rstrip(line)
line=string.split(line)
if(line[0]=="PING"):
s.send("PONG %s\r\n" % line[1])
Loading…
Cancel
Save