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
|
/*
* This file is part of telepathy-idle
*
* Copyright (C) 2006-2007 Collabora Limited
* Copyright (C) 2006-2007 Nokia Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* 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
*/
#include "idle-handles.h"
#include <glib.h>
#include <ctype.h>
#include <string.h>
#include <telepathy-glib/errors.h>
#include <telepathy-glib/handle-repo-dynamic.h>
static gboolean _nickname_is_valid(const gchar *nickname) {
gsize len;
gunichar ucs4char;
const gchar *char_pos = nickname;
len = g_utf8_strlen(nickname, -1);
if (!len)
return FALSE;
while (char_pos != NULL) {
ucs4char = g_utf8_get_char_validated(char_pos, -1);
switch (*char_pos) {
case '[':
case ']':
case '\\':
case '`':
case '_':
case '^':
case '{':
case '|':
case '}':
case '-':
break;
case '\0':
return TRUE;
break;
default:
if (!(g_unichar_isalpha(ucs4char) || ((char_pos != nickname) && g_unichar_isdigit(ucs4char))))
return FALSE;
break;
}
char_pos = g_utf8_find_next_char(char_pos, NULL);
}
return TRUE;
}
static gboolean _channelname_is_valid(const gchar *channel) {
static const gchar not_allowed_chars[] = {' ', '\007', ',', '\r', '\n', ':', '\0'};
if ((channel[0] != '#') && (channel[0] != '!') && (channel[0] != '&') && (channel[0] != '+'))
return FALSE;
gsize len = strlen(channel);
if ((len < 2) || (len > 50))
return FALSE;
if (channel[0] == '!') {
for (int i = 0; i < 5; i++) {
if (!g_ascii_isupper(channel[i + 1]) && !isdigit(channel[i + 1]))
return FALSE;
}
}
const gchar *tmp = strchr(channel + 1, ':');
if (tmp != NULL) {
for (const gchar *tmp2 = channel + 1; tmp2 != tmp; tmp2++) {
if (strchr(not_allowed_chars, *tmp2))
return FALSE;
}
for (const gchar *tmp2 = tmp + 1; tmp2 != channel + len; tmp2++) {
if (strchr(not_allowed_chars, *tmp2))
return FALSE;
}
} else {
for (const gchar *tmp2 = channel + 1; tmp2 != channel + len; tmp2++) {
if (strchr(not_allowed_chars, *tmp2))
return FALSE;
}
}
return TRUE;
}
static gchar *_nick_normalize_func(TpHandleRepoIface *repo, const gchar *id, gpointer ctx, GError **error) {
if (!_nickname_is_valid(id)) {
g_set_error(error, TP_ERRORS, TP_ERROR_INVALID_HANDLE, "invalid nickname");
return NULL;
}
gchar *normalized = g_utf8_strdown(id, -1);
return normalized;
}
static gchar *_channel_normalize_func(TpHandleRepoIface *repo, const gchar *id, gpointer ctx, GError **error) {
gchar *channel = g_strdup(id);
if ((channel[0] != '#') && (channel[0] != '!') && (channel[0] != '&') && (channel[0] != '+')) {
gchar *tmp = channel;
channel = g_strdup_printf("#%s", channel);
g_free(tmp);
}
if (!_channelname_is_valid(channel)) {
g_set_error(error, TP_ERRORS, TP_ERROR_INVALID_HANDLE, "invalid channel ID");
return NULL;
}
gchar *normalized = g_utf8_strdown(channel, -1);
g_free(channel);
return normalized;
}
void idle_handle_repos_init(TpHandleRepoIface **handles) {
g_assert(handles != NULL);
handles[TP_HANDLE_TYPE_CONTACT] = (TpHandleRepoIface *) g_object_new(TP_TYPE_DYNAMIC_HANDLE_REPO,
"handle-type", TP_HANDLE_TYPE_CONTACT,
"normalize-function", _nick_normalize_func,
"default-normalize-context", NULL,
NULL);
handles[TP_HANDLE_TYPE_ROOM] = (TpHandleRepoIface *) g_object_new(TP_TYPE_DYNAMIC_HANDLE_REPO,
"handle-type", TP_HANDLE_TYPE_ROOM,
"normalize-function", _channel_normalize_func,
"default-normalize-context", NULL,
NULL);
}
|