summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2016-05-10 03:46:48 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2016-05-10 03:57:08 +0200
commitfe8f9e3af8614a3b9f520c8734f3be569ba899e6 (patch)
tree61ff157fc3bbf98d592ee99badd1e92ad9f23431
parent7ae32e3ca639d31231787f638a5a4635faadf719 (diff)
downloadalternative-nick-fe8f9e3af8614a3b9f520c8734f3be569ba899e6.tar.gz
alternative-nick-fe8f9e3af8614a3b9f520c8734f3be569ba899e6.tar.bz2
alternative-nick-fe8f9e3af8614a3b9f520c8734f3be569ba899e6.zip
Try to get our default nick, if we used an alternative one
-rw-r--r--alternativ_nick.py39
1 files changed, 36 insertions, 3 deletions
diff --git a/alternativ_nick.py b/alternativ_nick.py
index 61385cb..e7d6a26 100644
--- a/alternativ_nick.py
+++ b/alternativ_nick.py
@@ -7,6 +7,20 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
+import sys
+import threading
+
+
+# Duration to wait before trying to get the nick again (in seconds)
+NICKLOOP_DELAY = 60
+
+
+def _start_nickloop(phenny):
+ def nickloop():
+ print >> sys.stderr, ('Trying to get: %s' % phenny.config.nick)
+ phenny.write(('NICK', phenny.config.nick))
+ threading.Timer(NICKLOOP_DELAY, nickloop).start()
+
def _set_nick(wrapper, nick):
# The phenny argument of the event handler is only a wrapper to enable the
@@ -40,9 +54,28 @@ def _build_nick(phenny):
def nickname_in_use(phenny, input):
- new_nick = _build_nick(phenny)
- _set_nick(phenny, new_nick)
- phenny.write(('NICK', new_nick))
+ # The nickname in use event can occur during connection phase if the
+ # supplied nick is already taken, or if we try to get the default nick and
+ # it is still taken. We remember the connection state, to decide what to
+ # do.
+ if phenny.data.get('alternativ_nick.connected'):
+ _start_nickloop(phenny)
+ else:
+ new_nick = _build_nick(phenny)
+ _set_nick(phenny, new_nick)
+ phenny.data['alternativ_nick.active'] = True
+ phenny.write(('NICK', new_nick))
nickname_in_use.rule = r'(.*)'
nickname_in_use.event = '433'
nickname_in_use.thread = False
+
+
+def connected(phenny, input):
+ # The '001' event is the first message after establishing the connection
+ # and providing a valid NICK and USER. If we used an alternativ nick, we
+ # could start trying to get back the default nick.
+ phenny.data['alternativ_nick.connected'] = True
+ if phenny.data.get('alternativ_nick.active'):
+ _start_nickloop(phenny)
+connected.rule = r'(.*)'
+connected.event = '001'