Commit 1ab1279a authored by Romain Bignon's avatar Romain Bignon

discovering message is sent by server, and there is only one try per message

parent cca2d09c
Pipeline #10 failed with stages
......@@ -32,16 +32,27 @@ import string
import sys
import ssl
import time
from collections import defaultdict
from random import choice
from optparse import OptionParser
class Logger(list):
score = list() #TODO
def __init__(self, *args, **kwargs):
super(Logger, self).__init__(*args, **kwargs)
self.scores = defaultdict(dict)
def log_message(self, nickname):
self.append((datetime.datetime.now().strftime('%H:%M:%S'), nickname))
def find(self, who, timestamp, nickname):
if timestamp in self.scores[who]:
return False
res = (timestamp, nickname) in self
self.scores[who][timestamp] = res
return res
class Channel(object):
def __init__(self, server, name):
......@@ -77,6 +88,17 @@ class Channel(object):
if not self.members:
self.server.remove_channel(self)
def message(self, sender, command, message, exclude=None):
if isinstance(sender, Client):
prefix = self.anon_prefix
self.logger.log_message(sender.nickname)
else:
prefix = sender.name
line = ":%s %s %s" % (prefix, command, message)
for client in self.members:
if client != exclude:
client.message(line)
NICKNAMES = ['Roger', 'Bertrand', 'Gilbert', 'Jocelyne', 'Hector', 'Liliane', 'Patoche']
@property
def anon_prefix(self):
......@@ -195,13 +217,6 @@ class Client(object):
else:
self.reply("422 %s :MOTD File is missing" % self.nickname)
def message_channel(self, channel, command, message, include_self=False):
line = ":%s %s %s" % (channel.anon_prefix, command, message)
channel.logger.log_message(self.nickname)
for client in channel.members:
if client != self or include_self:
client.message(line)
def join_channel(self, channel):
channel.add_member(self)
self.channels[irc_lower(channel.name)] = channel
......@@ -320,7 +335,7 @@ class Client(object):
message = args[1]
if self.server.has_channel(targetname):
channel = self.server.get_channel(targetname)
self.message_channel(channel, 'PRIVMSG', "%s :%s" % (channel.name, message))
channel.message(self, 'PRIVMSG', "%s :%s" % (channel.name, message), exclude=self)
else:
self.reply("401 %s %s :No such channel"
% (self.nickname, targetname))
......@@ -421,9 +436,7 @@ class Client(object):
if len(args) > 1:
newtopic = args[1]
channel.topic = newtopic
self.message_channel(
channel, "TOPIC", "%s :%s" % (channelname, newtopic),
True)
channel.message(self, "TOPIC", "%s :%s" % (channelname, newtopic))
else:
if channel.topic:
self.reply("332 %s %s :%s"
......@@ -462,10 +475,10 @@ class Client(object):
if not channel:
self.reply("442 %s :You're not on that channel" % channelname)
if (timestamp, nickname) in channel.logger:
self.message_channel(channel, "PRIVMSG", "%s :%s has found %s" % (channel.name, self.nickname, nickname), True)
if channel.logger.find(self.nickname, timestamp, nickname):
channel.message(self.server, "PRIVMSG", "%s :%s has found %s" % (channel.name, self.nickname, nickname))
else:
self.reply("Failed!")
channel.message(self.server, "PRIVMSG", "%s :%s failed to discover who is behind %s" % (channel.name, self.nickname, timestamp))
class Server(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment