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

import uuid,re
import imaplib, socket
import sys
import os

from .plugin import PluginAbstract, ModList
from .imapmail import ImapMail
#from .mailwhitelist import WhileListUpload


msg0100 = 'fetching configuration for: \'{0}\''


#class GosaGroupMail(PluginAbstract, WhileListUpload, metaclass=ModList):
class GosaGroupMail(PluginAbstract, ImapMail, metaclass=ModList):

	def __init__(self, postcmd):

		super().__init__(postcmd)
		super(PluginAbstract, self).__init__(postcmd)
		#super(ImapMail, self).__init__()


	def _getImapConfig(self, imap_name):

		imap_cfg_def = {
			'mailboxbase' : (),
			'mailboxsharebase' : ('Shared Folders', ),
			'mailboxlist': (
				'Sent',
				'Drafts',
				'Templates',
				),
			'mailboxblacklist' : (),
			'mailuserblacklist' : (),
			'proxyacl' : 'lrswipkxtecd',
			'mailarchivemode' : '',
			'mailarchive' : '',
			'appendtimestamp' : True,
			'deletemailbox' : False,
			'skipspecial' : True,
			'mountbase' : '/mnt/pygosa',
			'sshhost' : '',
			'sshuser' : 'root',
			'sshpwd' : '',
			'sshkey' : '',
			'sshport' : '',
			'sshhkeysave' : True,
			'sshhkeypol' : 'reject',
			'groupmailautosubscribe' : True,
			}

		imap_cfg_ver = {
			'mailboxbase' : ('list', 'str', ),
			'mailboxsharebase' : ('list', 'str', ),
			'mailboxlist': ('list', 'str', ),
			'mailboxblacklist' : ('list', 'str', ),
			'mailuserblacklist' : ('list', 'str', ),
			'proxyacl' : ('str', ),
			'mailarchivemode' : ('str', ),
			'mailarchive' : ('list', 'str', ),
			'appendtimestamp' : ('bool', ),
			'deletemailbox' : ('bool', ),
			'skipspecial' : ('bool', ),
			'mountbase' : ('str', ),
			'sshhost' : ('str', ),
			'sshuser' : ('str', ),
			'sshpwd' : ('str', ),
			'sshkey' : ('str', ),
			'sshport' : ('int', ),
			'sshhkeysave' : ('bool', ),
			'sshhkeypol' : ('str', ),
			'groupmailautosubscribe' : ('bool', ),
			}

		# get imap configuration
		self.logger.debug(msg0100.format(self.class_name + '/' + imap_name))
		self.config.updateDefaults(self.class_name + '/' + imap_name, imap_cfg_def)
		self.config.updateVerify(imap_cfg_ver)

		return self.config.getSection(self.class_name + '/' + imap_name)


	def create(self):

		super().create()

		self.modGroupMail()
		#self.whitelistUpload()


	def modify(self):

		super().modify()

		self.modGroupMail()
		#self.whitelistUpload()


	def preremove(self):

		super().remove()

		self.delMailbox()
		#self.whitelistUpload()

