2013-06-28 21:10:28 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2015, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
2013-08-01 23:45:51 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2013-08-01 23:45:51 +00:00
|
|
|
This file is part of transWhat
|
|
|
|
|
|
|
|
transWhat is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
transwhat is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2013-06-28 21:10:28 +00:00
|
|
|
import argparse
|
2015-09-03 18:04:29 +00:00
|
|
|
import traceback
|
2013-06-28 21:10:28 +00:00
|
|
|
import logging
|
|
|
|
import asyncore
|
|
|
|
import sys, os
|
|
|
|
import e4u
|
2015-09-02 15:04:49 +00:00
|
|
|
import Queue
|
2016-01-07 15:56:55 +00:00
|
|
|
import threadutils
|
2013-06-28 21:10:28 +00:00
|
|
|
|
|
|
|
sys.path.insert(0, os.getcwd())
|
|
|
|
|
|
|
|
from Spectrum2.iochannel import IOChannel
|
|
|
|
|
|
|
|
from whatsappbackend import WhatsAppBackend
|
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
|
|
|
|
|
|
|
# Arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
2015-12-31 19:43:10 +00:00
|
|
|
parser.add_argument('--debug', action='store_true')
|
|
|
|
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)
|
|
|
|
parser.add_argument('-j', type=str, 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
|
|
|
|
loggingfile = '/var/log/spectrum2/' + args.j + '/backends/backend.log'
|
2013-06-28 21:10:28 +00:00
|
|
|
# Logging
|
|
|
|
logging.basicConfig( \
|
2015-09-03 18:04:29 +00:00
|
|
|
filename=loggingfile,\
|
2015-12-31 19:43:10 +00:00
|
|
|
format = "%(asctime)-15s %(levelname)s %(name)s: %(message)s", \
|
2013-06-28 21:10:28 +00:00
|
|
|
level = logging.DEBUG if args.debug else logging.INFO \
|
|
|
|
)
|
|
|
|
|
|
|
|
# Handler
|
|
|
|
def handleTransportData(data):
|
2016-01-02 20:40:11 +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
|
|
|
|
|
|
|
e4u.load()
|
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
closed = False
|
|
|
|
def connectionClosed():
|
|
|
|
global closed
|
|
|
|
closed = True
|
|
|
|
|
2013-06-28 21:10:28 +00:00
|
|
|
# Main
|
2015-09-03 18:04:29 +00:00
|
|
|
io = IOChannel(args.host, args.port, handleTransportData, connectionClosed)
|
2013-06-28 21:10:28 +00:00
|
|
|
|
2015-12-02 21:06:56 +00:00
|
|
|
plugin = WhatsAppBackend(io, args.j)
|
2013-06-28 21:10:28 +00:00
|
|
|
|
2015-12-18 18:33:32 +00:00
|
|
|
plugin.handleBackendConfig({
|
2015-12-31 19:43:10 +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
|
|
|
|
2016-01-07 15:56:55 +00:00
|
|
|
|
2015-09-02 15:04:49 +00:00
|
|
|
while True:
|
2015-09-03 18:04:29 +00:00
|
|
|
try:
|
|
|
|
asyncore.loop(timeout=1.0, count=10, use_poll = True)
|
|
|
|
try:
|
2015-12-31 19:43:10 +00:00
|
|
|
callback = YowStack._YowStack__detachedQueue.get(False) #doesn't block
|
2015-09-03 18:04:29 +00:00
|
|
|
callback()
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
if closed:
|
|
|
|
break
|
2016-01-07 15:56:55 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
callback = threadutils.eventQueue.get_nowait()
|
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
callback()
|
2015-09-03 18:04:29 +00:00
|
|
|
except SystemExit:
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
logger = logging.getLogger('transwhat')
|
|
|
|
logger.error(traceback.format_exc())
|