From 81bc95fa063570375c3c2617bf15f7a502e2b8c5 Mon Sep 17 00:00:00 2001 From: eLBati Date: Wed, 28 Nov 2018 20:34:50 +0100 Subject: [PATCH] ADD mail_check_mailbox_size Send an email with a summary of remote IMAP mailbox folders, with their total space used. --- mail_check_mailbox_size/README.rst | 90 ++++ mail_check_mailbox_size/__init__.py | 3 + mail_check_mailbox_size/__manifest__.py | 23 + mail_check_mailbox_size/data/cron_data.xml | 17 + mail_check_mailbox_size/models/__init__.py | 3 + mail_check_mailbox_size/models/fetchmail.py | 137 ++++++ mail_check_mailbox_size/readme/CONFIGURE.rst | 5 + .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 3 + .../static/description/index.html | 428 ++++++++++++++++++ .../views/fetchmail_view.xml | 18 + 11 files changed, 728 insertions(+) create mode 100644 mail_check_mailbox_size/README.rst create mode 100644 mail_check_mailbox_size/__init__.py create mode 100644 mail_check_mailbox_size/__manifest__.py create mode 100644 mail_check_mailbox_size/data/cron_data.xml create mode 100644 mail_check_mailbox_size/models/__init__.py create mode 100644 mail_check_mailbox_size/models/fetchmail.py create mode 100644 mail_check_mailbox_size/readme/CONFIGURE.rst create mode 100644 mail_check_mailbox_size/readme/CONTRIBUTORS.rst create mode 100644 mail_check_mailbox_size/readme/DESCRIPTION.rst create mode 100644 mail_check_mailbox_size/static/description/index.html create mode 100644 mail_check_mailbox_size/views/fetchmail_view.xml diff --git a/mail_check_mailbox_size/README.rst b/mail_check_mailbox_size/README.rst new file mode 100644 index 00000000..290d334d --- /dev/null +++ b/mail_check_mailbox_size/README.rst @@ -0,0 +1,90 @@ +================== +Check mailbox size +================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github + :target: https://github.com/OCA/social/tree/10.0/mail_check_mailbox_size + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-10-0/social-10-0-mail_check_mailbox_size + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/205/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Send an email with a summary of remote IMAP mailbox folders, with their total space used. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To enable the mailbox check, you have to configure an Incoming Mail Server setting + +Server Type = IMAP Server + +Select 'Check Size' and fill 'Contacts to notify' with the list of partners to be notified with the summary of mailbox folders size. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Agile Business Group + +Contributors +~~~~~~~~~~~~ + +* Lorenzo Battistini + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-eLBati| image:: https://github.com/eLBati.png?size=40px + :target: https://github.com/eLBati + :alt: eLBati + +Current `maintainer `__: + +|maintainer-eLBati| + +This module is part of the `OCA/social `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_check_mailbox_size/__init__.py b/mail_check_mailbox_size/__init__.py new file mode 100644 index 00000000..cde864ba --- /dev/null +++ b/mail_check_mailbox_size/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import models diff --git a/mail_check_mailbox_size/__manifest__.py b/mail_check_mailbox_size/__manifest__.py new file mode 100644 index 00000000..2692c019 --- /dev/null +++ b/mail_check_mailbox_size/__manifest__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Lorenzo Battistini +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +{ + "name": "Check mailbox size", + "summary": "Send an email summarizing the current space used by a mailbox", + "version": "10.0.1.0.0", + "development_status": "Beta", + "website": "https://github.com/OCA/social", + "author": "Agile Business Group, Odoo Community Association (OCA)", + "maintainers": ["eLBati"], + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": [ + "fetchmail", + ], + "data": [ + "views/fetchmail_view.xml", + "data/cron_data.xml" + ], +} diff --git a/mail_check_mailbox_size/data/cron_data.xml b/mail_check_mailbox_size/data/cron_data.xml new file mode 100644 index 00000000..edbdcbb4 --- /dev/null +++ b/mail_check_mailbox_size/data/cron_data.xml @@ -0,0 +1,17 @@ + + + + + Check mailbox size + 1 + days + -1 + + fetchmail.server + _check_mailboxes_size_cron + () + + + + + diff --git a/mail_check_mailbox_size/models/__init__.py b/mail_check_mailbox_size/models/__init__.py new file mode 100644 index 00000000..b98d86e0 --- /dev/null +++ b/mail_check_mailbox_size/models/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import fetchmail diff --git a/mail_check_mailbox_size/models/fetchmail.py b/mail_check_mailbox_size/models/fetchmail.py new file mode 100644 index 00000000..348eaeb2 --- /dev/null +++ b/mail_check_mailbox_size/models/fetchmail.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- + +import logging +from odoo import models, fields, api, _ + +_logger = logging.getLogger(__name__) + + +class Fetchmail(models.Model): + _inherit = 'fetchmail.server' + + check_mailbox_size = fields.Boolean("Check Size") + mailbox_size_partner_ids = fields.Many2many( + "res.partner", string="Contacts to notify", + domain=[('email', '!=', False)] + ) + + @api.model + def _check_mailboxes_size_cron(self): + """ Method called by cron to fetch mails from servers """ + return self.search([ + ('state', '=', 'done'), + ('type', '=', 'imap'), + ('check_mailbox_size', '=', True) + ])._check_mailbox_size() + + @api.multi + def _check_mailbox_size(self): + for server in self: + result_msg = '' + imap_server = None + try: + if not server.mailbox_size_partner_ids: + continue + result_msg += _('

