[Tkabber-dev] r175 - trunk/plugins/antispam

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Sat Feb 21 02:40:52 MSK 2009


Author: gebb
Date: 2009-02-21 02:40:52 +0300 (Sat, 21 Feb 2009)
New Revision: 175

Modified:
   trunk/plugins/antispam/ChangeLog
   trunk/plugins/antispam/antispam.tcl
Log:
Bug fixed: Offline spam messages were not blocked. 
The processing of all offline messages is now postponed until the roster arrives.

Modified: trunk/plugins/antispam/ChangeLog
===================================================================
--- trunk/plugins/antispam/ChangeLog	2009-02-19 10:28:31 UTC (rev 174)
+++ trunk/plugins/antispam/ChangeLog	2009-02-20 23:40:52 UTC (rev 175)
@@ -1,3 +1,11 @@
+2009-02-21 Otto Gebb
+
+	* Bug fixed: Offline spam messages were not blocked. 
+	The processing of all offline messages is now postponed 
+	until the roster arrives. However, offline spam messages 
+	are still shown if a disconnect occurs before the roster 
+	has arrived, because we can't validate the sender then.
+
 2009-02-19 Otto Gebb
 
 	* Bug fixed: All offline messages were blocked. Added check if

Modified: trunk/plugins/antispam/antispam.tcl
===================================================================
--- trunk/plugins/antispam/antispam.tcl	2009-02-19 10:28:31 UTC (rev 174)
+++ trunk/plugins/antispam/antispam.tcl	2009-02-20 23:40:52 UTC (rev 175)
@@ -102,38 +102,79 @@
 	return no
 }
 
-# Sets the "roster loaded" flag for the specified connection.
+# Disables the postponing of messages and shows offline
+# messages (except spam) for the specified connection.
 #
 proc antispam::roster_loaded_handler {xlib} {
 	variable roster_loaded
+	variable roster_wait_failed
 	set roster_loaded($xlib) 1
+	set roster_wait_failed($xlib) 0
+	process_postponed_msgs $xlib
 }
 
-# Clears the "roster loaded" flag for the specified connection.
+# Shows all offline messages (including possible spam)
+# if the roster failed to load and 
+# enables the postponing of messages.
 #
 proc antispam::disconnected_handler {xlib} {
 	variable roster_loaded
+	variable roster_wait_failed
+	if { ![is_ roster_loaded $xlib] } {
+		# Disconnect occurred before the roster has arrived.
+		set roster_wait_failed($xlib) 1
+		process_postponed_msgs $xlib
+	}
 	set roster_loaded($xlib) 0
+	set roster_wait_failed($xlib) 0
 }
 
-# Checks if the roster is loaded for
-# the specified connection.
+# Checks if there are postponed messages.
 #
-proc antispam::is_roster_loaded {xlib} {
-	variable roster_loaded
-	upvar 0 roster_loaded($xlib) rl
-	if { [info exists rl] && $rl } {
+proc antispam::postponed_msgs_exist {xlib} {
+	variable postponed_msgs
+	if { [info exists postponed_msgs($xlib)]
+		&& $postponed_msgs($xlib) ne "" } {
 		return yes
 	}
 	return no
 }
 
+# Shows postponed messages and clears the postponed
+# message list for the specified connection.
+#
+proc antispam::process_postponed_msgs {xlib} {
+	variable postponed_msgs
+	if { ![postponed_msgs_exist $xlib] } {
+		return
+	}
+	foreach {xlib from id type is_subject subject body args} \
+	$postponed_msgs($xlib) {
+		eval [list hook::run process_message_hook \
+			$xlib $from $id $type $is_subject $subject $body] $args
+	}
+	set postponed_msgs($xlib) {}
+}
+
+
+# Checks if the specified array element exists and is true.
+#
+proc antispam::is_ {arr xlib} {
+	variable $arr
+	upvar 0 ${arr}($xlib) el
+	if { [info exists el] && $el } {
+		return yes
+	}
+	return no
+}
+
 # Blocks messages from unknown JIDs
 # and requests the password (sending $options(message)).
 #
 proc antispam::process_message {xlib from id type is_subject subject body args} {
 	variable options
 	variable answered
+	variable postponed_msgs
 	global whitelist
 	# Do nothing if antispam is disabled.
 	if { !$options(enabled) } {
@@ -197,18 +238,6 @@
 			"BLOCK_EMPTY_ERR: blocking empty error from $from."
 		return stop
 	}
-	# Do not block messages if roster has not been initialized.
-	if { ![is_roster_loaded $xlib] } {
-		debugmsg antispam \
-			"ACCEPT: roster not initialized."
-		return
-	}
-	# Do not block messages from contacts in roster.
-	if {[roster::find_jid $xlib $sender_barejid] ne "" } {
-		debugmsg antispam \
-			"ACCEPT: sender in roster."
-		return
-	}
 	# Do nothing if regexp rules are not met.
 	if { ![meets_rules $from] } {
 		debugmsg antispam \
@@ -225,16 +254,48 @@
 			"ACCEPT: correct password given."
 		return
 	}
+	
+	# Do not block offline messages if
+	# the roster couldn't be loaded.
+	if {[is_ roster_wait_failed $xlib] } {
+		debugmsg antispam \
+			"ACCEPT: disconnected before loading roster."
+		return
+	}
 
+	# Postpone messages if the roster has not been initialized.
+	if { ![is_ roster_loaded $xlib] } {
+		debugmsg antispam \
+			"POSTPONE: roster not initialized."
+		lappend postponed_msgs($xlib) \
+			$xlib $from $id $type $is_subject $subject $body $args
+		return stop
+	}
+
+	# Do not block messages from contacts in roster.
+	if {[roster::find_jid $xlib $sender_barejid] ne "" } {
+		debugmsg antispam \
+			"ACCEPT: sender in roster."
+		return
+	}
+	
+
 	# If code flow reached this point, 
 	# the message is considered spam.
 
-	# Suppress error messages from spammers.
+	# Suppress error messages from spammers without 
+	# sending the request.
 	if { $type eq "error" } {
 		debugmsg antispam \
 			"BLOCK_SPAM: blocking error from $from."
 		return stop
 	}
+	# Don't send the request when processing an offline message.
+	if { [postponed_msgs_exist $xlib] } {
+		debugmsg antispam \
+			"BLOCK_SPAM: blocking offline message from $from."
+		return stop
+	}
 	debugmsg antispam \
 		"BLOCK_SPAM: blocking message from $from."
 	# Increment the number of times we replied to this JID.
@@ -244,7 +305,7 @@
 	} else {
 		incr answ
 	}
-	# Stop sending request after doing it 1 time
+	# Stop sending the request after doing it 1 time
 	# (should we make this number customizable?),
 	# but still prevent messages from being shown to the user.
 	if { $answ > 1 } {



More information about the Tkabber-dev mailing list