2 * Copyright (C) 2010 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include "empathy-string-parser.h"
26 #include "empathy-smiley-manager.h"
28 #define SCHEMES "([a-zA-Z\\+]+)"
29 #define INVALID_CHARS "\\s\"'"
30 #define INVALID_CHARS_EXT INVALID_CHARS "\\[\\]<>(){},;:?"
31 #define BODY "([^"INVALID_CHARS"]+)"
32 #define BODY_END "([^"INVALID_CHARS"]*)[^"INVALID_CHARS_EXT".]"
33 #define BODY_STRICT "([^"INVALID_CHARS_EXT"]+)"
34 #define URI_REGEX "("SCHEMES"://"BODY_END")" \
35 "|((www|ftp)\\."BODY_END")" \
36 "|((mailto:)?"BODY_STRICT"@"BODY"\\."BODY_END")"
39 uri_regex_dup_singleton (void)
41 static GRegex *uri_regex = NULL;
43 /* We intentionally leak the regex so it's not recomputed */
45 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
48 return g_regex_ref (uri_regex);
52 empathy_string_parser_substr (const gchar *text,
54 EmpathyStringParser *parsers,
57 if (parsers != NULL && parsers[0].match_func != NULL) {
58 parsers[0].match_func (text, len,
59 parsers[0].replace_func, parsers + 1,
65 empathy_string_match_link (const gchar *text,
67 EmpathyStringReplace replace_func,
68 EmpathyStringParser *sub_parsers,
72 GMatchInfo *match_info;
76 uri_regex = uri_regex_dup_singleton ();
77 match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
82 g_match_info_fetch_pos (match_info, 0, &s, &e);
85 /* Append the text between last link (or the
86 * start of the message) and this link */
87 empathy_string_parser_substr (text + last,
93 replace_func (text + s, e - s, NULL, user_data);
96 } while (g_match_info_next (match_info, NULL));
99 empathy_string_parser_substr (text + last, len - last,
100 sub_parsers, user_data);
102 g_match_info_free (match_info);
103 g_regex_unref (uri_regex);
107 empathy_string_match_smiley (const gchar *text,
109 EmpathyStringReplace replace_func,
110 EmpathyStringParser *sub_parsers,
114 EmpathySmileyManager *smiley_manager;
117 smiley_manager = empathy_smiley_manager_dup_singleton ();
118 hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
120 for (l = hits; l; l = l->next) {
121 EmpathySmileyHit *hit = l->data;
123 if (hit->start > last) {
124 /* Append the text between last smiley (or the
125 * start of the message) and this smiley */
126 empathy_string_parser_substr (text + last,
128 sub_parsers, user_data);
131 replace_func (text + hit->start, hit->end - hit->start,
136 empathy_smiley_hit_free (hit);
139 g_object_unref (smiley_manager);
141 empathy_string_parser_substr (text + last, len - last,
142 sub_parsers, user_data);
146 empathy_string_match_all (const gchar *text,
148 EmpathyStringReplace replace_func,
149 EmpathyStringParser *sub_parsers,
152 replace_func (text, len, NULL, user_data);