Add [Shared] to config, with 'prefix' and 'owner' opts

'owner' is moved from [IRC] and [XMPP].
'prefix' controls what prefix commands have.
This commit is contained in:
Alexei Sorokin 2016-08-01 21:12:35 +03:00
parent efe3be890b
commit c94ef4e3a1
2 changed files with 22 additions and 21 deletions

View File

@ -1,9 +1,12 @@
[Shared]
prefix = .
owner = somebody
[IRC]
channel = #daemons
nick = pasarela
server = irc.freenode.net
port = 6667
owner = somebody
[XMPP]
jid = becario@daemons.cf

View File

@ -21,7 +21,6 @@ class IRCBot(SingleServerIRCBot):
self.opts = opts
self.nick = opts['nick']
self.owner = opts['owner']
self.inter = inter
def _on_join(self, c, e):
@ -70,7 +69,6 @@ class XMPPBot(sleekxmpp.ClientXMPP):
self.jabber = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd'])
self.opts = opts
self.nick = opts['nick']
self.owner = opts['owner']
self.inter = inter
def register_handlers(self):
@ -123,7 +121,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
class Intermedia:
def __init__(self, irc_chan, xmpp_muc):
def __init__(self, shared_opts, irc_chan, xmpp_muc):
self.irc_chan = irc_chan
self.xmpp_muc = xmpp_muc
self.ircbot = None
@ -131,6 +129,9 @@ class Intermedia:
self.irc_users = []
self.xmpp_users = []
self.prefix = shared_opts['prefix']
self.owner = shared_opts['owner']
def set_bots(self, ircbot, xmppbot):
self.ircbot = ircbot
self.xmppbot = xmppbot
@ -170,13 +171,8 @@ class Intermedia:
try:
msg = body.replace('\r\n', '\n').replace('\r', '\n').split('\n')
if from_net == 'irc':
owner = self.ircbot.owner
elif from_net == 'xmpp':
owner = self.xmppbot.owner
if msg and len(msg) > 0:
if len(msg) == 1 and msg[0] == '.users':
if len(msg) == 1 and msg[0] == self.prefix + 'users':
irc_users = ', '.join(self.get_irc_users())
xmpp_users = ', '.join(self.get_xmpp_users())
@ -192,9 +188,9 @@ class Intermedia:
for answer in [irc_users]:
self.to_xmpp(answer)
elif len(msg) == 1 and msg[0] == '.help':
answer = 'The only command I have is \'.users\''+ \
'. Also, my owner is ' + owner + '.'
elif len(msg) == 1 and msg[0] == self.prefix + 'help':
answer = 'The only command I have is \'' + self.prefix + \
'users\'. Also, my owner is ' + self.owner + '.'
if from_net == 'irc':
self.to_irc(answer)
@ -202,13 +198,13 @@ class Intermedia:
self.to_xmpp(answer)
else:
prefix = '[' + nick + '] '
prefix_me = '***' + nick + ' '
nick_prefix = '[' + nick + '] '
nick_prefix_me = '***' + nick + ' '
if (not re.match('^/me .+$', msg[0])):
msg[0] = prefix + msg[0]
msg[0] = nick_prefix + msg[0]
else:
msg[0] = prefix_me + re.split('^/me ', msg[0])[1]
msg[0] = nick_prefix_me + re.split('^/me ', msg[0])[1]
if from_net == 'irc':
self.to_xmpp('\n'.join(msg))
@ -218,7 +214,7 @@ class Intermedia:
# Separately as with a standard prefix, always.
for m in msg[1:]:
time.sleep(0.5)
self.to_irc(prefix + m)
self.to_irc(nick_prefix + m)
except Exception as e:
print(e)
@ -245,6 +241,7 @@ class Intermedia:
if __name__ == '__main__':
parser = SafeConfigParser()
shared_opts = {}
xmpp_opts = {}
irc_opts = {}
@ -253,20 +250,21 @@ if __name__ == '__main__':
else:
parser.read('config.ini')
shared_opts['prefix'] = parser.get('Shared', 'prefix')
shared_opts['owner'] = parser.get('Shared', 'owner')
irc_opts['chan'] = parser.get('IRC', 'channel')
irc_opts['nick'] = parser.get('IRC', 'nick')
irc_opts['server'] = parser.get('IRC', 'server')
irc_opts['port'] = int(parser.get('IRC', 'port'))
irc_opts['owner'] = parser.get('IRC', 'owner')
xmpp_opts['jid'] = parser.get('XMPP', 'jid')
xmpp_opts['passwd'] = parser.get('XMPP', 'password')
xmpp_opts['muc'] = parser.get('XMPP', 'muc')
xmpp_opts['nick'] = parser.get('XMPP', 'nick')
xmpp_opts['owner'] = irc_opts['owner']
try:
inter = Intermedia(irc_opts['chan'], xmpp_opts['muc'])
inter = Intermedia(shared_opts, irc_opts['chan'], xmpp_opts['muc'])
ircbot = IRCBot(irc_opts, inter)
xmppbot = XMPPBot(xmpp_opts, inter)
inter.set_bots(ircbot, xmppbot)