obsolete/pa/message.c
bc9193a5
 #include "pa_mod.h"
 #include "message.h"
553fbaa1
 #include "../../id.h"
 #include "../../parser/parse_from.h"
bc9193a5
 #include <cds/sstr.h>
 
553fbaa1
 #include <xcap/msg_rules.h>
 
 static int xcap_get_msg_rules(str *uid, 
45a11963
 		msg_rules_t **dst, str *filename,
626094a5
 		struct sip_msg *m)
553fbaa1
 {
 	xcap_query_params_t xcap;
 	int res;
 	/* str u; */
 	
 	/* get only presentity name, not whole uri
 	 * can't use parse_uri because of absence 
 	 * of protocol specification ! */
 	/* if (get_user_from_uri(uri, &u) != 0) u = *uri; */
 
 	memset(&xcap, 0, sizeof(xcap));
626094a5
 	if (fill_xcap_params) fill_xcap_params(m, &xcap);
45a11963
 	res = get_msg_rules(uid, filename, &xcap, dst);
553fbaa1
 	return res;
 }
 
 static int get_sender_uri(struct sip_msg* _m, str* uri)
 {
 	struct sip_uri puri;
 	int res = 0;
 	
8c245a86
 	if (parse_headers(_m, HDR_FROM_F, 0) < 0) {
 		ERR("Error while parsing headers\n");
 		return -1;
 	}
 	
553fbaa1
 	uri->s = get_from(_m)->uri.s;
 	uri->len = get_from(_m)->uri.len;
 
 	if (parse_uri(uri->s, uri->len, &puri) < 0) {
 		LOG(L_ERR, "Error while parsing URI\n");
 		return -1;
 	}
 	
 	uri->s = puri.user.s;
 	if ((!uri->s) || (puri.user.len < 1)) {
 		uri->s = puri.host.s;
 		uri->len = puri.host.len;
 		res = 1; /* it is uri without username ! */
 	}
 	uri->len = puri.host.s + puri.host.len - uri->s;
 	return res;
 }
 
45a11963
 int authorize_message(struct sip_msg* _m, char* _filename, char*_st)
bc9193a5
 {
 	/* get and process XCAP authorization document */
 	/* may modify the message or its body */
 
553fbaa1
 	str uid = STR_NULL;
 	msg_rules_t *rules = NULL;
 	msg_handling_t mh = msg_handling_allow;
 	str sender_uri = STR_NULL;
45a11963
 	str tmp = STR_NULL;
 	str *filename = NULL;
 	int len;
553fbaa1
 	
 	get_sender_uri(_m, &sender_uri);
 	
 	if (get_to_uid(&uid, _m) < 0) {
 		ERR("get_to_uid failed\n");
 		/* enabled */
 		return 1;
 	}
 	
45a11963
 	if (_filename) {
 		len =strlen(_filename);
 		if (len > 0) {
 			tmp.s = _filename;
 			tmp.len = len;
 			filename = &tmp;
 		}
 	}
 		
 	if (xcap_get_msg_rules(&uid, &rules, filename, _m) < 0) {
553fbaa1
 		/* enabled */
03cd055b
 		DBG("get_msg_rules failed\n");
553fbaa1
 		return 1;
 	}
 	
 	if (get_msg_rules_action(rules, &sender_uri, &mh) != 0)
 		mh = msg_handling_allow;
 
 	free_msg_rules(rules);
bc9193a5
 
553fbaa1
 	switch (mh) {
 		case msg_handling_block: 
 			DBG("XCAP AUTH MESSAGE: block\n");
 			return -1;
 		case msg_handling_allow: 
 			DBG("XCAP AUTH MESSAGE: allow\n");
 			return 1;
 	}
bc9193a5
 	
 	return -1;
 }