nua_glib.c 71.5 KB
Newer Older
1 2 3 4 5 6
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005-2006 Nokia Corporation.
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
7 8
 * Copyright (C) 2005 Collabora Ltd.
 *
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@file nua_glib.c Interface towards libsofia-sip-ua.
 * 
 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
 * @author Rob Taylor <rob.taylor@collabora.co.uk>
 * @author Pekka Pessi <pekka.pessi@nokia.com>
31
 * @author Martti Mela <martti.mela@nokia.com>
32 33 34 35 36 37 38 39 40 41 42 43 44
 */

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

#include <glib.h>

#include "sofia-sip/nua_glib.h"
#include "nua_glib_priv.h"
45
#include "nua_glib_op.h"
46 47 48
#include "nua_glib_marshal.h"

/*=============== Class and Object init ===============*/
49
G_DEFINE_TYPE(NuaGlib, nua_glib, G_TYPE_OBJECT);
50 51 52 53
  
/*signal enum*/
enum
{
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  NGSIG_CALL_FORKED = 1,
  NGSIG_INCOMING_INVITE,
  NGSIG_INCOMING_REINVITE,
  NGSIG_CALL_STATE_CHANGED,
  NGSIG_INCOMING_ACTIVE,
  NGSIG_CALL_TERMINATED,
  NGSIG_INCOMING_PRACK,
  NGSIG_INCOMING_BYE,
  NGSIG_INCOMING_CANCEL,
  NGSIG_INCOMING_MESSAGE,
  NGSIG_INCOMING_INFO,
  NGSIG_INCOMING_REFER,
  NGSIG_INCOMING_NOTIFY,

  NGSIG_ERROR,
  NGSIG_SHUTDOWN,

  NGSIG_REGISTER_ANSWERED,
  NGSIG_UNREGISTER_ANSWERED,
  NGSIG_PUBLISH_ANSWERED,
  NGSIG_INVITE_ANSWERED,
  NGSIG_BYE_ANSWERED,
76
  NGSIG_CANCEL_ANSWERED,
77 78 79 80 81 82 83 84 85 86 87
  NGSIG_MESSAGE_ANSWERED,
  NGSIG_INFO_ANSWERED,
  NGSIG_REFER_ANSWERED,
  NGSIG_SUBSCRIBE_ANSWERED,
  NGSIG_UNSUBSCRIBE_ANSWERED,
  NGSIG_NOTIFY_ANSWERED,
  NGSIG_OPTIONS_ANSWERED,

  NGSIG_AUTH_REQUIRED,

  NGSIG_LAST_SIGNAL
88 89
};

90 91
/*=============== Static variables ===============*/

92
static guint signals[NGSIG_LAST_SIGNAL] = {0};
93 94 95

enum
{
96 97 98
  PROP_ADDRESS = 1,
  PROP_PASSWORD,
  PROP_CONTACT,
99 100
  PROP_PROXY,
  PROP_REGISTRAR,
101
  PROP_STUN_SERVER,
102 103 104 105 106
  LAST_PROPERTY
};

static GObjectClass *parent_class=NULL;  

107 108
/*=============== Private function declarations ===============*/

109
static int sof_init(NuaGlibPrivate *priv, const char *contact);
110
static void priv_submit_authlist(NuaGlibOp *op);
111 112
static void priv_oper_handle_auth (NuaGlib *self, NuaGlibOp *op, sip_t const *sip, tagi_t *tags);
static void priv_oper_check_response_for_auth(NuaGlib *self, NuaGlibOp *op, int status, sip_t const *sip, tagi_t *tags);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static void sof_callback(nua_event_t event,
		  int status, char const *phrase,
		  nua_t *nua, NuaGlib *self,
		  nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		  tagi_t tags[]);

static void sof_r_register(int status, char const *phrase,
		      nua_t *nua, NuaGlib *self,
		      nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		      tagi_t tags[]);

static void sof_r_unregister(int status, char const *phrase,
		      nua_t *nua, NuaGlib *self,
		      nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		      tagi_t tags[]);

static void sof_r_publish(int status, char const *phrase,
		   nua_t *nua, NuaGlib *self,
		   nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		   tagi_t tags[]);

static void sof_r_invite(int status, char const *phrase,
		  nua_t *nua, NuaGlib *self,
		  nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		  tagi_t tags[]);
static void sof_i_fork(int status, char const *phrase,
		nua_t *nua, NuaGlib *self,
		nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		tagi_t tags[]);

static void sof_i_invite(nua_t *nua, NuaGlib *self,
		  nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		  tagi_t tags[]);

static void sof_i_state(int status, char const *phrase, 
		 nua_t *nua, NuaGlib *self,
		 nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		 tagi_t tags[]);

static void sof_i_active(nua_t *nua, NuaGlib *self,
		    nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		    tagi_t tags[]);

