# Initial idea and Copyright (c) New MUC moderation menu plugin (Jet <megaxbit@xmpp.ru>)
# Modifications, bugfixes etc. (c) Pretty MUC menu plugin 17-02-2010 (ancestor <ancestor@jabber.org>)
# This plugin provides an improved version of Tkabber MUC moderation menu.

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

    if {![::plugins::is_registered prettymucmenu]} {
	::plugins::register prettymucmenu \
			    -namespace [namespace current] \
			    -source [info script] \
			    -description [::msgcat::mc "New MUC moderation menu."] \
			    -loadcommand [namespace code load] \
			    -unloadcommand [namespace code unload]
	return
    }
	set mcloc(Owner)		[::msgcat::mc "Owner"]
	set mcloc(Admin)		[::msgcat::mc "Admin"]
	set mcloc(Member)		[::msgcat::mc "Member"]
	set mcloc(None)			[::msgcat::mc "None"]
	set mcloc(Moderator)		[::msgcat::mc "Moderator"]
	set mcloc(Participant)		[::msgcat::mc "Participant"]
	set mcloc(Visitor)		[::msgcat::mc "Visitor"]
	set mcloc(Change_affiliation)	[::msgcat::mc "Change affiliation"]
	set mcloc(Change_role)		[::msgcat::mc "Change role"]
}

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

proc prettymucmenu::set_reason {type val down xlib jid reason reschatid} {
	::muc::change_item_attr $xlib $jid $type $val down $reason $reschatid
}

proc prettymucmenu::pre_proc {xlib jid reschatid oldval newval reason type} {
	switch -- $type {
		role			{set rval [::xmpp::muc::CompareRoles $oldval $newval]}
		affiliation		{set rval [::xmpp::muc::CompareAffiliations $oldval $newval]}
		default return
	}

	if {$rval > 0} {
		set pos down
	} elseif {$rval < 0} {
		set pos up
	} else return

	::muc::change_item_attr $xlib $jid $type $newval $pos $reason $reschatid
}

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

