From 5d9378d900051f1bd08fde9849a4c53bac84438c Mon Sep 17 00:00:00 2001 From: eLBati Date: Wed, 28 Nov 2018 20:34:50 +0100 Subject: [PATCH 01/10] 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 + + + + + + + + + + + From 18822e8bb6714fdd22c25058aefa7e7b75b922ef Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 11:15:57 +0100 Subject: [PATCH 02/10] Use comma for better list Co-Authored-By: eLBati --- mail_check_mailbox_size/__manifest__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mail_check_mailbox_size/__manifest__.py b/mail_check_mailbox_size/__manifest__.py index 2692c019..0d370de2 100644 --- a/mail_check_mailbox_size/__manifest__.py +++ b/mail_check_mailbox_size/__manifest__.py @@ -18,6 +18,6 @@ ], "data": [ "views/fetchmail_view.xml", - "data/cron_data.xml" + "data/cron_data.xml", ], } From c956db1877379384fdf8ff4deeef18409ef9b5b1 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 11:28:22 +0100 Subject: [PATCH 03/10] Steps for 11.0 migration We are going straight to 12.0, but this commit includes the changes for the 11.0 accorging to the wiki [1] [1] https://github.com/OCA/maintainer-tools/wiki/Migration-to-version-11.0 --- mail_check_mailbox_size/__init__.py | 2 -- mail_check_mailbox_size/__manifest__.py | 3 +-- mail_check_mailbox_size/data/cron_data.xml | 5 ++--- mail_check_mailbox_size/models/__init__.py | 2 -- mail_check_mailbox_size/models/fetchmail.py | 2 -- 5 files changed, 3 insertions(+), 11 deletions(-) diff --git a/mail_check_mailbox_size/__init__.py b/mail_check_mailbox_size/__init__.py index cde864ba..0650744f 100644 --- a/mail_check_mailbox_size/__init__.py +++ b/mail_check_mailbox_size/__init__.py @@ -1,3 +1 @@ -# -*- coding: utf-8 -*- - from . import models diff --git a/mail_check_mailbox_size/__manifest__.py b/mail_check_mailbox_size/__manifest__.py index 0d370de2..32956340 100644 --- a/mail_check_mailbox_size/__manifest__.py +++ b/mail_check_mailbox_size/__manifest__.py @@ -1,11 +1,10 @@ -# -*- 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", + "version": "11.0.1.0.0", "development_status": "Beta", "website": "https://github.com/OCA/social", "author": "Agile Business Group, Odoo Community Association (OCA)", diff --git a/mail_check_mailbox_size/data/cron_data.xml b/mail_check_mailbox_size/data/cron_data.xml index edbdcbb4..130d0e09 100644 --- a/mail_check_mailbox_size/data/cron_data.xml +++ b/mail_check_mailbox_size/data/cron_data.xml @@ -7,9 +7,8 @@ days -1 - fetchmail.server - _check_mailboxes_size_cron - () + + model._check_mailboxes_size_cron() diff --git a/mail_check_mailbox_size/models/__init__.py b/mail_check_mailbox_size/models/__init__.py index b98d86e0..bb4ec554 100644 --- a/mail_check_mailbox_size/models/__init__.py +++ b/mail_check_mailbox_size/models/__init__.py @@ -1,3 +1 @@ -# -*- coding: utf-8 -*- - from . import fetchmail diff --git a/mail_check_mailbox_size/models/fetchmail.py b/mail_check_mailbox_size/models/fetchmail.py index 348eaeb2..4a7e0f6f 100644 --- a/mail_check_mailbox_size/models/fetchmail.py +++ b/mail_check_mailbox_size/models/fetchmail.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import logging from odoo import models, fields, api, _ From b8e7c40ac800485de764ee8c8bfb9e3fc3b7c157 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 11:32:17 +0100 Subject: [PATCH 04/10] Bump version to 12.0 According to the wiki, no other changes should be needed. --- mail_check_mailbox_size/__manifest__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mail_check_mailbox_size/__manifest__.py b/mail_check_mailbox_size/__manifest__.py index 32956340..8622deb5 100644 --- a/mail_check_mailbox_size/__manifest__.py +++ b/mail_check_mailbox_size/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Check mailbox size", "summary": "Send an email summarizing the current space used by a mailbox", - "version": "11.0.1.0.0", + "version": "12.0.1.0.0", "development_status": "Beta", "website": "https://github.com/OCA/social", "author": "Agile Business Group, Odoo Community Association (OCA)", From f36b9499a68db83d5a0805d64a399eaac690dc77 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 11:44:26 +0100 Subject: [PATCH 05/10] Add myself as contributor --- mail_check_mailbox_size/readme/CONTRIBUTORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/mail_check_mailbox_size/readme/CONTRIBUTORS.rst b/mail_check_mailbox_size/readme/CONTRIBUTORS.rst index f7ee132f..724c2050 100644 --- a/mail_check_mailbox_size/readme/CONTRIBUTORS.rst +++ b/mail_check_mailbox_size/readme/CONTRIBUTORS.rst @@ -1 +1,2 @@ * Lorenzo Battistini +* Leonardo Pistone From a02be3fddd735814ff0f00e0449f74fc99a60083 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 11:46:59 +0100 Subject: [PATCH 06/10] Regenerate README with oca-gen-addon-readme --- mail_check_mailbox_size/README.rst | 13 ++++++++----- .../static/description/index.html | 8 +++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/mail_check_mailbox_size/README.rst b/mail_check_mailbox_size/README.rst index 290d334d..a586b4e2 100644 --- a/mail_check_mailbox_size/README.rst +++ b/mail_check_mailbox_size/README.rst @@ -14,19 +14,21 @@ Check mailbox size :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 + :target: https://github.com/OCA/social/tree/12.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 + :target: https://translation.odoo-community.org/projects/social-12-0/social-12-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 + :target: https://runbot.odoo-community.org/runbot/205/12.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. +This is especially relevant in cases where invoices are received by email to make sure they can get through. + **Table of contents** .. contents:: @@ -47,7 +49,7 @@ 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 `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -63,6 +65,7 @@ Contributors ~~~~~~~~~~~~ * Lorenzo Battistini +* Leonardo Pistone Maintainers ~~~~~~~~~~~ @@ -85,6 +88,6 @@ Current `maintainer `__: |maintainer-eLBati| -This module is part of the `OCA/social `_ project on GitHub. +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/static/description/index.html b/mail_check_mailbox_size/static/description/index.html index 50101e5b..b1929602 100644 --- a/mail_check_mailbox_size/static/description/index.html +++ b/mail_check_mailbox_size/static/description/index.html @@ -367,8 +367,9 @@ ul.auto-toc { !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> -

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

