~ubuntu-branches/ubuntu/maverick/ekiga/maverick

« back to all changes in this revision

Viewing changes to src/endpoints/sip-endpoint.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Eugen Dedu, Eugen Dedu, Loic Minier
  • Date: 2008-09-27 10:00:00 UTC
  • mfrom: (1.1.8 upstream)
  • mto: (1.4.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20080927100000-l5k5werb6czr5b3h
Tags: 3.0.1-1
[ Eugen Dedu ]
* New version.  (Closes: #500089).
* Add our own changelog file in /usr/share/doc.
* Remove gnomemeeting transitional package.
* Discover new interfaces.  (Closes: #488199).
* Compile with dbus support.  (Closes: #467212).
* Numeric keypad inserts digits at correct position.  (Closes: #440159).
* Use libnotify upon call.  (Closes: #412604).
* Symlink identical GNOME help files, to reduce size.  (Closes: #505536).
* Explicitely build-depends on a few dev packages, even if they were
  pulled out anyway by the other dependencies.

[ Loic Minier ]
* Use clean:: instead of clean: in rules.
* Don't disable Uploaders: generation for control.in -> control generation
  in rules.
* Fix some tabs which were size 4 anyway.
* Generate a PO template during build by calling intltool-update -p in
  install; thanks Ubuntu and Martin Pitt; closes: #505535.
* Also let the -dbg depend on ${misc:Depends}.
* Cleanup rules; in particular, use dpkg-parsechangelog and honor
  distclean/clean failures, remove old clean rules, commented out stuff,
  gtk-only stuff.
* Pass -s to dh_* in binary-arch.
* Use debian/*.links and debian/*.manpages instead of symlink manually or
  passing files to dh_installman.
* Use ftp.gnome.org in copyright.
* Switch to quilt and fix target deps in the process; build-dep on quilt
  instead of dpatch; rename news.dpatch to 00_news.patch and refresh;
  replace 00list with series.
* Install autotools-dev config.guess and .sub after patching.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* Ekiga -- A VoIP and Video-Conferencing application
 
3
 * Copyright (C) 2000-2006 Damien Sandras
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software Foundation,
 
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
18
 *
 
19
 *
 
20
 * Ekiga is licensed under the GPL license and as a special exc_managertion,
 
21
 * you have permission to link or otherwise combine this program with the
 
22
 * programs OPAL, OpenH323 and PWLIB, and distribute the combination,
 
23
 * without applying the requirements of the GNU GPL to the OPAL, OpenH323
 
24
 * and PWLIB programs, as long as you do follow the requirements of the
 
25
 * GNU GPL for all the rest of the software thus combined.
 
26
 */
 
27
 
 
28
 
 
29
/*
 
30
 *                         sipendpoint.cpp  -  description
 
31
 *                         --------------------------------
 
32
 *   begin                : Wed 8 Dec 2004
 
33
 *   copyright            : (C) 2000-2006 by Damien Sandras
 
34
 *   description          : This file contains the SIP Endpoint class.
 
35
 *
 
36
 */
 
37
 
 
38
 
 
39
#include "config.h"
 
40
 
 
41
#include <algorithm>
 
42
#include <sstream>
 
43
 
 
44
#include "sip-endpoint.h"
 
45
 
 
46
#include "opal-bank.h"
 
47
#include "opal-call.h"
 
48
 
 
49
#include "presence-core.h"
 
50
#include "account-core.h"
 
51
#include "chat-core.h"
 
52
#include "personal-details.h"
 
53
#include "opal-account.h"
 
54
 
 
55
static void
 
56
presence_status_in_main (Ekiga::PresenceFetcher* fetcher,
 
57
                         std::string uri,
 
58
                         std::string presence,
 
59
                         std::string status)
 
60
{
 
61
  fetcher->presence_received.emit (uri, presence);
 
62
  fetcher->status_received.emit (uri, status);
 
63
}
 
64
 
 
65
static void
 
66
push_message_in_main (SIP::Dialect* dialect,
 
67
                      const std::string uri,
 
68
                      const std::string name,
 
69
                      const std::string msg)
 
70
{
 
71
  dialect->push_message (uri, name, msg);
 
72
}
 
73
 
 
74
static void
 
75
push_notice_in_main (SIP::Dialect* dialect,
 
76
                     const std::string uri,
 
77
                     const std::string name,
 
78
                     const std::string msg)
 
79
{
 
80
  dialect->push_notice (uri, name, msg);
 
81
}
 
82
 
 
83
namespace Opal {
 
84
 
 
85
  namespace Sip {
 
86
 
 
87
    class dialer : public PThread
 
88
    {
 
89
      PCLASSINFO(dialer, PThread);
 
90
 
 
91
  public:
 
92
 
 
93
      dialer (const std::string & uri, Opal::CallManager & _manager) 
 
94
        : PThread (1000, AutoDeleteThread), 
 
95
        dial_uri (uri),
 
96
        manager (_manager) 
 
97
      {
 
98
        this->Resume ();
 
99
      };
 
100
 
 
101
      void Main () 
 
102
        {
 
103
          PString token;
 
104
          manager.SetUpCall ("pc:*", dial_uri, token);
 
105
        };
 
106
 
 
107
  private:
 
108
      const std::string dial_uri;
 
109
      Opal::CallManager & manager;
 
110
    };
 
111
 
 
112
 
 
113
    class subscriber : public PThread
 
114
    {
 
115
      PCLASSINFO(subscriber, PThread);
 
116
 
 
117
  public:
 
118
      subscriber (const Opal::Account & _account,
 
119
                  Opal::Sip::EndPoint & _manager) 
 
120
        : PThread (1000, AutoDeleteThread),
 
121
        account (_account),
 
122
        manager (_manager) 
 
123
      {
 
124
        this->Resume ();
 
125
      };
 
126
 
 
127
      void Main () 
 
128
        {
 
129
          manager.Register (account);
 
130
        };
 
131
 
 
132
  private:
 
133
      const Opal::Account & account;
 
134
      Opal::Sip::EndPoint & manager;
 
135
    };
 
136
  };
 
137
};
 
138
 
 
139
 
 
140
 
 
141
/* The class */
 
142
Opal::Sip::EndPoint::EndPoint (Opal::CallManager & _manager, 
 
143
                               Ekiga::ServiceCore & _core, 
 
144
                               unsigned _listen_port)
 
145
:   SIPEndPoint (_manager),
 
146
    manager (_manager), 
 
147
    core (_core),
 
148
    presence_core (*(dynamic_cast<Ekiga::PresenceCore *> (core.get ("presence-core")))),
 
149
    runtime (*(dynamic_cast<Ekiga::Runtime *> (core.get ("runtime")))),
 
150
    account_core (*(dynamic_cast<Ekiga::AccountCore *> (core.get ("account-core"))))
 
151
{
 
152
  Ekiga::ChatCore* chat_core;
 
153
 
 
154
  protocol_name = "sip";
 
155
  uri_prefix = "sip:";
 
156
  listen_port = _listen_port;
 
157
 
 
158
  chat_core = dynamic_cast<Ekiga::ChatCore *> (core.get ("chat-core"));
 
159
  dialect = new SIP::Dialect (core, sigc::mem_fun (this, &Opal::Sip::EndPoint::send_message));
 
160
  chat_core->add_dialect (*dialect);
 
161
 
 
162
  /* Timeouts */
 
163
  SetAckTimeout (PTimeInterval (0, 32));
 
164
  SetPduCleanUpTimeout (PTimeInterval (0, 1));
 
165
  SetInviteTimeout (PTimeInterval (0, 6));
 
166
  SetNonInviteTimeout (PTimeInterval (0, 6));
 
167
  SetRetryTimeouts (500, 4000);
 
168
  SetMaxRetries (8);
 
169
 
 
170
  /* Start listener */
 
171
  set_listen_port (listen_port);
 
172
 
 
173
  /* Update the User Agent */
 
174
  SetUserAgent ("Ekiga/" PACKAGE_VERSION);
 
175
 
 
176
  /* Ready to take calls */
 
177
  manager.AddRouteEntry("sip:.* = pc:<db>");
 
178
  manager.AddRouteEntry("pc:.* = sip:<da>");
 
179
 
 
180
  /* NAT Binding */
 
181
  SetNATBindingRefreshMethod (SIPEndPoint::EmptyRequest);
 
182
 
 
183
  Ekiga::PersonalDetails *details = dynamic_cast<Ekiga::PersonalDetails *> (_core.get ("personal-details"));
 
184
  if (details)
 
185
    publish (*details);
 
186
}
 
187
 
 
188
 
 
189
Opal::Sip::EndPoint::~EndPoint ()
 
190
{
 
191
  delete dialect;
 
192
}
 
193
 
 
194
 
 
195
bool Opal::Sip::EndPoint::populate_menu (Ekiga::Contact &contact,
 
196
                                         const std::string uri,
 
197
                                         Ekiga::MenuBuilder &builder)
 
198
{
 
199
  return menu_builder_add_actions (contact.get_name (), uri, builder);
 
200
}
 
201
 
 
202
 
 
203
bool Opal::Sip::EndPoint::populate_menu (Ekiga::Presentity& presentity,
 
204
                                         const std::string uri,
 
205
                                         Ekiga::MenuBuilder & builder)
 
206
{
 
207
  return menu_builder_add_actions (presentity.get_name (), uri, builder);
 
208
}
 
209
 
 
210
 
 
211
bool Opal::Sip::EndPoint::menu_builder_add_actions (const std::string& fullname,
 
212
                                                    const std::string& uri,
 
213
                                                    Ekiga::MenuBuilder & builder)
 
214
{
 
215
  bool populated = false;
 
216
 
 
217
 
 
218
  std::list<std::string> uris;
 
219
  std::list<std::string> accounts;
 
220
 
 
221
  if (!(uri.find ("sip:") == 0 || uri.find (":") == string::npos))
 
222
    return false;
 
223
 
 
224
  if (uri.find ("@") == string::npos) {
 
225
 
 
226
    Opal::Bank *bank = dynamic_cast<Opal::Bank*> (core.get ("opal-account-store"));
 
227
 
 
228
    if (bank) {
 
229
 
 
230
      for (Opal::Bank::iterator it = bank->begin ();
 
231
           it != bank->end ();
 
232
           it++) {
 
233
 
 
234
        if (it->get_protocol_name () == "SIP" && it->is_active ()) {
 
235
 
 
236
          std::stringstream uristr;
 
237
          std::string str = uri;
 
238
 
 
239
          for (unsigned i = 0 ; i < str.length() ; i++) {
 
240
 
 
241
            if (str [i] == ' ' || str [i] == '-') {
 
242
              str.erase (i,1);
 
243
              i--;
 
244
            }
 
245
          }
 
246
 
 
247
          if (str.find ("sip:") == string::npos)
 
248
            uristr << "sip:" << str;
 
249
          else
 
250
            uristr << str;
 
251
 
 
252
          uristr << "@" << it->get_host ();
 
253
 
 
254
          uris.push_back (uristr.str ());
 
255
          accounts.push_back (it->get_name ());
 
256
        }
 
257
      }
 
258
    }
 
259
  }
 
260
  else {
 
261
    uris.push_back (uri);
 
262
    accounts.push_back ("");
 
263
  }
 
264
 
 
265
  std::list<std::string>::iterator ita = accounts.begin ();
 
266
  for (std::list<std::string>::iterator it = uris.begin ();
 
267
       it != uris.end ();
 
268
       it++) {
 
269
 
 
270
    std::stringstream call_action;
 
271
    std::stringstream transfer_action;
 
272
    if (!(*ita).empty ()) {
 
273
      call_action << _("Call") << " [" << (*ita) << "]";
 
274
      transfer_action << _("Transfer") << " [" << (*ita) << "]";
 
275
    }
 
276
    else {
 
277
      call_action << _("Call");
 
278
      transfer_action << _("Transfer");
 
279
    }
 
280
 
 
281
    if (0 == GetConnectionCount ())
 
282
      builder.add_action ("call", call_action.str (),
 
283
                          sigc::bind (sigc::mem_fun (this, &Opal::Sip::EndPoint::on_dial), (*it)));
 
284
    else 
 
285
      builder.add_action ("transfer", transfer_action.str (),
 
286
                          sigc::bind (sigc::mem_fun (this, &Opal::Sip::EndPoint::on_transfer), (*it)));
 
287
 
 
288
    ita++;
 
289
  }
 
290
 
 
291
  ita = accounts.begin ();
 
292
  for (std::list<std::string>::iterator it = uris.begin ();
 
293
       it != uris.end ();
 
294
       it++) {
 
295
 
 
296
    std::stringstream msg_action;
 
297
    if (!(*ita).empty ()) 
 
298
      msg_action << _("Message") << " [" << (*ita) << "]";
 
299
    else
 
300
      msg_action << _("Message");
 
301
 
 
302
    builder.add_action ("message", msg_action.str (),
 
303
                        sigc::bind (sigc::mem_fun (this, &Opal::Sip::EndPoint::on_message), (*it), fullname));
 
304
 
 
305
    ita++;
 
306
  }
 
307
 
 
308
  populated = true;
 
309
 
 
310
  return populated;
 
311
}
 
312
 
 
313
 
 
314
void Opal::Sip::EndPoint::fetch (const std::string _uri)
 
315
{
 
316
  std::string::size_type loc = _uri.find ("@", 0);
 
317
  std::string domain;
 
318
 
 
319
  if (loc != string::npos) 
 
320
    domain = _uri.substr (loc+1);
 
321
 
 
322
  // It is not in the list of uris for which a subscribe is active
 
323
  if (std::find (subscribed_uris.begin (), subscribed_uris.end (), _uri) == subscribed_uris.end ()) {
 
324
 
 
325
    // We are registered yet
 
326
    if (std::find (domains.begin (), domains.end (), domain) != domains.end ()) {
 
327
 
 
328
      Subscribe (SIPSubscribe::Presence, 300, PString (_uri.c_str ()));
 
329
      subscribed_uris.push_back (_uri);
 
330
    }
 
331
    else {
 
332
     
 
333
      to_subscribe_uris.push_back (_uri);
 
334
    }
 
335
  }
 
336
}
 
337
 
 
338
 
 
339
void Opal::Sip::EndPoint::unfetch (const std::string uri)
 
340
{
 
341
  if (std::find (subscribed_uris.begin (), subscribed_uris.end (), uri) != subscribed_uris.end ()) {
 
342
 
 
343
    Subscribe (SIPSubscribe::Presence, 0, PString (uri.c_str ()));
 
344
    subscribed_uris.remove (uri);
 
345
  }
 
346
}
 
347
 
 
348
 
 
349
void Opal::Sip::EndPoint::publish (const Ekiga::PersonalDetails & details)
 
350
{
 
351
  std::string hostname = (const char *) PIPSocket::GetHostName ();
 
352
  std::string short_status = ((Ekiga::PersonalDetails &) (details)).get_short_status ();
 
353
  std::string long_status = ((Ekiga::PersonalDetails &) (details)).get_long_status ();
 
354
 
 
355
  for (std::list<std::string>::iterator it = aors.begin ();
 
356
       it != aors.end ();
 
357
       it++) {
 
358
    std::string to = it->substr (4);
 
359
    PString data;
 
360
    data += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
 
361
 
 
362
    data += "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"pres:";
 
363
    data += to;
 
364
    data += "\">\r\n";
 
365
 
 
366
    data += "<tuple id=\"";
 
367
    data += to; 
 
368
    data += "_on_";
 
369
    data += hostname;
 
370
    data += "\">\r\n";
 
371
 
 
372
    data += "<note>";
 
373
    data += short_status.c_str ();
 
374
    if (!long_status.empty ()) {
 
375
      data += " - ";
 
376
      data += long_status.c_str ();
 
377
    }
 
378
    data += "</note>\r\n";
 
379
 
 
380
    data += "<status>\r\n";
 
381
    data += "<basic>";
 
382
    data += "open";
 
383
    data += "</basic>\r\n";
 
384
    data += "</status>\r\n";
 
385
 
 
386
    data += "<contact priority=\"1\">sip:";
 
387
    data += to;
 
388
    data += "</contact>\r\n";
 
389
 
 
390
    data += "</tuple>\r\n";
 
391
    data += "</presence>\r\n";
 
392
 
 
393
    Publish (to, data, 300); // TODO: allow to change the 500 
 
394
  }
 
395
}
 
396
 
 
397
 
 
398
bool Opal::Sip::EndPoint::send_message (const std::string & _uri, 
 
399
                                        const std::string & _message)
 
400
{
 
401
  if (!_uri.empty () && (_uri.find ("sip:") == 0 || _uri.find (':') == string::npos) && !_message.empty ()) {
 
402
 
 
403
    SIPEndPoint::Message (_uri, _message);
 
404
 
 
405
    return true;
 
406
  }
 
407
 
 
408
  return false;
 
409
}
 
410
 
 
411
 
 
412
bool Opal::Sip::EndPoint::dial (const std::string & uri)
 
413
{
 
414
  std::stringstream ustr;
 
415
 
 
416
  if (uri.find ("sip:") == 0 || uri.find (":") == string::npos) {
 
417
 
 
418
    if (uri.find (":") == string::npos)
 
419
      ustr << "sip:" << uri;
 
420
    else
 
421
      ustr << uri;
 
422
 
 
423
    new Opal::Sip::dialer (ustr.str (), manager);
 
424
 
 
425
    return true;
 
426
  }
 
427
 
 
428
  return false;
 
429
}
 
430
 
 
431
 
 
432
const std::string & Opal::Sip::EndPoint::get_protocol_name () const
 
433
{
 
434
  return protocol_name;
 
435
}
 
436
 
 
437
 
 
438
void Opal::Sip::EndPoint::set_dtmf_mode (unsigned mode)
 
439
{
 
440
  switch (mode) {
 
441
 
 
442
    // SIP Info
 
443
  case 0:
 
444
    SetSendUserInputMode (OpalConnection::SendUserInputAsTone);
 
445
    break;
 
446
 
 
447
    // RFC2833
 
448
  case 1:
 
449
    SetSendUserInputMode (OpalConnection::SendUserInputAsInlineRFC2833);
 
450
    break;
 
451
  default:
 
452
    break;
 
453
  }
 
454
}
 
455
 
 
456
 
 
457
unsigned Opal::Sip::EndPoint::get_dtmf_mode () const
 
458
{
 
459
  // SIP Info
 
460
  if (GetSendUserInputMode () == OpalConnection::SendUserInputAsTone)
 
461
    return 0;
 
462
 
 
463
  // RFC2833
 
464
  if (GetSendUserInputMode () == OpalConnection::SendUserInputAsInlineRFC2833)
 
465
    return 1;
 
466
 
 
467
  return 1;
 
468
}
 
469
 
 
470
 
 
471
bool Opal::Sip::EndPoint::set_listen_port (unsigned port)
 
472
{
 
473
  unsigned udp_min, udp_max;
 
474
 
 
475
  listen_iface.protocol = "udp";
 
476
  listen_iface.voip_protocol = "sip";
 
477
  listen_iface.id = "*";
 
478
 
 
479
  manager.get_udp_ports (udp_min, udp_max);
 
480
 
 
481
  if (port > 0 && port >= udp_min && port <= udp_max) {
 
482
 
 
483
    std::stringstream str;
 
484
    RemoveListener (NULL);
 
485
 
 
486
    str << "udp$*:" << port;
 
487
    if (!StartListeners (PStringArray (str.str ()))) {
 
488
 
 
489
      port = udp_min;
 
490
      str << "udp$*:" << port;
 
491
      while (port <= udp_max) {
 
492
 
 
493
        if (StartListeners (PStringArray (str.str ()))) {
 
494
 
 
495
          listen_iface.port = port;
 
496
          return true;
 
497
        }
 
498
 
 
499
        port++;
 
500
      }
 
501
    }
 
502
    else
 
503
      listen_iface.port = port;
 
504
  }
 
505
 
 
506
  return false;
 
507
}
 
508
 
 
509
 
 
510
const Ekiga::CallProtocolManager::Interface & Opal::Sip::EndPoint::get_listen_interface () const
 
511
{
 
512
  return listen_iface;
 
513
}
 
514
 
 
515
 
 
516
 
 
517
void Opal::Sip::EndPoint::set_forward_uri (const std::string & uri)
 
518
{
 
519
  forward_uri = uri;
 
520
}
 
521
 
 
522
 
 
523
const std::string & Opal::Sip::EndPoint::get_forward_uri () const
 
524
{
 
525
  return forward_uri;
 
526
}
 
527
 
 
528
 
 
529
void Opal::Sip::EndPoint::set_outbound_proxy (const std::string & uri)
 
530
{
 
531
  outbound_proxy = uri;
 
532
  SetProxy (SIPURL (outbound_proxy));
 
533
}
 
534
 
 
535
 
 
536
const std::string & Opal::Sip::EndPoint::get_outbound_proxy () const
 
537
{
 
538
  return outbound_proxy;
 
539
}
 
540
 
 
541
 
 
542
void Opal::Sip::EndPoint::set_nat_binding_delay (unsigned delay)
 
543
{
 
544
  SetNATBindingTimeout (PTimeInterval (0, delay));
 
545
}
 
546
 
 
547
 
 
548
unsigned Opal::Sip::EndPoint::get_nat_binding_delay ()
 
549
{
 
550
  return GetNATBindingTimeout ().GetSeconds ();
 
551
}
 
552
 
 
553
 
 
554
bool Opal::Sip::EndPoint::subscribe (const Opal::Account & account)
 
555
{
 
556
  if (account.get_protocol_name () != "SIP" || account.is_active ())
 
557
    return false;
 
558
 
 
559
  new subscriber (account, *this);
 
560
  return true;
 
561
}
 
562
 
 
563
 
 
564
bool Opal::Sip::EndPoint::unsubscribe (const Opal::Account & account)
 
565
{
 
566
  if (account.get_protocol_name () != "SIP" || !account.is_active ())
 
567
    return false;
 
568
 
 
569
  new subscriber (account, *this);
 
570
  return true;
 
571
}
 
572
 
 
573
 
 
574
void Opal::Sip::EndPoint::ShutDown ()
 
575
{
 
576
  listeners.RemoveAll ();
 
577
 
 
578
  for (PSafePtr<SIPTransaction> transaction(transactions, PSafeReference);      transaction != NULL; ++transaction)
 
579
    transaction->WaitForCompletion();
 
580
 
 
581
  while (activeSIPHandlers.GetSize() > 0) {
 
582
    PSafePtr<SIPHandler> handler = activeSIPHandlers;
 
583
    activeSIPHandlers.Remove(handler);
 
584
  }
 
585
 
 
586
  SIPEndPoint::ShutDown ();
 
587
}
 
588
 
 
589
 
 
590
void Opal::Sip::EndPoint::Register (const Opal::Account & account)
 
591
{
 
592
  std::stringstream aor;
 
593
  std::string host = account.get_host ();
 
594
  std::string::size_type loc = host.find (":", 0);
 
595
  if (loc != std::string::npos)
 
596
    host = host.substr (0, loc);
 
597
 
 
598
  if (account.get_username ().find ("@") == std::string::npos) 
 
599
    aor << account.get_username () << "@" << host;
 
600
  else
 
601
    aor << account.get_username ();
 
602
 
 
603
  SIPRegister::Params params;
 
604
  params.m_addressOfRecord = aor.str ();
 
605
  params.m_registrarAddress = account.get_host ();
 
606
  params.m_authID = account.get_authentication_username ();
 
607
  params.m_password = account.get_password ();
 
608
  params.m_expire = (account.is_enabled () ? account.get_timeout () : 0);
 
609
  params.m_minRetryTime = 0;
 
610
  params.m_maxRetryTime = 0;
 
611
 
 
612
  if (!SIPEndPoint::Register (params))
 
613
    OnRegistrationFailed (aor.str (), SIP_PDU::MaxStatusCode, account.is_enabled ());
 
614
}
 
615
 
 
616
 
 
617
void Opal::Sip::EndPoint::OnRegistered (const PString & _aor,
 
618
                                        bool was_registering)
 
619
{
 
620
  std::string aor = (const char *) _aor;
 
621
  std::string::size_type found;
 
622
  std::string::size_type loc = aor.find ("@", 0);
 
623
  std::string server;
 
624
  std::stringstream strm;
 
625
 
 
626
  if (aor.find (uri_prefix) == std::string::npos) 
 
627
    strm << uri_prefix << aor;
 
628
  else
 
629
    strm << aor;
 
630
 
 
631
  std::list<std::string>::iterator it = find (aors.begin (), aors.end (), aor);
 
632
 
 
633
  if (was_registering) {
 
634
 
 
635
    if (it == aors.end ())
 
636
      aors.push_back (strm.str ());
 
637
  }
 
638
  else {
 
639
 
 
640
    if (it != aors.end ())
 
641
      aors.remove (strm.str ());
 
642
  }
 
643
 
 
644
  if (loc != string::npos) {
 
645
 
 
646
    server = aor.substr (loc+1);
 
647
 
 
648
    if (server.empty ())
 
649
      return;
 
650
 
 
651
    if (was_registering && std::find (domains.begin (), domains.end (), server) == domains.end ()) 
 
652
      domains.push_back (server);
 
653
 
 
654
    if (!was_registering && std::find (domains.begin (), domains.end (), server) != domains.end ()) 
 
655
      domains.remove (server);
 
656
 
 
657
    if (was_registering) {
 
658
      for (std::list<std::string>::const_iterator iter = to_subscribe_uris.begin (); 
 
659
           iter != to_subscribe_uris.end () ; ) { 
 
660
 
 
661
        found = (*iter).find (server, 0);
 
662
        if (found != string::npos) {
 
663
 
 
664
          Subscribe (SIPSubscribe::Presence, 300, PString ((*iter).c_str ()));
 
665
          subscribed_uris.push_back (*iter);
 
666
          to_subscribe_uris.remove (*iter++);
 
667
        }
 
668
        else
 
669
          ++iter;
 
670
      }
 
671
    }
 
672
    else {
 
673
      for (std::list<std::string>::const_iterator iter = subscribed_uris.begin (); 
 
674
           iter != subscribed_uris.end () ; ) { 
 
675
 
 
676
        found = (*iter).find (server, 0);
 
677
        if (found != string::npos) {
 
678
 
 
679
          Unsubscribe (SIPSubscribe::Presence, PString ((*iter).c_str ()));
 
680
          to_subscribe_uris.push_back (*iter);
 
681
          subscribed_uris.remove (*iter++);
 
682
        }
 
683
        else
 
684
          iter++;
 
685
      }
 
686
    }
 
687
  }
 
688
 
 
689
  /* Subscribe for MWI */
 
690
  if (!IsSubscribed (SIPSubscribe::MessageSummary, aor))
 
691
    Subscribe (SIPSubscribe::MessageSummary, 3600, aor);
 
692
 
 
693
  /* Signal */
 
694
  Ekiga::Account *account = account_core.find_account (strm.str ());
 
695
  if (account)
 
696
    runtime.run_in_main (sigc::bind (account->registration_event.make_slot (),
 
697
                                     was_registering ? Ekiga::AccountCore::Registered : Ekiga::AccountCore::Unregistered,
 
698
                                     std::string ()));
 
699
}
 
700
 
 
701
 
 
702
void Opal::Sip::EndPoint::OnRegistrationFailed (const PString & _aor,
 
703
                                                SIP_PDU::StatusCodes r,
 
704
                                                bool wasRegistering)
 
705
{
 
706
  std::stringstream strm;
 
707
  std::string info;
 
708
  std::string aor = (const char *) _aor;
 
709
 
 
710
  if (aor.find (uri_prefix) == std::string::npos) 
 
711
    strm << uri_prefix << aor;
 
712
  else
 
713
    strm << aor;
 
714
 
 
715
  switch (r) {
 
716
 
 
717
  case SIP_PDU::Failure_BadRequest:
 
718
    info = _("Bad request");
 
719
    break;
 
720
 
 
721
  case SIP_PDU::Failure_PaymentRequired:
 
722
    info = _("Payment required");
 
723
    break;
 
724
 
 
725
  case SIP_PDU::Failure_UnAuthorised:
 
726
    info = _("Unauthorized");
 
727
    break;
 
728
 
 
729
  case SIP_PDU::Failure_Forbidden:
 
730
    info = _("Forbidden");
 
731
    break;
 
732
 
 
733
  case SIP_PDU::Failure_RequestTimeout:
 
734
    info = _("Timeout");
 
735
    break;
 
736
 
 
737
  case SIP_PDU::Failure_Conflict:
 
738
    info = _("Conflict");
 
739
    break;
 
740
 
 
741
  case SIP_PDU::Failure_TemporarilyUnavailable:
 
742
    info = _("Temporarily unavailable");
 
743
    break;
 
744
 
 
745
  case SIP_PDU::Failure_NotAcceptable:
 
746
    info = _("Not acceptable");
 
747
    break;
 
748
 
 
749
  case SIP_PDU::IllegalStatusCode:
 
750
    info = _("Illegal status code");
 
751
    break;
 
752
 
 
753
  case SIP_PDU::Redirection_MultipleChoices:
 
754
    info = _("Multiple choices");
 
755
    break;
 
756
 
 
757
  case SIP_PDU::Redirection_MovedPermanently:
 
758
    info = _("Moved permanently");
 
759
    break;
 
760
 
 
761
  case SIP_PDU::Redirection_MovedTemporarily:
 
762
    info = _("Moved temporarily");
 
763
    break;
 
764
 
 
765
  case SIP_PDU::Redirection_UseProxy:
 
766
    info = _("Use proxy");
 
767
    break;
 
768
 
 
769
  case SIP_PDU::Redirection_AlternativeService:
 
770
    info = _("Alternative service");
 
771
    break;
 
772
 
 
773
  case SIP_PDU::Failure_NotFound:
 
774
    info = _("Not found");
 
775
    break;
 
776
 
 
777
  case SIP_PDU::Failure_MethodNotAllowed:
 
778
    info = _("Method not allowed");
 
779
    break;
 
780
 
 
781
  case SIP_PDU::Failure_ProxyAuthenticationRequired:
 
782
    info = _("Proxy auth. required");
 
783
    break;
 
784
 
 
785
  case SIP_PDU::Failure_LengthRequired:
 
786
    info = _("Length required");
 
787
    break;
 
788
 
 
789
  case SIP_PDU::Failure_RequestEntityTooLarge:
 
790
    info = _("Request entity too big");
 
791
    break;
 
792
 
 
793
  case SIP_PDU::Failure_RequestURITooLong:
 
794
    info = _("Request URI too long");
 
795
    break;
 
796
 
 
797
  case SIP_PDU::Failure_UnsupportedMediaType:
 
798
    info = _("Unsupported media type");
 
799
    break;
 
800
 
 
801
  case SIP_PDU::Failure_UnsupportedURIScheme:
 
802
    info = _("Unsupported URI scheme");
 
803
    break;
 
804
 
 
805
  case SIP_PDU::Failure_BadExtension:
 
806
    // Translators : The extension we are trying to register does not exist 
 
807
    info = _("Bad extension");
 
808
    break;
 
809
 
 
810
  case SIP_PDU::Failure_ExtensionRequired:
 
811
    info = _("Extension required");
 
812
    break;
 
813
 
 
814
  case SIP_PDU::Failure_IntervalTooBrief:
 
815
    info = _("Interval too brief");
 
816
    break;
 
817
 
 
818
  case SIP_PDU::Failure_LoopDetected:
 
819
    info = _("Loop detected");
 
820
    break;
 
821
 
 
822
  case SIP_PDU::Failure_TooManyHops:
 
823
    info = _("Too many hops");
 
824
    break;
 
825
 
 
826
  case SIP_PDU::Failure_AddressIncomplete:
 
827
    info = _("Address incomplete");
 
828
    break;
 
829
 
 
830
  case SIP_PDU::Failure_Ambiguous:
 
831
    info = _("Ambiguous");
 
832
    break;
 
833
 
 
834
  case SIP_PDU::Failure_BusyHere:
 
835
    info = _("Busy Here");
 
836
    break;
 
837
 
 
838
  case SIP_PDU::Failure_RequestTerminated:
 
839
    info = _("Request terminated");
 
840
    break;
 
841
 
 
842
  case SIP_PDU::Failure_NotAcceptableHere:
 
843
    info = _("Not acceptable here");
 
844
    break;
 
845
 
 
846
  case SIP_PDU::Failure_BadEvent:
 
847
    info = _("Bad event");
 
848
    break;
 
849
 
 
850
  case SIP_PDU::Failure_RequestPending:
 
851
    info = _("Request pending");
 
852
    break;
 
853
 
 
854
  case SIP_PDU::Failure_Undecipherable:
 
855
    info = _("Undecipherable");
 
856
    break;
 
857
 
 
858
  case SIP_PDU::Failure_InternalServerError:
 
859
    info = _("Internal server error");
 
860
    break;
 
861
 
 
862
  case SIP_PDU::Failure_NotImplemented:
 
863
    info = _("Not implemented");
 
864
    break;
 
865
 
 
866
  case SIP_PDU::Failure_BadGateway:
 
867
    info = _("Bad gateway");
 
868
    break;
 
869
 
 
870
  case SIP_PDU::Failure_ServiceUnavailable:
 
871
    info = _("Service unavailable");
 
872
    break;
 
873
 
 
874
  case SIP_PDU::Failure_ServerTimeout:
 
875
    info = _("Server timeout");
 
876
    break;
 
877
 
 
878
  case SIP_PDU::Failure_SIPVersionNotSupported:
 
879
    info = _("SIP version not supported");
 
880
    break;
 
881
 
 
882
  case SIP_PDU::Failure_MessageTooLarge:
 
883
    info = _("Message too large");
 
884
    break;
 
885
 
 
886
  case SIP_PDU::GlobalFailure_BusyEverywhere:
 
887
    info = _("Busy everywhere");
 
888
    break;
 
889
 
 
890
  case SIP_PDU::GlobalFailure_Decline:
 
891
    info = _("Decline");
 
892
    break;
 
893
 
 
894
  case SIP_PDU::GlobalFailure_DoesNotExistAnywhere:
 
895
    info = _("Does not exist anymore");
 
896
    break;
 
897
 
 
898
  case SIP_PDU::GlobalFailure_NotAcceptable:
 
899
    info = _("Globally not acceptable");
 
900
    break;
 
901
 
 
902
  case SIP_PDU::Local_TransportError:
 
903
  case SIP_PDU::Local_BadTransportAddress:
 
904
    info = _("Transport error");
 
905
    break;
 
906
 
 
907
  case SIP_PDU::Failure_TransactionDoesNotExist:
 
908
  case SIP_PDU::Failure_Gone:
 
909
  case SIP_PDU::MaxStatusCode:
 
910
  default:
 
911
    info = _("Failed");
 
912
 
 
913
  case SIP_PDU::Information_Trying:
 
914
  case SIP_PDU::Information_Ringing:
 
915
  case SIP_PDU::Information_CallForwarded:
 
916
  case SIP_PDU::Information_Queued:
 
917
  case SIP_PDU::Information_Session_Progress:
 
918
  case SIP_PDU::Successful_OK:
 
919
  case SIP_PDU::Successful_Accepted:
 
920
    break;
 
921
  }
 
922
 
 
923
  /* Signal the SIP Endpoint */
 
924
  SIPEndPoint::OnRegistrationFailed (strm.str ().c_str (), r, wasRegistering);
 
925
 
 
926
  /* Signal */
 
927
  Ekiga::Account *account = account_core.find_account (strm.str ());
 
928
  if (account)
 
929
    runtime.run_in_main (sigc::bind (account->registration_event.make_slot (),
 
930
                                     wasRegistering ? Ekiga::AccountCore::RegistrationFailed : Ekiga::AccountCore::UnregistrationFailed,
 
931
                                     info));
 
932
}
 
933
 
 
934
 
 
935
void Opal::Sip::EndPoint::OnMWIReceived (const PString & party, OpalManager::MessageWaitingType /*type*/, const PString & info)
 
936
{
 
937
  std::string mwi = info;
 
938
  std::transform (mwi.begin(), mwi.end(), mwi.begin(), ::tolower);
 
939
  if (mwi == "no")
 
940
    mwi = "0/0";
 
941
 
 
942
  /* Signal */
 
943
  Ekiga::Account *account = account_core.find_account (party);
 
944
  if (account)
 
945
    runtime.run_in_main (sigc::bind (account->mwi_event.make_slot (), info));
 
946
}
 
947
 
 
948
 
 
949
bool Opal::Sip::EndPoint::OnIncomingConnection (OpalConnection &connection,
 
950
                                                unsigned options,
 
951
                                                OpalConnection::StringOptions * stroptions)
 
952
{
 
953
  PTRACE (3, "Opal::Sip::EndPoint\tIncoming connection");
 
954
 
 
955
  if (!forward_uri.empty () && manager.get_unconditional_forward ())
 
956
    connection.ForwardCall (forward_uri);
 
957
  else if (manager.GetCallCount () > 1) { 
 
958
 
 
959
    if (!forward_uri.empty () && manager.get_forward_on_busy ())
 
960
      connection.ForwardCall (forward_uri);
 
961
    else {
 
962
      connection.ClearCall (OpalConnection::EndedByLocalBusy);
 
963
    }
 
964
  }
 
965
  else {
 
966
 
 
967
    Opal::Call *call = dynamic_cast<Opal::Call *> (&connection.GetCall ());
 
968
    if (call) {
 
969
 
 
970
      if (!forward_uri.empty () && manager.get_forward_on_no_answer ()) 
 
971
        call->set_no_answer_forward (manager.get_reject_delay (), forward_uri);
 
972
      else
 
973
        call->set_reject_delay (manager.get_reject_delay ());
 
974
    }
 
975
 
 
976
    return SIPEndPoint::OnIncomingConnection (connection, options, stroptions);
 
977
  }
 
978
 
 
979
  return false;
 
980
}
 
981
 
 
982
 
 
983
bool Opal::Sip::EndPoint::OnReceivedMESSAGE (OpalTransport & transport,
 
984
                                             SIP_PDU & pdu)
 
985
{
 
986
  PString *last = NULL;
 
987
  PString *val = NULL;
 
988
 
 
989
  PString from = pdu.GetMIME().GetFrom();   
 
990
  PINDEX j = from.Find (';');
 
991
  if (j != P_MAX_INDEX)
 
992
    from = from.Left(j); // Remove all parameters
 
993
  j = from.Find ('<');
 
994
  if (j != P_MAX_INDEX && from.Find ('>') == P_MAX_INDEX)
 
995
    from += '>';
 
996
 
 
997
  PWaitAndSignal m(msgDataMutex);
 
998
  last = msgData.GetAt (SIPURL (from).AsString ());
 
999
  if (!last || *last != pdu.GetMIME ().GetFrom ()) {
 
1000
 
 
1001
    val = new PString (pdu.GetMIME ().GetFrom ());
 
1002
    msgData.SetAt (SIPURL (from).AsString (), val);
 
1003
 
 
1004
    SIPURL uri = from;
 
1005
    uri.Sanitise (SIPURL::RequestURI);
 
1006
    std::string display_name = (const char *) uri.GetDisplayName ();
 
1007
    std::string message_uri = (const char *) uri.AsString ();
 
1008
    std::string _message = (const char *) pdu.GetEntityBody ();
 
1009
 
 
1010
    runtime.run_in_main (sigc::bind (sigc::ptr_fun (push_message_in_main), dialect, message_uri, display_name, _message));
 
1011
  }
 
1012
 
 
1013
  return SIPEndPoint::OnReceivedMESSAGE (transport, pdu);
 
1014
}
 
1015
 
 
1016
 
 
1017
void Opal::Sip::EndPoint::OnMessageFailed (const SIPURL & messageUrl,
 
1018
                                           SIP_PDU::StatusCodes /*reason*/)
 
1019
{
 
1020
  SIPURL to = messageUrl;
 
1021
  to.Sanitise (SIPURL::ToURI);
 
1022
  std::string uri = (const char *) to.AsString ();
 
1023
  std::string display_name = (const char *) to.GetDisplayName ();
 
1024
 
 
1025
  runtime.run_in_main (sigc::bind (sigc::ptr_fun (push_notice_in_main),
 
1026
                                   dialect, uri, display_name,
 
1027
                                   _("Could not send message")));
 
1028
}
 
1029
 
 
1030
 
 
1031
SIPURL Opal::Sip::EndPoint::GetRegisteredPartyName (const SIPURL & host)
 
1032
{
 
1033
  PString local_address;
 
1034
  PIPSocket::Address address;
 
1035
  WORD port;
 
1036
  PString url;
 
1037
  SIPURL registration_address;
 
1038
 
 
1039
  /* If we are registered to an account corresponding to host, use it.
 
1040
  */
 
1041
  PSafePtr<SIPHandler> info = activeSIPHandlers.FindSIPHandlerByDomain(host.GetHostName (), SIP_PDU::Method_REGISTER, PSafeReadOnly);
 
1042
  if (info != NULL) {
 
1043
 
 
1044
    return SIPURL ("\"" + GetDefaultDisplayName () + "\" <" + info->GetTargetAddress ().AsString () + ">");
 
1045
  }
 
1046
  else {
 
1047
 
 
1048
    /* If we are not registered to host, 
 
1049
     * then use the default account as outgoing identity.
 
1050
     * If we are exchanging messages with a peer on our network,
 
1051
     * then do not use the default account as outgoing identity.
 
1052
     */
 
1053
    if (host.GetHostAddress ().GetIpAndPort (address, port) && !manager.IsLocalAddress (address)) {
 
1054
 
 
1055
      Ekiga::Account *account = account_core.find_account ("ekiga.net");
 
1056
 
 
1057
      if (account)
 
1058
        return SIPURL ("\"" + GetDefaultDisplayName () + "\" <" + PString(account->get_aor ()) + ">");
 
1059
    }
 
1060
  }
 
1061
 
 
1062
  /* As a last resort, ie not registered to host, no default account or
 
1063
   * dialog with a local peer, then use the local address 
 
1064
   */
 
1065
  local_address = GetListeners()[0].GetLocalAddress();
 
1066
 
 
1067
  PINDEX j = local_address.Find ('$');
 
1068
  if (j != P_MAX_INDEX)
 
1069
    local_address = local_address.Mid (j+1);
 
1070
  SIPURL myself = 
 
1071
    SIPURL ("\"" + GetDefaultDisplayName () + "\" <" + PString ("sip:") + GetDefaultLocalPartyName() + "@" + local_address + ";transport=udp>");
 
1072
 
 
1073
  return myself;
 
1074
}
 
1075
 
 
1076
 
 
1077
void 
 
1078
Opal::Sip::EndPoint::OnPresenceInfoReceived (const PString & user,
 
1079
                                             const PString & basic,
 
1080
                                             const PString & note)
 
1081
{
 
1082
  PINDEX j;
 
1083
  PCaselessString b = basic;
 
1084
  PCaselessString s = note;
 
1085
 
 
1086
  std::string status;
 
1087
  std::string presence = "unknown";
 
1088
 
 
1089
  if (b.Find ("Open") != P_MAX_INDEX)
 
1090
    presence = "online";
 
1091
  else
 
1092
    presence = "offline";
 
1093
 
 
1094
  if (s.Find ("Away") != P_MAX_INDEX)
 
1095
    presence = "away";
 
1096
  else if (s.Find ("On the phone") != P_MAX_INDEX
 
1097
           || s.Find ("Ringing") != P_MAX_INDEX) 
 
1098
    presence = "inacall";
 
1099
  else if (s.Find ("dnd") != P_MAX_INDEX
 
1100
           || s.Find ("Do Not Disturb") != P_MAX_INDEX) 
 
1101
    presence = "dnd";
 
1102
 
 
1103
  else if (s.Find ("Free For Chat") != P_MAX_INDEX) 
 
1104
    presence = "freeforchat";
 
1105
 
 
1106
  if ((j = s.Find (" - ")) != P_MAX_INDEX)
 
1107
    status = (const char *) note.Mid (j + 3);
 
1108
 
 
1109
  SIPURL sip_uri = SIPURL (user);
 
1110
  sip_uri.Sanitise (SIPURL::ExternalURI);
 
1111
  std::string _uri = sip_uri.AsString ();
 
1112
 
 
1113
  /**
 
1114
   * TODO
 
1115
   * Wouldn't it be convenient to emit the signal and have the presence core listen to it ?
 
1116
   */
 
1117
  runtime.run_in_main (sigc::bind (sigc::ptr_fun (presence_status_in_main), this, _uri, presence, status));
 
1118
}
 
1119
 
 
1120
 
 
1121
void Opal::Sip::EndPoint::on_dial (std::string uri)
 
1122
{
 
1123
  manager.dial (uri);
 
1124
}
 
1125
 
 
1126
 
 
1127
void Opal::Sip::EndPoint::on_message (std::string uri,
 
1128
                                      std::string name)
 
1129
{
 
1130
  dialect->start_chat_with (uri, name);
 
1131
}
 
1132
 
 
1133
 
 
1134
void Opal::Sip::EndPoint::on_transfer (std::string uri)
 
1135
{
 
1136
  /* FIXME : we don't handle several calls here */
 
1137
  for (PSafePtr<OpalConnection> connection(connectionsActive, PSafeReference); connection != NULL; ++connection)
 
1138
    if (!PIsDescendant(&(*connection), OpalPCSSConnection))
 
1139
      connection->TransferConnection (uri);
 
1140
}