1+ import asyncio
2+
13from cloudbot import hook
4+ from cloudbot .util import colors
25
36
47@hook .command (autohelp = False , permissions = ["botcontrol" ])
5- def conncheck (nick , bot , notice ):
8+ def conncheck (nick , bot , notice ):
69 """This command is an effort to make the bot reconnect to a network if it has been disconnected."""
710 # For each irc network return a notice on the connection state and send a message from
811 # each connection to the nick that used the command.
@@ -14,3 +17,49 @@ def conncheck(nick, bot, notice):
1417 bot .connections [conn ].connect ()
1518 # Send a message from each irc network connection to the nick that issued the command
1619 bot .connections [conn ].message (nick , "just letting you know I am here. {}" .format (conn ))
20+
21+
22+ @asyncio .coroutine
23+ @hook .command (autohelp = False , permissions = ["botcontrol" ], singlethread = True )
24+ def reconnect (conn , text , bot ):
25+ """[connection] - Reconnects to [connection] or the current connection if not specified"""
26+ if not text :
27+ to_reconnect = conn
28+ else :
29+ try :
30+ to_reconnect = bot .connections [text .lower ()]
31+ except KeyError :
32+ return "Connection '{}' not found" .format (text )
33+
34+ if to_reconnect .connected :
35+ to_reconnect .quit ("Reconnecting..." )
36+ yield from asyncio .sleep (1 )
37+ to_reconnect ._quit = False
38+
39+ coro = to_reconnect .connect ()
40+ try :
41+ yield from asyncio .wait_for (coro , 30 )
42+ except asyncio .TimeoutError :
43+ return "Connection timed out"
44+ except Exception as e :
45+ return "{}: {}" .format (type (e ).__name__ , e )
46+
47+ return "Reconnected to '{}'" .format (conn .name )
48+
49+
50+ def format_conn (conn ):
51+ if conn .connected :
52+ out = "$(green){name}$(clear)"
53+ else :
54+ out = "$(red){name}$(clear)"
55+
56+ return colors .parse (out .format (name = conn .name ))
57+
58+
59+ @hook .command ("connlist" , "listconns" , autohelp = False , permissions = ["botcontrol" ])
60+ def list_conns (bot ):
61+ """- Lists all current connections and their status"""
62+ conns = ', ' .join (
63+ map (format_conn , bot .connections .values ())
64+ )
65+ return "Current connections: {}" .format (conns )
0 commit comments