How to Show Photo message in Emesene

48 views
Skip to first unread message

Xiang Wang

unread,
Dec 13, 2012, 10:36:06 PM12/13/12
to Riccardo Bortolato, eme...@googlegroups.com
Dear Ricardo Bortolato,

Sorry to bother you again  that , I was confused on how emesene to show received photo message and how to call the API to send a photo message.

Can u give me some samples.

Thanks in advance.

--

Best Regards~


Xiang Wang

CDC, TrendMicro


Riccardo (c10ud)

unread,
Dec 14, 2012, 4:22:38 AM12/14/12
to Xiang Wang, eme...@googlegroups.com
Hello,
it depends on what you intend with photo messages: we don't support putting pictures directly in the webkit view but we instead send them with a normal file-transfer. (It's a bit tricky but it's implemented in papylib and gtk)

However that's a different story for emoticons and custom emoticons: custom emoticons are received from peers, cached and then showed in the ui through some javascript hack (in the webkit webview) or some gtk tricks in the gtk textbox.

greets

2012/12/14 Xiang Wang <wxje...@gmail.com>

Xiang Wang

unread,
Dec 15, 2012, 11:54:18 AM12/15/12
to Riccardo (c10ud), eme...@googlegroups.com
Dear Riccard,

Hmm,  from your comments, It seems there is not a easy way to handle Pic message.

At present , I can fetch picture binary data from back end, Can I just show the picture in a communication box?

And I will implement a API which can Send picture just with its path as a parameter. I think this API just be called by file sent API in emesene level.

Riccardo (c10ud)

unread,
Dec 16, 2012, 4:39:46 PM12/16/12
to Xiang Wang, eme...@googlegroups.com
Hello,
it's not easy, but once you figure out the protocol part, i think you can reuse/adapt the custom-emoticon method, which i think it would suit your needs.
As for the "sending" part, you can just use the send file api and act accordingly in case you want to share a picture instead (so you don't have to add events/actions to emesene base protocol)

greets

2012/12/15 Xiang Wang <wxje...@gmail.com>

Xiang Wang

unread,
Dec 20, 2012, 4:38:10 AM12/20/12
to Riccardo (c10ud), eme...@googlegroups.com
https://github.com/emesene/emesene/blob/master/emesene/gui/gtkui/AdiumTextBox.py#L220

What does the three parameter "_creator, _friendly, path" mean?
How can I call this function?

Xiang Wang

unread,
Dec 20, 2012, 4:42:08 AM12/20/12
to Riccardo (c10ud), eme...@googlegroups.com
And I saw that this function is called only by MSN client to show custom emoticons.

Why not emesene provides a common interface to handle these picture data from network.
At last , it will call p2p_finished function any way.

Riccardo (c10ud)

unread,
Dec 20, 2012, 6:03:20 AM12/20/12
to Xiang Wang, eme...@googlegroups.com
Unfortunately this has been engineered on top of msn protocol because it was the only one requiring it (and supported?) so it's kind of specific to this usage

However, you can see in papylib/Worker.py:

            shortcut, hash_ = emotes.insert_resized((short, msnobj._data),
                                                    filename)

            path = os.path.join(emotes.path, hash_)
            received[short] = path
            self.session.p2p_finished(account, 'emoticon', msnobj._creator,
                                      shortcut, path)

where creator is the sending buddy's email, shortcut is a shortcut for the emoticon (like :) is for smiley face) and path is the path where the image is cached

The output extensions will automatically get those p2p_finished events with 'emoticon' as type and handle them accordingly with their extra arguments:

$ grep -ri "'emoticon'" *
e3/papylib/Worker.py:            self.session.p2p_finished(account, 'emoticon', msnobj._creator,
gui/gtkui/TextBox.py:        if _type == 'emoticon':
gui/gtkui/AdiumTextBox.py:        if _type == 'emoticon':
gui/qt4ui/widgets/AdiumChatOutput.py:        if _type == 'emoticon':

I think you can easily reuse this facility to show whatever you want to, see how i'd do it for AdiumTextBox.py:

    def update_p2p(self, account, _type, *what):
        ''' new p2p data has been received (custom emoticons) '''
        if _type == 'emoticon':
            _creator, _friendly, path = what
            _id = base64.b64encode(_creator+xml.sax.saxutils.unescape(_friendly)) #see gui/base/MarkupParser.py
            mystr = "var now=new Date();var x=document.images;for(var i=0;i<x.length;i++){if(x[i].name=='%s'){x[i].src='%s?'+now.getTime();}}" % (_id, path)
            self.view.execute_script(mystr)
        elif _type == 'picture':
            new, expanded, arguments, tuple = what
            _id = base64.b64encode(_creator+xml.sax.saxutils.unescape(_friendly)) #see gui/base/MarkupParser.py
            mystr = "var now=new Date();var x=document.images;for(var i=0;i<x.length;i++){if(x[i].name=='%s'){x[i].src='%s?'+now.getTime();}}" % (_id, path)
            self.view.execute_script(mystr)

the _id thing is just some data we create using buddy's email+emoticon's friendly name so if you have 10 emoticons in the same page with the same sender and shortcut they will all get updated by the subsequent javascript hack using that as an _id for distinguish them.

At the end of the day, you just need to:
1) implement the picture receiving function in your library
2) receive the picture and save it in emesene's cache
3) call the p2p_finished action with the arguments you'll need in the ui (e.g. new 'picture' type and some other stuff..)
4) handle the new 'picture' type in the output widgets as you please

2012/12/20 Xiang Wang <wxje...@gmail.com>

Riccardo (c10ud)

unread,
Dec 20, 2012, 6:07:29 AM12/20/12
to Xiang Wang, eme...@googlegroups.com
I need to add that all of this works because in the received message text there's a placeholder for those incoming emoticons (not received yet), so you have to handle that stuff too

2012/12/20 Riccardo (c10ud) <c10u...@gmail.com>

Xiang Wang

unread,
Jan 21, 2013, 1:25:52 AM1/21/13
to Riccardo (c10ud), eme...@googlegroups.com
Dear Riccardo,

Here is my callback function to show pic message,

But it did not work.

Would you please help to verify the code listed below?

 
   def _on_buddy_pic_message(self, message):
       
        '''handle the ceception of a picture message'''
        #print message
        data = json.loads(message)
        from_uin = str(data['from_uin'])
        account = from_uin
       
        import base64
       
        photo_bin = base64.b64decode(data['off_pic'])

        pictures = self.session.caches.get_picture_cache(account)

        photo_hash = hashlib.sha1()
        photo_hash.update(photo_bin)
        photo_hash = photo_hash.hexdigest()
       
        photo_path = os.path.join(pictures.path, photo_hash)

        if photo_hash not in pictures:
            pictures.insert_raw(StringIO.StringIO(photo_bin))
           
        print photo_path
        self.session.p2p_finished(account, 'emoticon', account, '---', photo_path)
       
        msgobj = e3.Message(e3.Message.TYPE_MESSAGE, 'picture', account)
       
        pict = {'---' : photo_path}
       
        if account in self.conversations:
            cid = self.conversations[account]
        else:
            cid = time.time()
            self.conversations[account] = cid
            self.rconversations[cid] = [account]
            self.session.conv_first_action(cid, [account])

        self.session.conv_message(cid, account, msgobj, pict)
        e3.Logger.log_message(self.session, None, msgobj, False)
Reply all
Reply to author
Forward
0 new messages