I've got a small bot communicating with users on ICQ, it's using Twisted.Words, Oscar protocol. I need to see their online status, but that seems to be only possible when I have them in my buddy list. So here comes the question:
How do I add a buddy to my buddy list in Twisted.Words Oscar?
That's pretty weird, but there seems to be nothing about it in the API docs and I couldn't find any good clues in the oscar.py source code. :\
Finally I came up with a solution, after hours of looking at the code of oscar.py and at OSCAR protocol documentation.
So here we go. Go to the function gotBuddyList(self, l) in this example: http://twistedmatrix.com/documents/current/words/examples/oscardemo.py
You might have your own analogue, that's a callback function called when the SSI is received. It's bound like this:
self.requestSSI().addCallback(self.gotBuddyList)
So inside this gotBuddyList(self, l) function you put this:
self.groupAll = l[0][0]
In my case, this contains the first buddy group in my buddy list (which was created manually in advance, from a regular ICQ client). The l variable is the SSI received from the server and it contains your buddy groups, buddies in those groups and other stuff like settings or something. That's according to the OSCAR docs.
I'm going to add my buddies to the first group in my list. If you have your own cases or want to create a more flexible solution, you'll have to make more investigation on that.
Next, when you want to add a new buddy to your buddy list, you do this (assuming this is still inside one of your BOSConnection's implementation class methods):
buddy = oscar.SSIBuddy(the_uin_to_add) # put the UIN of the buddy to add in the argument
try:
buddyID = max(self.groupAll.usersToID.itervalues()) + 1 # incrementing the buddyID
except ValueError: # if the group is empty yet
buddyID = 1
self.groupAll.addUser(buddyID, buddy) # adding it to the group
self.addItemSSI(buddy) # actually sending the stuff to the server
And here you are, the buddy is in your list now. If he's online, you'll immediately get an updateBuddy event, containing the info about his online status and so on.
Here I couldn't really understand what the buddyID is. There's no info explaining it. But I finally assumed that it's just an inner ID inside the group the buddy is in. It's limited by 32767. I decided to go from 1 and increment it by one from the highest in the group each time.
That's all I have. I hope it can help someone once. If you can add anything or correct me, I'll be glad to see your comments!