浏览代码

Add option to disable parsing for txt mail

Guénaël Muller 7 年前
父节点
当前提交
ac48a15e2c
共有 4 个文件被更改,包括 17 次插入12 次删除
  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. 11 12
      tracim/tracim/lib/email_fetcher.py

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

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
 email.reply.use_html_parsing = true
230
+email.reply.use_txt_parsing = true
230
 
231
 
231
 ## Radical (CalDav server) configuration
232
 ## Radical (CalDav server) configuration
232
 # radicale.server.host = 0.0.0.0
233
 # radicale.server.host = 0.0.0.0

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

388
             'email.reply.use_html_parsing',
388
             'email.reply.use_html_parsing',
389
             True,
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
         self.TRACKER_JS_PATH = tg.config.get(
396
         self.TRACKER_JS_PATH = tg.config.get(
393
             'js_tracker_path',
397
             'js_tracker_path',

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

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
             use_html_parsing=cfg.EMAIL_REPLY_USE_HTML_PARSING,
181
+            use_txt_parsing=cfg.EMAIL_REPLY_USE_TXT_PARSING,
181
         )
182
         )
182
         self._fetcher.run()
183
         self._fetcher.run()
183
 
184
 

+ 11 - 12
tracim/tracim/lib/email_fetcher.py 查看文件

58
 
58
 
59
     def get_body(
59
     def get_body(
60
             self,
60
             self,
61
-            use_html_parsing=True
61
+            use_html_parsing=True,
62
+            use_txt_parsing=True,
62
     ) -> typing.Optional[str]:
63
     ) -> typing.Optional[str]:
63
         body_part = self._get_mime_body_message()
64
         body_part = self._get_mime_body_message()
64
         body = None
65
         body = None
68
             if content_type == CONTENT_TYPE_TEXT_PLAIN:
69
             if content_type == CONTENT_TYPE_TEXT_PLAIN:
69
                 txt_body = body_part.get_payload(decode=True).decode(
70
                 txt_body = body_part.get_payload(decode=True).decode(
70
                     charset)
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
             elif content_type == CONTENT_TYPE_TEXT_HTML:
77
             elif content_type == CONTENT_TYPE_TEXT_HTML:
74
                 html_body = body_part.get_payload(decode=True).decode(
78
                 html_body = body_part.get_payload(decode=True).decode(
80
         return body
84
         return body
81
 
85
 
82
     @classmethod
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
     def _sanitize_html_body(cls, html_body: str) -> str:
87
     def _sanitize_html_body(cls, html_body: str) -> str:
92
         soup = BeautifulSoup(html_body, 'html.parser')
88
         soup = BeautifulSoup(html_body, 'html.parser')
93
         config = BEAUTIFULSOUP_HTML_BODY_SANITIZE_CONFIG
89
         config = BEAUTIFULSOUP_HTML_BODY_SANITIZE_CONFIG
187
         delay: int,
183
         delay: int,
188
         endpoint: str,
184
         endpoint: str,
189
         token: str,
185
         token: str,
190
-        use_html_parsing: bool
186
+        use_html_parsing: bool,
187
+        use_txt_parsing: bool,
191
     ) -> None:
188
     ) -> None:
192
         """
189
         """
193
         Fetch mail from a mailbox folder through IMAP and add their content to
190
         Fetch mail from a mailbox folder through IMAP and add their content to
215
         self.endpoint = endpoint
212
         self.endpoint = endpoint
216
         self.token = token
213
         self.token = token
217
         self.use_html_parsing = use_html_parsing
214
         self.use_html_parsing = use_html_parsing
215
+        self.use_txt_parsing = use_txt_parsing
218
 
216
 
219
         self._is_active = True
217
         self._is_active = True
220
 
218
 
320
                    'content_id': mail.get_key(),
318
                    'content_id': mail.get_key(),
321
                    'payload': {
319
                    'payload': {
322
                        'content': mail.get_body(
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
             try:
324
             try:
326
                 r = requests.post(self.endpoint, json=msg)
325
                 r = requests.post(self.endpoint, json=msg)