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