2011-02-24 04:14:30 +01:00
|
|
|
/* X-Chat
|
|
|
|
* Copyright (C) 1998 Peter Zelezny.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2013-01-02 23:58:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2011-02-24 04:14:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2012-10-24 21:33:02 +02:00
|
|
|
#include "hexchat.h"
|
|
|
|
#include "hexchatc.h"
|
2011-02-24 04:14:30 +01:00
|
|
|
#include "cfgfiles.h"
|
|
|
|
#include "fe.h"
|
|
|
|
#include "tree.h"
|
|
|
|
#include "url.h"
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *url_tree = NULL;
|
2012-10-01 21:53:25 +02:00
|
|
|
GTree *url_btree = NULL;
|
2013-01-02 23:50:26 +01:00
|
|
|
static int do_an_re (const char *word, int *start, int *end, int *type);
|
|
|
|
static GRegex *re_url (void);
|
|
|
|
static GRegex *re_host (void);
|
2013-06-18 13:01:39 +02:00
|
|
|
static GRegex *re_host6 (void);
|
2013-01-02 23:50:26 +01:00
|
|
|
static GRegex *re_email (void);
|
|
|
|
static GRegex *re_nick (void);
|
|
|
|
static GRegex *re_channel (void);
|
|
|
|
static GRegex *re_path (void);
|
2011-02-24 04:14:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
url_free (char *url, void *data)
|
|
|
|
{
|
|
|
|
free (url);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
url_clear (void)
|
|
|
|
{
|
|
|
|
tree_foreach (url_tree, (tree_traverse_func *)url_free, NULL);
|
|
|
|
tree_destroy (url_tree);
|
|
|
|
url_tree = NULL;
|
2012-10-01 21:53:25 +02:00
|
|
|
g_tree_destroy (url_btree);
|
|
|
|
url_btree = NULL;
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
url_save_cb (char *url, FILE *fd)
|
|
|
|
{
|
|
|
|
fprintf (fd, "%s\n", url);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-13 10:03:39 +02:00
|
|
|
url_save_tree (const char *fname, const char *mode, gboolean fullpath)
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
|
|
|
|
if (fullpath)
|
2012-10-30 11:35:39 +01:00
|
|
|
fd = hexchat_fopen_file (fname, mode, XOF_FULLPATH);
|
2011-02-24 04:14:30 +01:00
|
|
|
else
|
2012-10-30 11:35:39 +01:00
|
|
|
fd = hexchat_fopen_file (fname, mode, 0);
|
2011-02-24 04:14:30 +01:00
|
|
|
if (fd == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tree_foreach (url_tree, (tree_traverse_func *)url_save_cb, fd);
|
|
|
|
fclose (fd);
|
|
|
|
}
|
|
|
|
|
2012-10-13 10:03:39 +02:00
|
|
|
static void
|
|
|
|
url_save_node (char* url)
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
2012-10-13 10:03:39 +02:00
|
|
|
FILE *fd;
|
|
|
|
|
|
|
|
/* open <config>/url.log in append mode */
|
2012-10-30 11:35:39 +01:00
|
|
|
fd = hexchat_fopen_file ("url.log", "a", 0);
|
2012-10-13 10:03:39 +02:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf (fd, "%s\n", url);
|
|
|
|
fclose (fd);
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
url_find (char *urltext)
|
|
|
|
{
|
2012-10-01 21:53:25 +02:00
|
|
|
return (g_tree_lookup_extended (url_btree, urltext, NULL, NULL));
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
url_add (char *urltext, int len)
|
|
|
|
{
|
2012-03-15 23:58:52 +01:00
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
|
2012-10-13 10:03:39 +02:00
|
|
|
/* we don't need any URLs if we have neither URL grabbing nor URL logging enabled */
|
2012-10-22 15:55:43 +02:00
|
|
|
if (!prefs.hex_url_grabber && !prefs.hex_url_logging)
|
2012-10-13 10:03:39 +02:00
|
|
|
{
|
2012-03-15 23:58:52 +01:00
|
|
|
return;
|
2012-10-13 10:03:39 +02:00
|
|
|
}
|
2012-03-15 23:58:52 +01:00
|
|
|
|
|
|
|
data = malloc (len + 1);
|
2011-02-24 04:14:30 +01:00
|
|
|
if (!data)
|
2012-10-13 10:03:39 +02:00
|
|
|
{
|
2011-02-24 04:14:30 +01:00
|
|
|
return;
|
2012-10-13 10:03:39 +02:00
|
|
|
}
|
2011-02-24 04:14:30 +01:00
|
|
|
memcpy (data, urltext, len);
|
|
|
|
data[len] = 0;
|
|
|
|
|
|
|
|
if (data[len - 1] == '.') /* chop trailing dot */
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
data[len] = 0;
|
|
|
|
}
|
2012-10-07 13:27:44 +02:00
|
|
|
/* chop trailing ) but only if there's no counterpart */
|
|
|
|
if (data[len - 1] == ')' && strchr (data, '(') == NULL)
|
2012-10-13 10:03:39 +02:00
|
|
|
{
|
2011-02-24 04:14:30 +01:00
|
|
|
data[len - 1] = 0;
|
2012-10-13 10:03:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-22 15:55:43 +02:00
|
|
|
if (prefs.hex_url_logging)
|
2012-10-13 10:03:39 +02:00
|
|
|
{
|
|
|
|
url_save_node (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the URL is saved already, only continue if we need the URL grabber too */
|
2012-10-22 15:55:43 +02:00
|
|
|
if (!prefs.hex_url_grabber)
|
2012-10-13 10:03:39 +02:00
|
|
|
{
|
|
|
|
free (data);
|
|
|
|
return;
|
|
|
|
}
|
2011-02-24 04:14:30 +01:00
|
|
|
|
2012-10-01 21:53:25 +02:00
|
|
|
if (!url_tree)
|
|
|
|
{
|
|
|
|
url_tree = tree_new ((tree_cmp_func *)strcasecmp, NULL);
|
|
|
|
url_btree = g_tree_new ((GCompareFunc)strcasecmp);
|
|
|
|
}
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
if (url_find (data))
|
|
|
|
{
|
|
|
|
free (data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-15 23:58:52 +01:00
|
|
|
size = tree_size (url_tree);
|
|
|
|
/* 0 is unlimited */
|
2012-10-22 15:55:43 +02:00
|
|
|
if (prefs.hex_url_grabber_limit > 0 && size >= prefs.hex_url_grabber_limit)
|
2012-03-15 23:58:52 +01:00
|
|
|
{
|
|
|
|
/* the loop is necessary to handle having the limit lowered while
|
2012-10-30 11:35:39 +01:00
|
|
|
HexChat is running */
|
2012-10-22 15:55:43 +02:00
|
|
|
size -= prefs.hex_url_grabber_limit;
|
2012-03-15 23:58:52 +01:00
|
|
|
for(; size > 0; size--)
|
2012-10-01 21:53:25 +02:00
|
|
|
{
|
|
|
|
char *pos;
|
|
|
|
|
|
|
|
pos = tree_remove_at_pos (url_tree, 0);
|
|
|
|
g_tree_remove (url_btree, pos);
|
|
|
|
free (pos);
|
|
|
|
}
|
2012-03-15 23:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tree_append (url_tree, data);
|
2012-10-01 21:53:25 +02:00
|
|
|
g_tree_insert (url_btree, data, GINT_TO_POINTER (tree_size (url_tree) - 1));
|
2011-02-24 04:14:30 +01:00
|
|
|
fe_url_add (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if a word is clickable. This is called on mouse motion events, so
|
|
|
|
keep it FAST! This new version was found to be almost 3x faster than
|
|
|
|
2.4.4 release. */
|
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
static int laststart = 0;
|
|
|
|
static int lastend = 0;
|
|
|
|
static int lasttype = 0;
|
|
|
|
|
2013-01-11 10:39:21 +01:00
|
|
|
static int
|
|
|
|
strchrs (char c, char *s)
|
|
|
|
{
|
|
|
|
while (*s)
|
|
|
|
if (c == *s++)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NICKPRE "~+!@%%&"
|
2011-02-24 04:14:30 +01:00
|
|
|
int
|
2013-01-02 23:50:26 +01:00
|
|
|
url_check_word (const char *word)
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
2013-01-02 23:50:26 +01:00
|
|
|
laststart = lastend = lasttype = 0;
|
|
|
|
if (do_an_re (word, &laststart, &lastend, &lasttype))
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
2013-01-02 23:50:26 +01:00
|
|
|
switch (lasttype)
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
2013-01-11 10:39:21 +01:00
|
|
|
char *str;
|
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
case WORD_NICK:
|
2013-01-11 10:39:21 +01:00
|
|
|
if (strchrs (word[laststart], NICKPRE))
|
2013-01-02 23:50:26 +01:00
|
|
|
laststart++;
|
2013-01-12 01:12:52 +01:00
|
|
|
str = g_strndup (&word[laststart], lastend - laststart);
|
2013-01-11 10:39:21 +01:00
|
|
|
if (!userlist_find (current_sess, str))
|
2013-01-02 23:50:26 +01:00
|
|
|
lasttype = 0;
|
2013-01-12 01:12:52 +01:00
|
|
|
g_free (str);
|
2013-01-02 23:50:26 +01:00
|
|
|
return lasttype;
|
|
|
|
case WORD_EMAIL:
|
|
|
|
if (!isalnum (word[laststart]))
|
|
|
|
laststart++;
|
|
|
|
/* Fall through */
|
|
|
|
case WORD_URL:
|
|
|
|
case WORD_HOST:
|
2013-06-18 13:01:39 +02:00
|
|
|
case WORD_HOST6:
|
2013-01-02 23:50:26 +01:00
|
|
|
case WORD_CHANNEL:
|
2013-01-03 00:03:16 +01:00
|
|
|
case WORD_PATH:
|
2013-01-02 23:50:26 +01:00
|
|
|
return lasttype;
|
|
|
|
default:
|
|
|
|
return 0; /* Should not occur */
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-02 23:50:26 +01:00
|
|
|
else
|
|
|
|
return 0;
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
|
|
|
|
2012-11-13 21:06:35 +01:00
|
|
|
/* List of IRC commands for which contents (and thus possible URLs)
|
|
|
|
* are visible to the user. NOTE: Trailing blank required in each. */
|
|
|
|
static char *commands[] = {
|
|
|
|
"NOTICE ",
|
|
|
|
"PRIVMSG ",
|
|
|
|
"TOPIC ",
|
|
|
|
"332 ", /* RPL_TOPIC */
|
|
|
|
"372 " /* RPL_MOTD */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
|
|
|
|
|
2011-02-24 04:14:30 +01:00
|
|
|
void
|
|
|
|
url_check_line (char *buf, int len)
|
|
|
|
{
|
2013-01-02 23:50:26 +01:00
|
|
|
GRegex *re(void);
|
|
|
|
GMatchInfo *gmi;
|
2011-02-24 04:14:30 +01:00
|
|
|
char *po = buf;
|
2013-01-02 23:50:26 +01:00
|
|
|
int i;
|
2012-11-13 21:06:35 +01:00
|
|
|
|
|
|
|
/* Skip over message prefix */
|
|
|
|
if (*po == ':')
|
|
|
|
{
|
|
|
|
po = strchr (po, ' ');
|
|
|
|
if (!po)
|
|
|
|
return;
|
|
|
|
po++;
|
|
|
|
}
|
|
|
|
/* Allow only commands from the above list */
|
|
|
|
for (i = 0; i < ARRAY_SIZE (commands); i++)
|
|
|
|
{
|
|
|
|
char *cmd = commands[i];
|
|
|
|
int len = strlen (cmd);
|
|
|
|
|
|
|
|
if (strncmp (cmd, po, len) == 0)
|
|
|
|
{
|
|
|
|
po += len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == ARRAY_SIZE (commands))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Skip past the channel name or user nick */
|
|
|
|
po = strchr (po, ' ');
|
|
|
|
if (!po)
|
|
|
|
return;
|
|
|
|
po++;
|
2011-02-24 04:14:30 +01:00
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
g_regex_match(re_url(), po, 0, &gmi);
|
|
|
|
while (g_match_info_matches(gmi))
|
|
|
|
{
|
|
|
|
int start, end;
|
|
|
|
|
|
|
|
g_match_info_fetch_pos(gmi, 0, &start, &end);
|
2013-03-29 21:11:17 +01:00
|
|
|
while (end > start && (po[end - 1] == '\r' || po[end - 1] == '\n'))
|
2013-03-31 21:14:52 +02:00
|
|
|
end--;
|
2013-01-02 23:50:26 +01:00
|
|
|
if (g_strstr_len (po + start, end - start, "://"))
|
|
|
|
url_add(po + start, end - start);
|
|
|
|
g_match_info_next(gmi, NULL);
|
|
|
|
}
|
|
|
|
g_match_info_free(gmi);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
url_last (int *lstart, int *lend)
|
|
|
|
{
|
|
|
|
*lstart = laststart;
|
|
|
|
*lend = lastend;
|
|
|
|
return lasttype;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-06-17 23:43:18 +02:00
|
|
|
do_an_re(const char *word, int *start, int *end, int *type)
|
2013-01-02 23:50:26 +01:00
|
|
|
{
|
|
|
|
typedef struct func_s {
|
|
|
|
GRegex *(*fn)(void);
|
|
|
|
int type;
|
|
|
|
} func_t;
|
|
|
|
func_t funcs[] =
|
|
|
|
{
|
|
|
|
{ re_url, WORD_URL },
|
2013-06-16 16:36:52 +02:00
|
|
|
{ re_email, WORD_EMAIL },
|
2013-01-02 23:50:26 +01:00
|
|
|
{ re_channel, WORD_CHANNEL },
|
2013-06-18 13:01:39 +02:00
|
|
|
{ re_host6, WORD_HOST6 },
|
2013-04-19 21:41:49 +02:00
|
|
|
{ re_host, WORD_HOST },
|
2013-01-02 23:50:26 +01:00
|
|
|
{ re_path, WORD_PATH },
|
|
|
|
{ re_nick, WORD_NICK }
|
|
|
|
};
|
2011-02-24 04:14:30 +01:00
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
GMatchInfo *gmi;
|
|
|
|
int k;
|
2011-02-24 04:14:30 +01:00
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
for (k = 0; k < sizeof funcs / sizeof (func_t); k++)
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
2013-01-02 23:50:26 +01:00
|
|
|
g_regex_match (funcs[k].fn(), word, 0, &gmi);
|
|
|
|
if (!g_match_info_matches (gmi))
|
2011-02-24 04:14:30 +01:00
|
|
|
{
|
2013-01-02 23:50:26 +01:00
|
|
|
g_match_info_free (gmi);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (g_match_info_matches (gmi))
|
|
|
|
{
|
|
|
|
g_match_info_fetch_pos (gmi, 0, start, end);
|
|
|
|
g_match_info_next (gmi, NULL);
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
2013-01-02 23:50:26 +01:00
|
|
|
g_match_info_free (gmi);
|
|
|
|
*type = funcs[k].type;
|
|
|
|
return TRUE;
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|
2013-01-02 23:50:26 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Miscellaneous description --- */
|
2013-01-15 22:26:11 +01:00
|
|
|
#define DOMAIN "[a-z0-9][-a-z0-9]*(\\.[-a-z0-9]+)*\\."
|
2013-01-15 22:19:29 +01:00
|
|
|
#define TLD "[a-z][-a-z0-9]*[a-z]"
|
2013-06-16 03:57:38 +02:00
|
|
|
#define IPADDR "[0-9]{1,3}(\\.[0-9]{1,3}){3}"
|
2013-06-16 21:16:58 +02:00
|
|
|
#define IPV6GROUP "([0-9a-f]{0,4})"
|
|
|
|
#define IPV6ADDR "((" IPV6GROUP "(:" IPV6GROUP "){7})" \
|
|
|
|
"|(" IPV6GROUP "(:" IPV6GROUP ")*:(:" IPV6GROUP ")+))" /* with :: compression */
|
2013-06-16 03:57:38 +02:00
|
|
|
#define HOST "(" DOMAIN TLD "|" IPADDR "|" IPV6ADDR ")"
|
2013-06-17 23:43:18 +02:00
|
|
|
/* In urls the IPv6 must be enclosed in square brackets */
|
|
|
|
#define HOST_URL "(" DOMAIN TLD "|" IPADDR "|" "\\[" IPV6ADDR "\\]" ")"
|
2013-06-18 13:01:39 +02:00
|
|
|
#define PORT "(:[1-9][0-9]{0,4})"
|
|
|
|
#define OPT_PORT "(" PORT ")?"
|
2013-01-02 23:50:26 +01:00
|
|
|
|
|
|
|
GRegex *
|
2013-06-18 13:07:46 +02:00
|
|
|
make_re (char *grist)
|
2013-01-02 23:50:26 +01:00
|
|
|
{
|
|
|
|
GRegex *ret;
|
|
|
|
GError *err = NULL;
|
|
|
|
|
2013-06-16 16:36:52 +02:00
|
|
|
ret = g_regex_new (grist, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0, &err);
|
2013-01-02 23:50:26 +01:00
|
|
|
g_free (grist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HOST description --- */
|
|
|
|
/* (see miscellaneous above) */
|
|
|
|
static GRegex *
|
|
|
|
re_host (void)
|
|
|
|
{
|
|
|
|
static GRegex *host_ret;
|
|
|
|
char *grist;
|
2013-02-13 21:52:49 +01:00
|
|
|
|
|
|
|
if (host_ret) return host_ret;
|
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
grist = g_strdup_printf (
|
2013-06-18 13:01:39 +02:00
|
|
|
"("
|
|
|
|
"(" HOST_URL PORT ")|(" HOST ")"
|
2013-01-02 23:50:26 +01:00
|
|
|
")"
|
|
|
|
);
|
2013-06-18 13:07:46 +02:00
|
|
|
host_ret = make_re (grist);
|
2013-01-02 23:50:26 +01:00
|
|
|
return host_ret;
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:01:39 +02:00
|
|
|
static GRegex *
|
|
|
|
re_host6 (void)
|
|
|
|
{
|
|
|
|
static GRegex *host6_ret;
|
|
|
|
char *grist;
|
|
|
|
|
|
|
|
if (host6_ret) return host6_ret;
|
|
|
|
|
|
|
|
grist = g_strdup_printf (
|
|
|
|
"("
|
|
|
|
"(" IPV6ADDR ")|(" "\\[" IPV6ADDR "\\]" PORT ")"
|
|
|
|
")"
|
|
|
|
);
|
|
|
|
|
2013-06-18 13:07:46 +02:00
|
|
|
host6_ret = make_re (grist);
|
2013-06-18 13:01:39 +02:00
|
|
|
|
|
|
|
return host6_ret;
|
|
|
|
}
|
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
/* URL description --- */
|
|
|
|
#define SCHEME "(%s)"
|
|
|
|
#define LPAR "\\("
|
|
|
|
#define RPAR "\\)"
|
|
|
|
#define NOPARENS "[^() \t]*"
|
2013-06-16 16:36:52 +02:00
|
|
|
#define PATH \
|
|
|
|
"(" \
|
|
|
|
"(" LPAR NOPARENS RPAR ")" \
|
|
|
|
"|" \
|
|
|
|
"(" NOPARENS ")" \
|
|
|
|
")*" /* Zero or more occurrences of either of these */ \
|
|
|
|
"(?<![.,?!\\]])" /* Not allowed to end with these */
|
2013-06-16 21:00:35 +02:00
|
|
|
#define USERINFO "([-a-z0-9._~%]+(:[-a-z0-9._~%]*)?@)"
|
2013-06-16 16:36:52 +02:00
|
|
|
|
|
|
|
/* Flags used to describe URIs (RFC 3986)
|
|
|
|
*
|
|
|
|
* Bellow is an example of what the flags match.
|
|
|
|
*
|
|
|
|
* URI_AUTHORITY - http://example.org:80/foo/bar
|
|
|
|
* ^^^^^^^^^^^^^^^^
|
|
|
|
* URI_USERINFO/URI_OPT_USERINFO - http://user@example.org:80/foo/bar
|
|
|
|
* ^^^^^
|
|
|
|
* URI_PATH - http://example.org:80/foo/bar
|
|
|
|
* ^^^^^^^^
|
|
|
|
*/
|
|
|
|
#define URI_AUTHORITY (1 << 0)
|
|
|
|
#define URI_OPT_USERINFO (1 << 1)
|
|
|
|
#define URI_USERINFO (1 << 2)
|
|
|
|
#define URI_PATH (1 << 3)
|
2013-01-02 23:50:26 +01:00
|
|
|
|
2013-06-16 16:36:52 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
const char *scheme; /* scheme name. e.g. http */
|
|
|
|
const char *path_sep; /* string that begins the path */
|
2013-06-16 20:09:50 +02:00
|
|
|
int flags; /* see above (flag macros) */
|
2013-06-16 16:36:52 +02:00
|
|
|
} uri[] = {
|
2013-06-16 20:11:26 +02:00
|
|
|
{ "irc", "/", URI_PATH },
|
|
|
|
{ "ircs", "/", URI_PATH },
|
2013-06-16 16:36:52 +02:00
|
|
|
{ "rtsp", "/", URI_AUTHORITY | URI_PATH },
|
|
|
|
{ "feed", "/", URI_AUTHORITY | URI_PATH },
|
|
|
|
{ "teamspeak", "?", URI_AUTHORITY | URI_PATH },
|
|
|
|
{ "ftp", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "sftp", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "ftps", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "http", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "https", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "cvs", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "svn", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "git", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
2013-06-18 13:32:55 +02:00
|
|
|
{ "bzr", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
2013-06-16 16:36:52 +02:00
|
|
|
{ "rsync", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "mumble", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "ventrilo", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "xmpp", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "h323", ";", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "imap", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "pop", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "nfs", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "smb", "/", URI_AUTHORITY | URI_OPT_USERINFO | URI_PATH },
|
|
|
|
{ "ssh", "", URI_AUTHORITY | URI_OPT_USERINFO },
|
|
|
|
{ "sip", "", URI_AUTHORITY | URI_USERINFO },
|
|
|
|
{ "sips", "", URI_AUTHORITY | URI_USERINFO },
|
|
|
|
{ "magnet", "?", URI_PATH },
|
|
|
|
{ "mailto", "", URI_PATH },
|
|
|
|
{ "bitcoin", "", URI_PATH },
|
|
|
|
{ "gtalk", "", URI_PATH },
|
|
|
|
{ "steam", "", URI_PATH },
|
2013-06-16 20:09:50 +02:00
|
|
|
{ "file", "/", URI_PATH },
|
2013-06-18 13:32:55 +02:00
|
|
|
{ "callto", "", URI_PATH },
|
|
|
|
{ "skype", "", URI_PATH },
|
|
|
|
{ "geo", "", URI_PATH },
|
2013-06-19 11:07:45 +02:00
|
|
|
{ "spotify", "", URI_PATH },
|
2013-06-17 23:43:18 +02:00
|
|
|
{ NULL, "", 0}
|
2013-01-02 23:50:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static GRegex *
|
|
|
|
re_url (void)
|
|
|
|
{
|
2013-06-16 16:36:52 +02:00
|
|
|
static GRegex *url_ret = NULL;
|
|
|
|
GString *grist_gstr;
|
2013-01-02 23:50:26 +01:00
|
|
|
char *grist;
|
2013-06-16 16:36:52 +02:00
|
|
|
int i;
|
2013-01-02 23:50:26 +01:00
|
|
|
|
|
|
|
if (url_ret) return url_ret;
|
|
|
|
|
2013-06-16 16:36:52 +02:00
|
|
|
grist_gstr = g_string_new (NULL);
|
|
|
|
|
|
|
|
/* Add regex "host/path", representing a "schemeless" url */
|
2013-06-17 23:43:18 +02:00
|
|
|
g_string_append (grist_gstr, "(" HOST_URL OPT_PORT "/" "(" PATH ")?" ")");
|
2013-06-16 16:36:52 +02:00
|
|
|
|
|
|
|
for (i = 0; uri[i].scheme; i++)
|
|
|
|
{
|
|
|
|
g_string_append (grist_gstr, "|(");
|
|
|
|
g_string_append_printf (grist_gstr, "%s:", uri[i].scheme);
|
|
|
|
|
|
|
|
if (uri[i].flags & URI_AUTHORITY)
|
|
|
|
g_string_append (grist_gstr, "//");
|
|
|
|
|
|
|
|
if (uri[i].flags & URI_USERINFO)
|
|
|
|
g_string_append (grist_gstr, USERINFO);
|
|
|
|
else if (uri[i].flags & URI_OPT_USERINFO)
|
|
|
|
g_string_append (grist_gstr, USERINFO "?");
|
|
|
|
|
|
|
|
if (uri[i].flags & URI_AUTHORITY)
|
2013-06-17 23:43:18 +02:00
|
|
|
g_string_append (grist_gstr, HOST_URL OPT_PORT);
|
2013-06-16 16:36:52 +02:00
|
|
|
|
|
|
|
if (uri[i].flags & URI_PATH)
|
|
|
|
{
|
|
|
|
char *sep_escaped;
|
|
|
|
|
|
|
|
sep_escaped = g_regex_escape_string (uri[i].path_sep,
|
|
|
|
strlen(uri[i].path_sep));
|
|
|
|
|
|
|
|
g_string_append_printf(grist_gstr, "(" "%s" PATH ")?",
|
|
|
|
sep_escaped);
|
|
|
|
|
|
|
|
g_free(sep_escaped);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append(grist_gstr, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
grist = g_string_free (grist_gstr, FALSE);
|
|
|
|
|
2013-06-18 13:07:46 +02:00
|
|
|
url_ret = make_re (grist);
|
2013-06-16 16:36:52 +02:00
|
|
|
|
2013-01-02 23:50:26 +01:00
|
|
|
return url_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EMAIL description --- */
|
2013-06-17 23:43:18 +02:00
|
|
|
#define EMAIL "[a-z][-_a-z0-9]+@" "(" HOST_URL ")"
|
2013-01-02 23:50:26 +01:00
|
|
|
|
|
|
|
static GRegex *
|
|
|
|
re_email (void)
|
|
|
|
{
|
|
|
|
static GRegex *email_ret;
|
|
|
|
char *grist;
|
|
|
|
|
|
|
|
if (email_ret) return email_ret;
|
|
|
|
|
|
|
|
grist = g_strdup_printf (
|
2013-06-18 13:01:39 +02:00
|
|
|
"("
|
2013-01-02 23:50:26 +01:00
|
|
|
EMAIL
|
|
|
|
")"
|
|
|
|
);
|
2013-06-18 13:07:46 +02:00
|
|
|
email_ret = make_re (grist);
|
2013-01-02 23:50:26 +01:00
|
|
|
return email_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NICK description --- */
|
2013-01-11 10:39:21 +01:00
|
|
|
/* For NICKPRE see before url_check_word() */
|
2013-01-02 23:50:26 +01:00
|
|
|
#define NICKHYP "-"
|
|
|
|
#define NICKLET "a-z"
|
|
|
|
#define NICKDIG "0-9"
|
|
|
|
/* Note for NICKSPE: \\\\ boils down to a single \ */
|
|
|
|
#define NICKSPE "\\[\\]\\\\`_^{|}"
|
2013-04-06 20:51:22 +02:00
|
|
|
#if 0
|
2013-01-03 23:13:20 +01:00
|
|
|
#define NICK0 "[" NICKPRE "]?[" NICKLET NICKSPE "]"
|
2013-04-06 20:51:22 +02:00
|
|
|
#else
|
|
|
|
/* Allow violation of rfc 2812 by allowing digit as first char */
|
|
|
|
/* Rationale is that do_an_re() above will anyway look up what */
|
|
|
|
/* we find, and that WORD_NICK is the last item in the array */
|
|
|
|
/* that do_an_re() runs through. */
|
|
|
|
#define NICK0 "[" NICKPRE "]?[" NICKLET NICKDIG NICKSPE "]"
|
|
|
|
#endif
|
2013-01-03 23:24:04 +01:00
|
|
|
#define NICK1 "[" NICKHYP NICKLET NICKDIG NICKSPE "]*"
|
2013-01-02 23:50:26 +01:00
|
|
|
#define NICK NICK0 NICK1
|
|
|
|
|
|
|
|
static GRegex *
|
|
|
|
re_nick (void)
|
|
|
|
{
|
|
|
|
static GRegex *nick_ret;
|
|
|
|
char *grist;
|
|
|
|
|
|
|
|
if (nick_ret) return nick_ret;
|
|
|
|
|
|
|
|
grist = g_strdup_printf (
|
2013-06-18 13:01:39 +02:00
|
|
|
"("
|
2013-01-02 23:50:26 +01:00
|
|
|
NICK
|
|
|
|
")"
|
|
|
|
);
|
2013-06-18 13:07:46 +02:00
|
|
|
nick_ret = make_re (grist);
|
2013-01-02 23:50:26 +01:00
|
|
|
return nick_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CHANNEL description --- */
|
|
|
|
#define CHANNEL "#[^ \t\a,:]+"
|
|
|
|
|
|
|
|
static GRegex *
|
|
|
|
re_channel (void)
|
|
|
|
{
|
|
|
|
static GRegex *channel_ret;
|
|
|
|
char *grist;
|
|
|
|
|
|
|
|
if (channel_ret) return channel_ret;
|
|
|
|
|
|
|
|
grist = g_strdup_printf (
|
2013-06-18 13:01:39 +02:00
|
|
|
"("
|
2013-01-02 23:50:26 +01:00
|
|
|
CHANNEL
|
|
|
|
")"
|
|
|
|
);
|
2013-06-18 13:07:46 +02:00
|
|
|
channel_ret = make_re (grist);
|
2013-01-02 23:50:26 +01:00
|
|
|
return channel_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PATH description --- */
|
|
|
|
#ifdef WIN32
|
2013-03-11 17:55:29 +01:00
|
|
|
/* Windows path can be .\ ..\ or C: D: etc */
|
2013-06-16 16:36:52 +02:00
|
|
|
#define FS_PATH "^(\\.{1,2}\\\\|[a-z]:).*"
|
2013-01-02 23:50:26 +01:00
|
|
|
#else
|
|
|
|
/* Linux path can be / or ./ or ../ etc */
|
2013-06-16 16:36:52 +02:00
|
|
|
#define FS_PATH "^(/|\\./|\\.\\./).*"
|
2013-01-02 23:50:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static GRegex *
|
|
|
|
re_path (void)
|
|
|
|
{
|
|
|
|
static GRegex *path_ret;
|
|
|
|
char *grist;
|
|
|
|
|
|
|
|
if (path_ret) return path_ret;
|
|
|
|
|
|
|
|
grist = g_strdup_printf (
|
2013-06-18 13:01:39 +02:00
|
|
|
"("
|
2013-06-16 16:36:52 +02:00
|
|
|
FS_PATH
|
2013-01-02 23:50:26 +01:00
|
|
|
")"
|
|
|
|
);
|
2013-06-18 13:07:46 +02:00
|
|
|
path_ret = make_re (grist);
|
2013-01-02 23:50:26 +01:00
|
|
|
return path_ret;
|
2011-02-24 04:14:30 +01:00
|
|
|
}
|