diff options
Diffstat (limited to 'icq_roombot.py')
-rw-r--r-- | icq_roombot.py | 205 |
1 files changed, 135 insertions, 70 deletions
diff --git a/icq_roombot.py b/icq_roombot.py index ebb700c..c2a07fb 100644 --- a/icq_roombot.py +++ b/icq_roombot.py @@ -8,93 +8,158 @@ from random import randint uin = '370496181' password = 'megahal123' +class Member(object): + def __init__(self,id,name): + self.id = id + self.name = name + +class Room(object): + def __init__(self): + self.members = [] + self.msg_cbs = [] + + def addMessageCallback(self,cb): + self.msg_cbs.append(cb) + + def removeMessageCallback(self,cb): + self.msg_cbs.remove(cb) + + def join(self,member): + self.members.append(member) + + def leave(self,member): + self.members.remove(member) + + def getMemberById(self,id): + l=filter(lambda member: member.id == id, self.members) + if len(l) > 0: + return l[0] + else: + return None + + instance = None + @classmethod + def getInstance(klass): + if klass.instance == None: + klass.instance = Room() + return klass.instance + CAP_TYPING = '\x56\x3f\xc8\x09\x0b\x6f\x41\xbd\x9f\x79\x42\x26\x09\xdf\xa2\xf3' class icqBot( oscar.BOSConnection ): - capabilities = [oscar.CAP_CHAT,CAP_TYPING] + capabilities = [oscar.CAP_CHAT,CAP_TYPING] - def initDone( self ): - self.requestSelfInfo() - self.requestSSI().addCallback(self.gotBuddyList) - self.members = dict() + def initDone( self ): + self.requestSelfInfo() + self.requestSSI().addCallback(self.gotBuddyList) + self.room = Room.getInstance() + self.members = dict() - def oscar_04_14(self,snac): - pass #TODO: handle typing event - see omegle_icq.py + def oscar_04_14(self,snac): + pass #TODO: handle typing event - see omegle_icq.py - def updateBuddy(self, user): - print "Update buddy %s" % user + def updateBuddy(self, user): + print "Update buddy %s" % user - def gotBuddyList( self, buddylist ): - self.activateSSI() - self.setProfile("Forget ICQ, MSN, Yahoo and the other shitty protocols! Use Jabber!") - self.setIdleTime( 0 ) - self.clientReady() - for user in buddylist[0][0].users: - self.sendAuthorizationResponse(user.name, True, '') + def gotBuddyList( self, buddylist ): + self.activateSSI() + self.setProfile("Forget ICQ, MSN, Yahoo and the other shitty protocols! Use Jabber!") + self.setIdleTime( 0 ) + self.clientReady() + for user in buddylist[0][0].users: + self.sendAuthorizationResponse(user.name, True, '') - print buddylist + print buddylist - def receiveMessage( self, user, multiparts, flags ): - try: - msg = str(multiparts[0][0]) - if msg.strip() in ("/leave", "/LEAVE"): - self.members.pop(user.name) - self.sendMessage(user.name, "You left") - for member in self.members.keys(): - self.sendMessage(user.name, "%s(%s) left"%(member,self.members[member])) - elif msg.strip().startswith("/list"): - names = ", ".join( - map(lambda key: "%s(%s)"%(self.members[key],key), self.members.keys())) - self.sendMessage(user.name, "Connected to: " + names) - elif msg.strip().startswith("/join"): - nick = msg.split("/join")[-1:][0] or user.name - self.sendMessage(user.name, "========= Joined as %s - to leave type >/leave<" % nick) - self.sendMessage(user.name, "Connected to: " + ", ".join(self.members.values())) - for member in self.members.keys(): - self.sendMessage(member, "======== %s(%s) (re-)joined" % (nick,user.name)) - self.members.update( ( (user.name, nick), ) ) - elif user.name in self.members.keys(): - if msg.strip().startswith("/invite"): - who = msg.split("/invite")[-1:][0] or False - if who: - self.sendMessage(who, "%s invite you to join this room" % user.name) - self.sendMessage(user.name, "Invitation sent to %s" % who) - else: - for member in self.members.keys(): - if member == user.name: - continue - self.sendMessage(member, "%s: %s"%(self.members[user.name], msg) ) - else: - if randint(0,5) == 3: - self.sendMessage(user.name, "Use '/join NICKNAME', '/list' and '/leave'") - - self.sendAuthorizationResponse(user.name, True, '') - except Exception,e: - print "error: %s %s\n%s" % (user,multiparts,e) - except: - pass - - def chatReceiveMessage( self, chat, user, message ): - self.receiveMessage( self, user, message, 0 ) + def _getMessage(self,multiparts): + message = None + if len(multiparts[0]) == 2: + if multiparts[0][1] == "unicode": + message = unicode(multiparts[0][0]) + else: + message = multiparts[0][0].decode(multiparts[0][1],'latin1') + else: + message = str(multiparts[0][0]) + #filter qip \x00CHAR\x00CHAR - qip sucks at unicode? + message = filter(lambda x: x!=u"\x00", message) + return message + + def sendMessage(self, user, messageUtf8): + message = messageUtf8.encode('latin1','replace') + oscar.BOSConnection.sendMessage(self,user,message) + def receiveMessage( self, user, multiparts, flags ): + try: + try: + message = self._getMessage(multiparts) + except Exception,e: + print e + self.sendMessage(user.name, str(e)) + return + + if self.room.getMemberById(user.name) != None: + member = self.room.getMemberById(user.name) + + if message.strip() == "/leave": + self.room.leave(member) + self.sendMessage(user.name, 'You left') + for otherMember in self.room.members: + self.sendMessage(otherMember.id, "===== %s/%s left"%(member.name,member.id) ) + elif message.strip() == "/invite": + self.sendMessage(member.id, "===== geht nicht") + elif message.strip() == "/nick": + member.name = message.strip().split("/nick")[1] + elif message.strip() == "/msg": + self.sendMessage(member.id, "===== geht nicht") + elif message.strip() == "/list": + self.sendMessage(member.id, "===== List: %s"%", ".join(map(lambda m: "%s/%s"%(m.id,m.name),self.room.members)) ) + elif message.strip().startswith("/"): + self.sendMessage(member.id, "===== kennich nicht") + else: + for otherMember in self.room.members: + if otherMember.id == user.name: + continue + self.sendMessage(otherMember.id, message.strip()) + else: + if message.strip().startswith("/join"): + nick = "%s/%s"%(message.strip().split("/join")[1],user.name) + member = Member(user.name, nick) + self.room.join(member) + for otherMember in self.room.members: + self.sendMessage(otherMember.id, "===== %s/%s joined" % (member.name,member.id)) + else: + if randint(0,5) == 3: + self.sendMessage(user.name, "Use '/join Nickname' or ' /join Nickname'") + + self.sendAuthorizationResponse(user.name, True, '') + except Exception,e: + print "error: %s %s\n%s" % (user,multiparts,e) + + def chatReceiveMessage( self, chat, user, message ): + self.receiveMessage( self, user, message, 0 ) + """ def receiveSendFileRequest(self, *args): # def receiveSendFileRequest(self, user, file, description, cookie): print args + """ - def sendAuthorizationResponse(self, uin, success, responsString): - packet = struct.pack("b", len(uin)) + uin - if success: - packet += struct.pack("b", 1) - else: - packet += struct.pack("b", 0) - packet += struct.pack(">H", len(responsString)) + responsString - self.sendSNACnr(0x13, 0x1a, packet) + def sendAuthorizationResponse(self, uin, success, responsString): + packet = struct.pack("b", len(uin)) + uin + if success: + packet += struct.pack("b", 1) + else: + packet += struct.pack("b", 0) + packet += struct.pack(">H", len(responsString)) + responsString + self.sendSNACnr(0x13, 0x1a, packet) + + def connectionLost(self,*args,**kwargs): + print "Connection lost" + oscar.BOSConnection.connectionLost(self,*args,**kwargs) - def connectionLost(self,*args,**kwargs): - print "Connection lost" + class OscarCrap( oscar.OscarAuthenticator ): - BOSClass = icqBot + BOSClass = icqBot protocol.ClientCreator( reactor, OscarCrap, uin, password, icq=1 ).connectTCP( 'login.icq.com', 5238 ) |