#
# configuration for TurboSIP testing
#
# $Id$
#


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

loadmodule "modules/sl/sl.so"
loadmodule "modules/print/print.so"
loadmodule "modules/tm/tm.so"
loadmodule "modules/rr/rr.so"
loadmodule "modules/maxfwd/maxfwd.so"
loadmodule "modules/mysql/mysql.so"
loadmodule "modules/usrloc/usrloc.so"
loadmodule "modules/auth/auth.so"


route{
	# filter local stateless ACK generated by authentication of mf replies
	sl_filter_ACK();

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

        # Do some strict routing here
	rewriteFromRoute();

        if (uri=~"mail\.iptel\.org" | uri=~":6060") {
		log("Request is for voicemail\n");
		forward("fox.iptel.org", 6060);
	} else {
		if (uri=~"iptel\.org" | uri=~"195\.37\.77\.101") {
			log("Request is for iptel.org\n");	
	
			if (method=="REGISTER") {
				log("Request is REGISTER\n");
				if (!authorize("iptel.org")) {
	            			log("REGISTER has no credentials, sending challenge\n");
                    			challenge("iptel.org");
					break;
				};
	        		log("REGISTER is authorized, saving location\n");
				save_contact("location");
				break;
			};

			if (uri=~"sip:9040@.*") {
				setuser("jiri");
			};

			if (uri=~"sip:[0-9]+@.*") {
				if (uri=~"sip:001795061546@.*" | uri=~"sip:[79][0-9][0-9][0-9]@.*") {
					log("Free PSTN\n");
				} else {
					if (!(src_ip==192.168.99.3) & !(authorize("iptel.org"))) {
						challenge("iptel.org");
						break;
					};
					
					if (uri=~"sip:0[1-9]+@.*") {
						if (!is_in_group("local")) {
							sl_send_reply("403", "Toodle Noodle...");
							break;
						};
					} else if (uri=~"sip:00[1-9][0-9]+@.*") {
						if (!is_in_group("ld")) {
							sl_send_reply("403", "Toodle Noodle...");
							break;
						};
					} else if (uri=~"sip:000[1-9][0-9]+@.*") {
						if (!is_in_group("int")) {
							sl_send_reply("403", "Toodle Noodle...");
							break;
						};
					} else {
						sl_send_reply("403", "Toodle Noodle...");
						break;
					};
				};
				addRecordRoute();
				rewritehostport("192.168.99.3:5060");
			} else {
				# native SIP destinations
				if (!lookup_contact("location")) {
					log("Unable to lookup contact, sending 404\n");
					sl_send_reply("404", "Not Found");
					break;
				};
			};
                } else {
			if (!(src_ip==192.168.99.3) & !(authorize("iptel.org"))) {
				challenge("iptel.org");
				break;
			};
		};

		t_relay();
       };
}