# $Id: autosubscribe.tcl 163 2008-11-24 17:23:07Z kostix $
# Allows to automatically approve incoming presence subscription
# requests, optionally asking for the mutual presence subscription.
# Written by Konstantin Khomoutov <flatworm@users.sourceforge.net>
# See licence.terms for the terms of distribution.
# See README for details.

namespace eval autosubscribe {
	variable options

	custom::defgroup Plugins [::msgcat::mc "Plugins options."] -group Tkabber

	set group "Auto-subscribe"

	custom::defgroup $group \
		[::msgcat::mc "Automatically approves presence subscription\
			requests coming from remote entities.\
			Optionally can add the the requesting entity to your roster\
			and send mutual presence subscription request to it."] \
		-group Plugins

	custom::defvar options(enabled) 0 \
		[::msgcat::mc "Enables or disables automatic approving\
			of presence subscription requests."] \
		-group $group \
		-type boolean

if 0 {
	custom::defvar options(add_to_roster) 1 \
		[::msgcat::mc "Adds subscribed entities to your roster."] \
		-group $group \
		-type boolean

	custom::defvar options(roster_group) "Auto-subscribed" \
		[::msgcat::mc "Roster group to assign auto-subscribed\
			entities to."] \
		-group $group \
		-type string
}

	custom::defvar options(ask_subscription) 0 \
		[::msgcat::mc "Sends mutual presence subscription requst to\
			subscribed entities.\
			Note that this setting has no effect if adding subscribed\
			entities to the roster is disabled."] \
		-group $group \
		-type boolean

	custom::defvar options(reason) "Please approve my subscription" \
		[::msgcat::mc "Presence subscription reason used for requesting\
			mutual subscription requests."] \
		-group $group \
		-type string

	hook::add client_presence_hook \
		[namespace current]::subscribe 40
}

proc autosubscribe::subscribe {xlib from type x args} {
	variable options

	if {!([string equal $type subscribe] && $options(enabled))} return

if 0 {
	if {$options(add_to_roster)} {
		upvar 0 options(roster_group) roster_group
		lappend attrs jid $from
		if {$roster_group != ""} {
			lappend attrs group $roster_group
		}
		::xmpp::sendIQ $xlib set \
			-query [::xmpp::xml::create query \
				-xmlns jabber:iq:roster \
				-subelement [::xmpp::xml::create item -attrs $attrs]]
	}
}

	::xmpp::sendPresence $xlib -to $from -type subscribed

	if {$options(ask_subscription)} {
		::xmpp::sendPresence $xlib -to $from -type subscribe \
			-status $options(reason)
	}
}

