# 
# Incoming Message Filter for Pidgin
#   Michael Banack <bob5972@ucf.ics.uci.edu>
#   17-Oct-2008
#

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA


use Purple;

%PLUGIN_INFO = (
    perl_api_version => 2,
    name => "Filter",
    version => "0.1",
    summary => "Filters incoming messages",
    description => "Replaces incoming messages",
    author => "Michael Banack <bob5972@ucf.ics.uci.edu>",
    url => "http://www.banack.net/projects/",

    load => "plugin_load"
);

sub plugin_init {
    return %PLUGIN_INFO;
}

sub plugin_load {
    $plugin = shift;
    
    Purple::Debug::info("filter", "plugin_load() - begin\n");

    # A pointer to the handle to which the signal belongs
    $convs_handle = Purple::Conversations::get_handle();
    
    # Connect the perl sub 'sending_im_msg_cb' to the event 'sending-im-msg'
    #Purple::Signal::connect($convs_handle, "sending-im-msg", $plugin, \&sending_im_msg_cb, "xxx");
    
    # Connect the perl sub 'receiving_im_msg_cb' to the event 'receiving-im-msg'
    Purple::Signal::connect($convs_handle, "receiving-im-msg", $plugin, \&receiving_im_msg_cb, "yyy");

	#Test = grfg
	@badWords=("(zbgure)?s+h+p+x+","(tbq?)?q+n+z+a+","(oh+yy)?f+u+vg+");
	
	foreach(@badWords)
	{
		#Purple::Debug::info("filter","Filtering: ".$_."\n");
		$_ =~ tr/a-mn-z/n-za-m/;
	}
	
    Purple::Debug::info("filter", "plugin_load() - filter plugin loaded.\n");
}

sub plugin_unload {
    my $plugin = shift;
    Purple::Debug::info("filter", "plugin_unload() - filter plugin unloaded.\n");
}

sub receiving_im_msg_cb {
    my ($account, $who, $msg, $conv, $flags) = @_;
	#Purple::Debug::info("filter", "Received Message: ".$msg."\n");	
	#Purple::Debug::info("filter", "Pre-Filter: ".$_[2]."\n");
	
	my $i;
	my $size=@badWords;
	
	for($i=0;$i<$size;$i+=1)
	{
		$_[2] = " ".$_[2]." ";
		$_[2] =~ s/(\s|>)$badWords[$i]((ed)|(it)|(ing)|(ty))?/\1**expletive deleted**/gi;
		$_[2] =~ s/^ //;
		$_[2] =~ s/ $//;
		#Purple::Debug::info("filter", "Testing for: ".$i."\n");
	}
	
	#Purple::Debug::info("filter", "Post-Filter: ".$_[2]."\n");
	#Purple::Debug::info("filter", "Delievered Message: ".$msg."\n");
}


