### ----- IM Gateway [SMS+Jabber] config file for 'bat.iptel.org' ----  

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

debug=3          # debug level (cmd line: -dddddddddd)
fork=yes
#fork=no
log_stderror=no     # (cmd line: -E)
#log_stderror=yes   # (cmd line: -E)
check_via=yes       # (cmd. line: -v)
dns=on              # (cmd. line: -r)
rev_dns=yes         # (cmd. line: -R)
port=5080
children=2

# advertise IP address in Via (as opposed to advertising DNS name
# which is annoying for downstream servers and some phones can
# not handle DNS at all)
listen=195.37.77.100

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

loadmodule "../sip_router/modules/sl/sl.so"
loadmodule "../sip_router/modules/print/print.so"
loadmodule "../sip_router/modules/tm/tm.so"
loadmodule "../sip_router/modules/maxfwd/maxfwd.so"
loadmodule "../sip_router/modules/sms/sms.so"
loadmodule "../sip_router/modules/textops/textops.so"
loadmodule "../sip_router/modules/mysql/mysql.so"
loadmodule "../sip_router/modules/jabber/jabber.so"

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

# -- sms params --
modparam("sms","modems","Falcom [d=/dev/ttyS0;b=9600;p=9254;m=new;l=10;r=2]")
modparam("sms","networks","D1[c=491710765000;m=10]")
modparam("sms","links","Falcom[D1]")
modparam("sms","domain","iptel.org")
modparam("sms","max_sms_parts",3)
modparam("sms","use_contact",1)

# -- tm params --
modparam("tm", "fr_timer", 10 )
modparam("tm", "fr_inv_timer", 10 )
modparam("tm", "wt_timer", 10 )

# -- jabber params --
modparam("jabber","db_url","sql://s2jgw:47s2jgw11@127.0.0.1/sip_jab")
modparam("jabber","jaddress","bat.iptel.org")
modparam("jabber","jport",5222)
modparam("jabber","workers",2)
modparam("jabber","max_jobs",10)
modparam("jabber","delay_time",15)
modparam("jabber","cache_time",1800)


# -------------------------  request routing logic -------------------

# main routing logic

route{

	# filter too old messages
	log("LOG: Checking maxfwd\n");
	if (!mf_process_maxfwd_header("10")) {
		log("LOG: Too many hops\n");
		sl_send_reply("483","Too Many Hops");
		break;
	};

	if ((search("To:.*@icq\.iptel\.org")) || (search("To:.*@icq\.bat\.iptel\.org"))
	 || (search("To:.*@msn\.iptel\.org")) || (search("To:.*@msn\.bat\.iptel\.org"))
	 || (search("To:.*@aim\.iptel\.org")) || (search("To:.*@aim\.bat\.iptel\.org"))
	 || (search("To:.*@yahoo\.iptel\.org")) || (search("To:.*@yahoo\.bat\.iptel\.org")))
	{
		### ----- JABBER GATEWAY ------
		if (! t_newtran())
		{
			# retransmit whatever we have
			# it's useless to do any retransmision, because we haven't
			# sent any statefull reply. (bogdan)
			#t_retransmit_reply();
			break;
		} else {
			if (method=="MESSAGE")
			{
				log("MESSAGE received -> sending as JABBER\n");
				if (jab_send_message())
				{
					sl_send_reply("202","Accepted");
				}else{
					sl_send_reply("502","Bad gateway");
				};
			}else{
				log("NON_Message request received for JABBER gateway->dropt!\n");
				sl_send_reply("501","Not implemented");
			};
			# transaction conclude it -- junk it now (it will
			# stay there until WAIT timer hits)
			t_release();
		};
		t_unref();
	} else {
		### ----- SMS GATEWAY ------
		if (len_gt( max_len )) {
			sl_send_reply("513", "Riesengross -- Message too large");
			break;
		};

		# accept only req coming from iptel.org
		if (!src_ip==195.37.77.101 |
		!( uri=~"iptel.org" | uri=~"195\.37\.77\.100" ))
		{
			log("SER:Forbidden request: wrong src_ip or req_uri\n");
			sl_send_reply("403","Forbidden");
			break;
		};

		#accept only MESSAGE requests
		if (!method=="MESSAGE")
		{
			sl_send_reply("501","Not Implemented");
			break;
		};

		# SMS expects the numbers as follows <int> <area> <nr>
		# align numbering to it
		/*
		if (uri=~"sip:001" ) {
			strip(2);
			prefix("49");
		} else if (uri=~"sip:\+491") {
			strip(1);
		} else if (uri=~"sip:000491") {
			strip(3);
		} else {
			sl_send_reply("403", "SMS only to German 01* networks");
			break;
		};
		*/

		if (! t_newtran())
		{
			# retransmit whatever we have
			# it's useless to do any retransmision, because we haven't
			# sent any statefull reply. (bogdan)
			#t_retransmit_reply();
			break;
		} else {
			# do what you want to do
			if (sms_send_msg_to_net("D1"))
			{
				# for sending replies, we woun't use the statefull
				# function because it is not able to add the Contact
				# header (in fact to add any rpl_lump :-( )
				# things went well, send ok upstream
				if (!sl_send_reply("202", "yes sir, SMS sent over"))
				{
					# if replying failed, retry statelessly
					sl_reply_error();
				};
			} else {
				if (!sl_send_reply("502", "Bad gateway - SMS error"))
				{
					# if replying failed, retry statelessly
					sl_reply_error();
				};
			};
			# transaction conclude it -- junk it now (it will
			# stay there until WAIT timer hits)
			t_release();
		};
		t_unref();
	};
}