Debian Codegraph @

src/polyglot/smtplib.py

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 # License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
4
5 import sys
6 from functools import partial
7
8
9 class FilteredLog:
10
11 ' Hide AUTH credentials from the log '
12
13 def __init__(self, debug_to=None):
14 self.debug_to = debug_to or partial(print, file=sys.stderr)
15
16 def __call__(self, *a):
17 if a and len(a) == 2 and a[0] == 'send:':
18 a = list(a)
19 raw = a[1]
20 if len(raw) > 100:
21 raw = raw[:100] + (b'...' if isinstance(raw, bytes) else '...')
22 q = b'AUTH ' if isinstance(raw, bytes) else 'AUTH '
23 if q in raw:
24 raw = 'AUTH <censored>'
25 a[1] = raw
26 self.debug_to(*a)
27
28
29 import smtplib
30
31
32 class SMTP(smtplib.SMTP):
33
34 def __init__(self, *a, **kw):
35 self.debug_to = FilteredLog(kw.pop('debug_to', None))
36 super().__init__(*a, **kw)
37
38 def _print_debug(self, *a):
39 if self.debug_to is not None:
40 self.debug_to(*a)
41 else:
42 super()._print_debug(*a)
43
44
45 class SMTP_SSL(smtplib.SMTP_SSL):
46
47 def __init__(self, *a, **kw):
48 self.debug_to = FilteredLog(kw.pop('debug_to', None))
49 super().__init__(*a, **kw)
50
51 def _print_debug(self, *a):
52 if self.debug_to is not None:
53 self.debug_to(*a)
54 else:
55 super()._print_debug(*a)