implemented threading/queueing

This commit is contained in:
Shauna 2013-12-21 20:53:58 -05:00
parent 2fb0d12c31
commit 1aab2059a9

62
bot.py
View File

@ -1,27 +1,39 @@
# Import some necessary libraries. # Import some necessary libraries.
import socket import socket
import time import time
import Queue
from threading import Thread
# Some basic variables used to configure the bot # Some basic variables used to configure the bot
server = "irc.freenode.net" # Server server = "irc.freenode.net" # Server
channel = "#openscienceframework" # Channel channel = "#openscienceframework" # Channel
botnick = "ShaunaBot" # Your bots nick botnick = "ShaunaBot" # Your bots nick
# Connecting to the channel, defining basic functions # Connects to server and joins channel
def ping(): # Responds to server Pings.
ircsock.send("PONG :pingis\n")
def joinchan(chan): # This function is used to join channels.
ircsock.send("JOIN "+ chan +"\n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667 ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication
ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
def joinchan(chan): # This function is used to join channels.
ircsock.send("JOIN "+ chan +"\n")
joinchan(channel) # Join the channel using the functions we previously defined joinchan(channel) # Join the channel using the functions we previously defined
# Creates separate thread for reading messages from the server
def getIRC():
while True:
ircmsg = ircsock.recv(2048) # receive data from the server <-------- this needs to be fixed! Threading? Buffer file?
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
q.put(ircmsg)
print(ircmsg) # Here we print what's coming from the server
q = Queue.LifoQueue()
t = Thread(target=getIRC)
t.daemon = True
t.start()
# Classes
class newcomer(object): # Newcomer class created when someone joins the room class newcomer(object): # Newcomer class created when someone joins the room
def __init__(self, nick): def __init__(self, nick):
@ -35,10 +47,13 @@ class newcomer(object): # Newcomer class created when someone joins the room
print "status of newcomer object changed to " + str(self.status) print "status of newcomer object changed to " + str(self.status)
def aroundFor(self): def aroundFor(self):
print "timecheck: " + str(self.nick) + " has been around for " + str(time.time() - self.born) # print "timecheck: " + str(self.nick) + " has been around for " + str(time.time() - self.born)
return time.time() - self.born return time.time() - self.born
## bot-specific functions
# Functions
def ping(): # Responds to server Pings.
ircsock.send("PONG :pingis\n")
def hello(speaker): # This function responds to a user that inputs "Hello Mybot" def hello(speaker): # This function responds to a user that inputs "Hello Mybot"
ircsock.send("PRIVMSG "+ channel +" :Hello! "+ speaker + "\n") ircsock.send("PRIVMSG "+ channel +" :Hello! "+ speaker + "\n")
@ -46,30 +61,27 @@ def hello(speaker): # This function responds to a user that inputs "Hello Mybot"
def welcome(speaker): def welcome(speaker):
ircsock.send("PRIVMSG "+ channel +" :Welcome "+ speaker + "!\n") ircsock.send("PRIVMSG "+ channel +" :Welcome "+ speaker + "!\n")
#### Main function
newList = [] newList = []
while 1: while 1:
ircmsg = ircsock.recv(2048) # receive data from the server <-------- this needs to be fixed! Threading? Buffer file? if q.empty() == 0:
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks. ircmsg = q.get()
print(ircmsg) # Here we print what's coming from the server speaker = ircmsg.split(":")[1].split("!")[0]
speaker = ircmsg.split(":")[1].split("!")[0] if ircmsg.find(":Hello "+ botnick) != -1: # Response to 'Hello botnick'
hello(speaker)
if ircmsg.find(":Hello "+ botnick) != -1: # Response to 'Hello botnick' if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
hello(speaker) ping()
if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond! if ircmsg.find("JOIN "+ channel) != -1: # If someone joins #channel
ping() newList.append(newcomer(speaker)) # Create a newcomer object and append to list.
if ircmsg.find("JOIN "+ channel) != -1: # If someone joins #channel
newList.append(newcomer(speaker)) # Create a newcomer object and append to list.
print newList
for i in newList: for i in newList:
print i.aroundFor()
print i.status
if i.aroundFor() > 3 and i.status == 0: if i.aroundFor() > 3 and i.status == 0:
print welcome(i.nick) print welcome(i.nick)
i.changeStatus(1) i.changeStatus(1)