dustbot/modules/links.py

63 lines
1.9 KiB
Python

# links/relays channels to a specified link name
# works cross network. all chans linked to same name are relayed
import asyncio
import bot
# globals
try:
_G
except NameError:
_G = {
'links': {} # strLinkName: [c,c]
}
async def link( c, lname ):
if lname not in _G['links'].keys(): _G['links'][lname] = []
if c not in _G['links'][lname]:
c['con'].say_to( c['dst'], '> Adding this chan to link named \'{}\'...'.format( lname ) )
_G['links'][lname].append( c )
else: c['con'].say_to( c['dst'], '> This chan is already linked to that name...' )
async def unlink( c, lname ):
if lname not in _G['links'].keys():
c['con'].say_to( c['dst'], '> Link name not found.' )
return
if c in _G['links'][lname]:
c['con'].say_to( c['dst'], '> Unlinking this chan from link named \'{}\'...'.format( lname ) )
_G['links'][lname].remove( c )
# if nothing else in link, remove name too
if not _G['links'][lname]: _G['links'].pop( lname, None )
else: c['con'].say_to( c['dst'], '> This chan is not linked to link name \'{}\'...'.format( lname ) )
@bot.parser()
def parse_linked_chat( con, chan, nick, msg ):
for lname in _G['links'].keys():
for c in _G['links'][lname]:
if con == c['con'] and chan == c['dst']:
for ctx in _G['links'][lname]:
if ctx['con'] != con or ctx['dst'] != chan:
# disable callback for recursion prevention!
ctx['con'].say_to( ctx['dst'], '[{}:{}] <{}> {}'.format(
lname, _G['links'][lname].index( c ) + 1, nick, msg ), callback=False )
break
@bot.command( 'link', True )
async def cmd_link( p, c ):
lname = ''
if p: lname = p[0].lower()
else:
c['con'].say_to( c['dst'], '> Please specify a link name.' )
return
await link( c, lname )
@bot.command( 'unlink', True )
async def cmd_unlink( p, c ):
lname = ''
if p: lname = p[0].lower()
else:
c['con'].say_to( c['dst'], '> Please specify a link name.' )
return
await unlink( c, lname )