Преглед изворни кода

Add Option to Disable Html Mail Parsing

Guénaël Muller пре 7 година
родитељ
комит
3ea955dabc

+ 1 - 0
tracim/development.ini.base Прегледај датотеку

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

+ 4 - 0
tracim/tracim/config/app_cfg.py Прегледај датотеку

@@ -384,6 +384,10 @@ class CFG(object):
384 384
         self.EMAIL_REPLY_IMAP_USE_SSL = asbool(tg.config.get(
385 385
             'email.reply.imap.use_ssl',
386 386
         ))
387
+        self.EMAIL_REPLY_USE_HTML_PARSING = asbool(tg.config.get(
388
+            'email.reply.use_html_parsing',
389
+            True,
390
+        ))
387 391
 
388 392
         self.TRACKER_JS_PATH = tg.config.get(
389 393
             'js_tracker_path',

+ 1 - 0
tracim/tracim/lib/daemons.py Прегледај датотеку

@@ -177,6 +177,7 @@ class MailFetcherDaemon(Daemon):
177 177
             # FIXME - G.M - 2017-11-15 - proper tracim url formatting
178 178
             endpoint=cfg.WEBSITE_BASE_URL + "/events",
179 179
             token=cfg.EMAIL_REPLY_TOKEN,
180
+            use_html_parsing=cfg.EMAIL_REPLY_USE_HTML_PARSING,
180 181
         )
181 182
         self._fetcher.run()
182 183
 

+ 13 - 4
tracim/tracim/lib/email_fetcher.py Прегледај датотеку

@@ -56,7 +56,10 @@ class DecodedMail(object):
56 56
     def get_special_key(self) -> typing.Optional[str]:
57 57
         return self._decode_header(TRACIM_SPECIAL_KEY_HEADER)
58 58
 
59
-    def get_body(self) -> typing.Optional[str]:
59
+    def get_body(
60
+            self,
61
+            use_html_parsing=True
62
+    ) -> typing.Optional[str]:
60 63
         body_part = self._get_mime_body_message()
61 64
         body = None
62 65
         if body_part:
@@ -70,13 +73,15 @@ class DecodedMail(object):
70 73
             elif content_type == CONTENT_TYPE_TEXT_HTML:
71 74
                 html_body = body_part.get_payload(decode=True).decode(
72 75
                     charset)
73
-                html_body = str(ParsedHTMLMail(html_body))
76
+                if use_html_parsing:
77
+                    html_body = str(ParsedHTMLMail(html_body))
74 78
                 body = DecodedMail._sanitize_html_body(html_body)
75 79
 
76 80
         return body
77 81
 
78 82
     @classmethod
79 83
     def _parse_txt_body(cls, txt_body: str) -> str:
84
+        # TODO - G.M - 2017-11-30 - Add option to disable parsing
80 85
         txt_body = EmailReplyParser.parse_reply(txt_body)
81 86
         html_body = markdown.markdown(txt_body)
82 87
         body = DecodedMail._sanitize_html_body(html_body)
@@ -182,6 +187,7 @@ class MailFetcher(object):
182 187
         delay: int,
183 188
         endpoint: str,
184 189
         token: str,
190
+        use_html_parsing: bool
185 191
     ) -> None:
186 192
         """
187 193
         Fetch mail from a mailbox folder through IMAP and add their content to
@@ -196,6 +202,7 @@ class MailFetcher(object):
196 202
         :param delay: seconds to wait before fetching new mail again
197 203
         :param endpoint: tracim http endpoint where decoded mail are send.
198 204
         :param token: token to authenticate http connexion
205
+        :param use_html_parsing: parse html mail
199 206
         """
200 207
         self._connection = None
201 208
         self.host = host
@@ -207,6 +214,7 @@ class MailFetcher(object):
207 214
         self.delay = delay
208 215
         self.endpoint = endpoint
209 216
         self.token = token
217
+        self.use_html_parsing = use_html_parsing
210 218
 
211 219
         self._is_active = True
212 220
 
@@ -217,7 +225,7 @@ class MailFetcher(object):
217 225
                 self._connect()
218 226
                 messages = self._fetch()
219 227
                 # TODO - G.M -  2017-11-22 retry sending unsended mail
220
-                # These mails are return by _notify_tracim, flag them with "unseen"
228
+                # These mails are return by _notify_tracim, flag them with "unseen" # nopep8
221 229
                 # or store them until new _notify_tracim call
222 230
                 cleaned_mails = [DecodedMail(msg) for msg in messages]
223 231
                 self._notify_tracim(cleaned_mails)
@@ -311,7 +319,8 @@ class MailFetcher(object):
311 319
                    'user_mail': mail.get_from_address(),
312 320
                    'content_id': mail.get_key(),
313 321
                    'payload': {
314
-                       'content': mail.get_body(),
322
+                       'content': mail.get_body(
323
+                           use_html_parsing=self.use_html_parsing),
315 324
                    }}
316 325
             try:
317 326
                 r = requests.post(self.endpoint, json=msg)