2014-09-13 19:24:08 +02:00
|
|
|
# Welcome to WelcomeBot. Find source, documentation, etc here: https://github.com/shaunagm/WelcomeBot/ Licensed https://creativecommons.org/licenses/by-sa/2.0/
|
2014-05-01 23:19:45 +02:00
|
|
|
|
2013-12-22 01:03:18 +01:00
|
|
|
# Import some necessary libraries.
|
2014-09-13 19:24:08 +02:00
|
|
|
import socket, sys, time, csv, Queue, random, re, pdb, select
|
2013-12-22 02:53:58 +01:00
|
|
|
from threading import Thread
|
2014-05-03 17:06:19 +02:00
|
|
|
|
2014-05-01 23:19:45 +02:00
|
|
|
# Some basic variables used to configure the bot.
|
2014-05-05 23:57:46 +02:00
|
|
|
server = "irc.freenode.net"
|
2014-09-13 21:14:32 +02:00
|
|
|
channel = "#openhatch" # Please use #openhatch-bots rather than #openhatch for testing
|
|
|
|
botnick = "WelcomeBot"
|
|
|
|
channel_greeters = ['shauna', 'paulproteus', 'marktraceur']
|
2014-09-13 19:24:08 +02:00
|
|
|
hello_list = [r'hello', r'hi', r'hey', r'yo', r'sup']
|
2014-05-03 17:06:19 +02:00
|
|
|
help_list = [r'help', r'info', r'faq', r'explain yourself']
|
2014-05-01 23:19:45 +02:00
|
|
|
|
2013-12-22 02:53:58 +01:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
#########################
|
|
|
|
### Class Definitions ###
|
|
|
|
#########################
|
|
|
|
|
|
|
|
# Defines a bot
|
|
|
|
class Bot(object):
|
|
|
|
|
|
|
|
def __init__(self, nick_source='nicks.csv', wait_time=60):
|
|
|
|
self.nick_source = nick_source
|
|
|
|
self.wait_time = wait_time
|
|
|
|
self.known_nicks = []
|
|
|
|
with open(self.nick_source, 'rb') as csv_file:
|
|
|
|
csv_file_data = csv.reader(csv_file, delimiter=',', quotechar='|')
|
|
|
|
for row in csv_file_data:
|
|
|
|
self.known_nicks.append(row)
|
|
|
|
self.newcomers = []
|
|
|
|
self.hello_regex = re.compile(get_regex(hello_list), re.I) # Regexed version of hello list
|
|
|
|
self.help_regex = re.compile(get_regex(help_list), re.I) # Regexed version of help list
|
|
|
|
|
|
|
|
# Adds the current newcomer's nick to nicks.csv and known_nicks.
|
|
|
|
def add_known_nick(self,new_known_nick):
|
|
|
|
new_known_nick = new_known_nick.replace("_", "")
|
|
|
|
self.known_nicks.append([new_known_nick])
|
|
|
|
with open(self.nick_source, 'a') as csvfile:
|
|
|
|
nickwriter = csv.writer(csvfile, delimiter=',', quotechar='|',
|
|
|
|
quoting=csv.QUOTE_MINIMAL)
|
|
|
|
nickwriter.writerow([new_known_nick])
|
2013-12-22 01:03:18 +01:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
# Defines a newcomer object
|
|
|
|
class NewComer(object):
|
2013-12-22 01:03:18 +01:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
def __init__(self, nick, bot):
|
|
|
|
self.nick = nick
|
|
|
|
self.born = time.time()
|
|
|
|
bot.newcomers.append(self)
|
2013-12-22 01:03:18 +01:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
def around_for(self):
|
|
|
|
return time.time() - self.born
|
2014-01-11 06:50:13 +01:00
|
|
|
|
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
#########################
|
|
|
|
### Startup Functions ###
|
|
|
|
#########################
|
2014-05-01 23:19:45 +02:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
# Creates a socket that will be used to send and receive messages,
|
|
|
|
# then connects the socket to an IRC server and joins the channel.
|
|
|
|
def irc_start(): # pragma: no cover (this excludes this function from testing)
|
|
|
|
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
ircsock.connect((server, 6667)) # Here we connect to server using port 6667.
|
|
|
|
return ircsock
|
|
|
|
|
|
|
|
def join_irc(ircsock):
|
|
|
|
ircsock.send("USER {0} {0} {0} :This is http://openhatch.org/'s greeter bot"
|
|
|
|
".\n".format(botnick)) # bot authentication
|
|
|
|
ircsock.send("NICK {}\n".format(botnick)) # Assign the nick to the bot.
|
|
|
|
ircsock.send("JOIN {} \n".format(channel)) # Joins channel
|
2014-05-01 23:19:45 +02:00
|
|
|
|
|
|
|
# Reads the messages from the server and adds them to the Queue and prints
|
|
|
|
# them to the console. This function will be run in a thread, see below.
|
2014-09-13 19:24:08 +02:00
|
|
|
def msg_handler(ircsock): # pragma: no cover (this excludes this function from testing)
|
|
|
|
new_msg = ircsock.recv(2048) # receive data from the server
|
|
|
|
new_msg = new_msg.strip('\n\r') # removing any unnecessary linebreaks
|
|
|
|
print(new_msg) #### Potentially make this a log instead?
|
|
|
|
return new_msg
|
2014-05-01 23:19:45 +02:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
# Called by bot on startup. Builds a regex that matches one of the options + (space) botnick.
|
|
|
|
def get_regex(options):
|
|
|
|
pattern = "("
|
|
|
|
for s in options:
|
|
|
|
pattern += s
|
|
|
|
pattern += "|"
|
|
|
|
pattern = pattern[:-1]
|
|
|
|
pattern += ").({})".format(botnick)
|
|
|
|
return pattern
|
2013-12-22 02:53:58 +01:00
|
|
|
|
2013-12-22 01:03:18 +01:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
#########################
|
|
|
|
### General Functions ###
|
|
|
|
#########################
|
2013-12-22 01:03:18 +01:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
# Welcomes the "person" passed to it.
|
|
|
|
def welcome_nick(newcomer, ircsock):
|
|
|
|
ircsock.send("PRIVMSG {0} :Welcome {1}! The channel is pretty quiet "
|
|
|
|
"right now, so I thought I'd say hello, and ping some people "
|
|
|
|
"(like {2}) that you're here. If no one responds for a "
|
|
|
|
"while, try emailing us at hello@openhatch.org or just try "
|
|
|
|
"coming back later. FYI, you're now on my list of known "
|
|
|
|
"nicknames, so I won't bother you again."
|
|
|
|
"\n".format(channel, newcomer, greeter_string("and", channel_greeters)))
|
|
|
|
|
|
|
|
# Checks and manages the status of newcomers.
|
|
|
|
def process_newcomers(bot, newcomerlist, ircsock, welcome=1):
|
|
|
|
for person in newcomerlist:
|
|
|
|
if welcome == 1:
|
|
|
|
welcome_nick(person.nick, ircsock)
|
|
|
|
bot.add_known_nick(person.nick)
|
|
|
|
bot.newcomers.remove(person)
|
|
|
|
|
|
|
|
# Checks for messages.
|
|
|
|
def parse_messages(ircmsg):
|
|
|
|
try:
|
|
|
|
actor = ircmsg.split(":")[1].split("!")[0] # and get the nick of the msg sender
|
2014-09-14 00:13:10 +02:00
|
|
|
return " ".join(ircmsg.split()), actor
|
2014-09-13 19:24:08 +02:00
|
|
|
except:
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
# Parses messages and responds to them appropriately.
|
|
|
|
def message_response(bot, ircmsg, actor, ircsock):
|
|
|
|
|
|
|
|
# if someone other than a newcomer speaks into the channel
|
|
|
|
if ircmsg.find("PRIVMSG " + channel) != -1 and actor not in [i.nick for i in bot.newcomers]:
|
|
|
|
process_newcomers(bot,bot.newcomers, ircsock, welcome=0) # Process/check newcomers without welcoming them
|
|
|
|
|
|
|
|
# if someone (other than the bot) joins the channel
|
|
|
|
if ircmsg.find("JOIN " + channel) != -1 and actor != botnick:
|
2014-10-18 06:24:25 +02:00
|
|
|
if [actor.replace("_", "")] not in bot.known_nicks + [i.nick for i in bot.newcomers]: # And they're new
|
2014-09-13 19:24:08 +02:00
|
|
|
NewComer(actor, bot)
|
2014-09-15 07:08:58 +02:00
|
|
|
|
|
|
|
# if someone changes their nick while still in newcomers update that nick
|
|
|
|
if ircmsg.find("NICK :") != -1 and actor != botnick:
|
|
|
|
for i in bot.newcomers: # if that person was in the newlist
|
|
|
|
if i.nick == actor:
|
|
|
|
i.nick = ircmsg.split(":")[2] # update to new nick
|
2014-09-13 19:24:08 +02:00
|
|
|
|
|
|
|
# If someone parts or quits the #channel...
|
|
|
|
if ircmsg.find("PART " + channel) != -1 or ircmsg.find("QUIT") != -1:
|
|
|
|
for i in bot.newcomers: # and that person is on the newlist
|
|
|
|
if actor == i.nick:
|
|
|
|
bot.newcomers.remove(i) # remove them from the list
|
|
|
|
|
|
|
|
# If someone talks to (or refers to) the bot.
|
|
|
|
if botnick.lower() and "PRIVMSG".lower() in ircmsg.lower():
|
|
|
|
if bot.hello_regex.search(ircmsg):
|
|
|
|
bot_hello(random.choice(hello_list), actor, ircsock)
|
|
|
|
if bot.help_regex.search(ircmsg):
|
|
|
|
bot_help(ircsock)
|
|
|
|
|
|
|
|
# If someone tries to change the wait time...
|
|
|
|
if ircmsg.find(botnick + " --wait-time ") != -1:
|
|
|
|
bot.wait_time = wait_time_change(actor, ircmsg, ircsock) # call this to check and change it
|
|
|
|
|
|
|
|
# If the server pings us then we've got to respond!
|
|
|
|
if ircmsg.find("PING :") != -1:
|
|
|
|
pong(ircsock)
|
|
|
|
|
|
|
|
|
|
|
|
#############################################################
|
|
|
|
### Bot Response Functions (called by message_response()) ###
|
|
|
|
#############################################################
|
|
|
|
|
|
|
|
# Responds to a user that inputs "Hello Mybot".
|
|
|
|
def bot_hello(greeting, actor, ircsock):
|
2014-05-01 23:19:45 +02:00
|
|
|
ircsock.send("PRIVMSG {0} :{1} {2}\n".format(channel, greeting, actor))
|
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
# Explains what the bot is when queried.
|
|
|
|
def bot_help(ircsock):
|
2014-05-01 23:19:45 +02:00
|
|
|
ircsock.send("PRIVMSG {} :I'm a bot! I'm from here <https://github"
|
|
|
|
".com/shaunagm/oh-irc-bot>. You can change my behavior by "
|
2014-05-04 01:46:56 +02:00
|
|
|
"submitting a pull request or by talking to shauna"
|
|
|
|
".\n".format(channel))
|
|
|
|
|
|
|
|
# Returns a grammatically correct string of the channel_greeters.
|
2014-09-13 19:24:08 +02:00
|
|
|
def greeter_string(conjunction, greeters):
|
|
|
|
greeterstring = ""
|
|
|
|
if len(greeters) > 2:
|
|
|
|
for name in greeters[:-1]:
|
|
|
|
greeterstring += "{}, ".format(name)
|
|
|
|
greeterstring += "{0} {1}".format(conjunction, greeters[-1])
|
|
|
|
elif len(greeters) == 2:
|
|
|
|
greeterstring = "{0} {1} {2}".format(greeters[0], conjunction,
|
|
|
|
greeters[1])
|
2014-05-04 01:46:56 +02:00
|
|
|
else:
|
2014-09-13 19:24:08 +02:00
|
|
|
greeterstring = greeters[0]
|
|
|
|
return greeterstring
|
2014-01-11 02:19:10 +01:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
# Changes the wait time from the channel.
|
|
|
|
def wait_time_change(actor, ircmsg, ircsock):
|
2014-05-04 01:46:56 +02:00
|
|
|
for admin in channel_greeters:
|
2014-05-01 23:19:45 +02:00
|
|
|
if actor == admin:
|
2014-09-13 19:24:08 +02:00
|
|
|
finder = re.search(r'\d\d*', re.search(r'--wait-time \d\d*', ircmsg)
|
|
|
|
.group())
|
2014-05-01 23:19:45 +02:00
|
|
|
ircsock.send("PRIVMSG {0} :{1} the wait time is changing to {2} "
|
|
|
|
"seconds.\n".format(channel, actor, finder.group()))
|
2014-05-05 23:57:46 +02:00
|
|
|
return int(finder.group())
|
2014-09-13 19:24:08 +02:00
|
|
|
ircsock.send("PRIVMSG {0} :{1} you are not authorized to make that "
|
|
|
|
"change. Please contact one of the channel greeters, like {2}, for "
|
|
|
|
"assistance.\n".format(channel, actor, greeter_string("or", channel_greeters)))
|
2014-05-01 23:19:45 +02:00
|
|
|
|
2014-09-13 19:24:08 +02:00
|
|
|
# Responds to server Pings.
|
|
|
|
def pong(ircsock):
|
|
|
|
ircsock.send("PONG :pingis\n")
|
|
|
|
|
|
|
|
|
|
|
|
##########################
|
|
|
|
### The main function. ###
|
|
|
|
##########################
|
|
|
|
|
|
|
|
def main():
|
|
|
|
ircsock = irc_start()
|
|
|
|
join_irc(ircsock)
|
|
|
|
WelcomeBot = Bot()
|
|
|
|
while 1: # Loop forever
|
2014-10-18 06:24:25 +02:00
|
|
|
ready_to_read, b, c = select.select([ircsock],[],[], 1) # b&c are ignored here
|
|
|
|
process_newcomers(WelcomeBot, [i for i in WelcomeBot.newcomers if i.around_for() > WelcomeBot.wait_time],ircsock)
|
2014-09-13 19:24:08 +02:00
|
|
|
if ready_to_read:
|
2014-10-18 06:24:25 +02:00
|
|
|
ircmsg = msg_handler(ircsock) # gets message from ircsock
|
|
|
|
ircmsg, actor = parse_messages(ircmsg) # parses it or returns None
|
2014-09-13 19:24:08 +02:00
|
|
|
if ircmsg is not None: # If we were able to parse it
|
|
|
|
message_response(WelcomeBot, ircmsg, actor, ircsock) # Respond to the parsed message
|
|
|
|
|
|
|
|
if __name__ == "__main__": # This line tells the interpreter to only execute main() if the program is being run, not imported.
|
|
|
|
sys.exit(main())
|
2014-05-01 23:19:45 +02:00
|
|
|
|