#
# simple quick-start config script
# Please refer to the Core CookBook at https://www.kamailio.org/wikidocs/
# for an explanation of possible statements, functions and parameters.
#

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

debug=3      # debug level (cmd line: -dddddddddd)
fork=yes
log_stderror=no    # (cmd line: -E)
children=4

listen=127.0.0.1 
port=5059

dns=no
rev_dns=no

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

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

loadmodule "sl/sl.so"
loadmodule "rr/rr.so"
loadmodule "maxfwd/maxfwd.so"
loadmodule "usrloc/usrloc.so"
loadmodule "pv/pv.so"
loadmodule "registrar/registrar.so"
loadmodule "textops/textops.so"
loadmodule "auth/auth.so"
loadmodule "auth_db/auth_db.so"
loadmodule "uri_db/uri_db.so"
loadmodule "xlog/xlog.so"

# ----------------- setting module-specific parameters ---------------
# -- usrloc params --

# Uncomment this if you want to use SQL database 
# for persistent storage and comment the previous line
modparam("usrloc", "db_mode", 3)
modparam("usrloc", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")

# -- auth params --
# Uncomment if you are using auth module
#
modparam("auth_db", "calculate_ha1", 1)

# If you set "calculate_ha1" parameter to 1 (which true in this config), 
# uncomment also the following parameter)
#
modparam("auth_db", "password_column", "password")

# -- rr params --
# add value to ;lr param to make some broken UAs happy
modparam("rr", "enable_full_lr", 1)


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

# main routing logic

route{

	if (!mf_process_maxfwd_header("10")) {
		sl_send_reply("483", "Too Many Hops");
		exit;
	}

	if (is_method("ACK")) {
		xlog("ACK\n");
		sl_send_reply("200", "OK");
		exit;
	}

	if (uri == myself) {
		if(method==REGISTER) {
			if (!www_authorize("localhost", "subscriber")) {
				www_challenge("localhost", "0");
			} else {
				if(check_to() && check_from()) {
					sl_send_reply("200", "OK");
				}
			}
		} else {
			if (!proxy_authorize("localhost", "subscriber")) {
				proxy_challenge("localhost", "0");
			} else {
				if(check_to() && check_from() && does_uri_exist()) {
					sl_send_reply("200", "OK");
				}
			}
		}
		exit;
	}
}
