summaryrefslogtreecommitdiff
path: root/folks/im-details.vala
blob: 943523449d74dd509ccb12f5ed8fe8ced9c24481 (plain)
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
/*
 * Copyright (C) 2010 Collabora Ltd.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Philip Withnall <philip.withnall@collabora.co.uk>
 */

using GLib;
using Gee;

/**
 * Errors related to IM addresses and IM address handling.
 */
public errordomain Folks.ImDetailsError
{
  /**
   * The specified IM address could not be parsed.
   */
  INVALID_IM_ADDRESS
}

/**
 * IM addresses exposed by an object implementing {@link PresenceDetails}.
 *
 * @since 0.1.13
 */
public interface Folks.ImDetails : Object
{
  /**
   * A mapping of IM protocol to an (unordered) set of IM addresses.
   *
   * Each mapping is from an arbitrary protocol identifier to a set of IM
   * addresses on that protocol for the contact, listed in no particular order.
   *
   * There must be no duplicate IM addresses in each set, though a given
   * IM address may be present in the sets for different protocols.
   *
   * All the IM addresses must be normalised using
   * {@link ImDetails.normalise_im_address} before being added to this property.
   *
   * @since 0.5.1
   */
  public abstract MultiMap<string, string> im_addresses
    {
      get; set;
    }

  /**
   * Normalise an IM address so that it's suitable for string comparison.
   *
   * IM addresses for various protocols can be represented in different ways,
   * only one of which is canonical. In order to allow simple string comparisons
   * of IM addresses to work, the IM addresses must be normalised beforehand.
   *
   * If the provided IM address is invalid,
   * {@link Folks.ImDetailsError.INVALID_IM_ADDRESS} will be thrown. Note that this
   * isn't guaranteed to be thrown for all invalid addresses, but if it is
   * thrown, the address is guaranteed to be invalid.
   *
   * @param im_address the address to normalise
   * @param protocol the protocol of this im_address
   *
   * @since 0.2.0
   * @throws Folks.ImDetailsError if the provided IM address was invalid
   */
  public static string normalise_im_address (string im_address, string protocol)
      throws Folks.ImDetailsError
    {
      if (protocol == "aim" || protocol == "myspace")
        {
          return im_address.replace (" ", "").down ().normalize ();
        }
      else if (protocol == "irc" || protocol == "yahoo" ||
          protocol == "yahoojp" || protocol == "groupwise")
        {
          return im_address.down ().normalize ();
        }
      else if (protocol == "jabber")
        {
          /* Parse the JID */
          string[] parts = im_address.split ("/", 2);

          if (parts.length < 1)
            {
              throw new ImDetailsError.INVALID_IM_ADDRESS (
                  /* Translators: the parameter is an IM address. */
                  _("The IM address '%s' could not be understood."),
                  im_address);
            }

          string resource = null;
          if (parts.length == 2)
            resource = parts[1];

          parts = parts[0].split ("@", 2);

          if (parts.length < 1)
            {
              throw new ImDetailsError.INVALID_IM_ADDRESS (
                  /* Translators: the parameter is an IM address. */
                  _("The IM address '%s' could not be understood."),
                  im_address);
            }

          string node, domain;
          if (parts.length == 2)
            {
              node = parts[0];
              domain = parts[1];
            }
          else
            {
              node = null;
              domain = parts[0];
            }

          if ((node != null && node == "") ||
              (domain == null || domain == "") ||
              (resource != null && resource == ""))
            {
              throw new ImDetailsError.INVALID_IM_ADDRESS (
                  /* Translators: the parameter is an IM address. */
                  _("The IM address '%s' could not be understood."),
                  im_address);
            }

          domain = domain.down ();
          if (node != null)
            node = node.down ();

          /* Build a new JID */
          string normalised = null;

          if (node != null && resource != null)
            {
              normalised = "%s@%s/%s".printf (node, domain, resource);
            }
          else if (node != null)
            {
              normalised = "%s@%s".printf (node, domain);
            }
          else if (resource != null)
            {
              normalised = "%s/%s".printf (domain, resource);
            }
          else
            {
              throw new ImDetailsError.INVALID_IM_ADDRESS (
                  /* Translators: the parameter is an IM address. */
                  _("The IM address '%s' could not be understood."),
                  im_address);
            }

          return normalised.normalize (-1, NormalizeMode.NFKC);
        }
      else
        {
          /* Fallback */
          return im_address.normalize ();
        }
    }
}