2013-06-28 21:10:28 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import argparse
|
2015-09-03 18:04:29 +00:00
|
|
|
import traceback
|
2013-06-28 21:10:28 +00:00
|
|
|
import logging
|
|
|
|
import asyncore
|
2019-04-25 20:41:45 +00:00
|
|
|
import sys
|
2013-06-28 21:10:28 +00:00
|
|
|
|
2019-04-25 21:49:11 +00:00
|
|
|
import Spectrum2
|
2015-06-21 12:01:05 +00:00
|
|
|
from yowsup.common import YowConstants
|
2015-09-02 15:04:49 +00:00
|
|
|
from yowsup.stacks import YowStack
|
2013-06-28 21:10:28 +00:00
|
|
|
|
2019-04-25 20:41:45 +00:00
|
|
|
from .whatsappbackend import WhatsAppBackend
|
|
|
|
from . import threadutils
|
|
|
|
|
2013-06-28 21:10:28 +00:00
|
|
|
# Arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
2015-12-31 19:43:10 +00:00
|
|
|
parser.add_argument('--debug', action='store_true')
|
2016-04-10 01:01:22 +00:00
|
|
|
parser.add_argument('--log', type=str)
|
2015-12-31 19:43:10 +00:00
|
|
|
parser.add_argument('--host', type=str, required=True)
|
|
|
|
parser.add_argument('--port', type=int, required=True)
|
|
|
|
parser.add_argument('--service.backend_id', metavar="ID", type=int, required=True)
|
|
|
|
parser.add_argument('config', type=str)
|
2016-04-09 22:21:58 +00:00
|
|
|
parser.add_argument('-j', type=str, metavar="JID", required=True)
|
2013-06-28 21:10:28 +00:00
|
|
|
|
|
|
|
args, unknown = parser.parse_known_args()
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
YowConstants.PATH_STORAGE='/var/lib/spectrum2/' + args.j
|
2016-04-10 01:01:22 +00:00
|
|
|
|
|
|
|
if args.log is None:
|
2017-09-09 18:34:23 +00:00
|
|
|
args.log = '/var/log/spectrum2/' + args.j + '/backends/backend.log'
|
2016-04-10 01:01:22 +00:00
|
|
|
|
2013-06-28 21:10:28 +00:00
|
|
|
# Logging
|
2016-04-10 01:01:22 +00:00
|
|
|
logging.basicConfig(
|
2017-09-09 18:34:23 +00:00
|
|
|
filename = args.log,
|
|
|
|
format = "%(asctime)-15s %(levelname)s %(name)s: %(message)s",
|
|
|
|
level = logging.DEBUG if args.debug else logging.INFO
|
2013-06-28 21:10:28 +00:00
|
|
|
)
|
|
|
|
|
2016-04-12 16:21:04 +00:00
|
|
|
if args.config is not None:
|
2019-04-25 21:49:11 +00:00
|
|
|
specConf = Spectrum2.Config(args.config)
|
2016-04-12 16:21:04 +00:00
|
|
|
else:
|
2017-09-09 18:34:23 +00:00
|
|
|
specConf = None
|
2016-04-12 16:21:04 +00:00
|
|
|
|
2013-06-28 21:10:28 +00:00
|
|
|
# Handler
|
|
|
|
def handleTransportData(data):
|
2017-09-09 18:34:23 +00:00
|
|
|
try:
|
|
|
|
plugin.handleDataRead(data)
|
|
|
|
except SystemExit as e:
|
|
|
|
raise e
|
|
|
|
except:
|
|
|
|
logger = logging.getLogger('transwhat')
|
|
|
|
logger.error(traceback.format_exc())
|
2013-06-28 21:10:28 +00:00
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
closed = False
|
|
|
|
def connectionClosed():
|
2017-09-09 18:34:23 +00:00
|
|
|
global closed
|
|
|
|
closed = True
|
2015-09-03 18:04:29 +00:00
|
|
|
|
2013-06-28 21:10:28 +00:00
|
|
|
# Main
|
2019-04-25 21:49:11 +00:00
|
|
|
io = Spectrum2.IOChannel(args.host, args.port, handleTransportData, connectionClosed)
|
2013-06-28 21:10:28 +00:00
|
|
|
|
2016-04-12 16:21:04 +00:00
|
|
|
plugin = WhatsAppBackend(io, args.j, specConf)
|
2013-06-28 21:10:28 +00:00
|
|
|
|
2015-12-18 18:33:32 +00:00
|
|
|
plugin.handleBackendConfig({
|
2017-09-09 18:34:23 +00:00
|
|
|
'features': [
|
|
|
|
('send_buddies_on_login', 1),
|
|
|
|
('muc', 'true'),
|
|
|
|
],
|
2015-12-18 18:33:32 +00:00
|
|
|
})
|
2015-12-01 21:21:38 +00:00
|
|
|
|
2017-09-09 18:34:23 +00:00
|
|
|
def main():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
asyncore.loop(timeout=1.0, count=10, use_poll = True)
|
|
|
|
try:
|
|
|
|
callback = YowStack._YowStack__detachedQueue.get(False) #doesn't block
|
|
|
|
callback()
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
if closed:
|
|
|
|
break
|
|
|
|
while True:
|
|
|
|
try:
|
2017-10-29 19:31:15 +00:00
|
|
|
callback = transWhat.threadutils.eventQueue.get_nowait()
|
2017-09-09 18:34:23 +00:00
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
callback()
|
|
|
|
except SystemExit:
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
logger = logging.getLogger('transwhat')
|
|
|
|
logger.error(traceback.format_exc())
|