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

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

debug=9          # 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=5070
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_mod.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"
loadmodule "../sip_router/modules/pike/pike.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 )

# -- pike params --
# no more than minimum 5 or maximum 5*3 msg per 10 secs - only for sms
modparam("pike","sampling_time_unit",60)
modparam("pike","reqs_density_per_unit",5)
modparam("pike","removel_latency",30)

# -- jabber params --
modparam("jabber","db_url","mysql://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",17)
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;
	};

	# messages too large are denied
	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" ))
	{
		sl_send_reply("403","Forbidden");
		log("SER:Forbidden request: wrong src_ip or req_uri\n");
		break;
	};

	# we are not interested in non-MESSAGE requests
	if (!method=="MESSAGE")
	{
		sl_send_reply("501","Not Implemented");
		break;
	};

	# error occured ...
	if (! t_newtran())
	{
		sl_reply_error();
		break;
	};

	# do what you want to do -  first, it's for SMS or JABBER ?
	if ( search("To:.*@icq\.iptel\.org")
			| search("To:.*@msn\.iptel\.org")
			| search("To:.*@aim\.iptel\.org")
			| search("To:.*@yahoo\.iptel\.org") )
	{
		### ----- JABBER GATEWAY ------
		log("MESSAGE received -> sending as JABBER\n");
		if (jab_send_message())
		{
			if (!t_reply("202","Accepted"))
			{
				# if replying failed, retry statelessly
				sl_reply_error();
			};
		}else{
			if (!t_reply("502","Bad gateway - IM error"))
			{
				# if replying failed, retry statelessly
				sl_reply_error();
			};
		};
		break; # End of Jabber
	};

	### ------ SMS GATEWAY --------
	# let's block ips that send a lot of request!!
	if (!pike_check_req() )
	{
		break;
	};
	# SMS expects only the numbers as follows +<int> <area> <nr>
	if (!(uri=~"sip:+[^0][0-9]{3}"))
	{
		log("SER: invalid number format!!\n");
		if (!t_reply("502","Bad gateway - invalid number"))
		{
			# if replying failed, retry statelessly
			sl_reply_error();
		};
		break;
	};
	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 (!t_reply("202", "yes sir, SMS sent over"))
		{
			# if replying failed, retry statelessly
			sl_reply_error();
		};
	} else {
		if (!t_reply("502", "Bad gateway - SMS error"))
		{
			# if replying failed, retry statelessly
			sl_reply_error();
		};
		break;
	};
}