/* 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 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include #include #include #include #include "xchat.h" #include "xchatc.h" #include "cfgfiles.h" #include "fe.h" #include "tree.h" #include "url.h" #ifdef HAVE_STRINGS_H #include #endif void *url_tree = NULL; GTree *url_btree = NULL; 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; g_tree_destroy (url_btree); url_btree = NULL; } static int url_save_cb (char *url, FILE *fd) { fprintf (fd, "%s\n", url); return TRUE; } void url_save_tree (const char *fname, const char *mode, gboolean fullpath) { FILE *fd; if (fullpath) fd = xchat_fopen_file (fname, mode, XOF_FULLPATH); else fd = xchat_fopen_file (fname, mode, 0); if (fd == NULL) return; tree_foreach (url_tree, (tree_traverse_func *)url_save_cb, fd); fclose (fd); } static void url_save_node (char* url) { FILE *fd; /* open /url.log in append mode */ fd = xchat_fopen_file ("url.log", "a", 0); if (fd == NULL) { return; } fprintf (fd, "%s\n", url); fclose (fd); } static int url_find (char *urltext) { return (g_tree_lookup_extended (url_btree, urltext, NULL, NULL)); } static void url_add (char *urltext, int len) { char *data; int size; /* we don't need any URLs if we have neither URL grabbing nor URL logging enabled */ if (!prefs.url_grabber && !prefs.url_logging) { return; } data = malloc (len + 1); if (!data) { return; } memcpy (data, urltext, len); data[len] = 0; if (data[len - 1] == '.') /* chop trailing dot */ { len--; data[len] = 0; } /* chop trailing ) but only if there's no counterpart */ if (data[len - 1] == ')' && strchr (data, '(') == NULL) { data[len - 1] = 0; } if (prefs.url_logging) { url_save_node (data); } /* the URL is saved already, only continue if we need the URL grabber too */ if (!prefs.url_grabber) { free (data); return; } if (!url_tree) { url_tree = tree_new ((tree_cmp_func *)strcasecmp, NULL); url_btree = g_tree_new ((GCompareFunc)strcasecmp); } if (url_find (data)) { free (data); return; } size = tree_size (url_tree); /* 0 is unlimited */ if (prefs.url_grabber_limit > 0 && size >= prefs.url_grabber_limit) { /* the loop is necessary to handle having the limit lowered while xchat is running */ size -= prefs.url_grabber_limit; for(; size > 0; size--) { char *pos; pos = tree_remove_at_pos (url_tree, 0); g_tree_remove (url_btree, pos); free (pos); } } tree_append (url_tree, data); g_tree_insert (url_btree, data, GINT_TO_POINTER (tree_size (url_tree) - 1)); 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. */ int url_check_word (char *word, int len) { #define D(x) (x), ((sizeof (x)) - 1) static const struct { const char *s; int len; } prefix[] = { { D("irc.") }, { D("ftp.") }, { D("www.") }, { D("irc://") }, { D("ftp://") }, { D("http://") }, { D("https://") }, { D("file://") }, { D("rtsp://") }, { D("ut2004://") }, }, suffix[] = { { D(".org") }, { D(".net") }, { D(".com") }, { D(".edu") }, { D(".html") }, { D(".info") }, { D(".name") }, /* Some extra common suffixes. foo.blah/baz.php etc should work now, rather than needing http:// at the beginning. */ { D(".php") }, { D(".htm") }, { D(".aero") }, { D(".asia") }, { D(".biz") }, { D(".cat") }, { D(".coop") }, { D(".int") }, { D(".jobs") }, { D(".mobi") }, { D(".museum") }, { D(".pro") }, { D(".tel") }, { D(".travel") }, { D(".xxx") }, { D(".asp") }, { D(".aspx") }, { D(".shtml") }, { D(".xml") }, }; #undef D const char *at, *dot; int i, dots; /* this is pretty much the same as in logmask_is_fullpath() except with length checks and .\ for portable mode */ #ifdef WIN32 if ((len > 1 && word[0] == '\\') || (len > 2