proc prettymucmenu::add_groupchat_user_menu_items_new {type m xlib jid} {
	variable users
    set group [::xmpp::jid::stripResource $jid]

    if {![::muc::is_compatible $group]} return

    set chatid [chat::chatid $xlib $group]

    if {![chat::is_groupchat $chatid]} {
	return
    }

    switch -- $type {
	chat { set reschatid [chat::chatid $xlib $jid] }
	roster { set reschatid $chatid }
    }
	
	$m add command -label [::msgcat::mc "Whois"] \
	-command [list muc::whois $xlib $jid $reschatid]
	$m add separator

	foreach r {none visitor participant moderator} {set mstate(role,$r) normal}
	foreach a {outcast none member admin owner} {set mstate(aff,$a) normal}

	set aff [::muc::get_affiliation $xlib $jid]
	set role [::muc::get_role $xlib $jid]
	
	set my_jid [::chat::our_jid $chatid]	
	set my_aff [::muc::get_affiliation $xlib $my_jid]
	set my_role [::muc::get_role $xlib $my_jid]
	
	if {$jid == $my_jid} {
		foreach s [array names mstate] {set mstate($s) disabled}
	}

	if {$aff == "owner" || $aff == "admin"} {
		foreach s [array names mstate role,*] {set mstate($s) disabled}
	}

	if {$my_role == "moderator"} {
		switch -- $my_aff {
			none {
				foreach s [array names mstate] {set mstate($s) disabled}
				if {$aff == "none" && $role != "moderator"} {
						set mstate(role,none) normal
						set mstate(role,visitor) normal
						set mstate(role,participant) normal
				}
				if {$role != "moderator"} {set mstate(role,none) normal}
			}
			member {
				foreach s [array names mstate] {set mstate($s) disabled}
				if {($aff == "none" || $aff == "member") && $role != "moderator"} {
						set mstate(role,none) normal
						set mstate(role,visitor) normal
						set mstate(role,participant) normal
				}
				if {$role != "moderator"} {set mstate(role,none) normal}
			}
			admin {
				if {$aff == "admin" || $aff == "owner"} {
					foreach s [array names mstate] {set mstate($s) disabled}
				}
				set mstate(aff,admin) disabled
				set mstate(aff,owner) disabled
			}
			owner {}			
			default {
				foreach s [array names mstate] {set mstate($s) disabled}
			}
		}
	} else {
		foreach s [array names mstate] {set mstate($s) disabled}
	}

	set mstate(role,$role) normal
	set mstate(aff,$aff) normal	
	
	$m add command -label [::msgcat::mc "Kick"] -state $mstate(role,none) \
	-command [list muc::change_item_attr $xlib $jid role none down "" $reschatid]

    $m add command -label [::msgcat::mc "Ban"] -state $mstate(aff,outcast) \
	-command [list muc::change_item_attr $xlib $jid affiliation outcast down "" $reschatid]
#-------------------------------------------------------------------------------------
	set ::affvar $aff	
	set am [menu $m.amuc -tearoff 0]
	$am add radiobutton -label $::plugins::prettymucmenu::mcloc(Owner) -state $mstate(aff,owner) \
		-value owner -variable affvar \
		-command [list ::plugins::prettymucmenu::pre_proc \
		$xlib $jid $reschatid $aff owner "" affiliation]		
	$am add radiobutton -label $::plugins::prettymucmenu::mcloc(Admin) -state $mstate(aff,admin) \
		-value admin -variable affvar \
		-command [list ::plugins::prettymucmenu::pre_proc \
		$xlib $jid $reschatid $aff admin "" affiliation]
	$am add radiobutton -label $::plugins::prettymucmenu::mcloc(Member) -state $mstate(aff,member) \
		-value member -variable affvar \
		-command [list ::plugins::prettymucmenu::pre_proc \
		$xlib $jid $reschatid $aff member "" affiliation]
	$am add radiobutton -label $::plugins::prettymucmenu::mcloc(None) -state $mstate(aff,none) \
		-value none -variable affvar \
		-command [list ::plugins::prettymucmenu::pre_proc \
		$xlib $jid $reschatid $aff none "" affiliation]
	$m add cascade -label $::plugins::prettymucmenu::mcloc(Change_affiliation) -menu $am 

	set ::rolevar $role	
	set rm [menu $m.rmuc -tearoff 0]
	$rm add radiobutton -label $::plugins::prettymucmenu::mcloc(Moderator) -state $mstate(role,moderator) \
		-value moderator -variable rolevar \
		-command [list ::plugins::prettymucmenu::pre_proc \
		$xlib $jid $reschatid $role moderator "" role]	
	$rm add radiobutton -label $::plugins::prettymucmenu::mcloc(Participant) -state $mstate(role,participant) \
		-value participant -variable rolevar \
		-command [list ::plugins::prettymucmenu::pre_proc \
		$xlib $jid $reschatid $role participant "" role]		
	$rm add radiobutton -label $::plugins::prettymucmenu::mcloc(Visitor) -state $mstate(role,visitor) \
		-value visitor -variable rolevar \
		-command [list ::plugins::prettymucmenu::pre_proc \
		$xlib $jid $reschatid $role visitor "" role]
	$m add cascade -label $::plugins::prettymucmenu::mcloc(Change_role) -menu $rm
}

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

proc prettymucmenu::load {} {
	hook::remove chat_create_user_menu_hook \
	  [list ::ifacetk::muc::add_groupchat_user_menu_items chat] 43
	hook::remove roster_create_groupchat_user_menu_hook \
	  [list ::ifacetk::muc::add_groupchat_user_menu_items roster] 39
	
	hook::add chat_create_user_menu_hook \
	  [list [namespace current]::add_groupchat_user_menu_items_new chat] 43
	hook::add roster_create_groupchat_user_menu_hook \
	  [list [namespace current]::add_groupchat_user_menu_items_new roster] 39
}

proc prettymucmenu::unload {} {
	hook::remove chat_create_user_menu_hook \
	  [list [namespace current]::add_groupchat_user_menu_items_new chat] 43
	hook::remove roster_create_groupchat_user_menu_hook \
	  [list [namespace current]::add_groupchat_user_menu_items_new roster] 39
	
	hook::add chat_create_user_menu_hook \
	  [list ::ifacetk::muc::add_groupchat_user_menu_items chat] 43
	hook::add roster_create_groupchat_user_menu_hook \
	  [list ::ifacetk::muc::add_groupchat_user_menu_items roster] 39
}