+

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.

+

This is especially relevant in cases where invoices are received by email to make sure they can get through.

Table of contents

    @@ -393,7 +394,7 @@ ul.auto-toc {

    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.

    +feedback.

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

@@ -408,6 +409,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome

Contributors

@@ -419,7 +421,7 @@ 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.

+

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.

From b5013bee0c3213319ef7b0ea48bc6d760e55bec8 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 12:08:23 +0100 Subject: [PATCH 07/10] Add new state field in cron --- mail_check_mailbox_size/data/cron_data.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/mail_check_mailbox_size/data/cron_data.xml b/mail_check_mailbox_size/data/cron_data.xml index 130d0e09..50a2622e 100644 --- a/mail_check_mailbox_size/data/cron_data.xml +++ b/mail_check_mailbox_size/data/cron_data.xml @@ -8,6 +8,7 @@ -1 + code model._check_mailboxes_size_cron() From 1498437ae727225e2741c07cd3fc346cbb31a305 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 14:29:39 +0100 Subject: [PATCH 08/10] Decode bytes objects obtained in Python 3 The imaplib module in Python 3 returns payloads as bytes, which are not automatically converted to strs. That's a good thing! But we need to encode manually. --- mail_check_mailbox_size/models/fetchmail.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/mail_check_mailbox_size/models/fetchmail.py b/mail_check_mailbox_size/models/fetchmail.py index 4a7e0f6f..d1566a0e 100644 --- a/mail_check_mailbox_size/models/fetchmail.py +++ b/mail_check_mailbox_size/models/fetchmail.py @@ -53,7 +53,7 @@ class Fetchmail(models.Model): number_of_messages_all = 0 size_all = 0 for item in list: - x = item.split() + x = item.decode().split() mailbox = x[-1] # Select the desired folder @@ -64,13 +64,14 @@ class Fetchmail(models.Model): "Server %s responded %s for folder %s" % (server.name, number_of_messages, mailbox)) continue - number_of_messages_all += int(number_of_messages[0]) + number_of_messages_all += int( + number_of_messages[0].decode()) 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 = [int(msg_part.decode()) for msg_part in msg[0].split()] m.sort() if m: message_set = "%d:%d" % (m[0], m[-1]) @@ -78,7 +79,7 @@ class Fetchmail(models.Model): message_set, "(UID RFC822.SIZE)") for i in range(m[-1]): tmp = sizes_response[i].split() - size_folder += int(tmp[-1].replace(')', '')) + size_folder += int(tmp[-1].decode().replace(')', '')) else: size_folder = 0 result_msg += ( @@ -87,7 +88,11 @@ class Fetchmail(models.Model): "%i" "%s" "" - ) % (mailbox, int(number_of_messages[0]), size_folder) + ) % ( + mailbox, + int(number_of_messages[0].decode()), + size_folder + ) size_all += size_folder result_msg += _( From 9d1c5cf7b5f700639f5655c0e55a745007dda32b Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 15:08:35 +0100 Subject: [PATCH 09/10] Handle mailboxes with spaces The IMAP4 protocol optionally uses double quoting when mailboxes contain spaces. This is always allowed. So to fix this, first we use shlex to perform double-quote-aware splitting (a bit like bash would split command line arguments) and then we send folder names with quotes to the IMAP server. I tested this manually with a couple real IMAP servers. --- mail_check_mailbox_size/models/fetchmail.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mail_check_mailbox_size/models/fetchmail.py b/mail_check_mailbox_size/models/fetchmail.py index d1566a0e..ee8e400e 100644 --- a/mail_check_mailbox_size/models/fetchmail.py +++ b/mail_check_mailbox_size/models/fetchmail.py @@ -1,4 +1,5 @@ import logging +import shlex from odoo import models, fields, api, _ _logger = logging.getLogger(__name__) @@ -53,12 +54,11 @@ class Fetchmail(models.Model): number_of_messages_all = 0 size_all = 0 for item in list: - x = item.decode().split() + x = shlex.split(item.decode()) mailbox = x[-1] - # Select the desired folder result, number_of_messages = imap_server.select( - mailbox, readonly=1) + '"{}"'.format(mailbox), readonly=1) if result != 'OK': _logger.info( "Server %s responded %s for folder %s" From 29f1d0c8a03f055f8d83c9b0b81eb0e91fd170a0 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 15:14:01 +0100 Subject: [PATCH 10/10] Improve variable name and avoid the list keyword --- mail_check_mailbox_size/models/fetchmail.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mail_check_mailbox_size/models/fetchmail.py b/mail_check_mailbox_size/models/fetchmail.py index ee8e400e..8a841f53 100644 --- a/mail_check_mailbox_size/models/fetchmail.py +++ b/mail_check_mailbox_size/models/fetchmail.py @@ -36,8 +36,7 @@ class Fetchmail(models.Model): "Starting to check mailbox size for server %s" % server.name) imap_server = server.connect() - # The list of all folders - result, list = imap_server.list() + result, folders = imap_server.list() if result != "OK": raise Exception(_("Server responded %s") % result) result_msg += _( @@ -53,7 +52,7 @@ class Fetchmail(models.Model): ) number_of_messages_all = 0 size_all = 0 - for item in list: + for item in folders: x = shlex.split(item.decode()) mailbox = x[-1] # Select the desired folder