Mercurial > libervia-backend
comparison sat/plugins/plugin_misc_smtp.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | ab2696e34d29 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 | |
23 log = getLogger(__name__) | 24 log = getLogger(__name__) |
24 from twisted.internet import defer | 25 from twisted.internet import defer |
25 from twisted.cred import portal, checkers, credentials | 26 from twisted.cred import portal, checkers, credentials |
26 from twisted.cred import error as cred_error | 27 from twisted.cred import error as cred_error |
27 from twisted.mail import smtp | 28 from twisted.mail import smtp |
40 C.PI_TYPE: "Misc", | 41 C.PI_TYPE: "Misc", |
41 C.PI_PROTOCOLS: [], | 42 C.PI_PROTOCOLS: [], |
42 C.PI_DEPENDENCIES: ["Maildir"], | 43 C.PI_DEPENDENCIES: ["Maildir"], |
43 C.PI_MAIN: "SMTP_server", | 44 C.PI_MAIN: "SMTP_server", |
44 C.PI_HANDLER: "no", | 45 C.PI_HANDLER: "no", |
45 C.PI_DESCRIPTION: _("""Create a SMTP server that you can use to send your "normal" type messages""") | 46 C.PI_DESCRIPTION: _( |
47 """Create a SMTP server that you can use to send your "normal" type messages""" | |
48 ), | |
46 } | 49 } |
47 | 50 |
48 | 51 |
49 class SMTP_server(object): | 52 class SMTP_server(object): |
50 | 53 |
60 | 63 |
61 def __init__(self, host): | 64 def __init__(self, host): |
62 log.info(_("Plugin SMTP Server initialization")) | 65 log.info(_("Plugin SMTP Server initialization")) |
63 self.host = host | 66 self.host = host |
64 | 67 |
65 #parameters | 68 # parameters |
66 host.memory.updateParams(self.params) | 69 host.memory.updateParams(self.params) |
67 | 70 |
68 port = int(self.host.memory.getParamA("SMTP Port", "Mail Server")) | 71 port = int(self.host.memory.getParamA("SMTP Port", "Mail Server")) |
69 log.info(_("Launching SMTP server on port %d") % port) | 72 log.info(_("Launching SMTP server on port %d") % port) |
70 | 73 |
86 | 89 |
87 def eomReceived(self): | 90 def eomReceived(self): |
88 """handle end of message""" | 91 """handle end of message""" |
89 mail = Parser().parsestr("\n".join(self.message)) | 92 mail = Parser().parsestr("\n".join(self.message)) |
90 try: | 93 try: |
91 self.host._sendMessage(parseaddr(mail['to'].decode('utf-8', 'replace'))[1], mail.get_payload().decode('utf-8', 'replace'), # TODO: manage other charsets | 94 self.host._sendMessage( |
92 subject=mail['subject'].decode('utf-8', 'replace'), mess_type='normal', profile_key=self.profile) | 95 parseaddr(mail["to"].decode("utf-8", "replace"))[1], |
96 mail.get_payload().decode( | |
97 "utf-8", "replace" | |
98 ), # TODO: manage other charsets | |
99 subject=mail["subject"].decode("utf-8", "replace"), | |
100 mess_type="normal", | |
101 profile_key=self.profile, | |
102 ) | |
93 except: | 103 except: |
94 exc_type, exc_value, exc_traceback = sys.exc_info() | 104 exc_type, exc_value, exc_traceback = sys.exc_info() |
95 log.error(_(u"Can't send message: %s") % exc_value) # The email is invalid or incorreclty parsed | 105 log.error( |
106 _(u"Can't send message: %s") % exc_value | |
107 ) # The email is invalid or incorreclty parsed | |
96 return defer.fail() | 108 return defer.fail() |
97 self.message = None | 109 self.message = None |
98 return defer.succeed(None) | 110 return defer.succeed(None) |
99 | 111 |
100 def connectionLost(self): | 112 def connectionLost(self): |
149 | 161 |
150 def __init__(self, host): | 162 def __init__(self, host): |
151 self.host = host | 163 self.host = host |
152 | 164 |
153 def requestAvatar(self, avatarID, mind, *interfaces): | 165 def requestAvatar(self, avatarID, mind, *interfaces): |
154 log.debug('requestAvatar') | 166 log.debug("requestAvatar") |
155 profile = avatarID.decode('utf-8') | 167 profile = avatarID.decode("utf-8") |
156 if smtp.IMessageDelivery not in interfaces: | 168 if smtp.IMessageDelivery not in interfaces: |
157 raise NotImplementedError | 169 raise NotImplementedError |
158 return smtp.IMessageDelivery, SatSmtpDelivery(self.host, profile), lambda: None | 170 return smtp.IMessageDelivery, SatSmtpDelivery(self.host, profile), lambda: None |
159 | 171 |
160 | 172 |
162 """ | 174 """ |
163 This credential checker check against SàT's profile and associated jabber's password | 175 This credential checker check against SàT's profile and associated jabber's password |
164 Check if the profile exists, and if the password is OK | 176 Check if the profile exists, and if the password is OK |
165 Return the profile as avatarId | 177 Return the profile as avatarId |
166 """ | 178 """ |
179 | |
167 implements(checkers.ICredentialsChecker) | 180 implements(checkers.ICredentialsChecker) |
168 credentialInterfaces = (credentials.IUsernamePassword, | 181 credentialInterfaces = ( |
169 credentials.IUsernameHashedPassword) | 182 credentials.IUsernamePassword, |
183 credentials.IUsernameHashedPassword, | |
184 ) | |
170 | 185 |
171 def __init__(self, host): | 186 def __init__(self, host): |
172 self.host = host | 187 self.host = host |
173 | 188 |
174 def _cbPasswordMatch(self, matched, profile): | 189 def _cbPasswordMatch(self, matched, profile): |
175 if matched: | 190 if matched: |
176 return profile.encode('utf-8') | 191 return profile.encode("utf-8") |
177 else: | 192 else: |
178 return failure.Failure(cred_error.UnauthorizedLogin()) | 193 return failure.Failure(cred_error.UnauthorizedLogin()) |
179 | 194 |
180 def requestAvatarId(self, credentials): | 195 def requestAvatarId(self, credentials): |
181 profiles = self.host.memory.getProfilesList() | 196 profiles = self.host.memory.getProfilesList() |
182 if not credentials.username in profiles: | 197 if not credentials.username in profiles: |
183 return defer.fail(cred_error.UnauthorizedLogin()) | 198 return defer.fail(cred_error.UnauthorizedLogin()) |
184 d = self.host.memory.asyncGetParamA("Password", "Connection", profile_key=credentials.username) | 199 d = self.host.memory.asyncGetParamA( |
200 "Password", "Connection", profile_key=credentials.username | |
201 ) | |
185 d.addCallback(credentials.checkPassword) | 202 d.addCallback(credentials.checkPassword) |
186 d.addCallback(self._cbPasswordMatch, credentials.username) | 203 d.addCallback(self._cbPasswordMatch, credentials.username) |
187 return d | 204 return d |
188 | 205 |
189 | 206 |
190 class SmtpServerFactory(smtp.SMTPFactory): | 207 class SmtpServerFactory(smtp.SMTPFactory): |
191 | |
192 def __init__(self, host): | 208 def __init__(self, host): |
193 self.protocol = smtp.ESMTP | 209 self.protocol = smtp.ESMTP |
194 self.host = host | 210 self.host = host |
195 _portal = portal.Portal(SmtpRealm(self.host)) | 211 _portal = portal.Portal(SmtpRealm(self.host)) |
196 _portal.registerChecker(SatProfileCredentialChecker(self.host)) | 212 _portal.registerChecker(SatProfileCredentialChecker(self.host)) |