Merge pull request #14 from HarHar/master

Fixes not joining
This commit is contained in:
Uriziel 2013-01-24 16:12:42 -08:00
commit 2612a0567e

View File

@ -6,6 +6,7 @@ import socket
import threading import threading
from syncplay import utils from syncplay import utils
from time import sleep from time import sleep
import traceback
class Bot(object): class Bot(object):
def __init__(self, server='irc.rizon.net', serverPassword='', port=6667, nick='SyncBot', nickservPass='', channel='', channelPassword='', functions=[]): def __init__(self, server='irc.rizon.net', serverPassword='', port=6667, nick='SyncBot', nickservPass='', channel='', channelPassword='', functions=[]):
@ -49,7 +50,7 @@ class Bot(object):
self.sock.recv(4096) #We don't want to join if nickserv hasn't done its job (shouldn't really matter, but good for vHost) self.sock.recv(4096) #We don't want to join if nickserv hasn't done its job (shouldn't really matter, but good for vHost)
if channel != '': if channel != '':
self.join(channel) self.join(channel, channelPassword)
self.active = True self.active = True
self.thread = threading.Thread(target=handlingThread, args=(self.sock, self)) self.thread = threading.Thread(target=handlingThread, args=(self.sock, self))
@ -65,17 +66,22 @@ class Bot(object):
def sp_paused(self, who, room): def sp_paused(self, who, room):
self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' has paused (room ' + room + ')') self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' has paused (room ' + room + ')')
def sp_fileplaying(self, who, filename, room): #for when syncplay knows what filename is being played def sp_fileplaying(self, who, filename, room): #for when syncplay knows what filename is being played
if filename == '':
return
self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' is playing "' + filename + '" (room ' + room + ')') self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' is playing "' + filename + '" (room ' + room + ')')
def sp_seek(self, who, fromTime, toTime, room): def sp_seek(self, who, fromTime, toTime, room):
self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' has jumped from ' + utils.formatTime(fromTime) + ' to ' + utils.formatTime(toTime) +' (room ' + room + ')') self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' has jumped from ' + utils.formatTime(fromTime) + ' to ' + utils.formatTime(toTime) +' (room ' + room + ')')
def sockSend(self, s): def sockSend(self, s):
self.sock.send(s + '\r\n') self.sock.send(s + u'\r\n')
def msg(self, who, message): def msg(self, who, message):
self.sockSend('PRIVMSG ' + who + ' :' + message) self.sockSend('PRIVMSG ' + who + ' :' + message)
def join(self, channel, passw=''): def join(self, channel, passw=''):
if passw != '': passw = ' ' + passw if passw != '': passw = ' ' + passw
self.sockSend('JOIN ' + channel + passw) self.sockSend('\r\nJOIN ' + channel + passw)
#Just to make sure we joined; doesn't hurt anyone
sleep(1)
self.sockSend('\r\nJOIN ' + channel + passw)
def part(self, channel, reason=''): def part(self, channel, reason=''):
self.sockSend('PART ' + channel + ' :' + reason) self.sockSend('PART ' + channel + ' :' + reason)
def quit(self, reason='Leaving'): def quit(self, reason='Leaving'):
@ -94,7 +100,7 @@ class Bot(object):
rooms = self.functions[1]() rooms = self.functions[1]()
if len(rooms) == 0: if len(rooms) == 0:
self.msg(to, chr(3) + '5Error!' + chr(15) + ' No rooms found on server') self.msg(to, chr(3) + '12Notice:' + chr(15) + ' No rooms found on server')
return return
out = 'Currently the Syncplay server hosts viewing sessions as follows: ' out = 'Currently the Syncplay server hosts viewing sessions as follows: '
@ -118,8 +124,21 @@ class Bot(object):
users = self.functions[4](room) users = self.functions[4](room)
paused = self.functions[5](room) paused = self.functions[5](room)
out = chr(2) + '<Paused>' + chr(15) if paused else chr(2) + '<Playing>' + chr(15) out = chr(2) + '<Paused>' + chr(15) if paused else chr(2) + '<Playing>' + chr(15)
out += ' [' + utils.formatTime(self.functions[2](room)) + '/' + utils.formatTime(users[0]['length']) + '] ' time = self.functions[2](room)
out += users[0]['file'] if time == None: time = 0
for u in users:
if u['length'] == None: continue
out += ' [' + utils.formatTime(time) + '/' + utils.formatTime(u['length']) + '] '
break
else:
out += ' '
for u in users:
if u['file'] == None: continue
out += u['file']
break
else:
out += '[no file]'
self.msg(to, out) self.msg(to, out)
out = 'Users: ' out = 'Users: '
i = 0 i = 0
@ -141,7 +160,7 @@ class Bot(object):
users = self.functions[4](room) users = self.functions[4](room)
for u in users: for u in users:
if u['nick'] == nickFrom: if u['nick'] == nickFrom:
self.functions[6](nickFrom, True) self.functions[0](nickFrom, True)
return return
self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick was not found on the server') self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick was not found on the server')
elif split[0].lower() == '!play': elif split[0].lower() == '!play':
@ -151,7 +170,7 @@ class Bot(object):
users = self.functions[4](room) users = self.functions[4](room)
for u in users: for u in users:
if u['nick'] == nickFrom: if u['nick'] == nickFrom:
self.functions[6](nickFrom, False) self.functions[0](nickFrom, False)
return return
self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick was not found on the server') self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick was not found on the server')
elif split[0].lower() == '!help': elif split[0].lower() == '!help':
@ -161,32 +180,39 @@ def handlingThread(sock, bot):
while bot.active: while bot.active:
rcvd = sock.recv(4096).split('\n') rcvd = sock.recv(4096).split('\n')
for line in rcvd: for line in rcvd:
line = line.replace('\r', '') try:
line = line.replace('\r', '')
if line.split(' ')[0] == 'PING': if line.split(' ')[0] == 'PING':
try: try:
sock.send('\r\nPONG ' + line.split(' ')[1].replace(':', '') + '\r\n') sock.send('\r\nPONG ' + line.split(' ')[1].replace(':', '') + '\r\n')
except: #if we were fooled by the server :C except: #if we were fooled by the server :C
sock.send('\r\nPONG\r\n') sock.send('\r\nPONG\r\n')
#\r\n on the beggining too because if we send two things too fast, the IRC server can discern #\r\n on the beggining too because if we send two things too fast, the IRC server can discern
lsplit = line.split(':') lsplit = line.split(':')
if len(lsplit) >= 2: if len(lsplit) >= 2:
if 'PRIVMSG' in lsplit[1] or 'NOTICE' in lsplit[1]: if len(lsplit[1].split(' ')) >= 2:
# ---BEGIN WTF BLOCK--- if lsplit[1].split(' ')[1] == '404':
lsplit = line.split(':') bot.join(bot.channel, bot.channelPassword)
addrnfrom = ''
if '~' in lsplit[1]:
addrnfrom = lsplit[1].split('~')[1].split(' ')[0]
nfrom = lsplit[1].split('!')[0]
else:
nfrom = lsplit[1].split('!')[0]
if len(lsplit[1].split()) >= 3: if 'PRIVMSG' in lsplit[1] or 'NOTICE' in lsplit[1]:
to = lsplit[1].split()[2] # ---BEGIN WTF BLOCK---
msg = '' lsplit = line.split(':')
for brks in lsplit[2:]: addrnfrom = ''
msg += brks + ':' if '~' in lsplit[1]:
msg = msg[:-1].lstrip() addrnfrom = lsplit[1].split('~')[1].split(' ')[0]
# ---END WTF BLOCK- -- nfrom = lsplit[1].split('!')[0]
bot.irc_onMsg(nfrom, addrnfrom, to, msg) else:
nfrom = lsplit[1].split('!')[0]
if len(lsplit[1].split()) >= 3:
to = lsplit[1].split()[2]
msg = ''
for brks in lsplit[2:]:
msg += brks + ':'
msg = msg[:-1].lstrip()
# ---END WTF BLOCK- --
bot.irc_onMsg(nfrom, addrnfrom, to, msg)
except:
print traceback.format_exc()