diff --git a/locales/en.json b/locales/en.json index b7ecf232..dfb8ffa4 100644 --- a/locales/en.json +++ b/locales/en.json @@ -118,6 +118,7 @@ "mail_alias_remove_failed": "Unable to remove mail alias '{mail:s}'", "mail_domain_unknown": "Unknown mail address domain '{domain:s}'", "mail_forward_remove_failed": "Unable to remove mail forward '{mail:s}'", + "mailbox_used_space_dovecot_down": "Dovecot mailbox service need to be up, if you want to get mailbox used space", "maindomain_change_failed": "Unable to change the main domain", "maindomain_changed": "The main domain has been changed", "monitor_disabled": "The server monitoring has been disabled", diff --git a/src/yunohost/user.py b/src/yunohost/user.py index f1b47f79..7ea9852b 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -34,6 +34,7 @@ import re from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger +from yunohost.service import service_status logger = getActionLogger('yunohost.user') @@ -91,7 +92,7 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None): def user_create(auth, username, firstname, lastname, mail, password, - mailbox_quota=0): + mailbox_quota="0"): """ Create user @@ -403,20 +404,42 @@ def user_info(auth, username): result_dict['mail-forward'] = user['maildrop'][1:] if 'mailuserquota' in user: - if user['mailuserquota'][0] != '0': - cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] - userquota = subprocess.check_output(cmd,stderr=subprocess.STDOUT, - shell=True) - quotavalue = re.findall(r'\d+', userquota) - result = '%s (%s%s)' % ( _convertSize(eval(quotavalue[0])), - quotavalue[2], '%') - result_dict['mailbox-quota'] = { - 'limit' : user['mailuserquota'][0], - 'use' : result - } + userquota = user['mailuserquota'][0] + + if isinstance(userquota, int): + userquota = str(userquota) + + # Test if userquota is '0' or '0M' ( quota pattern is ^(\d+[bkMGT])|0$ ) + is_limited = not re.match('0[bkMGT]?', userquota) + storage_use = '?' + + if service_status("dovecot")["status"] != "running": + logger.warning(m18n.n('mailbox_used_space_dovecot_down')) else: - result_dict['mailbox-quota'] = m18n.n('unlimit') - + cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] + cmd_result = subprocess.check_output(cmd, stderr=subprocess.STDOUT, + shell=True) + # Exemple of return value for cmd: + # """Quota name=User quota Type=STORAGE Value=0 Limit=- %=0 + # Quota name=User quota Type=MESSAGE Value=0 Limit=- %=0""" + has_value = re.search(r'Value=(\d+)', cmd_result) + + if has_value: + storage_use = int(has_value.group(1)) + storage_use = _convertSize(storage_use) + + if is_limited: + has_percent = re.search(r'%=(\d+)', cmd_result) + + if has_percent: + percentage = int(has_percent.group(1)) + storage_use += ' (%s%%)' % percentage + + result_dict['mailbox-quota'] = { + 'limit' : userquota if is_limited else m18n.n('unlimit'), + 'use' : storage_use + } + if result: return result_dict else: