File: handles.c

package info (click to toggle)
telepathy-rakia 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 3,028 kB
  • ctags: 1,720
  • sloc: sh: 11,552; ansic: 9,892; python: 4,532; makefile: 421; xml: 15
file content (296 lines) | stat: -rw-r--r-- 7,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
254
255
256
257
258
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
289
290
291
292
293
294
295
296
/*
 * rakia/handles.c - Handler helpers
 * Copyright (C) 2005 Collabora Ltd.
 * Copyright (C) 2006-2011 Nokia Corporation
 *
 * This work 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 work 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 work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <stdlib.h>

#include <rakia/handles.h>
#include <sofia-sip/sip_header.h>

#define DEBUG_FLAG RAKIA_DEBUG_CONNECTION
#include "rakia/debug.h"

TpHandle
rakia_handle_ensure (TpBaseConnection *conn,
                     url_t const *uri,
                     char const *alias)
{
  TpHandleRepoIface *repo;
  gchar *str;
  TpHandle handle;

  g_return_val_if_fail (TP_IS_BASE_CONNECTION (conn), 0);
  g_return_val_if_fail (uri != NULL, 0);

  repo = tp_base_connection_get_handles (conn, TP_HANDLE_TYPE_CONTACT);

  str = url_as_string (NULL, uri);

  handle = tp_handle_ensure (repo, str, NULL, NULL);

  su_free (NULL, str);

  /* TODO: store the alias somehow (probably by moving this code
   * into RakiaBaseConnection and using a hash table in priv) */

  return handle;
}

TpHandle
rakia_handle_by_requestor (TpBaseConnection *conn,
                           sip_t const *sip)
{
  url_t const *uri;
  char const *display;

  g_return_val_if_fail (sip != NULL, 0);
  g_return_val_if_fail (sip->sip_from != NULL, 0);

  uri = sip->sip_from->a_url;
  display = sip->sip_from->a_display;

  return rakia_handle_ensure (conn, uri, display);
}

void
rakia_handle_unref (TpBaseConnection *conn,
                    TpHandle handle)
{
  /* no longer useful */
}

char const *
rakia_handle_inspect (TpBaseConnection *conn,
                      TpHandle handle)
{
  TpHandleRepoIface *repo;

  g_return_val_if_fail (TP_IS_BASE_CONNECTION (conn), NULL);
  g_return_val_if_fail (handle != 0, NULL);

  repo = tp_base_connection_get_handles (conn, TP_HANDLE_TYPE_CONTACT);

  return tp_handle_inspect (repo, handle);
}

static gboolean
priv_is_host (const gchar* str)
{
  static GRegex *host_regex = NULL;

#define DOMAIN "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
#define TLD "[a-z]([-a-z0-9]*[a-z0-9])?"

  if (host_regex == NULL)
    {
      GError *error = NULL;

      host_regex = g_regex_new ("^("
            "("DOMAIN"\\.)*"TLD"\\.?|"      /* host name */
            "[0-9]{1,3}(\\.[0-9]{1,3}){3}|" /* IPv4 address */
            "\\[[0-9a-f:.]\\]"              /* IPv6 address, sloppily */
          ")$",
          G_REGEX_CASELESS | G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &error);

      if (error != NULL)
        g_error ("failed to compile the host regex: %s", error->message);
    }

#undef DOMAIN
#undef TLD

  return g_regex_match (host_regex, str, 0, NULL);
}

static gboolean
priv_is_tel_num (const gchar *str)
{
  static GRegex *tel_num_regex = NULL;

  if (tel_num_regex == NULL)
    {
      GError *error = NULL;

      tel_num_regex = g_regex_new (
          "^\\s*[\\+(]?\\s*[0-9][-.0-9()\\s]*$",
          G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &error);

      if (error != NULL)
        g_error ("failed to compile the telephone number regex: %s",
            error->message);
    }

  return g_regex_match (tel_num_regex, str, 0, NULL);
}

