#
# $Id$
#
# welcome message for new subscribers; based on exec
#

# ----------- global configuration parameters ------------------------

debug=3          # debug level (cmd line: -dddddddddd)
# must be yes since REGISTER processing causes an INVITE to be sent,
# which needs to be processed by another process
fork=yes
children=4

# debugging
log_stderror=yes # (cmd line: -E)

mhomed=yes

fifo="/tmp/ser_fifo"

# ------------------ module loading ----------------------------------

# Uncomment this if you want to use SQL database
loadmodule "modules/mysql/mysql.so"

loadmodule "modules/sl/sl.so"
loadmodule "modules/tm/tm.so"
loadmodule "modules/rr/rr.so"
loadmodule "modules/maxfwd/maxfwd.so"
loadmodule "modules/usrloc/usrloc.so"
loadmodule "modules/registrar/registrar.so"

loadmodule "modules/exec/exec.so"

# ----------------- setting module-specific parameters ---------------

# -- usrloc params --

modparam("usrloc", "db_mode",   2)
modparam("usrloc", "db_url", "mysql://ser:heslo@192.168.2.16/ser" )
# -------------------------  request routing logic -------------------

# main routing logic

route{

	# initial sanity checks -- messages with
	# max_forwards==0, or excessively long requests
	if (!mf_process_maxfwd_header("10")) {
		sl_send_reply("483","Too Many Hops");
		break;
	};
	if (len_gt( max_len )) {
		sl_send_reply("513", "Message too big");
		break;
	};

	# we record-route all messages -- to make sure that
	# subsequent messages will go through our proxy; that's
	# particularly good if upstream and downstream entities
	# use different transport protocol
	if (method=="INVITE") record_route();	# 1=loose routing

	# loose-route processing
	if (loose_route()) {
		t_relay();
		break;
	};

	log(1, "RR processing completed\n");

	# if the request is for other domain use UsrLoc
	# (in case, it does not work, use the following command
	# with proper names and addresses in it)
	if (uri==myself) {

		if (method=="REGISTER") {
			# save location before initiating welcome
			save("location");
			# welcome message
			route(3);
			break;
		};

		# native SIP destinations are handled using our USRLOC DB
		if (!lookup("location")) {
			sl_send_reply("404", "Not Found");
			break;
		};
	};
	t_relay();
}

# welcome message -- if a REGISTER succeeded, look if it is the first-time;
# if so, initiate click-to-dial transaction to set up call to an announcement
# server; edit the config values first to make it work
route[3] {
	if (!exec_msg('
		# config: 
		# --announcement server URI
		ANS="sip:7170@iptel.org"
		# --SIP domain
		DOMAIN="192.168.2.16"
		# ctd
		CTD=${HOME}/sip_router/examples/ctd.sh
		# ------------------------------------
		# check if first time ...
		SIP_UID=`echo $SIP_HF_TO | sed -e "s/^.*sip:\([a-zA-Z0-9_\.]*\)@.*$/\1/g"`
		QUERY="select flag from subscriber 
				where username=\"$SIP_UID\";
			update subscriber set flag=\"x\" 
				where username=\"$SIP_UID\" ";
			mysql -Bsuser -pheslo -e "$QUERY" ser| grep "^x$" > /dev/null
		# ... if so, c-t-d to announcement server
		if [ "$?" -ne 0 ] ; then
		# flag was not set to x yet -- first-time registration;
		# initiate a call from telephone of the user to an announcement server
			$CTD "sip:$SIP_UID@$DOMAIN" "$ANS" > /dev/null 2>&1
		fi
	')) {
		log(1, "welcome exec failed\n");
	}
}