Skip to content

Commit

Permalink
[IMP] mail_autogenerated_header: pre-commit stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaniszewska-dev committed Sep 12, 2024
1 parent 134ae8f commit 7ffad87
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 122 deletions.
38 changes: 22 additions & 16 deletions mail_autogenerated_header/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ Autogenerated headers
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github
:target: https://github.com/OCA/social/tree/12.0/mail_autogenerated_header
:target: https://github.com/OCA/social/tree/17.0/mail_autogenerated_header
:alt: OCA/social
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/social-12-0/social-12-0-mail_autogenerated_header
:target: https://translation.odoo-community.org/projects/social-17-0/social-17-0-mail_autogenerated_header
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/social&target_branch=12.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/social&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module was written to mark Odoo's emails as being autogenerated according to `RFC 3834 <https://tools.ietf.org/html/rfc3834>`_, section 5. This allows receiving mail servers to act accordingly by for example not sending a vacation autoreply.
This module was written to mark Odoo's emails as being autogenerated
according to `RFC 3834 <https://tools.ietf.org/html/rfc3834>`__, section
5. This allows receiving mail servers to act accordingly by for example
not sending a vacation autoreply.

On the receiving side, this module drops all notifications for autogenerated incoming e-mails.
On the receiving side, this module drops all notifications for
autogenerated incoming e-mails.

The combination of both avoids possible mail loops with misconfigured or broken email servers on the opposite side.
The combination of both avoids possible mail loops with misconfigured or
broken email servers on the opposite side.

**Table of contents**

Expand All @@ -43,37 +48,38 @@ Usage
=====

There's nothing the user has to do. Developers can set the context flag
``mail_autogenerated_header_disable`` in calls to ``send_email`` in order to
suppress adding any headers at all, and override
``_message_route_process_autoreply`` to fine tune dropping autogenerated mails
per model.
``mail_autogenerated_header_disable`` in calls to ``send_email`` in
order to suppress adding any headers at all, and override
``_message_route_process_autoreply`` to fine tune dropping autogenerated
mails per model.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/social/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/social/issues/new?body=module:%20mail_autogenerated_header%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/social/issues/new?body=module:%20mail_autogenerated_header%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~
-------

* Hunki Enterprises BV
* Therp BV

Contributors
~~~~~~~~~~~~
------------

