-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp2xmpp.py
executable file
·201 lines (167 loc) · 7.38 KB
/
smtp2xmpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import logging
import time
from optparse import OptionParser
import asyncio
import aiosmtpd
from aiosmtpd.controller import UnthreadedController
import socket
import email
import email.policy
import slixmpp
from slixmpp.componentxmpp import ComponentXMPP
from slixmpp.stanza.roster import Roster
from slixmpp.xmlstream import ElementBase
from slixmpp.xmlstream.stanzabase import ET, register_stanza_plugin
def create_server(self):
"""
Creates a 'server task' that listens on a Unix Socket file.
Does NOT actually start the protocol object itself;
_factory_invoker() is only called upon fist connection attempt.
"""
try:
sock=socket.fromfd(3, socket.AF_INET, socket.SOCK_STREAM)
return self.loop.create_server(
self._factory_invoker,
sock=sock,
ssl=self.ssl_context,
)
except:
return self.loop.create_server(
self._factory_invoker,
host=self.hostname,
port=self.port,
ssl=self.ssl_context,
)
aiosmtpd.controller.InetMixin._create_server = create_server
class MailHandler:
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
#if not address.endswith('@localhost'):
# return '550 not relaying to that domain'
envelope.rcpt_tos.append(address)
return '250 OK'
async def handle_DATA(self, server, session, envelope):
fromjid = envelope.mail_from.replace('localhost',xmpp.boundjid.bare).replace('127.0.0.1',xmpp.boundjid.bare)
body = email.message_from_bytes(envelope.content, policy=email.policy.default).get_body(preferencelist=('plain', 'html')).get_content()
logging.log(5,'Message from %s' % envelope.mail_from)
logging.log(5,'Message for %s' % envelope.rcpt_tos)
logging.log(5,'Message data:\n')
for ln in body.splitlines():
logging.log(5,f'> {ln}'.strip())
logging.log(5,'')
logging.log(5,'End of message')
if config['rosteronly']:
for jid in xmpp.roster:
xmpp.send_presence(pfrom=fromjid, pto=jid, pshow='xa')
xmpp.send_message(mfrom=fromjid, mto=jid, mbody=body)
else:
for rcpt in envelope.rcpt_tos:
rcpt = rcpt.replace('localhost',config['host']).replace('127.0.0.1',config['host'])
xmpp.send_presence(pfrom=fromjid, pto=rcpt, pshow='xa')
xmpp.send_message(mfrom=fromjid, mto=rcpt, mbody=body)
return '250 Message accepted for delivery'
class Config(ElementBase):
"""
In order to make loading and manipulating an XML config
file easier, we will create a custom stanza object for
our config XML file contents. See the documentation
on stanza objects for more information on how to create
and use stanza objects and stanza plugins.
We will reuse the IQ roster query stanza to store roster
information since it already exists.
"""
name = "config"
namespace = "slixmpp:config"
interfaces = {'component', 'host', 'secret', 'server', 'port', 'rosteronly'}
sub_interfaces = {'component', 'host', 'secret', 'server', 'port'}
bool_interfaces = {'rosteronly'}
register_stanza_plugin(Config, Roster)
class ConfigComponent(ComponentXMPP):
def __init__(self, config):
"""
Create a ConfigComponent.
Arguments:
config -- The XML contents of the config file.
config_file -- The XML config file object itself.
"""
ComponentXMPP.__init__(self, "{}.{}".format(config['component'],config['host']),
config['secret'],
config['server'],
config['port'])
# Store the roster information.
self.roster = config['roster']['items']
# The session_start event will be triggered when
# the component establishes its connection with the
# server and the XML streams are ready for use. We
# want to listen for this event so that we we can
# broadcast any needed initial presence stanzas.
self.add_event_handler("session_start", self.start)
# The message event is triggered whenever a message
# stanza is received. Be aware that that includes
# MUC messages and error messages.
#self.add_event_handler("message", self.message)
def start(self, event):
"""
Process the session_start event.
The typical action for the session_start event in a component
is to broadcast presence stanzas to all subscribers to the
component. Note that the component does not have a roster
provided by the XMPP server. In this case, we have possibly
saved a roster in the component's configuration file.
Since the component may use any number of JIDs, you should
also include the JID that is sending the presence.
Arguments:
event -- An empty dictionary. The session_start
event does not provide any additional
data.
"""
for jid in self.roster:
self.send_presence(pfrom=self.jid, pto=jid)
if __name__ == '__main__':
# Setup the command line arguments.
optp = OptionParser()
# Output verbosity options.
optp.add_option('-q', '--quiet', help='set logging to ERROR',
action='store_const', dest='loglevel',
const=logging.ERROR, default=logging.INFO)
optp.add_option('-d', '--debug', help='set logging to DEBUG',
action='store_const', dest='loglevel',
const=logging.DEBUG, default=logging.INFO)
optp.add_option('-v', '--verbose', help='set logging to COMM',
action='store_const', dest='loglevel',
const=5, default=logging.INFO)
# Component name and secret options.
optp.add_option("-c", "--config", help="path to config file",
dest="config", default="/etc/smtp2xmpp/config.xml")
opts, args = optp.parse_args()
# Setup logging.
logging.basicConfig(level=opts.loglevel,
format='%(levelname)-8s %(message)s')
# Load configuration data.
config_file = open(opts.config, 'r')
config_data = "\n".join([line for line in config_file])
config = Config(xml=ET.fromstring(config_data))
config_file.close()
# Setup the ConfigComponent and register plugins. Note that while plugins
# may have interdependencies, the order in which you register them does
# not matter.
xmpp = ConfigComponent(config)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data Forms
xmpp.register_plugin('xep_0060') # PubSub
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
try:
xmpp.connect()
#xmpp.process()
controller = UnthreadedController(MailHandler(), port=25, hostname="localhost", server_hostname="localhost", loop=xmpp.loop)
controller.begin()
#tasks = [asyncio.sleep(10)]
tasks = [xmpp.disconnected]
xmpp.loop.run_until_complete(asyncio.wait(tasks))
#xmpp.loop.run_forever()
except Exception as exception:
logging.exception(exception)
print("Done")