#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
updates.py - this file is part of SOGo

 Copyright (C) 2006-2010 Inverse inc.
 Copyright (C) 2011 Agence universitaire de la Francophonie - www.auf.org

PHP version author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
Python version author: Progfou <jean-christophe.andre@auf.org>

This file 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, or (at your option)
any later version.

This file 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; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

This script handles the automatic propagation of extensions pertaining
to a SOGo site. It requires Python 2.5 or later.
"""
import sys
import os.path
import cgi, cgitb 

BASE_URL = "https://sogo.vn.auf.org/plugins"

PLUGINS = {
    "sogo-connector@inverse.ca": {
        "application": "thunderbird",
        "version": "3.106",
        "filename": "sogo-connector-3.106.xpi",
    },
    "sogo-integrator@inverse.ca": {
        "application": "thunderbird",
        "version": "3.106",
        "filename": "sogo-integrator-3.106.xpi",
    },
    "{e2fda1a4-762b-4020-b5ad-a41df1933103}": {
        "application": "thunderbird",
        "version": "1.0b2.106i",
        "filename": "lightning-1.0b2.106i.xpi",
    },
}

APPLICATIONS = {
    "thunderbird": "<em:id>{3550f703-e582-4d05-9a08-453d09bdfdc6}</em:id>\
                    <em:minVersion>3.1.0</em:minVersion>\
                    <em:maxVersion>3.1.*</em:maxVersion>",
}

form = cgi.FieldStorage() 

pluginname = form.getvalue("plugin")
plugin = PLUGINS.get(pluginname, None)

if plugin:
    platform = form.getvalue("platform")
    if platform and os.path.exists(os.path.join(platform, plugin["filename"])):
        plugin["filename"] = os.path.join(platform, plugin["filename"])
    elif not os.path.exists(plugin["filename"]):
        plugin = False

if plugin:
    headers = [
        "Content-Type: text/xml; charset=utf-8",
        "Status: 200 OK",
    ]
    body = """<?xml version="1.0"?>
<!DOCTYPE RDF>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:extension:%(name)s">
    <em:updates>
      <Seq>
        <li>
          <Description>
            <em:version>%(version)s</em:version>
            <em:targetApplication>
              <Description>%(application)s
		<em:updateLink>%(base_url)s/%(filename)s</em:updateLink>
	      </Description>
            </em:targetApplication>
          </Description>
        </li>
      </Seq>
    </em:updates>
  </Description>
</RDF>""" % { 'name': pluginname, 'version': plugin['version'], \
              'application': APPLICATIONS[plugin["application"]], \
              'base_url': BASE_URL, 'filename': plugin['filename'] }
else:
    headers = [
        "Content-Type: text/plain; charset=utf-8",
        "Status: 404 Not found",
    ]
    body = "Plugin not found"

headers.append("Content-Length: %s" % len(body))
print '\r\n'.join(headers), '\r\n\r\n', body
sys.exit(0)