static void sof_i_terminated(int status, char const *phrase, 
		      nua_t *nua, NuaGlib *self,
		      nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		      tagi_t tags[]);

static void sof_i_prack(nua_t *nua, NuaGlib *self,
		 nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		 tagi_t tags[]);

static void sof_r_bye(int status, char const *phrase, 
166 167 168
		      nua_t *nua, NuaGlib *self,
		      nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		      tagi_t tags[]);
169 170 171 172 173 174 175 176 177

static void sof_i_bye(nua_t *nua, NuaGlib *self,
		 nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		 tagi_t tags[]);

static void sof_i_cancel(nua_t *nua, NuaGlib *self,
		    nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		    tagi_t tags[]);

178 179 180 181 182
static void sof_r_cancel(int status, char const *phrase, 
			 nua_t *nua, NuaGlib *self,
			 nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
			 tagi_t tags[]);

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static void sof_r_message(int status, char const *phrase,
		   nua_t *nua, NuaGlib *self,
		   nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		   tagi_t tags[]);

static void sof_i_message(nua_t *nua, NuaGlib *self,
		   nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		   tagi_t tags[]);

static void sof_r_info(int status, char const *phrase,
		nua_t *nua, NuaGlib *self,
		nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		tagi_t tags[]);

static void sof_i_info(nua_t *nua, NuaGlib *self,
		nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		tagi_t tags[]);

static void sof_r_refer(int status, char const *phrase,
		   nua_t *nua, NuaGlib *self,
		   nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		   tagi_t tags[]);

static void sof_i_refer(nua_t *nua, NuaGlib *self,
		   nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		   tagi_t tags[]);

static void sof_r_subscribe(int status, char const *phrase,
		     nua_t *nua, NuaGlib *self,
		     nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		     tagi_t tags[]);

static void sof_r_unsubscribe(int status, char const *phrase,
		       nua_t *nua, NuaGlib *self,
		       nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		       tagi_t tags[]);

static void sof_r_notify(int status, char const *phrase,
		   nua_t *nua, NuaGlib *self,
		   nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		   tagi_t tags[]);

static void sof_i_notify(nua_t *nua, NuaGlib *self,
		    nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		    tagi_t tags[]);

static void sof_r_options(int status, char const *phrase,
		   nua_t *nua, NuaGlib *self,
		   nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		   tagi_t tags[]);

static void sof_r_shutdown(int status, char const *phrase, 
		    nua_t *nua, NuaGlib *self,
		    nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		    tagi_t tags[]);

static void sof_r_get_params(int status, char const *phrase,
		      nua_t *nua, NuaGlib *self,
		      nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
		      tagi_t tags[]);

static void sof_i_error(nua_t *nua, NuaGlib *self, nua_handle_t *nh, NuaGlibOp *op, 
		 int status, char const *phrase,
		 tagi_t tags[]);

/*=============== Function definitions ===============*/

void final_shutdown(NuaGlib *self)
{
  g_object_unref(self);
}
254 255 256

static GObject *
nua_glib_constructor (GType                  type,
257 258
		      guint                  n_construct_properties,
		      GObjectConstructParam *construct_properties)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
{
  GObject *obj;
  int res = 0;
  GSource *gsource;

  {
    /* Invoke parent constructor.
     * this calls our init, and then set_property with any
     * CONSTRUCT params
     */
    NuaGlibClass *klass;
    klass = NUA_GLIB_CLASS (g_type_class_peek (NUA_GLIB_TYPE));
    parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
    obj = parent_class->constructor (type,
                                     n_construct_properties,
                                     construct_properties);
  }
  
  NuaGlib *self = NUA_GLIB(obj);

  /* create a su event loop and connect it to glib */
  self->priv->root = su_root_source_create(self);
  assert(self->priv->root);
  gsource = su_root_gsource(self->priv->root);
  assert(gsource);
  g_source_attach(gsource, NULL);

  /*check address has been set*/
  g_assert(strcmp(self->priv->address, "no-address-set")!=0);

289
  res = sof_init(self->priv, self->priv->contact);
290 291 292

  if (res != -1) {
    self->priv->nua = nua_create(self->priv->root, 
293 294
				 sof_callback, self,
				 NUTAG_SOA_NAME("default"),
295 296
				 TAG_IF(self->priv->stun_server,
					STUNTAG_SERVER(self->priv->stun_server)),
297 298
				 TAG_IF(self->priv->contact,
					NUTAG_URL(self->priv->contact)),
299
				 /* XXX: SOATAG_CAPS_SDP_STR(local_caps), */
300
				 TAG_NULL());
301 302 303 304 305 306 307
    if (self->priv->nua) {

      nua_set_params(self->priv->nua,
                     TAG_IF(self->priv->proxy, NUTAG_PROXY(self->priv->proxy)),
                     TAG_IF(self->priv->registrar, NUTAG_REGISTRAR(self->priv->registrar)),
                     NUTAG_ENABLEMESSAGE(1),
                     NUTAG_ENABLEINVITE(1),
308 309
                     /* NUTAG_SESSION_TIMER(s_e), */
                     /* NUTAG_MIN_SE(min_se), */
310 311
                     SOATAG_AF(SOA_AF_IP4_IP6),
                     SIPTAG_FROM_STR(self->priv->address),
312
                     /* NUTAG_CERTIFICATE_DIR(getenv("SIPCERTDIR")),*/
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
                     TAG_NULL());
      nua_get_params(self->priv->nua, TAG_ANY(), TAG_NULL());

      self->priv->init=TRUE;
      g_signal_connect_after(self, "shutdown", (GCallback)final_shutdown, NULL);
    }
  }
  return obj;
}

