~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/browser/stringformatter.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-07-05 14:31:38 UTC
  • mfrom: (13316.10.21 bug-740208-obfuscate-ws)
  • Revision ID: launchpad@pqm.canonical.com-20110705143138-yd0hdrvbeslquzfm
[r=deryck][bug=740208] obfuscated email addresses for anonymous
 webservice requests

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
from canonical.launchpad.webapp.interfaces import ILaunchBag
35
35
from lp.answers.interfaces.faq import IFAQSet
36
36
from lp.registry.interfaces.person import IPersonSet
 
37
from lp.services.utils import (
 
38
    re_email_address,
 
39
    obfuscate_email,
 
40
    )
37
41
 
38
42
 
39
43
def escape(text, quote=True):
237
241
 
238
242
def extract_email_addresses(text):
239
243
    '''Unique email addresses in the text.'''
240
 
    matches = re.finditer(FormattersAPI._re_email, text)
 
244
    matches = re.finditer(re_email_address, text)
241
245
    return list(set([match.group() for match in matches]))
242
246
 
243
247
 
361
365
            # devaluing the return on effort for spammers that consider
362
366
            # using Launchpad.
363
367
            if not FormattersAPI._linkify_url_should_be_ignored(url):
364
 
                link_string = ('<a rel="nofollow" '
365
 
                               'href="%(url)s">%(linked_text)s</a>%(trailers)s' % {
366
 
                                    'url': cgi.escape(url, quote=True),
367
 
                                    'linked_text': add_word_breaks(cgi.escape(url)),
368
 
                                    'trailers': cgi.escape(trailers)
369
 
                                    })
 
368
                link_string = (
 
369
                    '<a rel="nofollow" '
 
370
                    'href="%(url)s">%(linked_text)s</a>%(trailers)s' % {
 
371
                        'url': cgi.escape(url, quote=True),
 
372
                        'linked_text': add_word_breaks(cgi.escape(url)),
 
373
                        'trailers': cgi.escape(trailers)
 
374
                        })
370
375
                return link_string
371
376
            else:
372
377
                return full_url
776
781
            output.append(line)
777
782
        return '\n'.join(output)
778
783
 
779
 
    # This is a regular expression that matches email address embedded in
780
 
    # text. It is not RFC 2821 compliant, nor does it need to be. This
781
 
    # expression strives to identify probable email addresses so that they
782
 
    # can be obfuscated when viewed by unauthenticated users. See
783
 
    # http://www.email-unlimited.com/stuff/email_address_validator.htm
784
 
 
785
 
    # localnames do not have [&?%!@<>,;:`|{}()#*^~ ] in practice
786
 
    # (regardless of RFC 2821) because they conflict with other systems.
787
 
    # See https://lists.ubuntu.com
788
 
    #     /mailman/private/launchpad-reviews/2007-June/006081.html
789
 
 
790
 
    # This verson of the re is more than 5x faster that the orginal
791
 
    # version used in ftest/test_tales.testObfuscateEmail.
792
 
    _re_email = re.compile(r"""
793
 
        \b[a-zA-Z0-9._/="'+-]{1,64}@  # The localname.
794
 
        [a-zA-Z][a-zA-Z0-9-]{1,63}    # The hostname.
795
 
        \.[a-zA-Z0-9.-]{1,251}\b      # Dot starts one or more domains.
796
 
        """, re.VERBOSE)              # ' <- font-lock turd
797
 
 
798
784
    def obfuscate_email(self):
799
785
        """Obfuscate an email address if there's no authenticated user.
800
786
 
813
799
        """
814
800
        if getUtility(ILaunchBag).user is not None:
815
801
            return self._stringtoformat
816
 
        text = self._re_email.sub(
817
 
            r'<email address hidden>', self._stringtoformat)
818
 
        text = text.replace(
819
 
            "<<email address hidden>>", "<email address hidden>")
820
 
        return text
 
802
        return obfuscate_email(self._stringtoformat)
821
803
 
822
804
    def linkify_email(self, preloaded_person_data=None):
823
805
        """Linkify any email address recognised in Launchpad.
831
813
        """
832
814
        text = self._stringtoformat
833
815
 
834
 
        matches = re.finditer(self._re_email, text)
 
816
        matches = re.finditer(re_email_address, text)
835
817
        for match in matches:
836
818
            address = match.group()
837
819
            person = None