| 227 | | class MailHandler(Handler): |
|---|
| 228 | | """Define filter for email address in JID""" |
|---|
| 229 | | |
|---|
| 230 | | def __init__(self): |
|---|
| 231 | | Handler.__init__(self) |
|---|
| 232 | | self.dest_jid_regexp = re.compile(".*%.*") |
|---|
| 233 | | |
|---|
| 234 | | def filter(self, stanza, lang_class): |
|---|
| 235 | | """Return empty array if JID match '.*%.*@componentJID'""" |
|---|
| 236 | | node = stanza.get_to().node |
|---|
| 237 | | if node is not None and self.dest_jid_regexp.match(node): |
|---|
| 238 | | bare_from_jid = unicode(stanza.get_from().bare()) |
|---|
| 239 | | accounts = Account.select(Account.q.user_jid == bare_from_jid) |
|---|
| 240 | | if accounts.count() == 0: |
|---|
| 241 | | raise NoAccountError() |
|---|
| 242 | | else: |
|---|
| 243 | | default_account = accounts.newClause(\ |
|---|
| 244 | | SMTPAccount.q.default_account == True) |
|---|
| 245 | | if default_account.count() > 0: |
|---|
| 246 | | return default_account |
|---|
| 247 | | else: |
|---|
| 248 | | return accounts |
|---|
| 249 | | return None |
|---|
| 250 | | |
|---|
| 251 | | class MailPresenceHandler(DefaultPresenceHandler): |
|---|
| 252 | | """Define filter for legacy JIDs presence handling""" |
|---|
| 253 | | def __init__(self): |
|---|
| 254 | | Handler.__init__(self) |
|---|
| 255 | | self.dest_jid_regexp = re.compile(".*%.*") |
|---|
| 256 | | |
|---|
| 257 | | def filter(self, stanza, lang_class): |
|---|
| 258 | | """Return empty array if JID match '.*%.*@componentJID'""" |
|---|
| 259 | | node = stanza.get_to().node |
|---|
| 260 | | if node is not None and self.dest_jid_regexp.match(node): |
|---|
| 261 | | bare_from_jid = unicode(stanza.get_from().bare()) |
|---|
| 262 | | return [] # Not None |
|---|
| 263 | | return None |
|---|
| 264 | | |
|---|
| 265 | | class SendMailMessageHandler(MailHandler): |
|---|
| 266 | | def __init__(self): |
|---|
| 267 | | MailHandler.__init__(self) |
|---|
| 268 | | self.__logger = logging.getLogger(\ |
|---|
| 269 | | "jmc.jabber.component.SendMailMessageHandler") |
|---|
| 270 | | |
|---|
| 271 | | def send_mail_result(self, message, lang_class, to_email): |
|---|
| 272 | | return [Message(from_jid=message.get_to(), |
|---|
| 273 | | to_jid=message.get_from(), |
|---|
| 274 | | subject=lang_class.send_mail_ok_subject, |
|---|
| 275 | | body=lang_class.send_mail_ok_body % (to_email))] |
|---|
| 276 | | |
|---|
| 277 | | def handle(self, message, lang_class, accounts): |
|---|
| 278 | | to_node = message.get_to().node |
|---|
| 279 | | to_email = to_node.replace('%', '@', 1) |
|---|
| 280 | | accounts[0].send_email(\ |
|---|
| 281 | | accounts[0].create_email(accounts[0].default_from, |
|---|
| 282 | | to_email, |
|---|
| 283 | | message.get_subject(), |
|---|
| 284 | | message.get_body())) |
|---|
| 285 | | return self.send_mail_result(message, lang_class, to_email) |
|---|
| 286 | | |
|---|
| 287 | | class RootSendMailMessageHandler(SendMailMessageHandler): |
|---|
| 288 | | """Handle message sent to root JID""" |
|---|
| 289 | | |
|---|
| 290 | | def __init__(self): |
|---|
| 291 | | SendMailMessageHandler.__init__(self) |
|---|
| 292 | | self.to_regexp = re.compile("^\s*(to|TO|To)\s*:\s*(?P<to_email>.*)") |
|---|
| 293 | | self.__logger = logging.getLogger(\ |
|---|
| 294 | | "jmc.jabber.component.RootSendMailMessageHandler") |
|---|
| 295 | | |
|---|
| 296 | | def filter(self, message, lang_class): |
|---|
| 297 | | name = message.get_to().node |
|---|
| 298 | | bare_from_jid = unicode(message.get_from().bare()) |
|---|
| 299 | | accounts = SMTPAccount.select(\ |
|---|
| 300 | | AND(SMTPAccount.q.default_account == True, |
|---|
| 301 | | SMTPAccount.q.user_jid == bare_from_jid)) |
|---|
| 302 | | if accounts.count() != 1: |
|---|
| 303 | | self.__logger.error("No default account found for user " + |
|---|
| 304 | | str(bare_from_jid)) |
|---|
| 305 | | if accounts.count() == 0: |
|---|
| 306 | | accounts = SMTPAccount.select(\ |
|---|
| 307 | | SMTPAccount.q.user_jid == bare_from_jid) |
|---|
| 308 | | return accounts |
|---|
| 309 | | |
|---|
| 310 | | def handle(self, message, lang_class, accounts): |
|---|
| 311 | | to_email = None |
|---|
| 312 | | lines = message.get_body().split('\n') |
|---|
| 313 | | message_body = [] |
|---|
| 314 | | while to_email is None \ |
|---|
| 315 | | and lines: |
|---|
| 316 | | line = lines.pop(0) |
|---|
| 317 | | match = self.to_regexp.match(line) |
|---|
| 318 | | if match: |
|---|
| 319 | | to_email = match.group("to_email") |
|---|
| 320 | | else: |
|---|
| 321 | | message_body.append(line) |
|---|
| 322 | | message_body.extend(lines) |
|---|
| 323 | | if to_email is not None: |
|---|
| 324 | | accounts[0].send_email(\ |
|---|
| 325 | | accounts[0].create_email(accounts[0].default_from, |
|---|
| 326 | | to_email, |
|---|
| 327 | | message.get_subject(), |
|---|
| 328 | | "\n".join(message_body))) |
|---|
| 329 | | return self.send_mail_result(message, lang_class, to_email) |
|---|
| 330 | | else: |
|---|
| 331 | | return [Message(from_jid=message.get_to(), |
|---|
| 332 | | to_jid=message.get_from(), |
|---|
| 333 | | stanza_type="error", |
|---|
| 334 | | subject=lang_class.send_mail_error_no_to_header_subject, |
|---|
| 335 | | body=lang_class.send_mail_error_no_to_header_body)] |
|---|
| 336 | | |
|---|
| 337 | | class MailSubscribeHandler(DefaultSubscribeHandler, MailHandler): |
|---|
| 338 | | """Use DefaultSubscribeHandler handle method and MailHandler filter. |
|---|
| 339 | | Filter email address in JID. Accept and add to LegacyJID table. |
|---|
| 340 | | """ |
|---|
| 341 | | |
|---|
| 342 | | def __init__(self): |
|---|
| 343 | | DefaultSubscribeHandler.__init__(self) |
|---|
| 344 | | MailHandler.__init__(self) |
|---|
| 345 | | |
|---|
| 346 | | def filter(self, stanza, lang_class): |
|---|
| 347 | | return MailHandler.filter(self, stanza, lang_class) |
|---|
| 348 | | |
|---|
| 349 | | def handle(self, stanza, lang_class, accounts): |
|---|
| 350 | | result = DefaultSubscribeHandler.handle(self, stanza, lang_class, accounts) |
|---|
| 351 | | to_node = stanza.get_to().node |
|---|
| 352 | | to_email = to_node.replace('%', '@', 1) |
|---|
| 353 | | LegacyJID(legacy_address=to_email, |
|---|
| 354 | | jid=unicode(stanza.get_to()), |
|---|
| 355 | | account=accounts[0]) |
|---|
| 356 | | return result |
|---|
| 357 | | |
|---|
| 358 | | class MailUnsubscribeHandler(DefaultUnsubscribeHandler, MailHandler): |
|---|
| 359 | | """Use DefaultUnsubscribeHandler handle method and MailHandler filter. |
|---|
| 360 | | """ |
|---|
| 361 | | |
|---|
| 362 | | def __init__(self): |
|---|
| 363 | | DefaultUnsubscribeHandler.__init__(self) |
|---|
| 364 | | MailHandler.__init__(self) |
|---|
| 365 | | |
|---|
| 366 | | def filter(self, stanza, lang_class): |
|---|
| 367 | | return MailHandler.filter(self, stanza, lang_class) |
|---|
| 368 | | |
|---|
| 369 | | def handle(self, stanza, lang_class, accounts): |
|---|
| 370 | | result = DefaultUnsubscribeHandler.handle(self, stanza, lang_class, accounts) |
|---|
| 371 | | legacy_jid = LegacyJID.select(\ |
|---|
| 372 | | LegacyJID.q.jid == unicode(stanza.get_to())) |
|---|
| 373 | | if legacy_jid.count() == 1: |
|---|
| 374 | | legacy_jid[0].destroySelf() |
|---|
| 375 | | return result |
|---|
| 376 | | |
|---|