static void
nua_glib_init(NuaGlib *self)
{
  self->priv =  g_new0(NuaGlibPrivate, 1);
 /* initialize sofia su OS abstraction layer */
  su_init();
  su_home_init(self->priv->home);
}

static void
nua_glib_dispose(GObject *obj)
{
  NuaGlib *self = NUA_GLIB(obj);

  if (self->priv->init)
  {
    g_free((gpointer)self->priv->contact);

    nua_shutdown(self->priv->nua);

    self->priv->init = FALSE;
    /*now hold a ref to ourselves that we drop when distruction is complete*/
    g_object_ref (obj);
346 347

    /* XXX: some start/stop nua funtions to do su_init/de_init?  su_deinit();*/
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  }

  /* Chain up to the parent class */
  G_OBJECT_CLASS (parent_class)->dispose (obj);
}

static void
nua_glib_finalize (GObject *obj)
{
  NuaGlib *self = NUA_GLIB(obj);
  su_root_break(self->priv->root);
  nua_destroy(self->priv->nua);
  su_root_destroy(self->priv->root), self->priv->root = NULL;
  su_home_deinit(self->priv->home);
  g_free(self->priv);

  /* Chain up to the parent class */
  G_OBJECT_CLASS (parent_class)->finalize (obj); 
}

368 369 370 371 372
static
void nua_glib_set_property(GObject      *object,
			   guint         property_id,
			   const GValue *value,
			   GParamSpec   *pspec)
373 374 375
{
  NuaGlib *self = (NuaGlib*) object;

376 377 378 379
#define STORE_PARAM(s, x)			\
  g_free ((gpointer)(s)->priv->x);		\
  (s)->priv->x = g_value_dup_string (value)

380 381 382 383 384
  switch (property_id) {
  case PROP_ADDRESS: {
    if (self->priv->nua)
    {
      nua_set_params(self->priv->nua,
385
                     SIPTAG_FROM_STR(self->priv->address),
386 387 388 389 390 391 392 393 394
                     TAG_NULL());
      nua_get_params(self->priv->nua, TAG_ANY(), TAG_NULL());
    }
    else /*setting in constructor*/
    {
      self->priv->address = su_strdup(self->priv->home, g_value_get_string (value));
    }
    break;
  }
395 396 397 398 399 400 401 402
  case PROP_PASSWORD: {
    STORE_PARAM(self, password);
    break;
  }
  case PROP_CONTACT: {
    STORE_PARAM(self, contact);
    break;
  }
403
  case PROP_PROXY: {
404
    STORE_PARAM(self, proxy);
405 406 407 408 409 410 411 412 413
    if (self->priv->nua)
    {
      nua_set_params(self->priv->nua,
                     NUTAG_PROXY(self->priv->proxy),
                     TAG_NULL());
    }
    break;
  }
  case PROP_REGISTRAR: {
414
    STORE_PARAM(self, registrar);
415 416 417 418 419 420 421 422
    if (self->priv->nua)
    {
      nua_set_params(self->priv->nua,
                     NUTAG_REGISTRAR(self->priv->registrar),
                     TAG_NULL());
    }
    break;
  }
423 424
  case PROP_STUN_SERVER: {
    STORE_PARAM(self, stun_server);
425 426
    break;
  }
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
 default:
    /* We don't have any other property... */
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object,property_id,pspec);
    break;
  }
}

static void
nua_glib_get_property (GObject      *object,
                        guint         property_id,
                        GValue       *value,
                        GParamSpec   *pspec)
{
  NuaGlib *self = (NuaGlib *) object;

  switch (property_id) {
  case PROP_ADDRESS: {
    g_value_set_string (value, self->priv->address);
    break;
  }
447 448 449 450 451 452 453 454
  case PROP_PASSWORD: {
    g_value_set_string (value, self->priv->password);
    break;
  }
  case PROP_CONTACT: {
    g_value_set_string (value, self->priv->contact);
    break;
  }
455 456 457 458 459 460 461 462
  case PROP_PROXY: {
    g_value_set_string (value, self->priv->proxy);
    break;
  }
  case PROP_REGISTRAR: {
    g_value_set_string (value, self->priv->registrar);
    break;
  }
463 464
  case PROP_STUN_SERVER: {
    g_value_set_string (value, self->priv->stun_server);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    break;
  }
  default:
    /* We don't have any other property... */
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object,property_id,pspec);
    break;
  }
}


