This commit is contained in:
emdee 2022-10-29 18:44:37 +00:00
parent 354f4eceb2
commit 04985b1fb2
2 changed files with 36 additions and 16 deletions

View File

@ -6,7 +6,7 @@ A bot that sync messages between IRC and Tox NGC group chat.
Hard forked from <https://github.com/aitjcize/tox-irc-sync> Hard forked from <https://github.com/aitjcize/tox-irc-sync>
and changed to use the Python wrapping from and changed to use the Python wrapping from
<https://git.macaw.me/emdee/toxygen_wrapper>. <https://git.plastiras.org/emdee/toxygen_wrapper>.
Just clone that repo and put the resulting directory on your Just clone that repo and put the resulting directory on your
```PYTHONPATH```. ```PYTHONPATH```.
@ -144,13 +144,13 @@ python3 tox-irc-sync.py \
## ChangeLog ## ChangeLog
* changed to use the Python wrapping from <https://git.macaw.me/emdee/toxygen_wrapper> * changed to use the Python wrapping from <https://git.plastiras.org/emdee/toxygen_wrapper>
* ```tox_irc_sync``` does SSL now. * ```tox_irc_sync``` does SSL now.
### Future Directions ### Future Directions
1. It's intended as a IRC->Tox NGC gateway but it could work the other way round. 1. It's intended as a IRC->Tox NGC gateway but it could work the other way round.
2. It could be a plugin under <https://git.macaw.me/emdee/toxygen> 2. It could be a plugin under <https://git.plastiras.org/emdee/toxygen>
which would broaden the range of callbacks that could be supported. which would broaden the range of callbacks that could be supported.
3. It could be a gateway to an existing NGC group with an invite bot. 3. It could be a gateway to an existing NGC group with an invite bot.

View File

