# -*- coding: utf8 -*-
# vim: expandtab

# Copyright 2002 Raphaël Hertzog
# Copyright 2006 Jeroen van Wolffelaar
# This file is distributed under the terms of the General Public License
# version 2 or (at your option) any later version.

import os, os.path, re, rfc822, time, email
from email import Utils, Header

from config import root

def save_msg_in_dir(msg, dir):
    """Add the message in the directory."""
    if not os.path.isdir(dir):
        os.makedirs(dir)
    info = extract_info(msg)

    targetfile = "%s/%s.txt" % (dir, info['timestamp'])
    if os.path.isfile(targetfile):
        raise("Aiee, already such message %s" % targetfile)
    f = open(targetfile, "w")
    f.write(msg.as_string())
    f.close()
    os.chmod(targetfile, 0664)

re_katie_install = re.compile(r"^\S+ \S+ (\S+)")
re_distribution = re.compile(r"^Distribution:\s*(\S+)", re.M)
re_urgency = re.compile(r"^Urgency:\s*(\S+)", re.M)

def extract_info(msg):
    """Extract pseudo-header informations in a dictionnary"""
    info = {}
    if msg.is_multipart():
        for part in msg.walk():
            if part.get_content_type() == "text/plain":
                body = part.get_payload(None, 1)
                break
    else:
        body = msg.get_payload(None, 1)
    lines = body.split("\n", 3)
    for i in range(3):
        res = re.match(r"^(\w+): (\S+)(.*)", lines[i])
        if res:
            field = res.group(1).lower()
            info[field] = res.group(2)
            if field == "subject":
                info[field] = info[field] + res.group(3)
        else:
            break

    if not info.has_key("subject"):
        subject = unicode(Header.make_header(Header.decode_header(msg.get("subject"))))
        if subject.split()[0] in ['Accepted', 'Installed']:
            # katie install mail
            version = re_katie_install.search(subject).group(1)
            distribution = re_distribution.search(body)
            if distribution:
                distribution = " in %s" % distribution.group(1)
            else:
                distribution = ""
            urgency = re_urgency.search(body)
            if urgency:
                urgency = " (%s)" % urgency.group(1)
            else:
                urgency = ""
            subject = "Accepted %s%s%s" % (version, distribution, urgency)
        info["subject"] = subject
    if msg.has_key("X-PTS-Subject"):
        info["subject"] = msg.get("X-PTS-Subject")
    if msg.has_key("X-PTS-Url"):
        info["url"] = msg.get("X-PTS-Url")
    if msg.has_key("X-PTS-Package"):
        info["package"] = msg.get("X-PTS-Package")
    if msg.has_key("date"):
        date = email.Utils.mktime_tz(email.Utils.parsedate_tz(msg.get("date")))
        info["timestamp"] = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(date))
        info["date"] = time.strftime("%Y-%m-%d", time.gmtime(date))
    if msg.has_key("From"):
        frm = msg.get("From")
        if msg.has_key("X-PTS-From"): frm = msg.get("X-PTS-From")
        (realname, address) = rfc822.parseaddr(frm)
        if realname:
            frm = realname
        else:
            frm = address
        frm = ensure_utf8(frm)
        try:
            frm = unicode(Header.make_header(Header.decode_header(frm)))
        except UnicodeError:
            pass
        info["from_name"] = frm
    return info

def store_news(pkg, msg):
    hash = pkg[0]
    if pkg[:3] == "lib":
        hash = pkg[:4]
    basedir = "%s/base/%s/%s" % (root, hash, pkg)
    save_msg_in_dir(msg, basedir+"/news")
    # Synchronize the XML document
    f = os.popen("%s/bin/update_news.py" % root, "w")
    f.write(pkg+"\n")
    f.close()
    # Regenerate the HTML page
    f = os.popen("%s/bin/generate_html.sh" % root, "w")
    f.write(pkg+"\n")
    f.close()


def ensure_utf8(str):
    try:
        str.decode("utf8")
    except UnicodeError:
        str = str.decode("latin1")
    return str
    
# Version Control Systems table
# Fields:
#   'tag' is as it would appear in a vcs-XXX field, e.g. 'svn' for Vcs-Svn
#       tag         common name     upstream URL 
vcs_table = {
        'arch':     ('Arch',        'http://www.gnuarch.org/arch/'),
        'bzr':      ('Bazaar',      'http://bazaar-vcs.org/'),
        'cvs':      ('CVS',         'http://www.nongnu.org/cvs/'),
        'darcs':    ('Darcs',       'http://abridgegame.org/darcs/'),
        'git':      ('Git',         'http://git.or.cz/'),
        'hg':       ('Mercurial',   'http://www.selenic.com/mercurial/'),
        'mtn':      ('Monotone',    'http://venge.net/monotone/'),
        'svn':      ('Subversion',  'http://subversion.tigris.org/'),
        }

