Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

145 lines (118 sloc) 4.564 kB
import asyncore, random
from hashlib import sha512
from datetime import datetime, timedelta
from ConfigParser import ConfigParser
from sys import argv, exit
from ircasync import *
from subprocess import Popen, PIPE
ETHICAL_MESSAGES = [
[ # unethical
'your chairman resigns after the plan is revealed to the public by "60 Minutes".',
'your company was fined 1.4m$ after a probe into your activities.',
'your company was fined a record 600m$ by the EU after reports of bribery.',
'accounting irregularities were exposed by a whistleblower, resulting in a fine for your company.',
'the prime minister announces new taxes on employees because of a percieved lack of local investment by your company in rural areas.',
],
[ # ethical
'a jury found no misconduct by board members.',
'testimony of key witnesses had to be striken from the record after finding they were all under the influence of drugs.',
'the prime minister issued a public apology over the government\'s handling of your company\'s case in the media.',
],
[ # unsure
'a royal commission is establisted into your activities.',
'the government announces a bailout package for your company.',
'your company\'s charity activities in the third world were recognised by the judge in giving your company a lenient sentence.',
'your company owns a 70% share in local newspapers and television stations, meaning this incident is never brought to the public\'s attention.',
],
]
ETHICAL_STATES = [
'unethical',
'ethical',
'unsure',
]
config = ConfigParser()
try:
config.readfp(open(argv[1]))
except:
try:
config.readfp(open('ethicsbot.ini'))
except:
print "Syntax:"
print " %s [config]" % argv[0]
print ""
print "If no configuration file is specified or there was an error, it will default to `ethicsbot.ini'."
print "If there was a failure reading the configuration, it will display this message."
exit(1)
# read config
SERVER = config.get('ethicsbot', 'server')
try: PORT = config.getint('ethicsbot', 'port')
except: PORT = DEFAULT_PORT
NICK = config.get('ethicsbot', 'nick')
CHANNEL = config.get('ethicsbot', 'channel')
VERSION = 'ethicsbot hg:%s; http://hg.micolous.id.au/ircbots/'
try: VERSION = VERSION % Popen(["hg","id"], stdout=PIPE).communicate()[0].strip()
except: VERSION = VERSION % 'unknown'
del Popen, PIPE
try: FLOOD_COOLDOWN = timedelta(seconds=config.getint('ethicsbot', 'flood_cooldown'))
except: FLOOD_COOLDOWN = timedelta(seconds=5)
try: NICKSERV_PASS = config.get('ethicsbot', 'nickserv_pass')
except: NICKSERV_PASS = None
message_buffer = []
last_message = datetime.now()
flooders = []
ignore_list = []
if config.has_section('ignore'):
for k,v in config.items('ignore'):
try:
ignore_list.append(re.compile(v, re.I))
except Exception, ex:
print "Error compiling regular expression in ignore list (%s):" % k
print " %s" % v
print ex
exit(1)
# main code
def handle_msg(event, match):
global message_buffer, MAX_MESSAGES, last_message, flooders, CHANNEL
msg = event.text
if event.channel.lower() != CHANNEL.lower():
# ignore messages not from our channel
return
if msg.startswith('?ethical'):
for item in ignore_list:
if item.search(event.origin) != None:
# ignore list item hit
print "Ignoring message from %s because of: %s" % (event.origin, item.pattern)
return
# now flood protect!
delta = event.when - last_message
last_message = event.when
if delta < FLOOD_COOLDOWN:
# 5 seconds between requests
# any more are ignored
print "Flood protection hit, %s of %s seconds were waited" % (delta.seconds, FLOOD_COOLDOWN.seconds)
return
parts = msg.split(' ')
query = (''.join(parts[1:])).lower()
if len(query) == 0:
event.reply("%s: you must give me an ethical conundrum to process!" % event.nick)
return
# hash the request
h = sha512()
h.update(query)
ethical = 0
for c in h.digest():
for x in xrange(1,9):
if ord(c) & (2 ** x) > 1:
ethical = (ethical + 1) % 3
event.reply('%s: (%s) %s' % (event.nick, ETHICAL_STATES[ethical], random.choice(ETHICAL_MESSAGES[ethical])))
def handle_welcome(event, match):
global NICKSERV_PASS
# Compliance with most network's rules to set this mode on connect.
event.connection.usermode("+B")
if NICKSERV_PASS != None:
event.connection.todo(['NickServ', 'identify', NICKSERV_PASS])
irc = IRC(nick=NICK, start_channels=[CHANNEL], version=VERSION)
irc.bind(handle_msg, PRIVMSG)
irc.bind(handle_welcome, RPL_WELCOME)
irc.make_conn(SERVER, PORT)
asyncore.loop()
Jump to Line
Something went wrong with that request. Please try again.