Hi All,

I'm in full agreement that Gajim should not support JISP (JEP-0038)
directly since it is deferred, but it would be nice to convert JISP
archives to something gajim could use.  Attached is a quick and dirty
script (i.e., needs some love) that will convert JISP emoticons to a
directory in ${HOME}/.gajim/emoticons.  If there is some interest
we could make this also create icon sets, as currently it ignore
non-emoticon icons. 

If you are interested in JISP give this script a shot, and I can add it
to the Wiki or whatever...  Also note, that the script will not work for
Windows users, so patches are welcome :)

-travis (aka nicfit)
#!/usr/bin/env python
################################################################################
#  Copyright (C) 2006  Travis Shirk <[EMAIL PROTECTED]>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  $Id$
################################################################################
import os, sys, zipfile
import xml.dom.minidom

GAJIM_HOME = os.path.expandvars('${HOME}/.gajim/emoticons')

def getElementData(elem):
    if not elem.hasChildNodes():
        return None

    elem.normalize()
    child = elem.firstChild
    while child:
        if child.nodeType == child.TEXT_NODE:
            return child.nodeValue
        child = child.nextSibling
    return None

def escapeEmoticon(e):
    escaped = ''
    for c in e:
        if c in ['\'', '\\']:
            escaped += '\\' + c
        else:
            escaped += c
    return escaped

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print >>sys.stderr, 'Usage: %s file.jisp' % \
                            os.path.basename(sys.argv[0])
        sys.exit(1)

    # Open JISP archive
    jisp_file = sys.argv[1]
    try:
        print 'Opening JISP archive file %s' % jisp_file
        jisp_archive = zipfile.ZipFile(sys.argv[1], 'r')
    except Exception, ex:
        print >>sys.stderr, ex
        sys.exit(2)

    # Read JISP metadata and images
    jisp_data = None
    jisp_images = {}
    for f in jisp_archive.namelist():
        if os.path.basename(f) == 'icondef.xml':
            jisp_data = jisp_archive.read(f)
        else:
            jisp_images[os.path.basename(f)] = jisp_archive.read(f)
    jisp_archive.close()

    # Parse JISP
    try:
        jisp_dom = xml.dom.minidom.parseString(jisp_data)

        root = jisp_dom.documentElement
        # Note, JEP-0038 does not declare any namespace for this schema,
        # therefore elements may NOT be prefixed.
        if root.tagName != 'icondef':
            raise Exception('Invalid root element name in JISP metadata: %s' % \
                            root.localName)

        # Get JISP name
        jisp_meta = root.getElementsByTagName('meta')[0]
        jisp_name = getElementData(jisp_meta.getElementsByTagName('name')[0])
        if not jisp_name:
            raise Exception('JISP metadata has no name value')
        jisp_version = \
            getElementData(jisp_meta.getElementsByTagName('version')[0])
        jisp_desc = \
            getElementData(jisp_meta.getElementsByTagName('description')[0])
        jisp_author_tags = jisp_meta.getElementsByTagName('author')

        # Check for an existing Gajim emoticon theme, we won't overwrite
        gajim_dir = os.path.join(GAJIM_HOME, jisp_name)
        if os.path.exists(gajim_dir):
            raise IOError('Gajim theme %s already exists, remove and retry' % \
                          gajim_dir)
        # Make theme dir
        os.makedirs(gajim_dir)

        print 'Writing emoticons.py...'
        emoticons_file = file(os.path.join(gajim_dir, 'emoticons.py'), 'w')
        emoticons_file.write('# -*- coding: utf-8 -*-\n')
        emoticons_file.write('#\n')
        emoticons_file.write('# Name: %s\n' % jisp_name.encode('utf-8'))
        emoticons_file.write('# Description:\n')
        for line in jisp_desc.split('\n'):
            emoticons_file.write('#\t%s\n' % line.encode('utf-8').strip())
        emoticons_file.write('# Version: %s\n' % jisp_version.encode('utf-8'))
        for a_tag in jisp_author_tags:
            emoticons_file.write('# Author: %s' % \
                                 getElementData(a_tag).encode('utf-8'))
            jid = a_tag.getAttribute('jid')
            print type(jid)
            if jid:
                emoticons_file.write(' <xmpp:%s>' % jid.encode('utf-8'))
            emoticons_file.write('\n')

        emoticons_file.write('#\n')
        emoticons_file.write('emoticons = {\n')

        found_emoticon = False
        for jisp_icon in root.getElementsByTagName('icon'):
            icon_name = ''
            icon_txt = []
            for child in jisp_icon.childNodes:
                if child.nodeType != child.ELEMENT_NODE:
                    continue

                if child.tagName == 'object':
                    icon_name = getElementData(child)
                    fp = file(os.path.join(gajim_dir, icon_name), 'wb')
                    fp.write(jisp_images[icon_name])
                    fp.close()
                elif child.tagName == 'text':
                    txt = getElementData(child)
                    txt = escapeEmoticon(txt)
                    icon_txt.append(txt)

            if icon_name and icon_txt:
                found_emoticon = True
                for txt in icon_txt:
                    print 'Adding emoticon %s --> %s' % (txt, icon_name)
                    emoticons_file.write("\t'%s': '%s',\n" % \
                                         (txt.encode('utf-8'),
                                          icon_name.encode('utf-8')))

        jisp_dom.unlink()
    except Exception, ex:
        print >>sys.stderr, 'Error parsing JISP metadata:', ex
        sys.exit(3)

    emoticons_file.write('}\n')
    emoticons_file.close()

    if not found_emoticon:
        print >>sys.stderr, "JISP archive did not contain any emoticons"


_______________________________________________
Gajim-devel mailing list
Gajim-devel@gajim.org
https://lists.gajim.org/cgi-bin/listinfo/gajim-devel

Reply via email to