summaryrefslogtreecommitdiffstats
path: root/alternative_nick.py
diff options
context:
space:
mode:
Diffstat (limited to 'alternative_nick.py')
-rw-r--r--alternative_nick.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/alternative_nick.py b/alternative_nick.py
new file mode 100644
index 0000000..66f958c
--- /dev/null
+++ b/alternative_nick.py
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+"""
+alternative_nick.py - Use an alternative nick, if your choosen nick is already taken
+Copyright 2016, Alexander Sulfrian <alex@spline.inf.fu-berlin.de>
+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)
+DEFAULT_NICK_DELAY = 60
+
+
+def _start_nickloop(phenny):
+ delay = getattr(phenny.config, 'nick_delay', DEFAULT_NICK_DELAY)
+ if not isinstance(delay, int):
+ return
+
+ def nickloop():
+ print >> sys.stderr, ('Trying to get: %s' % phenny.config.nick)
+ phenny.write(('NICK', phenny.config.nick))
+ threading.Timer(delay, nickloop).start()
+
+
+def _set_nick(wrapper, nick):
+ # The phenny argument of the event handler is only a wrapper to enable the
+ # reply and say methods. If we try to change the 'nick' attribute directly,
+ # the modification is lost. So we need to get the real phenny instance
+ # before.
+ phenny = wrapper.__getattribute__('bot')
+ phenny.nick = nick
+
+
+def _build_nick(phenny):
+ # We have different ways to generate an alternative_nick:
+ # * First we try to use the config option.
+ # * If it is not set or it does not work, we append an underscore after the
+ # default nick.
+ # * If even this did not work, we append a number and count upwards until a
+ # valid nick is found.
+
+ alternative_nick = getattr(phenny.config, 'alternative_nick', None)
+ if alternative_nick is not None and phenny.nick != alternative_nick:
+ return alternative_nick
+ else:
+ if phenny.data.get('alternative_nick.count') is not None:
+ count = phenny.data['alternative_nick.count']
+ count += 1
+ phenny.data['alternative_nick.count'] = count
+ return ('%s_%d' % (phenny.config.nick, count))
+ else:
+ phenny.data['alternative_nick.count'] = 0
+ return ('%s_' % phenny.config.nick)
+
+
+def nickname_in_use(phenny, input):
+ # 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('alternative_nick.connected'):
+ _start_nickloop(phenny)
+ else:
+ new_nick = _build_nick(phenny)
+ _set_nick(phenny, new_nick)
+ phenny.data['alternative_nick.active'] = True
+ phenny.write(('NICK', new_nick))
+nickname_in_use.rule = r'(.*)'
+nickname_in_use.event = '433'
+nickname_in_use.thread = False
+
+
+def nick_change(phenny, input):
+ # Monitor the NICK changes to detect, if we change our nick.
+ if input.nick == phenny.nick:
+ _set_nick(phenny, input)
+ print >> sys.stderr, ("Changed nick to: %s" % phenny.nick)
+
+ # Trying to identify (just in case it did not work, during startup)
+ if hasattr(phenny.config, 'password'):
+ phenny.msg('NickServ', 'IDENTIFY %s' % phenny.config.password)
+nick_change.rule = r'(.*)'
+nick_change.event = 'NICK'
+
+
+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['alternative_nick.connected'] = True
+ if phenny.data.get('alternative_nick.active'):
+ _start_nickloop(phenny)
+connected.rule = r'(.*)'
+connected.event = '001'