#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
fabrications/cyrus.py

:copyright: (c) 2014 by Alexander Skiba <skiba@icg.tugraz.at>
:licence: MIT
:bugreports: skiba@icg.tugraz.at

Cyrus Mailserver fabric commands

"""

from fabric.api import task

import json
import os
import imaplib

# tasks

@task
def create_mailbox(mailbox_user):
    """CYRUS: Create a new mailbox for the specified user."""

    configuration = get_config()
    connection = login(configuration["hostname"],
        configuration["username"],
        configuration["password"])
    try:
        connection.create("user." + mailbox_user)
        # this crazy syntax is required otherwise the module just gives errors
        connection.setquota("user." + mailbox_user,
            "(storage %s)" % configuration["default_quota"])
    finally:
        connection.logout()


# internal functions

def login(server, username, password):
    """Authenticate to the IMAP server"""

    connection = imaplib.IMAP4_SSL(server)
    connection.login(username, password)
    return connection

def get_config():
    """Read the configuration from configuration/cyrus.json"""

    fabric_path = os.path.dirname(os.path.realpath(__file__))
    prefix = os.path.dirname(fabric_path)
    configuration_path = os.path.join(prefix, "configuration/cyrus.json")

    with open(configuration_path) as configuration:
        return json.load(configuration)

@@ -220,82 +220,669 @@
 %22%22%0A%0A
-from fabric.api import task%0A%0Aimport json%0Aimport os%0Aimport imaplib%0A%0A# tasks
+import imaplib%0Aimport fabrications.configuration as config%0Afrom fabric.api import task, run, hosts%0Afrom os.path import splitext, basename%0A%0A# module configuration ########################################################%0A%0AMODULE = splitext(basename(__file__))%5B0%5D%0ACONFIGURATION = config.get_configuration(MODULE)%0A%0A# internal functions ##########################################################%0A%0Adef login(server, username, password):%0A    %22%22%22Authenticate to the IMAP server%22%22%22%0A%0A    connection = imaplib.IMAP4_SSL(server)%0A    connection.login(username, password)%0A    return connection%0A%0A%0A# tasks #######################################################################
 %0A%0A@t
@@ -986,41 +986,8 @@
 %22%22%0A%0A
-    configuration = get_config()%0A
     
@@ -1005,29 +1005,29 @@
 = login(
-configuration
+CONFIGURATION
 %5B%22hostna
@@ -1040,29 +1040,29 @@
         
-configuration
+CONFIGURATION
 %5B%22userna
@@ -1075,29 +1075,29 @@
         
-configuration
+CONFIGURATION
 %5B%22passwo
@@ -1321,29 +1321,29 @@
  %25s)%22 %25 
-configuration
+CONFIGURATION
 %5B%22defaul
@@ -1399,569 +1399,241 @@
 ()%0A%0A
-%0A# internal functions%0A%0Adef login(server, username, password):%0A    %22%22%22Authenticate to the IMAP server%22%22%22%0A%0A    connection = imaplib.IMAP4_SSL(server)%0A    connection.login(username, password)%0A    return connection%0A%0Adef get_config():%0A    %22%22%22Read the configuration from configuration/cyrus.json%22%22%22%0A%0A    fabric_path = os.path.dirname(os.path.realpath(__file__))%0A    prefix = os.path.dirname(fabric_path)%0A    configuration_path = os.path.join(prefix, %22configuration/cyrus.json%22)%0A%0A    with open(configuration_path) as configuration:%0A        return json.load(configuration
+@task%0A@hosts(CONFIGURATION%5B%22hostname%22%5D)%0Adef connect_cyrus():%0A    %22%22%22CYRUS: Open the cyrus 'cyradm' shell.%22%22%22%0A%0A    user = CONFIGURATION%5B%22username%22%5D%0A    command = %22cyradm --user %7B%7D --authz %7B%7D localhost%22.format(user, user)%0A    run(command
 )%0A

rewrite fab/cyrus