#Initial code: Block MUC private plugin 13-07-2009 (Jet <megaxbit@xmpp.ru>)
#Modifications: Block MUC messages plugin 05-01-2010 (ancestor <ancestor@jabber.org>)
#Allows to block MUC private and public messages from users with None, Moderator, Member affiliations and roles.

namespace eval bmucx {
	::msgcat::mcload [file join [file dirname [info script]] msgs]

    if {![::plugins::is_registered bmucx]} {
	::plugins::register bmucx \
			    -namespace [namespace current] \
			    -source [info script] \
			    -description [::msgcat::mc "Blocking MUC messages."] \
			    -loadcommand [namespace code load] \
			    -unloadcommand [namespace code unload]
	return
    }
	
	custom::defgroup Plugins [::msgcat::mc "Plugins options."] -group Tkabber
	custom::defgroup bmuch bmuch -group Plugins -type hidden
	custom::defgroup bmuchpr bmuchpr -group Plugins -type hidden
	custom::defvar bmuc_list "" [::msgcat::mc "MUC List."] -type string -group bmuch
	custom::defvar moders_list "" [::msgcat::mc "Moders List."] -type string -group bmuch
	custom::defvar members_list "" [::msgcat::mc "Members List."] -type string -group bmuch	
	custom::defvar bmuc_list_priv "" [::msgcat::mc "MUC Private List."] -type string -group bmuchpr
	custom::defvar moders_list_priv "" [::msgcat::mc "Moders Private List."] -type string -group bmuchpr
	custom::defvar members_list_priv "" [::msgcat::mc "Members Private List."] -type string -group bmuchpr
}

########################################################################################

proc bmucx::refresh_list {} {
	variable bmuc_list
	variable moders_list
	variable members_list
	set bmuc_list [array get ::blockvar]
	set moders_list [array get ::blockvar_moders]	
	set members_list [array get ::blockvar_members]
}

proc bmucx::refresh_list_priv {} {
	variable bmuc_list_priv
	variable moders_list_priv
	variable members_list_priv
	set bmuc_list_priv [array get ::blockvar_priv]
	set moders_list_priv [array get ::blockvar_moders_priv]	
	set members_list_priv [array get ::blockvar_members_priv]
}

########################################################################################

proc bmucx::load_bmucx_list {} {
	variable bmuc_list
	variable moders_list
	variable members_list
	variable bmuc_list_priv
	variable moders_list_priv
	variable members_list_priv

	array set ::blockvar $bmuc_list
	array set ::blockvar_moders $moders_list
	array set ::blockvar_members $members_list
	array set ::blockvar_priv $bmuc_list_priv
	array set ::blockvar_moders_priv $moders_list_priv
	array set ::blockvar_members_priv $members_list_priv
}

########################################################################################

proc bmucx::add_bmucx_menu {m xlib jid} {
	$m add cascade \
		-menu "$m.men45" -label [::msgcat::mc "Block MUC messages"] 
	
	set site_4_0 $m
	menu $site_4_0.men45 -tearoff 0 
	
	$site_4_0.men45 add cascade \
		-menu "$site_4_0.men45.men46" -label [::msgcat::mc "Public MUC messages"]
	
	set site_5_0 $site_4_0.men45
	menu $site_5_0.men46 -tearoff 0 
	
	$site_5_0.men46 add checkbutton -label [::msgcat::mc "On"] \
		-indicatoron 1 -variable ::blockvar($jid) -offvalue 0 -onvalue 1 \
		-command [namespace current]::refresh_list

	set mstate normal
	if {!$::blockvar($jid)} {set mstate disabled}

	$site_5_0.men46 add separator \
 
	$site_5_0.men46 add checkbutton -label [::msgcat::mc "Block messages from members"] \
		-indicatoron 1 -variable ::blockvar_members($jid) -offvalue 0 -onvalue 1 \
		-command [namespace current]::refresh_list -state $mstate
	$site_5_0.men46 add checkbutton -label [::msgcat::mc "Block messages from moderators"] \
		-indicatoron 1 -variable ::blockvar_moders($jid) -offvalue 0 -onvalue 1 \
		-command [namespace current]::refresh_list -state $mstate	

	$site_4_0.men45 add cascade \
		-menu "$site_4_0.men45.men47" -label [::msgcat::mc "Private MUC messages"]
	
	set site_5_0 $site_4_0.men45
	menu $site_5_0.men47 -tearoff 0
	
	$site_5_0.men47 add checkbutton -label [::msgcat::mc "On"] \
		-indicatoron 1 -variable ::blockvar_priv($jid) -offvalue 0 -onvalue 1 \
		-command [namespace current]::refresh_list_priv

	set mstatepriv normal
	if {!$::blockvar_priv($jid)} {set mstatepriv disabled}

	$site_5_0.men47 add separator \

	$site_5_0.men47 add checkbutton -label [::msgcat::mc "Block messages from members"] \
		-indicatoron 1 -variable ::blockvar_members_priv($jid) -offvalue 0 -onvalue 1 \
		-command [namespace current]::refresh_list_priv -state $mstatepriv 
	$site_5_0.men47 add checkbutton -label [::msgcat::mc "Block messages from moderators"] \
		-indicatoron 1 -variable ::blockvar_moders_priv($jid) -offvalue 0 -onvalue 1 \
		-command [namespace current]::refresh_list_priv -state $mstatepriv
}