/* Strip the non-essential characters from a string regarded as
 * a telephone number */
static gchar *
priv_strip_tel_num (const gchar *fuzzy)
{
  static GRegex *cruft_regex = NULL;

  if (cruft_regex == NULL)
    {
      GError *error = NULL;

      cruft_regex = g_regex_new ("[^+0-9]+",
          G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &error);

      if (error != NULL)
        g_error ("failed to compile the non-essential "
            "telephone number cruft regex: %s", error->message);
    }

  return g_regex_replace_literal (cruft_regex, fuzzy, -1, 0, "", 0, NULL);
}

static const char *
priv_lowercase_url_part (su_home_t *home, const char *src)
{
  size_t n = 0;
  size_t i;
  char *res;

  for (i = 0; src[i]; i++)
    {
      if (g_ascii_isupper (src[i]))
        {
          n = i + strlen (src + i);
          break;
        }
    }

  if (!src[i])
    return src;

  res = su_alloc (home, n + 1);
  memcpy (res, src, i);
  for (; i < n; i++)
    res[i] = g_ascii_tolower (src[i]);
  res[i] = '\0';

  return (const char *) res;
}

#define RAKIA_RESERVED_CHARS_ALLOWED_IN_USERNAME "!*'()&=+$,;?/"

gchar *
rakia_normalize_contact (const gchar *sipuri,
    const url_t *base_url,
    const gchar *transport,
    GError **error)
{
  su_home_t home[1] = { SU_HOME_INIT(home) };
  url_t *url;
  gchar *retval = NULL;
  char *c;

  url = url_make (home, sipuri);

  if (url == NULL ||
      (url->url_scheme == NULL && url->url_user == NULL))
    {
      /* we got username or phone number, local to our domain */
      gchar *user;

      if (base_url == NULL || base_url->url_host == NULL)
        {
          WARNING ("bare name given, but no account URL is set");
          goto error;
        }

      if (priv_is_tel_num (sipuri))
        {
          user = priv_strip_tel_num (sipuri);
        }
      else
        {
          user = g_uri_escape_string (sipuri,
              RAKIA_RESERVED_CHARS_ALLOWED_IN_USERNAME, FALSE);
        }

      if (base_url->url_type == url_sips)
        url = url_format (home, "sips:%s@%s",
            user, base_url->url_host);
      else
        url = url_format (home, "sip:%s@%s",
            user, base_url->url_host);

      g_free (user);

      if (!url) goto error;
    }
  else if (url->url_scheme == NULL)
    {
      /* Set the scheme to SIP or SIPS accordingly to the connection's
       * transport preference */
      if (transport != NULL
          && g_ascii_strcasecmp (transport, "tls") == 0)
        {
          url->url_type = url_sips;
          url->url_scheme = "sips";
        }
      else
        {
          url->url_type = url_sip;
          url->url_scheme = "sip";
        }
    }

  if (url_sanitize (url) != 0) goto error;

  /* scheme should've been set by now */
  if (url->url_scheme == NULL || (url->url_scheme[0] == 0))
    goto error;

  /* convert the scheme to lowercase */
  /* Note: we can't do it in place because url->url_scheme may point to
   * a static string */
  url->url_scheme = priv_lowercase_url_part (home, url->url_scheme);

  /* Check that if we have '@', the username isn't empty.
   * Note that we rely on Sofia-SIP to canonize the user name */
  if (url->url_user)
    {
      if (url->url_user[0] == 0) goto error;
    }

  /* host should be set and valid */
  if (url->url_host == NULL || !priv_is_host (url->url_host))
      goto error;

  /* convert host to lowercase */
  for (c = (char *) url->url_host; *c; c++)
    {
      *c = g_ascii_tolower (*c);
    }

  retval = g_strdup (url_as_string (home, url));

error:
  if (retval == NULL)
      g_set_error (error, TP_ERROR, TP_ERROR_INVALID_HANDLE,
          "invalid SIP URI");

  su_home_deinit (home);
  return retval;
}