]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-string-parser.c
cb7d3c3e191a89e135a2ab90cbbf07fe96891738
[empathy.git] / libempathy-gtk / empathy-string-parser.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  */
20
21 #include "config.h"
22
23 #include "empathy-string-parser.h"
24 #include "empathy-smiley-manager.h"
25 #include "empathy-ui-utils.h"
26
27 #define SCHEMES           "([a-zA-Z\\+]+)"
28 #define INVALID_CHARS     "\\s\"<>"
29 #define INVALID_CHARS_EXT INVALID_CHARS "\\[\\](){},;:"
30 #define INVALID_CHARS_FULL INVALID_CHARS_EXT "?'"
31 #define BODY              "([^"INVALID_CHARS_FULL"])([^"INVALID_CHARS_EXT"]*)"
32 #define BODY_END          "([^"INVALID_CHARS"]*)[^"INVALID_CHARS_FULL".]"
33 #define URI_REGEX         "("SCHEMES"://"BODY_END")" \
34                           "|((www|ftp)\\."BODY_END")" \
35                           "|((mailto:)?"BODY"@"BODY"\\."BODY_END")"
36
37 static GRegex *
38 uri_regex_dup_singleton (void)
39 {
40         static GRegex *uri_regex = NULL;
41
42         /* We intentionally leak the regex so it's not recomputed */
43         if (!uri_regex) {
44                 GError *error = NULL;
45
46                 uri_regex = g_regex_new (URI_REGEX, 0, 0, &error);
47                 if (uri_regex == NULL) {
48                         g_warning ("Failed to create reg exp: %s", error->message);
49                         g_error_free (error);
50                         return NULL;
51                 }
52         }
53
54         return g_regex_ref (uri_regex);
55 }
56
57 void
58 empathy_string_parser_substr (const gchar *text,
59                               gssize len,
60                               EmpathyStringParser *parsers,
61                               gpointer user_data)
62 {
63         if (parsers != NULL && parsers[0].match_func != NULL) {
64                 parsers[0].match_func (text, len,
65                                        parsers[0].replace_func, parsers + 1,
66                                        user_data);
67         }
68 }
69
70 void
71 empathy_string_match_link (const gchar *text,
72                            gssize len,
73                            EmpathyStringReplace replace_func,
74                            EmpathyStringParser *sub_parsers,
75                            gpointer user_data)
76 {
77         GRegex     *uri_regex;
78         GMatchInfo *match_info;
79         gboolean    match;
80         gint        last = 0;
81
82         uri_regex = uri_regex_dup_singleton ();
83         if (uri_regex == NULL) {
84                 empathy_string_parser_substr (text, len, sub_parsers, user_data);
85                 return;
86         }
87
88         match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
89         if (match) {
90                 gint s = 0, e = 0;
91
92                 do {
93                         g_match_info_fetch_pos (match_info, 0, &s, &e);
94
95                         if (s > last) {
96                                 /* Append the text between last link (or the
97                                  * start of the message) and this link */
98                                 empathy_string_parser_substr (text + last,
99                                                               s - last,
100                                                               sub_parsers,
101                                                               user_data);
102                         }
103
104                         replace_func (text + s, e - s, NULL, user_data);
105
106                         last = e;
107                 } while (g_match_info_next (match_info, NULL));
108         }
109
110         empathy_string_parser_substr (text + last, len - last,
111                                       sub_parsers, user_data);
112
113         g_match_info_free (match_info);
114         g_regex_unref (uri_regex);
115 }
116
117 void
118 empathy_string_match_smiley (const gchar *text,
119                              gssize len,
120                              EmpathyStringReplace replace_func,
121                              EmpathyStringParser *sub_parsers,
122                              gpointer user_data)
123 {
124         guint last = 0;
125         EmpathySmileyManager *smiley_manager;
126         GSList *hits, *l;
127
128         smiley_manager = empathy_smiley_manager_dup_singleton ();
129         hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
130
131         for (l = hits; l; l = l->next) {
132                 EmpathySmileyHit *hit = l->data;
133
134                 if (hit->start > last) {
135                         /* Append the text between last smiley (or the
136                          * start of the message) and this smiley */
137                         empathy_string_parser_substr (text + last,
138                                                       hit->start - last,
139                                                       sub_parsers, user_data);
140                 }
141
142                 replace_func (text + hit->start, hit->end - hit->start,
143                               hit, user_data);
144
145                 last = hit->end;
146
147                 empathy_smiley_hit_free (hit);
148         }
149         g_slist_free (hits);
150         g_object_unref (smiley_manager);
151
152         empathy_string_parser_substr (text + last, len - last,
153                                       sub_parsers, user_data);
154 }
155
156 void
157 empathy_string_match_all (const gchar *text,
158                           gssize len,
159                           EmpathyStringReplace replace_func,
160                           EmpathyStringParser *sub_parsers,
161                           gpointer user_data)
162 {
163         replace_func (text, len, NULL, user_data);
164 }
165
166 void
167 empathy_string_replace_link (const gchar *text,
168                              gssize len,
169                              gpointer match_data,
170                              gpointer user_data)
171 {
172         GString *string = user_data;
173         gchar *real_url;
174         gchar *title;
175         gchar *markup;
176
177         real_url = empathy_make_absolute_url_len (text, len);
178
179         /* Need to copy manually, because g_markup_printf_escaped does not work
180          * with string precision pitfalls. */
181         title = g_strndup (text, len);
182
183         /* Append the link inside <a href=""></a> tag */
184         markup = g_markup_printf_escaped ("<a href=\"%s\">%s</a>",
185                         real_url, title);
186
187         g_string_append (string, markup);
188
189         g_free (real_url);
190         g_free (title);
191         g_free (markup);
192 }
193
194 void
195 empathy_string_replace_escaped (const gchar *text,
196                                 gssize len,
197                                 gpointer match_data,
198                                 gpointer user_data)
199 {
200         GString *string = user_data;
201         gchar *escaped;
202         guint i;
203         gsize escaped_len, old_len;
204
205         escaped = g_markup_escape_text (text, len);
206         escaped_len = strlen (escaped);
207
208         /* Allocate more space to string (we really need a g_string_extend...) */
209         old_len = string->len;
210         g_string_set_size (string, old_len + escaped_len);
211         g_string_truncate (string, old_len);
212
213         /* Remove '\r' */
214         for (i = 0; i < escaped_len; i++) {
215                 if (escaped[i] != '\r')
216                         g_string_append_c (string, escaped[i]);
217         }
218
219         g_free (escaped);
220 }
221
222 gchar *
223 empathy_add_link_markup (const gchar *text)
224 {
225         EmpathyStringParser parsers[] = {
226                 {empathy_string_match_link, empathy_string_replace_link},
227                 {empathy_string_match_all, empathy_string_replace_escaped},
228                 {NULL, NULL}
229         };
230         GString *string;
231
232         g_return_val_if_fail (text != NULL, NULL);
233
234         string = g_string_sized_new (strlen (text));
235         empathy_string_parser_substr (text, -1, parsers, string);
236
237         return g_string_free (string, FALSE);
238 }
239