static void
nua_glib_class_init (NuaGlibClass *nua_glib_class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS(nua_glib_class);
  GParamSpec *param_spec;
 
  gobject_class->constructor = nua_glib_constructor;
  gobject_class->dispose = nua_glib_dispose;
  gobject_class->finalize = nua_glib_finalize;

  gobject_class->set_property = nua_glib_set_property;
  gobject_class->get_property = nua_glib_get_property;
  
  param_spec = g_param_spec_string("address",
                                   "NuaGlib construction property",
490
                                   "The address-of-record for this UA (e.g. 'sip:first.surname@myprovider.com')",
491 492 493 494 495 496
                                   "no-address-set", /*default value*/
                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
  g_object_class_install_property (gobject_class,
                                   PROP_ADDRESS,
                                   param_spec);

497
  param_spec = g_param_spec_string("password",
498
                                   "NuaGlib construction property",
499
                                   "SIP account password",
500
                                   NULL, /*default value*/
501
                                   G_PARAM_READWRITE);
502
  g_object_class_install_property (gobject_class,
503
                                   PROP_PASSWORD,
504 505
                                   param_spec);

506
  param_spec = g_param_spec_string("contact",
507
                                   "NuaGlib construction property",
508
                                   "local bind interface (e.g. 'sip:0.0.0.0:*') [optional]",
509 510 511
                                   NULL, /*default value*/
                                   G_PARAM_READWRITE );
  g_object_class_install_property (gobject_class,
512
                                   PROP_CONTACT,
513 514
                                   param_spec);

515
  param_spec = g_param_spec_string("proxy",
516
                                   "NuaGlib construction property",
517
                                   "SIP outgoing proxy URI (e.g. 'sip:sipproxy.myprovider.com') [optional]",
518 519 520
                                   NULL, /*default value*/
                                   G_PARAM_READWRITE );
  g_object_class_install_property (gobject_class,
521
                                   PROP_PROXY,
522 523
                                   param_spec);

524 525 526 527 528 529 530 531
  param_spec = g_param_spec_string("registrar",
                                   "NuaGlib construction property",
                                   "SIP registrar URI (e.g. 'sip:sip.myprovider.com') [optional]",
                                   NULL, /*default value*/
                                   G_PARAM_READWRITE );
  g_object_class_install_property (gobject_class,
                                   PROP_REGISTRAR,
                                   param_spec);
532

533
  param_spec = g_param_spec_string("stun-server",
534
                                   "NuaGlib construction property",
535
                                   "STUN server address (FQDN or dotted-decimal) [optional]",
536 537 538
                                   "", /*default value*/
                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
  g_object_class_install_property (gobject_class,
539
                                   PROP_STUN_SERVER,
540 541
                                   param_spec);

542 543 544 545 546 547 548 549 550 551
  /**
   * NuaGlib::call-forked:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the call that was forked
   * @status: SIP status of fork (see SIP RFC)
   * @phrase: Reason for fork
   *
   * Emitted when an outgoing call has been forked.
   * This is when an INVITE request is answered with multiple 200 responses.
   */
552
  signals[NGSIG_CALL_FORKED] =
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
   g_signal_new("call-forked",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);


  /**
   * NuaGlib::incoming-invite:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call
   * @display: the display name of the invite recipient
   * @url: the url of the invite recipient
   * @subject: the subject of the invite (can be NULL)
   *
   * Emitted when an call invite is received
   * Should be answered with nua_glib_answer or nua_glib_decline
   */
572
  signals[NGSIG_INCOMING_INVITE] =
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
   g_signal_new("incoming-invite",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING,
    G_TYPE_NONE, 4, G_TYPE_POINTER , G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::incoming-reinvite:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   *
   * Emitted when an call invite is received for a call already in progress
   * Usually represents
   */
588
  signals[NGSIG_INCOMING_REINVITE] =
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
   g_signal_new("incoming-reinvite",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::call-state-changed:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @audio: #NuaGlibMediaActive describing audio availiablity
   * @video: #NuaGlibMediaActive describing video availiablity
   * @image: #NuaGlibMediaActive describing image availiablity
   * @chat: #NuaGlibMediaActive describing chat availiablity
   * @l_sdp: String containing any new local caps as SDP, can be NULL
   * @r_sdp: String containing any new remote cap as SDP, can be NULL
   *
   * Emitted when call state changes.
   */
 
610
  signals[NGSIG_CALL_STATE_CHANGED] =
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
   g_signal_new("call-state-changed",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_INT_INT_INT_STRING_STRING,
    G_TYPE_NONE, 7, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,
                    G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::incoming-active:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   *
   * Emitted when a call goes active.
   */
 
627
  signals[NGSIG_INCOMING_ACTIVE] =
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
   g_signal_new("incoming-active",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::call-terminated:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing callA
   * @status: SIP status of termination (see SIP RFC)
   *
   * This will be emitted after a call has been terminated.
   * 
   * A call is terminated, when
   * 1) an error response (300..599) is sent to an incoming initial INVITE 
   * 2) a reliable response (200..299 or reliable preliminary response) to an 
   * incoming initial INVITE is not acknowledged with ACK or PRACK 
   * 3) BYE is received or sent
   *
   * Any references you hold to @op should be dropped at this point
   */
 
652
  signals[NGSIG_CALL_TERMINATED] =
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
   g_signal_new("call-terminated",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT,
    G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);

  /**
   * NuaGlib::incoming-prack:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @rack: pointer to RAck that was PRACKed FIXME!!!
   *
   * Emitted when a PRACK is received
   */
 
669
  signals[NGSIG_INCOMING_PRACK] =
670 671 672 673 674 675 676 677 678 679 680 681 682
   g_signal_new("incoming-prack",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_POINTER,
    G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-bye:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   *
   */
683
  signals[NGSIG_INCOMING_BYE] =
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
   g_signal_new("incoming-bye",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-cancel:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of cancel (see SIP RFC)
   * @phrase: Reason for cancel 
   *
   * Emitted when incoming INVITE has been cancelled or no ACK has 
   * been received for an accepted call.
   */
 
702
  signals[NGSIG_INCOMING_CANCEL] =
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
   g_signal_new("incoming-cancel",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-message:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call
   * @display: the display name of the invite recipient
   * @url: the url of the invite recipient
   * @subject: the subject of the invite (can be NULL)
   *
   * Emitted when a message is received
   */
720
  signals[NGSIG_INCOMING_MESSAGE] =
721 722 723 724
   g_signal_new("incoming-message",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
725 726
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING_STRING,
    G_TYPE_NONE, 5, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
727 728 729 730 731 732 733 734 735 736 737

  /**
   * NuaGlib::incoming-info:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call
   * @display_name: the display name of the info sender
   * @address: the address of the info sender
   * @subject: the subject of the invite (can be NULL)
   *
   * Emitted when an INFO message is received
   */
738
  signals[NGSIG_INCOMING_INFO] =
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
   g_signal_new("incoming-info",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING,
    G_TYPE_NONE, 4, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::incoming-refer:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation for the call that originated this refer
   * @display: the display name of the REFERER 
   * @address: the address of the REFERER 
   * @refer_address: the address you have been requested to refer to
   * @op2: a pointer to an operation to use to respond to this refer, NULL if 
   *       REFER url was not a SIP uri.
   *
   * Emitted when a REFER message is received
   * You should reply to this with #nua_glib_notify to cancel it or 
   * #nua_glib_follow_refer, and probably close * the call that referred 
   * you to follow it.
   */
761
  signals[NGSIG_INCOMING_REFER] =
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
   g_signal_new("incoming-refer",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING_POINTER,
    G_TYPE_NONE, 5, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
                    G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-notify:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call, NULL if 
   *      this was a rogue notify
   * @event: name of event, can be NULL
   * @content-type: content type of event, can be NULL
   * @payload: payload of event
   *
   * Emitted when an NOTIFY message is received
   */
 
782
  signals[NGSIG_INCOMING_NOTIFY] =
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
   g_signal_new("incoming-notify",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING,
    G_TYPE_NONE, 4, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::error:
   * @nua_glib: the object that received the signal
   * @status: SIP status for error (see SIP RFC)
   * @phrase: Reason for error
   *
   * If you get this, run around waving your hands and screaming.
   */
 
799
  signals[NGSIG_ERROR] =
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
   g_signal_new("error",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__INT_STRING,
    G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::register-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of REGISTER answer (see SIP RFC)
   * @phrase: Reason for REGISTER answer 
   *
   * if status >=300 and not 401 or 407, you should drop any references you hold 
   * on @op
   */
817
  signals[NGSIG_REGISTER_ANSWERED] =
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
   g_signal_new("register-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::unregister-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of REGISTER answer (see SIP RFC)
   * @phrase: Reason for REGISTER answer 
   *
   * if status >=300 and not 401 or 407, you should drop any references you hold 
   * on @op
   */
 
836
  signals[NGSIG_UNREGISTER_ANSWERED] =
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
   g_signal_new("unregister-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::publish-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of PUBLISH answer (see SIP RFC)
   * @phrase: Reason for PUBLISH answer 
   *
   * if status >=300 and not 401 or 407, you should drop any references you hold 
   * on @op
   */
 
855
  signals[NGSIG_PUBLISH_ANSWERED] =
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
   g_signal_new("publish-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::invite-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of INVITE answer (see SIP RFC)
   * @phrase: Reason for INVITE answer 
   *
   */
871
  signals[NGSIG_INVITE_ANSWERED] =
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
   g_signal_new("invite-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::bye-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of bye answer (see SIP RFC)
   * @phrase: Reason for bye answer 
   *
   */
887
  signals[NGSIG_BYE_ANSWERED] =
888 889 890 891 892 893 894
   g_signal_new("bye-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
  /**
   * NuaGlib::cancel-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of CANCEL answer (see SIP RFC)
   * @phrase: Reason for CANCEL answer 
   *
   */
  signals[NGSIG_CANCEL_ANSWERED] =
   g_signal_new("cancel-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

911 912 913 914 915 916 917 918
  /**
   * NuaGlib::message-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of message answer (see SIP RFC)
   * @phrase: Reason for message answer
   *
   */
919
  signals[NGSIG_MESSAGE_ANSWERED] =
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
   g_signal_new("message-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::info-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of INFO answer (see SIP RFC)
   * @phrase: Reason for INFO answer
   *
   */
935
  signals[NGSIG_INFO_ANSWERED] =
936 937 938 939 940 941 942 943 944 945 946 947 948 949
   g_signal_new("info-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::refer-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of REFER answer (see SIP RFC)
   * @phrase: Reason for REFER answer 
   *
950 951
   * XXX: should we pass the even header as param (see nua_refer()
   * documentation and SIPTAG_EVENT())
952
   */
953
  signals[NGSIG_REFER_ANSWERED] =
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
   g_signal_new("refer-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::subscribe-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of SUBSCRIBE answer (see SIP RFC)
   * @phrase: Reason for SUBSCRIBE answer
   *
   */
969
  signals[NGSIG_SUBSCRIBE_ANSWERED] =
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
   g_signal_new("subscribe-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::unsubscribe-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of UNSUBSCRIBE answer (see SIP RFC)
   * @phrase: Reason for UNSUBSCRIBE answer
   *
   *You should remove any references you have to @op after receiving this signal
   */
986
  signals[NGSIG_UNSUBSCRIBE_ANSWERED] =
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
   g_signal_new("unsubscribe-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::notify-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of NOTIFY answer (see SIP RFC)
   * @phrase: Reason for NOTIFY answer 
   *
   */
1002
  signals[NGSIG_NOTIFY_ANSWERED] =
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
   g_signal_new("notify-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::options-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of options answer (see SIP RFC)
   * @phrase: Reason for options answer 
   *
   */
1018
  signals[NGSIG_OPTIONS_ANSWERED] =
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
   g_signal_new("options-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::shutdown:
   * @nua_glib: the object that received the signal
   *
   * The sip stack is shutting down, 
   * drop all #NuaGlibOp references you hold
   * and unref the object it was emited on
   */
1034
 signals[NGSIG_SHUTDOWN] =
1035 1036 1037 1038 1039 1040 1041
   g_signal_new("shutdown",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__VOID,
    G_TYPE_NONE, 0, NULL);

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
  /**
   * NuaGlib::auth_required:
   * @nua_glib: the object that received the signal
   * @op: pointer to the related operation
   * @method: string describing the method (RFC2617: "Basic", "Digest", ..)
   * @realm: realm of the challenge (RFC2617) 
   *
   * The sip stack is shutting down, 
   * drop all #NuaGlibOp references you hold
   * and unref the object it was emited on
   */
 signals[NGSIG_AUTH_REQUIRED] =
   g_signal_new("auth-required",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
}

/* ====================================================================== */
int sof_init(NuaGlibPrivate *priv, const char *contact)
{
  priv->name = "UA";
  priv->contact = g_strdup(contact);

  su_root_threading(priv->root, 0);

  return 0;
}


static void 
sof_callback(nua_event_t event,
                    int status, char const *phrase,
                    nua_t *nua, NuaGlib *self,
                    nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
                    tagi_t tags[])
{
  g_return_if_fail(self);

  switch (event) {
  case nua_r_shutdown:    
    sof_r_shutdown(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_get_params:    
    sof_r_get_params(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_register:
    sof_r_register(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_unregister:
    sof_r_unregister(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_options:
    sof_r_options(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_invite:
    sof_r_invite(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_fork:
    sof_i_fork(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_i_invite:
    sof_i_invite(nua, self, nh, op, sip, tags);
    return;

  case nua_i_prack:
    sof_i_prack(nua, self, nh, op, sip, tags);
    return;

  case nua_i_active:
    sof_i_active(nua, self, nh, op, sip, tags);
    return;

  case nua_i_state:
    sof_i_state(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_i_terminated:
    sof_i_terminated(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_bye:
    sof_r_bye(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_bye:
    sof_i_bye(nua, self, nh, op, sip, tags);
    return;

1140 1141 1142 1143
  case nua_r_cancel:
    sof_r_cancel(status, phrase, nua, self, nh, op, sip, tags);
    return;

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
  case nua_r_message:
    sof_r_message(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_message:
    sof_i_message(nua, self, nh, op, sip, tags);
    return;

  case nua_r_info:
    sof_r_info(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_info:
    sof_i_info(nua, self, nh, op, sip, tags);
    return;

  case nua_r_refer:
    sof_r_refer(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_refer:
    sof_i_refer(nua, self, nh, op, sip, tags);
    return;
     
  case nua_r_subscribe:
    sof_r_subscribe(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_unsubscribe:
    sof_r_unsubscribe(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_publish:
    sof_r_publish(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_notify:
    sof_r_notify(status, phrase, nua, self, nh, op, sip, tags);
    return;
     
  case nua_i_notify:
    sof_i_notify(nua, self, nh, op, sip, tags);
    return;

  case nua_i_cancel:
    sof_i_cancel(nua, self, nh, op, sip, tags);
    return;

  case nua_i_error:
    sof_i_error(nua, self, nh, op, status, phrase, tags);
    return;

  default:
    break;
  }

  if (status > 100)
    g_warning("%s: unknown event %d: %03d %s\n", 
           self->priv->name, event, status, phrase);
  else
    g_warning("%s: unknown event %d\n", self->priv->name, event);

  tl_print(stderr, "", tags);
}

/* ====================================================================== */

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/**
 * Helper function called from all response callback handler.
 * Checks whether authentication is needed, and handles it if 
 * require. Marks succesful authentications as completed.
 */
static void priv_oper_check_response_for_auth(NuaGlib *self, NuaGlibOp *op, int status, sip_t const *sip, tagi_t *tags)
{
  if (status == 401 || status == 407) {
    priv_oper_handle_auth(self, op, sip, tags);
  }
  else if (status >= 200 && status <= 299){
    if (op->op_authstate != opa_none && 
	op->op_authstate != opa_auth_ok) {
      op->op_authstate = opa_auth_ok;
      SU_DEBUG_3(("%s: authorization of %s (%p) was succesful\n", 
1226
		  self->priv->name, sip_method_name(op->op_method, "<NONE>"), op));
1227 1228 1229 1230
    }
  }
}

1231 1232 1233
/**
 * Handles authentication challenge for operation 'op'.
 */
1234
static void priv_oper_handle_auth (NuaGlib *self, NuaGlibOp *op, sip_t const *sip, tagi_t *tags)
1235 1236 1237
{
  sip_www_authenticate_t const *wa = sip->sip_www_authenticate;
  sip_proxy_authenticate_t const *pa = sip->sip_proxy_authenticate;
1238 1239 1240
  sip_from_t const *sipfrom = sip->sip_from;
  const char *realm = NULL;

1241 1242 1243 1244 1245 1246 1247
  enter;

  tl_gets(tags, 
          SIPTAG_WWW_AUTHENTICATE_REF(wa),
          SIPTAG_PROXY_AUTHENTICATE_REF(pa),
          TAG_NULL());

1248
  SU_DEBUG_3(("%s: %s (%p) was unauthorized\n", self->priv->name, sip_method_name(op->op_method, "<NONE>"), op));
1249

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
  /* step: the initial challenge */
  if (op->op_authstate == opa_none) {
    if (wa) {
      sl_header_print(stdout, "Server auth: %s\n", (sip_header_t *)wa);
      realm = msg_params_find(wa->au_params, "realm=");  
      nua_glib_auth_add(self, op, wa->au_scheme, realm, sipfrom->a_url->url_user, self->priv->password);
    }
    if (pa) {
      sl_header_print(stdout, "Proxy auth: %s\n", (sip_header_t *)pa);
      realm = msg_params_find(pa->au_params, "realm=");  
      nua_glib_auth_add(self, op, pa->au_scheme, realm, sipfrom->a_url->url_user, self->priv->password);
    }

    op->op_authstate = opa_try_derived;
    priv_submit_authlist(op);
1265
  }
1266 1267 1268
  /* step: a new challenge and local credentials updated since last attempt */
  else if (op->op_authstate == opa_retry) {
    priv_submit_authlist(op);
1269
  }
1270 1271 1272
  /* step: a new challenge, ask for matching credentials */
  else if (op->op_authstate == opa_try_derived) {
    g_message("Requesting for additional authentication credentials %s(%s)",
1273
	      self->priv->name, sip_method_name(op->op_method, "<NONE>"));
1274
    op->op_authstate = opa_auth_req;
1275
    g_signal_emit(self, signals[NGSIG_AUTH_REQUIRED], 0, op, sip_method_name(op->op_method, "<NONE>"), realm);
1276 1277 1278
  }
  /* step: a new challenge, ask for matching credentials */
  else if (op->op_authstate == opa_auth_req) {
1279
    g_message("Failed auth for %s by %s",
1280
	      sip_method_name(op->op_method, "<NONE>"), self->priv->name);
1281
    op->op_authstate = opa_failed;
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
  }
}


/**
 * nua_glib_op_owner:
 *
 * get the owning NuaGlib for a given NuaGlibOp
 */
NuaGlib* 
nua_glib_op_owner(NuaGlibOp *op)
{
  return op->op_parent;
}

sip_method_t
nua_glib_op_method_type(NuaGlibOp *op)
{
  return op->op_method;
}
/**
 * nua_glib_op_set_data:
 * @op: op to attach data to
 * @data: data to attach
 *
 * Attach an applciation specific blob of data to a NuaGlibOp
 * The application is in charge of deleting the data when it 
 * removes any internal references to the @op. When it has done 
 * so, it should call this function with @data set to NULL
 * Failing to do so will cause an assertion.
 */
void
nua_glib_op_set_data(NuaGlibOp *op, gpointer data)
{
  op->data = data;
}

/**
 * nua_glib_op_get_data:
 * @op: op to get data from
 *
 * Get an application specific blob of data from a NuaGlibOp
 * Returns: attached data
 */
gpointer
nua_glib_op_get_data(NuaGlibOp *op)
{
  return op->data ;
}

/**
 * nua_glib_op_get_identity:
 * @op: op to get data from
 *
 * Get the identity of an operation
 * This is the contents of To: when initiating, From: when receiving.
 * Returns: the identity
 */
const gchar *
nua_glib_op_get_identity(NuaGlibOp *op)
{
  return op->op_ident;
}


static void 
sof_i_error(nua_t *nua, NuaGlib *self, nua_handle_t *nh, NuaGlibOp *op, 
            int status, char const *phrase,
            tagi_t tags[])
{
1352
  g_signal_emit(self, signals[NGSIG_ERROR], status, phrase);
1353 1354 1355 1356 1357 1358 1359
}

/**
 * nua_glib_invite:
 * @destination sip address to invite
 * @local_sdp an SDP blob describing local capabilites
 * 
1360
 * Invites 'destination_uri' to a new call.
1361
 *
1362 1363 1364 1365 1366 1367
 * Incomplete sessions can be hung-up with nua_glib_cancel(). Complete or
 * incomplete calls can be hung-up with nua_glib_bye()
 *
 * @see nua_invite() (libsofia-sip-ua/nua)
 *
 * Return value: operation descriptor for this operation, NULL if failure
1368
 */
1369
NuaGlibOp *nua_glib_invite(NuaGlib *self, const char *destination_uri, const char *local_sdp)
1370 1371 1372
{
  NuaGlibOp *op;

1373
  op = nua_glib_op_create(self, sip_method_invite, destination_uri, TAG_END());
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

  /* SDP O/A note: 
   *  - pass media information to nua_invite() in the 
   *    SOATAG_USER_SDP_STR() tag
   *  - see also: sof_i_state() and nua_glib_answer()
   */ 

  if (op) {
    nua_invite(op->op_handle,
               SOATAG_USER_SDP_STR(local_sdp),
               TAG_END());

    op->op_callstate |= opc_sent;
    return op;
  }
1389

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
  return NULL;
}

/*
 * invite response handler
 */
static void 
sof_r_invite(int status, char const *phrase, 
             nua_t *nua, NuaGlib *self,
             nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
             tagi_t tags[])
{
  if (status >= 300) {
    op->op_callstate &= ~opc_sent;
1404
    priv_oper_check_response_for_auth(self, op, status, sip, tags);
1405
  }
1406
  g_signal_emit(self, signals[NGSIG_INVITE_ANSWERED],0, op, status, phrase);
1407 1408 1409
}

/*
1410
 * Stack callback: incoming call-forked
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
 *
 * Releases forked calls
 */
static void 
sof_i_fork(int status, char const *phrase,
           nua_t *nua, NuaGlib *self,
           nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
           tagi_t tags[])
{
  nua_handle_t *nh2 = NULL;

1422
  g_signal_emit(self, signals[NGSIG_CALL_FORKED], 0, status, phrase, op);
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  
  g_warning("%s: call fork: %03d %s\n", self->priv->name, status, phrase);

  /* We just release forked calls. */
  tl_gets(tags, NUTAG_HANDLE_REF(nh2), TAG_END());
  g_return_if_fail(nh2);

  nua_bye(nh2, TAG_END());
  nua_handle_destroy(nh2);
}

/*
1435
 * Stack callback: incoming invite handler
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455