3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
6 from twisted.words.protocols import oscar
7 from twisted.internet import protocol, reactor
10 SN = raw_input('Username: ') # replace this with a screenname
11 PASS = getpass.getpass('Password: ')# replace this with a password
14 hostport = ('login.icq.com', 5238)
16 hostport = ('login.oscar.aol.com', 5190)
19 class B(oscar.BOSConnection):
20 capabilities = [oscar.CAP_CHAT]
22 self.requestSelfInfo().addCallback(self.gotSelfInfo)
23 self.requestSSI().addCallback(self.gotBuddyList)
24 def gotSelfInfo(self, user):
27 def gotBuddyList(self, l):
30 self.setProfile("""this is a test of the current twisted.oscar code.<br>
32 * send me a message, and you should get it back.<br>
33 * invite me to a chat room. i'll repeat what people say. say 'leave' and i'll go.<br>
34 * also, i hang out in '%s Chat'. join that, i'll repeat what you say there.<br>
35 * try warning me. just try it.<br>
37 if any of those features don't work, tell paul (Z3Penguin). thanks."""%SN)
40 self.createChat('%s Chat'%SN).addCallback(self.createdRoom)
41 def createdRoom(self, (exchange, fullName, instance)):
42 print 'created room',exchange, fullName, instance
43 self.joinChat(exchange, fullName, instance).addCallback(self.chatJoined)
44 def updateBuddy(self, user):
46 def offlineBuddy(self, user):
47 print 'offline', user.name
48 def receiveMessage(self, user, multiparts, flags):
49 print user.name, multiparts, flags
50 self.getAway(user.name).addCallback(self.gotAway, user.name)
51 if multiparts[0][0].find('away')!=-1:
52 self.setAway('I am away from my computer right now.')
53 elif multiparts[0][0].find('back')!=-1:
56 self.sendMessage(user.name,'<html><font color="#0000ff">'+self.awayMessage,autoResponse=1)
58 self.lastUser = user.name
59 self.sendMessage(user.name, multiparts, wantAck = 1, autoResponse = (self.awayMessage!=None)).addCallback( \
61 def messageAck(self, (username, message)):
62 print 'message sent to %s acked' % username
63 def gotAway(self, away, user):
65 print 'got away for',user,':',away
66 def receiveWarning(self, newLevel, user):
67 print 'got warning from', hasattr(user,'name') and user.name or None
68 print 'new warning level', newLevel
70 #username = self.lastUser
74 self.warnUser(username).addCallback(self.warnedUser, username)
75 def warnedUser(self, oldLevel, newLevel, username):
76 self.sendMessage(username,'muahaha :-p')
77 def receiveChatInvite(self, user, message, exchange, fullName, instance, shortName, inviteTime):
78 print 'chat invite from',user.name,'for room',shortName,'with message:',message
79 self.joinChat(exchange, fullName, instance).addCallback(self.chatJoined)
80 def chatJoined(self, chat):
81 print 'joined chat room', chat.name
82 print 'members:',map(lambda x:x.name,chat.members)
83 def chatReceiveMessage(self, chat, user, message):
84 print 'message to',chat.name,'from',user.name,':',message
85 if user.name!=self.name: chat.sendMessage(user.name+': '+message)
86 if message.find('leave')!=-1 and chat.name!='%s Chat'%SN: chat.leaveChat()
87 def chatMemberJoined(self, chat, member):
88 print member.name,'joined',chat.name
89 def chatMemberLeft(self, chat, member):
90 print member.name,'left',chat.name
91 print 'current members',map(lambda x:x.name,chat.members)
92 if chat.name!="%s Chat"%SN and len(chat.members)==1:
93 print 'leaving', chat.name
96 class OA(oscar.OscarAuthenticator):
99 protocol.ClientCreator(reactor, OA, SN, PASS, icq=icqMode).connectTCP(*hostport)