* Holger Brunn <[email protected]> (https://hunki-enterprises.com)
- Holger Brunn <[email protected]>
(https://hunki-enterprises.com)

Maintainers
~~~~~~~~~~~
-----------

This module is maintained by the OCA.

Expand All @@ -85,6 +91,6 @@ 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.

This module is part of the `OCA/social <https://github.com/OCA/social/tree/12.0/mail_autogenerated_header>`_ project on GitHub.
This module is part of the `OCA/social <https://github.com/OCA/social/tree/17.0/mail_autogenerated_header>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion mail_autogenerated_header/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"category": "Tools",
"summary": "Add headers to Odoo's mails indicating they are autogenerated",
"depends": [
'mail',
"mail",
],
}
80 changes: 53 additions & 27 deletions mail_autogenerated_header/models/ir_mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,77 @@


class IrMailServer(models.Model):
_inherit = 'ir.mail_server'
_inherit = "ir.mail_server"

@api.model
def send_email(
self, message, mail_server_id=None, smtp_server=None, smtp_port=None,
smtp_user=None, smtp_password=None, smtp_encryption=None,
smtp_debug=False, smtp_session=None,
self,
message,
mail_server_id=None,
smtp_server=None,
smtp_port=None,
smtp_user=None,
smtp_password=None,
smtp_encryption=None,
smtp_debug=False,
smtp_session=None,
):
""" Inject autogenerated header for autogoing mails """
"""Inject autogenerated header for autogoing mails"""

if not self.env.context.get('mail_autogenerated_header_disable') and\
self._send_email_set_autogenerated(
message, mail_server_id=mail_server_id,
smtp_server=smtp_server, smtp_port=smtp_port,
smtp_user=smtp_user, smtp_password=smtp_password,
smtp_encryption=smtp_encryption, smtp_debug=smtp_debug,
smtp_session=smtp_session,
):
if not self.env.context.get(
"mail_autogenerated_header_disable"
) and self._send_email_set_autogenerated(
message,
mail_server_id=mail_server_id,
smtp_server=smtp_server,
smtp_port=smtp_port,
smtp_user=smtp_user,
smtp_password=smtp_password,
smtp_encryption=smtp_encryption,
smtp_debug=smtp_debug,
smtp_session=smtp_session,
):
# MS Exchange's broken version as of
# http://blogs.technet.com/b/exchange/archive/2006/10/06/
# 3395024.aspx
message['Precedence'] = 'bulk'
message['X-Auto-Response-Suppress'] = 'OOF'
message["Precedence"] = "bulk"
message["X-Auto-Response-Suppress"] = "OOF"
# The right way to do it as of
# https://tools.ietf.org/html/rfc3834
message['Auto-Submitted'] = 'auto-generated'
message["Auto-Submitted"] = "auto-generated"

return super(IrMailServer, self).send_email(
message, mail_server_id=mail_server_id, smtp_server=smtp_server,
smtp_port=smtp_port, smtp_user=smtp_user,
smtp_password=smtp_password, smtp_encryption=smtp_encryption,
smtp_debug=smtp_debug, smtp_session=smtp_session,
message,
mail_server_id=mail_server_id,
smtp_server=smtp_server,
smtp_port=smtp_port,
smtp_user=smtp_user,
smtp_password=smtp_password,
smtp_encryption=smtp_encryption,
smtp_debug=smtp_debug,
smtp_session=smtp_session,
)

@api.model
def _send_email_set_autogenerated(
self, message, mail_server_id=None, smtp_server=None, smtp_port=None,
smtp_user=None, smtp_password=None, smtp_encryption=None,
smtp_debug=False, smtp_session=None,
self,
message,
mail_server_id=None,
smtp_server=None,
smtp_port=None,
smtp_user=None,
smtp_password=None,
smtp_encryption=None,
smtp_debug=False,
smtp_session=None,
):
"""Determine if some mail should have the autogenerated headers"""

mail = self.env['mail.mail'].search([
('message_id', '=', message['Message-Id']),
])
mail = self.env["mail.mail"].search(
[
("message_id", "=", message["Message-Id"]),
]
)
if not mail:
return False
return mail.subtype_id != self.env.ref('mail.mt_comment')
return mail.subtype_id != self.env.ref("mail.mt_comment")
28 changes: 17 additions & 11 deletions mail_autogenerated_header/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@
# Copyright 2022 Hunki Enterprises BV <https://hunki-enterprises.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import logging

from odoo import api, models

_logger = logging.getLogger(__name__)


class MailThread(models.AbstractModel):
_inherit = 'mail.thread'
_inherit = "mail.thread"

@api.model
def message_route_process(self, message, message_dict, routes):
""" Set context key to suppress notification for autogenerated incoming
mails """
"""Set context key to suppress notification for autogenerated incoming
mails"""
if self._message_route_process_autoreply(message, message_dict, routes):
_logger.info(
'Ignoring email %s from %s because it seems to be an auto '
'reply', message.get('Message-ID'), message.get('From'),
"Ignoring email %s from %s because it seems to be an auto " "reply",
message.get("Message-ID"),
message.get("From"),
)
self = self.with_context(mail_autogenerated_header=message)
return super(MailThread, self).message_route_process(
message, message_dict, routes,
message,
message_dict,
routes,
)

@api.model
def _message_route_process_autoreply(self, message, message_dict, routes):
""" Determine if some message is an autoreply """
"""Determine if some message is an autoreply"""
return (
message['Auto-Submitted'] and message['Auto-Submitted'] != 'no' or
message['X-Auto-Response-Suppress'] and set(
message['X-Auto-Response-Suppress'].split(', ')
) & set('AutoReply', 'All')
message["Auto-Submitted"]
and message["Auto-Submitted"] != "no"
or message["X-Auto-Response-Suppress"]
and set(message["X-Auto-Response-Suppress"].split(", "))
& set("AutoReply", "All")
)
24 changes: 16 additions & 8 deletions mail_autogenerated_header/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@


class ResPartner(models.Model):
_inherit = 'res.partner'
_inherit = "res.partner"

@api.model
def _notify(
self, message, rdata, record, force_send=False,
send_after_commit=True, model_description=False,
mail_auto_delete=True,
self,
message,
rdata,
record,
force_send=False,
send_after_commit=True,
model_description=False,
mail_auto_delete=True,
):
""" Inhibit notifications if this is the notification for an incoming
autogenerated mail from another system """
if self.env.context.get('mail_autogenerated_header'):
"""Inhibit notifications if this is the notification for an incoming
autogenerated mail from another system"""
if self.env.context.get("mail_autogenerated_header"):
return True
return super()._notify(
message, rdata, record, force_send=force_send,
message,
rdata,
record,
force_send=force_send,
send_after_commit=send_after_commit,
model_description=model_description,
mail_auto_delete=mail_auto_delete,
Expand Down
3 changes: 3 additions & 0 deletions mail_autogenerated_header/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions mail_autogenerated_header/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Holger Brunn \<<[email protected]>\>
(<https://hunki-enterprises.com>)
1 change: 0 additions & 1 deletion mail_autogenerated_header/readme/CONTRIBUTORS.rst

This file was deleted.

10 changes: 10 additions & 0 deletions mail_autogenerated_header/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This module was written to mark Odoo's emails as being autogenerated
according to [RFC 3834](https://tools.ietf.org/html/rfc3834), section 5.
This allows receiving mail servers to act accordingly by for example not
sending a vacation autoreply.

On the receiving side, this module drops all notifications for
autogenerated incoming e-mails.

The combination of both avoids possible mail loops with misconfigured or
broken email servers on the opposite side.
5 changes: 0 additions & 5 deletions mail_autogenerated_header/readme/DESCRIPTION.rst

This file was deleted.

5 changes: 5 additions & 0 deletions mail_autogenerated_header/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
There's nothing the user has to do. Developers can set the context flag
`mail_autogenerated_header_disable` in calls to `send_email` in order to
suppress adding any headers at all, and override
`_message_route_process_autoreply` to fine tune dropping autogenerated
mails per model.
5 changes: 0 additions & 5 deletions mail_autogenerated_header/readme/USAGE.rst

This file was deleted.

Loading

0 comments on commit 7ffad87

Please sign in to comment.