aboutsummaryrefslogtreecommitdiff
path: root/src/urlregex.c
blob: 3706fdbd08b1c2d9300e1923f09ae19329d1f29a (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <string.h>
#include "urlregex.h"

#define LP_BUG_BASE_URL "https://bugs.launchpad.net/bugs/"
#define HTTP_BASE_URL "http://"
#define MAILTO_BASE_URL "mailto:"

/* Adapted from src/terminal-screen.c in the gnome-terminal source */

#define USERCHARS "-[:alnum:]"
#define USERCHARS_CLASS "[" USERCHARS "]"
#define PASSCHARS_CLASS "[-[:alnum:]\\Q,?;.:/!%$^*&~\"#'\\E]"
#define HOSTCHARS_CLASS "[-[:alnum:]]"
#define HOST HOSTCHARS_CLASS "+(\\." HOSTCHARS_CLASS "+)*"
#define PORT "(?:\\:[[:digit:]]{1,5})?"
#define PATHCHARS_CLASS "[-[:alnum:]\\Q_$.+!*,:;@&=?/~#%\\E]"
#define PATHTERM_CLASS "[^\\Q]'.:}>) \t\r\n,\"\\E]"
#define SCHEME "(?:news:|telnet:|nntp:|file:\\/|https?:|ftps?:|sftp:|webcal:)"
#define USERPASS USERCHARS_CLASS "+(?:" PASSCHARS_CLASS "+)?"
#define URLPATH   "(?:(/"PATHCHARS_CLASS"+(?:[(]"PATHCHARS_CLASS"*[)])*"PATHCHARS_CLASS"*)*"PATHTERM_CLASS")?"

typedef enum {
  FLAVOR_AS_IS,
  FLAVOR_DEFAULT_TO_HTTP,
  FLAVOR_EMAIL,
  FLAVOR_LP
} UrlRegexFlavor;

typedef struct {
  const char        *pattern;
  UrlRegexFlavor     flavor;
  GRegexCompileFlags flags;
} UrlRegexPattern;

static UrlRegexPattern url_regex_patterns[] = {
  { SCHEME "//(?:" USERPASS "\\@)?" HOST PORT URLPATH, FLAVOR_AS_IS, G_REGEX_CASELESS },
  { "(?:www|ftp)" HOSTCHARS_CLASS "*\\." HOST PORT URLPATH, FLAVOR_DEFAULT_TO_HTTP, G_REGEX_CASELESS},
  { "(?:mailto:)?" USERCHARS_CLASS "[" USERCHARS ".]*\\@" HOSTCHARS_CLASS "+\\." HOST, FLAVOR_EMAIL, G_REGEX_CASELESS  },
  { "(?:lp: #)([[:digit:]]+)", FLAVOR_LP, G_REGEX_CASELESS}
};

static GRegex         **url_regexes;
static UrlRegexFlavor  *url_regex_flavors;
static guint            n_url_regexes;

static char *urlregex_expand(GMatchInfo *match_info, UrlRegexFlavor flavor);

/**
 * urlregex_init:
 *
 * Compiles all of the url matching regular expressions.
 * FIXME: Return immediately or error if initialized more than once
 **/
void
urlregex_init(void)
{
  guint i;

  n_url_regexes = G_N_ELEMENTS(url_regex_patterns);
  url_regexes = g_new0(GRegex*, n_url_regexes);
  url_regex_flavors = g_new0(UrlRegexFlavor, n_url_regexes);

  for (i = 0; i < n_url_regexes; i++) {
    GError *error = NULL;

    url_regexes[i] = g_regex_new(url_regex_patterns[i].pattern,
        url_regex_patterns[i].flags | G_REGEX_OPTIMIZE, 0, &error);

    if (error != NULL) {
      g_message("%s", error->message);
      g_error_free(error);
    }

    url_regex_flavors[i] = url_regex_patterns[i].flavor;
  }
}

/**
 * urlregex_count:
 *
 * Returns the number of available url patterns.
 **/
guint
urlregex_count(void)
{
  return n_url_regexes;
}

/**
 * urlregex_split:
 * @text: the text to split
 * @index: the pattern to use
 *
 * Splits the text into a list of MatchGroup objects.
 **/
GList *
urlregex_split(const char *text, guint index)
{
  GList *result = NULL;
  GRegex *pattern = url_regexes[index];
  GMatchInfo *match_info;
  int text_length = strlen(text);

  int start_pos = 0;
  int end_pos = 0;
  int last_pos = 0;
  int len = 0;

  gchar *token;
  gchar *expanded;

  g_regex_match(pattern, text, 0, &match_info);

  while (g_match_info_matches(match_info)) {
    /* Append previously unmatched text */
    g_match_info_fetch_pos(match_info, 0, &start_pos, &end_pos);
    len = start_pos - last_pos;
    if (len > 0) {
      token = g_strndup(text + last_pos, len);
      result = g_list_append(result, urlregex_matchgroup_new(token, token, NOT_MATCHED));
      g_free(token);
    }

    /* Append matched text */
    token = urlregex_expand(match_info, FLAVOR_AS_IS);
    expanded = urlregex_expand(match_info, url_regex_flavors[index]);
    result = g_list_append(result, urlregex_matchgroup_new(token, expanded, MATCHED));
    g_free(token);
    g_free(expanded);

    g_match_info_next(match_info, NULL);
    last_pos = end_pos;
  }
  /* Append the text after the last match */
  if (last_pos < text_length) {
    token = g_strdup(text + last_pos);
    result = g_list_append(result, urlregex_matchgroup_new(token, token, NOT_MATCHED));
    g_free(token);
  }

  g_match_info_free(match_info);

  return result;
}

/**
 * urlregex_expand:
 * @match_info: describes the matched url
 * @flavor: the type of url
 *
 * Expands the matched url based on the given flavor.
 **/
static char *
urlregex_expand(GMatchInfo *match_info, UrlRegexFlavor flavor)
{
  char *t1;
  char *t2;

  switch(flavor) {
    case FLAVOR_DEFAULT_TO_HTTP:
      t1 = g_match_info_fetch(match_info, 0);
      t2 = g_strconcat(HTTP_BASE_URL, t1, NULL);
      g_free(t1);
      return t2;
    case FLAVOR_EMAIL:
      t1 = g_match_info_fetch(match_info, 0);
      if (!g_str_has_prefix(t1, MAILTO_BASE_URL)) {
        t2 = g_strconcat(MAILTO_BASE_URL, t1, NULL);
        g_free(t1);
        return t2;
      }
      else
        return t1;
    case FLAVOR_LP:
      t1 = g_match_info_fetch(match_info, 1);
      t2 = g_strconcat(LP_BUG_BASE_URL, t1, NULL);
      g_free(t1);
      return t2;
    default:
      return g_match_info_fetch(match_info, 0);
  }
}

/**
 * urlregex_split_all:
 * @text: the text to split
 *
 * Splits the text into a list of MatchGroup objects, applying each url pattern
 * available in order to each of the unmatched sections, keeping the list flat.
 **/
GList *
urlregex_split_all(const char *text)
{
  GList *result = NULL;
  GList *temp = NULL;
  guint i;

  result = g_list_append(result, urlregex_matchgroup_new(text, text, NOT_MATCHED));

  /* Apply each regex in order to sections that haven't yet been matched */
  for (i = 0; i < n_url_regexes; i++) {
    GList *item;
    temp = NULL;
    for (item = result; item; item = item->next) {
      MatchGroup *group = (MatchGroup *)item->data;
      if (group->type == NOT_MATCHED) {
        GList *list = urlregex_split(group->text, i);
        GList *subitem;
        for (subitem = list; subitem; subitem = subitem->next) {
          MatchGroup *subgroup = (MatchGroup *)subitem->data;
          temp = g_list_append(temp, subgroup);
        }
        g_list_free(list);
        urlregex_matchgroup_free(group);
      }
      else {
        temp = g_list_append(temp, group);
      }
    }
    g_list_free(result);
    result = temp;
  }

  return result;
}

/**
 * urlregex_matchgroup_new:
 * @text: the original text
 * @expanded: the expanded url
 * @type: whether this is a matched or unmatched group
 *
 * Creates a new MatchGroup object.
 **/
MatchGroup *
urlregex_matchgroup_new(const char *text, const char *expanded, MatchType type)
{
  MatchGroup *result = g_new0(MatchGroup, 1);
  result->text = g_strdup(text);
  /* TODO: Save space using same data if text == expanded? */
  result->expanded = g_strdup(expanded);
  result->type = type;
  return result;
}

/**
 * urlregex_matchgroup_free:
 * @group: the match group
 *
 * Frees the MatchGroup object.
 **/
void
urlregex_matchgroup_free(MatchGroup *group)
{
  g_free(group->expanded);
  group->expanded = NULL;
  g_free(group->text);
  group->text = NULL;
  g_free(group);
}

/**
 * urlregex_matchgroup_list_free:
 * @list: the match group list
 *
 * Frees a list of MatchGroup objects returned from split or split_all.
 **/
void
urlregex_matchgroup_list_free(GList *list)
{
  GList *item;
  for (item = list; item; item = item->next) {
    urlregex_matchgroup_free((MatchGroup *)item->data);
  }
  g_list_free(list);
}