summaryrefslogtreecommitdiff
path: root/icq_roombot.py
blob: ebb700cf852b0694f08cc6dc094a66095ca1803d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
import struct

from twisted.words.protocols import oscar
from twisted.internet import reactor, protocol
from random import randint

uin = '370496181'
password = 'megahal123'

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]
 
	def initDone( self ):
		self.requestSelfInfo()
		self.requestSSI().addCallback(self.gotBuddyList)
		self.members = dict()

	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 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
 
	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 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 connectionLost(self,*args,**kwargs):
		print "Connection lost"


class OscarCrap( oscar.OscarAuthenticator ):
	BOSClass = icqBot


protocol.ClientCreator( reactor, OscarCrap, uin, password, icq=1 ).connectTCP( 'login.icq.com', 5238 )
reactor.run()