浏览代码

Add Option to Disable Html Mail Parsing

Guénaël Muller 7 年前
父节点
当前提交
3ea955dabc
共有 4 个文件被更改,包括 19 次插入4 次删除
  1. 1 0
      tracim/development.ini.base
  2. 4 0
      tracim/tracim/config/app_cfg.py
  3. 1 0
      tracim/tracim/lib/daemons.py
  4. 13 4
      tracim/tracim/lib/email_fetcher.py

+ 1 - 0
tracim/development.ini.base 查看文件

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

+ 4 - 0
tracim/tracim/config/app_cfg.py 查看文件

384
         self.EMAIL_REPLY_IMAP_USE_SSL = asbool(tg.config.get(
384
         self.EMAIL_REPLY_IMAP_USE_SSL = asbool(tg.config.get(
385
             'email.reply.imap.use_ssl',
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
         self.TRACKER_JS_PATH = tg.config.get(
392
         self.TRACKER_JS_PATH = tg.config.get(
389
             'js_tracker_path',
393
             'js_tracker_path',

+ 1 - 0
tracim/tracim/lib/daemons.py 查看文件

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

+ 13 - 4
tracim/tracim/lib/email_fetcher.py 查看文件

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