int sal_subscribe_accept(SalOp *op){
	osip_message_t *msg=NULL;
	eXosip_lock();
	eXosip_insubscription_build_answer(op->tid,202,&msg);
	if (msg==NULL){
		ms_error("Fail to build answer to subscribe.");
		eXosip_unlock();
		return -1;
	}
	if (op->base.contact){
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
		osip_message_set_contact(msg,op->base.contact);
	}
	eXosip_insubscription_send_answer(op->tid,202,msg);
	eXosip_unlock();
	return 0;
}
void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){	
	/*workaround a bug in eXosip: incoming SUBSCRIBES within dialog with expires: 0 are
	 recognized as new incoming subscribes*/
	SalOp *op=sal_find_in_subscribe_by_call_id(sal,ev->request->call_id);
	if (op){
		osip_header_t *h;
		osip_message_header_get_byname(ev->request,"expires",0,&h);
		if (h && h->hvalue && atoi(h->hvalue)==0){
			ms_warning("This susbscribe is not a new one but terminates an old one.");
			ev->did=op->did;
			ev->nid=op->nid;
			sal_exosip_subscription_closed(sal,ev);
		}else {
			osip_message_t *msg=NULL;
			ms_warning("Probably a refresh subscribe");
			eXosip_lock();
			eXosip_insubscription_build_answer(ev->tid,202,&msg);
			eXosip_insubscription_send_answer(ev->tid,202,msg);
			eXosip_unlock();
		}
	}else _sal_exosip_subscription_recv(sal,ev);
}
int sal_subscribe_decline(SalOp *op){
	eXosip_lock();
	eXosip_insubscription_send_answer(op->tid,401,NULL);
	eXosip_unlock();
	return 0;
}
int
eXosip_insubscription_automatic (eXosip_event_t * evt)
{
  eXosip_dialog_t *jd = NULL;
  eXosip_notify_t *jn = NULL;

  osip_header_t *event_header;
  if (evt->did <= 0 || evt->nid <= 0)
    return OSIP_BADPARAMETER;
  if (evt->request == NULL)
    return OSIP_BADPARAMETER;

  eXosip_notify_dialog_find (evt->did, &jn, &jd);
  if (jd == NULL || jn == NULL)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: No incoming subscription here?\n"));
      return OSIP_NOTFOUND;
    }

  osip_message_header_get_byname (evt->request, "event", 0, &event_header);
  if (event_header == NULL || event_header->hvalue == NULL)
    {
      eXosip_insubscription_send_answer (evt->tid, 400, NULL);
      return OSIP_SUCCESS;
    }

  /* this event should be handled internally */
  if (osip_strcasecmp (event_header->hvalue, "dialog") == 0)
    {
      /* send 200 ok to SUBSCRIBEs */

      if (evt->type == EXOSIP_IN_SUBSCRIPTION_NEW)
        {
          osip_message_t *answer;
          int i;

          i = eXosip_insubscription_build_answer (evt->tid, 202, &answer);
          if (i == 0)
            {
              i = eXosip_insubscription_send_answer (evt->tid, 202, answer);
            }
          if (i != 0)
            {
              i = eXosip_insubscription_send_answer (evt->tid, 400, NULL);
              return OSIP_SUCCESS;
            }

          /* send initial notify */
          i =
            _eXosip_insubscription_auto_send_notify (evt->did,
                                                     EXOSIP_SUBCRSTATE_ACTIVE,
                                                     PROBATION);
          if (i != 0)
            {
              /* delete subscription... */
              return OSIP_SUCCESS;
            }
        }
  } else
    {
      if (evt->type == EXOSIP_IN_SUBSCRIPTION_NEW)
        {
          eXosip_insubscription_send_answer (evt->tid, 489, NULL);
        }
    }

  return OSIP_SUCCESS;
}