@ -14,6 +14,9 @@ from threading import Thread
from random import shuffle from random import shuffle
from OpenSSL import SSL from OpenSSL import SSL
import warnings
warnings.filterwarnings('ignore')
import wrapper import wrapper
from wrapper.tox import Tox from wrapper.tox import Tox
from wrapper.toxav import ToxAV from wrapper.toxav import ToxAV
@ -37,7 +40,9 @@ LOG = logging.getLogger('app.'+'ts')
NAME = 'SyniTox' NAME = 'SyniTox'
# possible CA locations picks the first one # possible CA locations picks the first one
lCAs = ['/etc/ssl/cacert.pem'] lCAs = ['/etc/ssl/cacert.pem',
# debian
'/etc/ssl/certs/']
bot_toxname = 'SyniTox' bot_toxname = 'SyniTox'
@ -368,6 +373,7 @@ class SyniTox(Tox):
return True return True
def init_callbacks(self): def init_callbacks(self):
return
# wraps self with # wraps self with
LOG.info("Adding Tox init_callbacks") LOG.info("Adding Tox init_callbacks")
def gi_wrapped(iTox, friendid, invite_data, invite_len, *args): def gi_wrapped(iTox, friendid, invite_data, invite_len, *args):
@ -436,7 +442,16 @@ class SyniTox(Tox):
if not self._ssl_context: if not self._ssl_context:
self.start_ssl(self._oArgs.irc_host) self.start_ssl(self._oArgs.irc_host)
irc = SSL.Connection(self._ssl_context, irc) irc = SSL.Connection(self._ssl_context, irc)
irc.connect((self._oArgs.irc_host, self._oArgs.irc_port)) try:
host = ts.sDNSLookup(self._oArgs.irc_host)
except Exception as e:
LOG.warn(f"{self._oArgs.irc_host} errored in resolve {e}")
host = self._oArgs.irc_host
else:
if not host:
LOG.warn(f"{self._oArgs.irc_host} did not resolve.")
host = self._oArgs.irc_host
irc.connect((host, self._oArgs.irc_port))
irc.do_handshake() irc.do_handshake()
LOG.info('IRC SSL connected ') LOG.info('IRC SSL connected ')
else: else:
@ -453,10 +468,10 @@ class SyniTox(Tox):
LOG.warn(f"Error: {e}") LOG.warn(f"Error: {e}")
return return
self.irc = irc irc.send(bytes('NICK ' + nick + '\r\n', 'UTF-8' ))
self.irc.send(bytes('NICK ' + nick + '\r\n', 'UTF-8' )) irc.send(bytes('USER %s %s bla :%s\r\n' % (
self.irc.send(bytes('USER %s %s bla :%s\r\n' % (
ident, self._oArgs.irc_host, realname), 'UTF-8')) ident, self._oArgs.irc_host, realname), 'UTF-8'))
self.irc = irc
def dht_init(self): def dht_init(self):
if not self.bRouted(): return if not self.bRouted(): return
@ -681,6 +696,7 @@ class SyniTox(Tox):
if not dht_conneted: if not dht_conneted:
self.dht_init() self.dht_init()
LOG.info(f'Not DHT connected {iCount} iterating {10 + iDelay} seconds') LOG.info(f'Not DHT connected {iCount} iterating {10 + iDelay} seconds')
iDelay = iDelay + iDelay // 10
self.do(10 + iDelay) self.do(10 + iDelay)
#drop through #drop through
@ -715,23 +731,26 @@ class SyniTox(Tox):
self.do(20) self.do(20)
continue continue
LOG.info('Waiting on IRC.') LOG.info(f'Waiting on IRC to {self._oArgs.irc_host} on {self._oArgs.irc_port}')
iDelay = 10
readable = self.spin(20) readable = self.spin(20)
if not readable: if not readable:
LOG.info('Waited on IRC but nothing to read.') LOG.info('Waited on IRC but nothing to read.')
iDelay = iDelay + iDelay // 10
continue continue
try: try:
self.irc_readlines() self.irc_readlines()
except Exception as e: except Exception as e:
LOG.exception(f'IRC Error during read: {e}') LOG.warn(f'IRC Error during read: {e}')
# close irc? # close irc?
try: self.irc.close() try:
self.irc.close()
self.irc = None
except: pass except: pass
self.irc = None
self.irc_init()
continue continue
else:
iDelay = 10
return 0 return 0
@ -750,7 +769,6 @@ class SyniTox(Tox):
success = True success = True
break break
except socket.error: except socket.error:
self.irc_init()
sleep(1) sleep(1)
def on_connection_status(self, friendId, status): def on_connection_status(self, friendId, status):
@ -859,6 +877,7 @@ def iMain(oArgs, oOpts):
__builtins__.app = o __builtins__.app = o
o.start() o.start()
ret = o.iLoop() ret = o.iLoop()
o.quit()
except KeyboardInterrupt: except KeyboardInterrupt:
ret = 0 ret = 0
except ( SSL.Error, ) as e: except ( SSL.Error, ) as e:
@ -873,7 +892,6 @@ def iMain(oArgs, oOpts):
ret = 2 ret = 2
else: else:
ret = 0 ret = 0
o.quit()
return ret return ret
def oToxygenToxOptions(oArgs): def oToxygenToxOptions(oArgs):
@ -922,6 +940,7 @@ def oArgparse(lArgv):
CAcs.append(elt) CAcs.append(elt)
break break
parser.add_argument('--log_level', type=int, default=10)
parser.add_argument('--bot_name', type=str, default=bot_toxname) parser.add_argument('--bot_name', type=str, default=bot_toxname)
parser.add_argument('--max_sleep', type=int, default=3600, parser.add_argument('--max_sleep', type=int, default=3600,
help="max time to sleep waiting for routing before exiting") help="max time to sleep waiting for routing before exiting")
@ -931,6 +950,7 @@ def oArgparse(lArgv):
# parser.add_argument('--irc_type', type=str, default='', # parser.add_argument('--irc_type', type=str, default='',
# choices=['', 'startls', 'direct') # choices=['', 'startls', 'direct')
# does host == connect ? # does host == connect ?
# oftcnet6xg6roj6d7id4y4cu6dchysacqj2ldgea73qzdagufflqxrid.onion:6697
parser.add_argument('--irc_host', type=str, default='irc.oftc.net', parser.add_argument('--irc_host', type=str, default='irc.oftc.net',
help="irc.libera.chat will not work over Tor") help="irc.libera.chat will not work over Tor")
parser.add_argument('--irc_port', type=int, default=6667, parser.add_argument('--irc_port', type=int, default=6667,