Ignore duplicate messages
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
				
			|||||||
# Copyright (C) 2020 by eichhornchen, ÿnérant
 | 
					# Copyright (C) 2020 by eichhornchen, ÿnérant
 | 
				
			||||||
# SPDX-License-Identifier: GPL-3.0-or-later
 | 
					# SPDX-License-Identifier: GPL-3.0-or-later
 | 
				
			||||||
 | 
					from datetime import datetime
 | 
				
			||||||
from typing import Any, Tuple
 | 
					from typing import Any, Tuple
 | 
				
			||||||
from ipaddress import IPv6Address
 | 
					from ipaddress import IPv6Address
 | 
				
			||||||
from threading import Thread
 | 
					from threading import Thread
 | 
				
			||||||
@@ -49,6 +49,7 @@ class Squirrel(Hazelnut):
 | 
				
			|||||||
        self.last_line = -1
 | 
					        self.last_line = -1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.history = []
 | 
					        self.history = []
 | 
				
			||||||
 | 
					        self.received_messages = dict()
 | 
				
			||||||
        self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS)
 | 
					        self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS)
 | 
				
			||||||
        self.input_pad = curses.newpad(1, curses.COLS)
 | 
					        self.input_pad = curses.newpad(1, curses.COLS)
 | 
				
			||||||
        self.emoji_pad = curses.newpad(18, 12)
 | 
					        self.emoji_pad = curses.newpad(18, 12)
 | 
				
			||||||
@@ -252,6 +253,19 @@ class Squirrel(Hazelnut):
 | 
				
			|||||||
        if self.last_line == len(self.history) - 2:
 | 
					        if self.last_line == len(self.history) - 2:
 | 
				
			||||||
            self.last_line += 1
 | 
					            self.last_line += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def receive_message_from(self, msg: str, sender_id: int, nonce: int) -> bool:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        This method is called by a DataTLV, sent by a real person.
 | 
				
			||||||
 | 
					        This add the message in the history if not already done.
 | 
				
			||||||
 | 
					        Returns True iff the message was not already received previously.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        if (sender_id, nonce) in self.received_messages:
 | 
				
			||||||
 | 
					            return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.add_message(msg)
 | 
				
			||||||
 | 
					        self.received_messages[(sender_id, nonce)] = Message(msg, sender_id, nonce)
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def add_system_message(self, msg: str) -> None:
 | 
					    def add_system_message(self, msg: str) -> None:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Add a new system log message.
 | 
					        Add a new system log message.
 | 
				
			||||||
@@ -457,3 +471,21 @@ class Worm(Thread):
 | 
				
			|||||||
                    tlv.handle(self.squirrel, hazelnut)
 | 
					                    tlv.handle(self.squirrel, hazelnut)
 | 
				
			||||||
                self.squirrel.refresh_history()
 | 
					                self.squirrel.refresh_history()
 | 
				
			||||||
                self.squirrel.refresh_input()
 | 
					                self.squirrel.refresh_input()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Message:
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    This class symbolises the data sent by a real client, excluding system messages.
 | 
				
			||||||
 | 
					    This is useful to check unicity or to save and load messages.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    content: str
 | 
				
			||||||
 | 
					    # TODO: Replace the id by the good (potential) hazel
 | 
				
			||||||
 | 
					    sender_id: int
 | 
				
			||||||
 | 
					    nonce: int
 | 
				
			||||||
 | 
					    created_at: datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __init__(self, content: str, sender_id: int, nonce: int, created_at: datetime = None):
 | 
				
			||||||
 | 
					        self.content = content
 | 
				
			||||||
 | 
					        self.sender_id = sender_id
 | 
				
			||||||
 | 
					        self.nonce = nonce
 | 
				
			||||||
 | 
					        self.created_at = created_at or datetime.now()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -208,7 +208,10 @@ class DataTLV(TLV):
 | 
				
			|||||||
        TODO: Check that the tuple (sender_id, nonce) is unique to avoid duplicates.
 | 
					        TODO: Check that the tuple (sender_id, nonce) is unique to avoid duplicates.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        msg = self.data.decode('UTF-8')
 | 
					        msg = self.data.decode('UTF-8')
 | 
				
			||||||
        squirrel.add_message(msg)
 | 
					        if not squirrel.receive_message_from(msg, self.sender_id, self.nonce):
 | 
				
			||||||
 | 
					            # The message was already received
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        nickname_match = re.match("(.*): (.*)", msg)
 | 
					        nickname_match = re.match("(.*): (.*)", msg)
 | 
				
			||||||
        if nickname_match is None:
 | 
					        if nickname_match is None:
 | 
				
			||||||
            squirrel.send_packet(sender, Packet.construct(WarningTLV.construct(
 | 
					            squirrel.send_packet(sender, Packet.construct(WarningTLV.construct(
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user