Added pretty useless feature: change nick if it`s duplicate

This commit is contained in:
Tomasz Fluxid Kowalczyk 2012-02-15 21:09:50 +01:00
parent caaa176247
commit b428ee1983
2 changed files with 21 additions and 6 deletions

View File

@ -29,8 +29,9 @@ class SyncClientProtocol(CommandProtocol):
self.manager.stop() self.manager.stop()
CommandProtocol.handle_error(self, args) CommandProtocol.handle_error(self, args)
@arg_count(0) @arg_count(1)
def handle_init_hello(self, args): def handle_init_hello(self, args):
print 'Connected as', args[0]
self.change_state('connected') self.change_state('connected')
@arg_count(1, 2) @arg_count(1, 2)

View File

@ -1,6 +1,7 @@
#coding:utf8 #coding:utf8
from collections import deque from collections import deque
import re
import time import time
import random import random
@ -16,6 +17,7 @@ from .utils import parse_state
random.seed() random.seed()
CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
RE_NAME = re.compile('^(.*?)(\d*)$')
def random_chars(): def random_chars():
return ''.join(random.choice(CHARS) for _ in xrange(10)) return ''.join(random.choice(CHARS) for _ in xrange(10))
@ -33,6 +35,10 @@ class SyncServerProtocol(CommandProtocol):
if not len(args) == 1: if not len(args) == 1:
self.drop_with_error('Invalid arguments') self.drop_with_error('Invalid arguments')
return return
name = args[0].strip()
if not args:
self.drop_with_error('Invalid nickname')
return
self.factory.add_watcher(self, args[0]) self.factory.add_watcher(self, args[0])
self.change_state('connected') self.change_state('connected')
@ -111,8 +117,8 @@ class SyncServerProtocol(CommandProtocol):
def send_left(self, who): def send_left(self, who):
self.send_message('left', who) self.send_message('left', who)
def send_hello(self): def send_hello(self, name):
self.send_message('hello') self.send_message('hello', name)
states = dict( states = dict(
@ -164,8 +170,17 @@ class SyncFactory(Factory):
def buildProtocol(self, addr): def buildProtocol(self, addr):
return SyncServerProtocol(self) return SyncServerProtocol(self)
def add_watcher(self, watcher_proto, name): def add_watcher(self, watcher_proto, name):
allnames = (watcher.name.lower() for watcher in self.watchers.itervalues())
while name.lower() in allnames:
m = RE_NAME.match(name)
name, number = m.group(1), m.group(2)
if number:
number = str(int(number)+1)
else:
number = '1'
name += number
watcher = WatcherInfo(watcher_proto, name) watcher = WatcherInfo(watcher_proto, name)
if self.watchers: if self.watchers:
watcher.max_position = min(w.max_position for w in self.watchers.itervalues()) watcher.max_position = min(w.max_position for w in self.watchers.itervalues())
@ -175,7 +190,7 @@ class SyncFactory(Factory):
continue continue
receiver.watcher_proto.send_joined(name) receiver.watcher_proto.send_joined(name)
watcher_proto.send_present(receiver.name, receiver.filename) watcher_proto.send_present(receiver.name, receiver.filename)
watcher_proto.send_hello() watcher_proto.send_hello(name)
self.send_state_to(watcher) self.send_state_to(watcher)
self.send_ping_to(watcher) self.send_ping_to(watcher)
@ -320,7 +335,6 @@ class SyncFactory(Factory):
print ctime - watcher.last_ping_received print ctime - watcher.last_ping_received
if ctime - watcher.last_ping_received > 60: if ctime - watcher.last_ping_received > 60:
watcher.watcher_proto.drop() watcher.watcher_proto.drop()
print 'dropped', watcher.name
return return
watcher.watcher_proto.send_ping(chars) watcher.watcher_proto.send_ping(chars)