From 2b8faa735aaa744fbb7e68b546822d951eadcafb Mon Sep 17 00:00:00 2001
From: Daniel Winzen <d@winzen4.de>
Date: Sat, 30 Jul 2016 16:45:01 +0200
Subject: [PATCH] Prevent posting the same message twice, if no other message
 was posted in-between

---
 CHANGELOG |  1 +
 chat.php  | 12 +++++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index fa27d68..5574a97 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 Don't display empty option for system messages in delete messages by name
 Don't show a kick button on your own session in the list of active sessions
 Allow ignoring incognito chatters
+Prevent posting the same message twice, if no other message was posted in-between
 
 Version 1.20.6 - Jul. 23, 2016
 Simplify ignore logic + disallow ignoring chatters with higher status
diff --git a/chat.php b/chat.php
index bc33406..2bbb7b7 100644
--- a/chat.php
+++ b/chat.php
@@ -2911,11 +2911,11 @@ function create_hotlinks(){
 }
 
 function add_message(){
-	global $U;
+	global $U, $db;
 	if(empty($U['message'])){
 		return false;
 	}
-	$newmessage=array(
+	$message=array(
 		'postdate'	=>time(),
 		'poststatus'	=>$U['poststatus'],
 		'poster'	=>$U['nickname'],
@@ -2923,7 +2923,13 @@ function add_message(){
 		'text'		=>"<span class=\"usermsg\">$U[displaysend]".style_this($U['message'], $U['style']).'</span>',
 		'delstatus'	=>$U['status']
 	);
-	write_message($newmessage);
+	//prevent posting the same message twice, if no other message was posted in-between.
+	$stmt=$db->prepare('SELECT id FROM ' . PREFIX . 'messages WHERE poststatus=? AND poster=? AND recipient=? AND text=? AND id IN (SELECT * FROM (SELECT id FROM ' . PREFIX . 'messages ORDER BY id DESC LIMIT 1) AS t);');
+	$stmt->execute([$message['poststatus'], $message['poster'], $message['recipient'], $message['text']]);
+	if($stmt->fetch(PDO::FETCH_NUM)){
+		return false;
+	}
+	write_message($message);
 	return true;
 }