########################################################################################

proc bmucx::block_user_priv {xlib from} {
	set jid [::xmpp::jid::stripResource $from]	
	set chatid [chat::chatid $xlib $jid]

	if {[chat::is_groupchat $chatid]} {
		set my_jid [::chat::our_jid $chatid]

		if {$from == $my_jid || $from == $jid} {return 0}

		if {[info exists ::blockvar_priv($jid)] && $::blockvar_priv($jid)} {
			if {[catch {set role [::muc::get_role $xlib $from]}]} {return 0}
			if {$role == "moderator"} {
				if {[info exists ::blockvar_moders_priv($jid)] && $::blockvar_moders_priv($jid)} {
					return 1
				} else {return 0}
			}			
			if {[catch {set aff [::muc::get_affiliation $xlib $from]}]} {return 0}
			if {$aff == "member"} {
				if {[info exists ::blockvar_members_priv($jid)] && $::blockvar_members_priv($jid)} {
					return 1
				} else {return 0}	
			}			
			return 1
		} else {return 0}
	} else {return 0}	
}

proc bmucx::block_user {xlib from} {
	set jid [::xmpp::jid::stripResource $from]
	set chatid [chat::chatid $xlib $jid]

	if {[chat::is_groupchat $chatid]} {
		set my_jid [::chat::our_jid $chatid]

		if {$from == $my_jid || $from == $jid} {return 0}

		if {[info exists ::blockvar($jid)] && $::blockvar($jid)} {
			if {[catch {set role [::muc::get_role $xlib $from]}]} {return 0}
			if {$role == "moderator"} {
				if {[info exists ::blockvar_moders($jid)] && $::blockvar_moders($jid)} {
					return 1
				} else {return 0}
			}			
			if {[catch {set aff [::muc::get_affiliation $xlib $from]}]} {return 0}
			if {$aff == "member"} {
				if {[info exists ::blockvar_members($jid)] && $::blockvar_members($jid)} {
					return 1
				} else {return 0}	
			}			
			return 1
		} else {return 0}
	} else {return 0}	
}

proc bmucx::block_pub_messages {xlib from id type is_subject subject body err thread priority x} {
	if {$type == "groupchat" && [[namespace current]::block_user $xlib $from]} {
		return stop	
	}
}

proc bmucx::block_priv_messages {xlib from id type is_subject subject body err thread priority x} {
	if {$type != "groupchat" && [[namespace current]::block_user_priv $xlib $from]} {
		return stop	
	}
}

########################################################################################

proc bmucx::load {} {
	hook::add finload_hook [namespace current]::load_bmucx_list
	hook::add chat_create_conference_menu_hook [namespace current]::add_bmucx_menu 0
	hook::add process_message_hook [namespace current]::block_pub_messages 40
	hook::add process_message_hook [namespace current]::block_priv_messages 40
}

proc bmucx::unload {} {
	hook::remove finload_hook [namespace current]::load_bmucx_list
	hook::add chat_create_conference_menu_hook [namespace current]::add_bmucx_menu 0
	hook::add process_message_hook [namespace current]::block_pub_messages 40
	hook::add process_message_hook [namespace current]::block_priv_messages 40
}