Commits

Christian Boos  committed 1ea0c07

0.11.6: add a configuration setting for never obfuscating `mailto:` links.

i.e. when we have:
{{{
[trac]
never_obfuscate_mailto = true
}}}
the `mailto:user@example.org` wiki text will always be converted to a visible e-mail address, regardless of any other setting (`[trac] show_email_addresses` or `EMAIL_VIEW` permission).

Of course, this setting defaults to `false`, leaving a site having the default configuration safe w.r.t. #153.

Last minute feature needed for TracHacks ;-)

Closes #8650.

  • Participants
  • Parent commits dc40ca1
  • Branches 0.11-stable

Comments (0)

Files changed (3)

File trac/tests/wikisyntax.py

 import unittest
 
 from trac.attachment import Attachment
+from trac.mimeview.api import Context
+from trac.resource import Resource
 from trac.search.web_ui import SearchModule
+from trac.test import MockPerm
+from trac.web.href import Href
 from trac.wiki.tests import formatter
 
 SEARCH_TEST_CASES="""
     shutil.rmtree(tc.env.path)
     tc.env.reset_db()
 
+
+EMAIL_TEST_CASE_DEFAULT = u"""
+============================== mailto: obfuscated by default, like plain email
+user@example.org vs. mailto:user@example.org
+and [mailto:user@example.org Joe User]
+------------------------------
+<p>
+user@\u2026 vs. mailto:user@\u2026
+and Joe User
+</p>
+------------------------------
+"""
+
+def email_default_context():
+    class NoEmailViewPerm(MockPerm):
+        def has_permission(self, action, realm_or_resource=None, id=False,
+                           version=False):
+            return action != 'EMAIL_VIEW'
+        __contains__ = has_permission
+
+    context = Context(Resource('wiki', 'WikiStart'), href=Href('/'), 
+                      perm=NoEmailViewPerm())
+    context.req = None # 0.12 FIXME .req shouldn't be required by formatter
+    return context
+
+
+EMAIL_TEST_CASE_NEVER_OBFUSCATE = u"""
+============================== mailto: not obfuscated, unlike plain email
+user@example.org vs. mailto:user@example.org
+and [mailto:user@example.org Joe User]
+------------------------------
+<p>
+user@\u2026 vs. <a class="mail-link" href="mailto:user@example.org"><span class="icon">\xa0</span>mailto:user@example.org</a>
+and <a class="mail-link" href="mailto:user@example.org"><span class="icon">\xa0</span>Joe User</a>
+</p>
+------------------------------
+"""
+
+def email_never_obfuscate_setup(tc):
+    tc.env.config.set('trac', 'never_obfuscate_mailto', True)
+
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(formatter.suite(SEARCH_TEST_CASES, file=__file__))
                                   context=('wiki', 'WikiStart'),
                                   setup=attachment_setup,
                                   teardown=attachment_teardown))
+    suite.addTest(formatter.suite(EMAIL_TEST_CASE_DEFAULT, file=__file__, 
+                                  context=email_default_context()))
+    suite.addTest(formatter.suite(EMAIL_TEST_CASE_NEVER_OBFUSCATE, 
+                                  file=__file__,
+                                  context=email_default_context(),
+                                  setup=email_never_obfuscate_setup))
     return suite
 
 if __name__ == '__main__':

File trac/web/chrome.py

         """Show email addresses instead of usernames. If false, we obfuscate
         email addresses (''since 0.11'').""")
 
+    never_obfuscate_mailto = BoolOption('trac', 'never_obfuscate_mailto', 
+        'false',
+        """Never obfuscate `mailto:` links explicitly written in the wiki, 
+        even if `show_email_addresses` is false or the user has not the 
+        EMAIL_VIEW permission (''since 0.11.6'').""")
+
     show_ip_addresses = BoolOption('trac', 'show_ip_addresses', 'false',
         """Show IP addresses for resource edits (e.g. wiki).
         (''since 0.11.3'').""")

File trac/wiki/formatter.py

             return self._make_ext_link(ns+':'+target, label)
         elif ns == "mailto":
             from trac.web.chrome import Chrome
-            otarget = Chrome(self.env).format_emails(self.context, target)
-            olabel = Chrome(self.env).format_emails(self.context, label)
+            chrome = Chrome(self.env)
+            if chrome.never_obfuscate_mailto:
+                otarget, olabel = target, label
+            else:
+                otarget = chrome.format_emails(self.context, target)
+                olabel = chrome.format_emails(self.context, label)
             if (otarget, olabel) == (target, label):
                 return self._make_mail_link('mailto:'+target, label)
             else: