3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * Rewritten from scratch during Google Summer of Code 2012
8 * by Tomek Wasilczyk (http://www.wasilczyk.pl).
10 * Previously implemented by:
11 * - Arkadiusz Miskiewicz <misiek@pld.org.pl> - first implementation (2001);
12 * - Bartosz Oler <bartosz@bzimage.us> - reimplemented during GSoC 2005;
13 * - Krzysztof Klinikowski <grommasher@gmail.com> - some parts (2009-2011).
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
36 uin_t
ggp_str_to_uin(const char *str
)
41 if (!str
|| str
[0] < '0' || str
[0] > '9')
45 uin
= strtoul(str
, &endptr
, 10);
47 if (errno
== ERANGE
|| endptr
[0] != '\0')
53 const char * ggp_uin_to_str(uin_t uin
)
55 static char buff
[GGP_UIN_LEN_MAX
+ 1];
57 g_snprintf(buff
, GGP_UIN_LEN_MAX
+ 1, "%u", uin
);
62 uin_t
ggp_get_my_uin(PurpleConnection
*gc
)
64 g_return_val_if_fail(gc
!= NULL
, 0);
66 return ggp_str_to_uin(purple_account_get_username(
67 purple_connection_get_account(gc
)));
70 static gchar
* ggp_convert(const gchar
*src
, const char *srcenc
,
79 dst
= g_convert_with_fallback(src
, strlen(src
), dstenc
, srcenc
, "?",
82 purple_debug_error("gg", "error converting from %s to %s: %s\n",
83 srcenc
, dstenc
, err
->message
);
93 gchar
* ggp_convert_to_cp1250(const gchar
*src
)
95 return ggp_convert(src
, "UTF-8", "CP1250");
98 gchar
* ggp_convert_from_cp1250(const gchar
*src
)
100 return ggp_convert(src
, "CP1250", "UTF-8");
103 gboolean
ggp_password_validate(const gchar
*password
)
105 const int len
= strlen(password
);
106 if (len
< 6 || len
> 15)
108 return g_regex_match_simple("^[ a-zA-Z0-9~`!@#$%^&*()_+=[\\]{};':\",./?"
109 "<>\\\\|-]+$", password
, 0, 0);
112 gchar
* ggp_utf8_strndup(const gchar
*str
, gsize n
)
118 raw_len
= strlen(str
);
120 return g_strdup(str
);
122 end_ptr
= g_utf8_offset_to_pointer(str
, g_utf8_pointer_to_offset(str
, &str
[n
]));
123 raw_len
= end_ptr
- str
;
126 end_ptr
= g_utf8_prev_char(end_ptr
);
127 raw_len
= end_ptr
- str
;
130 g_assert(raw_len
<= n
);
132 return g_strndup(str
, raw_len
);
135 GSList
* ggp_list_copy_to_slist_deep(GList
*list
, GCopyFunc func
,
138 GSList
*new_list
= NULL
;
141 it
= g_list_first(list
);
143 new_list
= g_slist_append(new_list
, func(it
->data
, user_data
));
144 it
= g_list_next(it
);
149 GList
* ggp_strsplit_list(const gchar
*string
, const gchar
*delimiter
,
152 gchar
**splitted
, **it
;
155 it
= splitted
= g_strsplit(string
, delimiter
, max_tokens
);
157 list
= g_list_append(list
, *it
);
165 gchar
* ggp_strjoin_list(const gchar
*separator
, GList
*list
)
172 list_len
= g_list_length(list
);
173 str_array
= g_new(gchar
*, list_len
+ 1);
175 it
= g_list_first(list
);
178 str_array
[i
++] = it
->data
;
179 it
= g_list_next(it
);
183 joined
= g_strjoinv(separator
, str_array
);
189 const gchar
* ggp_ipv4_to_str(uint32_t raw_ip
)
191 static gchar buff
[INET_ADDRSTRLEN
];
194 g_snprintf(buff
, sizeof(buff
), "%d.%d.%d.%d",
195 ((raw_ip
>> 0) & 0xFF),
196 ((raw_ip
>> 8) & 0xFF),
197 ((raw_ip
>> 16) & 0xFF),
198 ((raw_ip
>> 24) & 0xFF));
203 GList
* ggp_list_truncate(GList
*list
, guint length
, GDestroyNotify free_func
)
205 while (g_list_length(list
) > length
) {
206 GList
*last
= g_list_last(list
);
207 free_func(last
->data
);
208 list
= g_list_delete_link(list
, last
);
213 gchar
* ggp_free_if_equal(gchar
*str
, const gchar
*pattern
)
215 if (g_strcmp0(str
, pattern
) == 0) {
222 const gchar
* ggp_date_strftime(const gchar
*format
, time_t date
)
225 static gchar buff
[30];
227 g_date_set_time_t(&g_date
, date
);
228 if (0 == g_date_strftime(buff
, sizeof(buff
), format
, &g_date
))
233 time_t ggp_date_from_iso8601(const gchar
*str
)
239 if (!g_time_val_from_iso8601(str
, &g_timeval
))
241 return g_timeval
.tv_sec
;
244 uint64_t * ggp_uint64dup(uint64_t val
)
246 uint64_t *ptr
= g_new(uint64_t, 1);
251 gint
ggp_int64_compare(gconstpointer _a
, gconstpointer _b
)
253 const int64_t *ap
= _a
, *bp
= _b
;
254 const int64_t a
= *ap
, b
= *bp
;
263 JsonParser
* ggp_json_parse(const gchar
*data
)
267 parser
= json_parser_new();
268 if (json_parser_load_from_data(parser
, data
, -1, NULL
))
271 if (purple_debug_is_unsafe())
272 purple_debug_warning("gg", "Invalid JSON: %s\n", data
);