summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2016-05-10 03:45:24 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2016-05-10 03:57:08 +0200
commit7ae32e3ca639d31231787f638a5a4635faadf719 (patch)
tree04b6af4c8093c9ceee8a5c67beedafed8c8db082
parentc07d3f35fd4900c463b2e64b2f8c16d79bbfe0a9 (diff)
downloadalternative-nick-7ae32e3ca639d31231787f638a5a4635faadf719.tar.gz
alternative-nick-7ae32e3ca639d31231787f638a5a4635faadf719.tar.bz2
alternative-nick-7ae32e3ca639d31231787f638a5a4635faadf719.zip
Use an alternative nick, if our default nick is taken
-rw-r--r--alternativ_nick.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/alternativ_nick.py b/alternativ_nick.py
index ea0ea52..61385cb 100644
--- a/alternativ_nick.py
+++ b/alternativ_nick.py
@@ -7,3 +7,42 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
+
+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 alternativ_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.
+
+ alternativ_nick = getattr(phenny.config, 'alternativ_nick', None)
+ if alternativ_nick is not None and phenny.nick != alternativ_nick:
+ return alternativ_nick
+ else:
+ if phenny.data.get('alternativ_nick.count') is not None:
+ count = phenny.data['alternativ_nick.count']
+ count += 1
+ phenny.data['alternativ_nick.count'] = count
+ return ('%s_%d' % (phenny.config.nick, count))
+ else:
+ phenny.data['alternativ_nick.count'] = 0
+ return ('%s_' % phenny.config.nick)
+
+
+def nickname_in_use(phenny, input):
+ new_nick = _build_nick(phenny)
+ _set_nick(phenny, new_nick)
+ phenny.write(('NICK', new_nick))
+nickname_in_use.rule = r'(.*)'
+nickname_in_use.event = '433'
+nickname_in_use.thread = False