Server %s

') % server.name + _logger.info( + "Starting to check mailbox size for server %s" + % server.name) + imap_server = server.connect() + # The list of all folders + result, list = imap_server.list() + if result != "OK": + raise Exception(_("Server responded %s") % result) + result_msg += _( + "" + "" + "" + "" + "" + "" + "" + "" + "" + ) + number_of_messages_all = 0 + size_all = 0 + for item in list: + x = item.split() + mailbox = x[-1] + + # Select the desired folder + result, number_of_messages = imap_server.select( + mailbox, readonly=1) + if result != 'OK': + _logger.info( + "Server %s responded %s for folder %s" + % (server.name, number_of_messages, mailbox)) + continue + number_of_messages_all += int(number_of_messages[0]) + + size_folder = 0 + # Go through all the messages in the selected folder + typ, msg = imap_server.search(None, 'ALL') + # Find the first and last messages + m = [int(msg_part) for msg_part in msg[0].split()] + m.sort() + if m: + message_set = "%d:%d" % (m[0], m[-1]) + result, sizes_response = imap_server.fetch( + message_set, "(UID RFC822.SIZE)") + for i in range(m[-1]): + tmp = sizes_response[i].split() + size_folder += int(tmp[-1].replace(')', '')) + else: + size_folder = 0 + result_msg += ( + "" + "" + "" + "" + "" + ) % (mailbox, int(number_of_messages[0]), size_folder) + size_all += size_folder + + result_msg += _( + "" + "" + "" + "" + "" + "" + "" + "" + "
Folder# MsgSize
%s%i%s
Sum%i" + "%10.3fMB
" + ) % (number_of_messages_all, size_all / 1e6) + except Exception as e: + result_msg = _( + "An error occured while checking mailbox %s:
%s" + ) % (server.name, str(e)) + finally: + if imap_server: + imap_server.logout() + if result_msg: + self.env['mail.mail'].create({ + 'subject': _("Mailbox size for server %s") % server.name, + 'body_html': result_msg, + 'recipient_ids': [ + (6, 0, server.mailbox_size_partner_ids.ids)] + }) + + @api.model + def _update_cron(self): + res = super(Fetchmail, self)._update_cron() + if self.env.context.get('fetchmail_cron_running'): + return res + try: + cron = self.env.ref( + 'mail_check_mailbox_size.ir_cron_check_mailbox_size') + cron.toggle(model=self._name, domain=[ + ('state', '=', 'done'), ('type', '=', 'imap'), + ('check_mailbox_size', '=', True), + ]) + except ValueError: + # Nevermind if ir_cron_check_mailbox_size cannot be found + pass + return res diff --git a/mail_check_mailbox_size/readme/CONFIGURE.rst b/mail_check_mailbox_size/readme/CONFIGURE.rst new file mode 100644 index 00000000..b024406f --- /dev/null +++ b/mail_check_mailbox_size/readme/CONFIGURE.rst @@ -0,0 +1,5 @@ +To enable the mailbox check, you have to configure an Incoming Mail Server setting + +Server Type = IMAP Server + +Select 'Check Size' and fill 'Contacts to notify' with the list of partners to be notified with the summary of mailbox folders size. diff --git a/mail_check_mailbox_size/readme/CONTRIBUTORS.rst b/mail_check_mailbox_size/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..f7ee132f --- /dev/null +++ b/mail_check_mailbox_size/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Lorenzo Battistini diff --git a/mail_check_mailbox_size/readme/DESCRIPTION.rst b/mail_check_mailbox_size/readme/DESCRIPTION.rst new file mode 100644 index 00000000..0d8bfb38 --- /dev/null +++ b/mail_check_mailbox_size/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +Send an email with a summary of remote IMAP mailbox folders, with their total space used. + +This is especially relevant in cases where invoices are received by email to make sure they can get through. diff --git a/mail_check_mailbox_size/static/description/index.html b/mail_check_mailbox_size/static/description/index.html new file mode 100644 index 00000000..50101e5b --- /dev/null +++ b/mail_check_mailbox_size/static/description/index.html @@ -0,0 +1,428 @@ + + + + + + +Check mailbox size + + + +
+

Check mailbox size

+ + +

Beta License: LGPL-3 OCA/social Translate me on Weblate Try me on Runbot

+

Send an email with a summary of remote IMAP mailbox folders, with their total space used.

+

Table of contents

+ +
+

Configuration

+

To enable the mailbox check, you have to configure an Incoming Mail Server setting

+

Server Type = IMAP Server

+

Select ‘Check Size’ and fill ‘Contacts to notify’ with the list of partners to be notified with the summary of mailbox folders size.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Agile Business Group
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

eLBati

+

This module is part of the OCA/social project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/mail_check_mailbox_size/views/fetchmail_view.xml b/mail_check_mailbox_size/views/fetchmail_view.xml new file mode 100644 index 00000000..1dd999c5 --- /dev/null +++ b/mail_check_mailbox_size/views/fetchmail_view.xml @@ -0,0 +1,18 @@ + + + + + view_email_server_form_check_size + fetchmail.server + + + + + + + + + + +