Browse Source

Add option to disable parsing for txt mail

Guénaël Muller 7 years ago
parent
commit
ac48a15e2c

+ 1 - 0
tracim/development.ini.base View File

@@ -227,6 +227,7 @@ email.reply.token = mysecuretoken
227 227
 # Delay in seconds between each check
228 228
 email.reply.check.heartbeat = 60
229 229
 email.reply.use_html_parsing = true
230
+email.reply.use_txt_parsing = true
230 231
 
231 232
 ## Radical (CalDav server) configuration
232 233
 # radicale.server.host = 0.0.0.0

+ 4 - 0
tracim/tracim/config/app_cfg.py View File

@@ -388,6 +388,10 @@ class CFG(object):
388 388
             'email.reply.use_html_parsing',
389 389
             True,
390 390
         ))
391
+        self.EMAIL_REPLY_USE_TXT_PARSING = asbool(tg.config.get(
392
+            'email.reply.use_txt_parsing',
393
+            True,
394
+        ))
391 395
 
392 396
         self.TRACKER_JS_PATH = tg.config.get(
393 397
             'js_tracker_path',

+ 1 - 0
tracim/tracim/lib/daemons.py View File

@@ -178,6 +178,7 @@ class MailFetcherDaemon(Daemon):
178 178
             endpoint=cfg.WEBSITE_BASE_URL + "/events",
179 179
             token=cfg.EMAIL_REPLY_TOKEN,
180 180
             use_html_parsing=cfg.EMAIL_REPLY_USE_HTML_PARSING,
181
+            use_txt_parsing=cfg.EMAIL_REPLY_USE_TXT_PARSING,
181 182
         )
182 183
         self._fetcher.run()
183 184
 

+ 11 - 12
tracim/tracim/lib/email_fetcher.py View File

@@ -58,7 +58,8 @@ class DecodedMail(object):
58 58
 
59 59
     def get_body(
60 60
             self,
61
-            use_html_parsing=True
61
+            use_html_parsing=True,
62
+            use_txt_parsing=True,
62 63
     ) -> typing.Optional[str]:
63 64
         body_part = self._get_mime_body_message()
64 65
         body = None
@@ -68,7 +69,10 @@ class DecodedMail(object):
68 69
             if content_type == CONTENT_TYPE_TEXT_PLAIN:
69 70
                 txt_body = body_part.get_payload(decode=True).decode(
70 71
                     charset)
71
-                body = DecodedMail._parse_txt_body(txt_body)
72
+                if use_txt_parsing:
73
+                    txt_body = EmailReplyParser.parse_reply(txt_body)
74
+                html_body = markdown.markdown(txt_body)
75
+                body = DecodedMail._sanitize_html_body(html_body)
72 76
 
73 77
             elif content_type == CONTENT_TYPE_TEXT_HTML:
74 78
                 html_body = body_part.get_payload(decode=True).decode(
@@ -80,14 +84,6 @@ class DecodedMail(object):
80 84
         return body
81 85
 
82 86
     @classmethod
83
-    def _parse_txt_body(cls, txt_body: str) -> str:
84
-        # TODO - G.M - 2017-11-30 - Add option to disable parsing
85
-        txt_body = EmailReplyParser.parse_reply(txt_body)
86
-        html_body = markdown.markdown(txt_body)
87
-        body = DecodedMail._sanitize_html_body(html_body)
88
-        return body
89
-
90
-    @classmethod
91 87
     def _sanitize_html_body(cls, html_body: str) -> str:
92 88
         soup = BeautifulSoup(html_body, 'html.parser')
93 89
         config = BEAUTIFULSOUP_HTML_BODY_SANITIZE_CONFIG
@@ -187,7 +183,8 @@ class MailFetcher(object):
187 183
         delay: int,
188 184
         endpoint: str,
189 185
         token: str,
190
-        use_html_parsing: bool
186
+        use_html_parsing: bool,
187
+        use_txt_parsing: bool,
191 188
     ) -> None:
192 189
         """
193 190
         Fetch mail from a mailbox folder through IMAP and add their content to
@@ -215,6 +212,7 @@ class MailFetcher(object):
215 212
         self.endpoint = endpoint
216 213
         self.token = token
217 214
         self.use_html_parsing = use_html_parsing
215
+        self.use_txt_parsing = use_txt_parsing
218 216
 
219 217
         self._is_active = True
220 218
 
@@ -320,7 +318,8 @@ class MailFetcher(object):
320 318
                    'content_id': mail.get_key(),
321 319
                    'payload': {
322 320
                        'content': mail.get_body(
323
-                           use_html_parsing=self.use_html_parsing),
321
+                           use_html_parsing=self.use_html_parsing,
322
+                           use_txt_parsing=self.use_txt_parsing),
324 323
                    }}
325 324
             try:
326 325
                 r = requests.post(self.endpoint, json=msg)