# Functions

`handle_subscription(domain)`

  This function processes SUBSCRIBE requests to "presence" and "presence.winfo" (see watcherinfo_notify parameter) events.

  Meaning of the parameters is as follows:

  - domain - This can be either "registrar" or "jabber".

  ## `handle_subscription` usage

      ...
      handle_subscription("registrar");
      ...

`handle_publish(domain)`

  Processes PUBLISH request and generates response to it.

`target_online(domain)`

  This function returns 1 if the target (using get_to_uid on current message) is online, -1 otherwise.

`check_subscription_status(status)`

  This function returns 1 if last subscription is in status given by parameter, -1 otherwise. The `status` can have values:

  pending

    Subscription was created but no status information will be sent to the watcher.

  active

    Subscription was established and all presence information will be sent to the watcher.

  terminated

    Subscription was terminated.

`store_winfo(domain)`

  This function stores data about currently created subscription into database for later dumping in the form of watcherinfo notification. It should be called after handle_subscription during processing SUBSCRIBE request.

`dump_stored_winfo(domain, event_package)`

  This function tries to send stored watcherinfo data in the context of existing watcherinfo subscription. It uses get_to_uid as target for notification. If there is no watcherinfo subscription to such presentity, no information is sent.

  If the client responds with 2xx on generated NOTIFY request, the stored information is removed from database.

  ## `target_online`, `store_winfo`, `dump_stored_winfo` usage

      ...
      if (method=="SUBSCRIBE") {
      	if (!t_newtran()) {
      		sl_reply_error();
      		break;
      	};

      	if (handle_subscription("registrar")) {; # uses uid from AVP (get_to_uid)
      		if (@msg.event=~"presence\.winfo") {
      			log(1, "subscription to watcherinfo\n");
      			dump_stored_winfo("registrar", "presence");
      			log(1, "OFFLINE AUTH: watcherinfo dumped\n");
      		}
      		else {
      			if ((@msg.event=~"presence") &&	(check_subscription_status("pending"))) {
      				log(1, "pending subscription to presence\n");
      				# if offline user and new "pending" subscription
      				if (!target_online("registrar") && (@to.tag=="")) {
      					log(1, "OFFLINE AUTH: storing for offline user\n");
      					store_winfo("registrar");
      				}
      			}
      		}
      	}
      	break;
      };
      ...

`authorize_message(filename)`

  This function reads and processes IM authorization data and returns 1 if message can be accepted by destination user. If there is no IM authorization document on given XCAP server, it returns 1 too (MESSAGE is enabled by default). If the sender is blocked by recipient rules, this function returns -1. For more details about authorization documents see .

  Filename parameter specifies the name of file on XCAP server which contains message authorization rules. It is im-rules.xml by default.

  ## `authorize_message` usage

      ...
      if (method=="MESSAGE") {
      	if (authorize_message("im-rules.xml")) {
      		if (!lookup("location")) {
      			sl_reply("404", "Not Found");
      			break;
      		};
      		if (!t_relay()) { sl_reply_error(); };
      	}
      	else {
      		sl_reply("403", "Forbidden");
      	}
      	break;
      }
      ...