summary refs log tree commit diff stats
path: root/src/common/url.c
blob: 92aeab0a41a28e2b04d2628fe4c72b504007445e (plain) (blame)
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
277
278
279
280
/* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "xchat.h"
#include "cfgfiles.h"
#include "fe.h"
#include "tree.h"
#include "url.h"
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

void *url_tree = 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;
}

static int
url_save_cb (char *url, FILE *fd)
{
	fprintf (fd, "%s\n", url);
	return TRUE;
}

void
url_save (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);
}

void
url_autosave (void)
{
	url_save ("url.save", "a", FALSE);
}

static int
url_find (char *urltext)
{
	int pos;

	if (tree_find (url_tree, urltext, (tree_cmp_func *)strcasecmp, NULL, &pos))
		return 1;
	return 0;
}

static void
url_add (char *urltext, int len)
{
	char *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;
	}
	if (data[len - 1] == ')')	/* chop trailing ) */
		data[len - 1] = 0;

	if (url_find (data))
	{
		free (data);
		return;
	}

	if (!url_tree)
		url_tree = tree_new ((tree_cmp_func *)strcasecmp, NULL);

	tree_insert (url_tree, data);
	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") },
	};
#undef D
	const char *at, *dot;
	int i, dots;

	if (len > 1 && word[1] == '#' && strchr("@+^%*#", word[0]))
		return WORD_CHANNEL;

	if ((word[0] == '#' || word[0] == '&') && word[1] != '#' && word[1] != 0)
		return WORD_CHANNEL;

	for (i = 0; i < G_N_ELEMENTS(prefix); i++)
	{
		int l;

		l = prefix[i].len;
		if (len > l)
		{
			int j;

			/* This is pretty much strncasecmp(). */
			for (j = 0; j < l; j++)
			{
				unsigned char c = word[j];
				if (tolower(c) != prefix[i].s[j])
					break;
			}
			if (j == l)
				return WORD_URL;
		}
	}

	at = strchr (word, '@');	  /* check for email addy */
	dot = strrchr (word, '.');
	if (at && dot)
	{
		if (at < dot)
		{
			if (strchr (word, '*'))
				return WORD_HOST;
			else
				return WORD_EMAIL;
		}
	}
 
	/* check if it's an IP number */
	dots = 0;
	for (i = 0; i < len; i++)
	{
		if (word[i] == '.' && i > 0)
			dots++;	/* allow 127.0.0.1:80 */
		else if (!isdigit ((unsigned char) word[i]) && word[i] != ':')
		{
			dots = 0;
			break;
		}
	}
	if (dots == 3)
		return WORD_HOST;

	if (len > 5)
	{
		for (i = 0; i < G_N_ELEMENTS(suffix); i++)
		{
			int l;

			l = suffix[i].len;
			if (len > l)
			{
				const unsigned char *p = &word[len - l];
				int j;

				/* This is pretty much strncasecmp(). */
				for (j = 0; j < l; j++)
				{
					if (tolower(p[j]) != suffix[i].s[j])
						break;
				}
				if (j == l)
					return WORD_HOST;
			}
		}

		if (word[len - 3] == '.' &&
			 isalpha ((unsigned char) word[len - 2]) &&
				isalpha ((unsigned char) word[len - 1]))
			return WORD_HOST;
	}

	return 0;
}

void
url_check_line (char *buf, int len)
{
	char *po = buf;
	char *start;
	int wlen;

	if (buf[0] == ':' && buf[1] != 0)
		po++;

	start = po;

	/* check each "word" (space separated) */
	while (1)
	{
		switch (po[0])
		{
		case 0:
		case ' ':
			wlen = po - start;
			if (wlen > 2)
			{
				if (url_check_word (start, wlen) == WORD_URL)
				{
					url_add (start, wlen);
				}
			}
			if (po[0] == 0)
				return;
			po++;
			start = po;
			break;

		default:
			po++;
		}
	}
}
func (userdata, &iter); if (gtk_tree_model_iter_children (model, &inner, &iter)) { do func (userdata, &inner); while (gtk_tree_model_iter_next (model, &inner)); } } while (gtk_tree_model_iter_next (model, &iter)); } } static void chanview_pop_cb (chanview *cv, GtkTreeIter *iter) { chan *ch; char *name; PangoAttrList *attr; gtk_tree_model_get (GTK_TREE_MODEL (cv->store), iter, COL_NAME, &name, COL_CHAN, &ch, COL_ATTR, &attr, -1); ch->impl = cv->func_add (cv, ch, name, NULL); if (attr) { cv->func_set_color (ch, attr); pango_attr_list_unref (attr); } g_free (name); } static void chanview_populate (chanview *cv) { model_foreach_1 (GTK_TREE_MODEL (cv->store), (void *)chanview_pop_cb, cv); } void chanview_set_impl (chanview *cv, int type) { /* cleanup the old one */ if (cv->func_cleanup) cv->func_cleanup (cv); switch (type) { case 0: cv->func_init = cv_tabs_init; cv->func_postinit = cv_tabs_postinit; cv->func_add = cv_tabs_add; cv->func_move_focus = cv_tabs_move_focus; cv->func_change_orientation = cv_tabs_change_orientation; cv->func_remove = cv_tabs_remove; cv->func_move = cv_tabs_move; cv->func_move_family = cv_tabs_move_family; cv->func_focus = cv_tabs_focus; cv->func_set_color = cv_tabs_set_color; cv->func_rename = cv_tabs_rename; cv->func_is_collapsed = cv_tabs_is_collapsed; cv->func_get_parent = cv_tabs_get_parent; cv->func_cleanup = cv_tabs_cleanup; break; default: cv->func_init = cv_tree_init; cv->func_postinit = cv_tree_postinit; cv->func_add = cv_tree_add; cv->func_move_focus = cv_tree_move_focus; cv->func_change_orientation = cv_tree_change_orientation; cv->func_remove = cv_tree_remove; cv->func_move = cv_tree_move; cv->func_move_family = cv_tree_move_family; cv->func_focus = cv_tree_focus; cv->func_set_color = cv_tree_set_color; cv->func_rename = cv_tree_rename; cv->func_is_collapsed = cv_tree_is_collapsed; cv->func_get_parent = cv_tree_get_parent; cv->func_cleanup = cv_tree_cleanup; break; } /* now rebuild a new tabbar or tree */ cv->func_init (cv); chanview_populate (cv); cv->func_postinit (cv); /* force re-focus */ if (cv->focused) cv->func_focus (cv->focused); } static void chanview_free_ch (chanview *cv, GtkTreeIter *iter) { chan *ch; gtk_tree_model_get (GTK_TREE_MODEL (cv->store), iter, COL_CHAN, &ch, -1); free (ch); } static void chanview_destroy_store (chanview *cv) /* free every (chan *) in the store */ { model_foreach_1 (GTK_TREE_MODEL (cv->store), (void *)chanview_free_ch, cv); g_object_unref (cv->store); } static void chanview_destroy (chanview *cv) { if (cv->func_cleanup) cv->func_cleanup (cv); if (cv->box) gtk_widget_destroy (cv->box); chanview_destroy_store (cv); free (cv); } static void chanview_box_destroy_cb (GtkWidget *box, chanview *cv) { cv->box = NULL; chanview_destroy (cv); } chanview * chanview_new (int type, int trunc_len, gboolean sort, gboolean use_icons, GtkStyle *style) { chanview *cv; cv = calloc (1, sizeof (chanview)); cv->store = gtk_tree_store_new (4, G_TYPE_STRING, G_TYPE_POINTER, PANGO_TYPE_ATTR_LIST, GDK_TYPE_PIXBUF); cv->style = style; cv->box = gtk_hbox_new (0, 0); cv->trunc_len = trunc_len; cv->sorted = sort; cv->use_icons = use_icons; gtk_widget_show (cv->box); chanview_set_impl (cv, type); g_signal_connect (G_OBJECT (cv->box), "destroy", G_CALLBACK (chanview_box_destroy_cb), cv); return cv; } /* too lazy for signals */ void chanview_set_callbacks (chanview *cv, void (*cb_focus) (chanview *, chan *, int tag, void *userdata), void (*cb_xbutton) (chanview *, chan *, int tag, void *userdata), gboolean (*cb_contextmenu) (chanview *, chan *, int tag, void *userdata, GdkEventButton *), int (*cb_compare) (void *a, void *b)) { cv->cb_focus = cb_focus; cv->cb_xbutton = cb_xbutton; cv->cb_contextmenu = cb_contextmenu; cv->cb_compare = cb_compare; } /* find a place to insert this new entry, based on the compare function */ static void chanview_insert_sorted (chanview *cv, GtkTreeIter *add_iter, GtkTreeIter *parent, void *ud) { GtkTreeIter iter; chan *ch; if (cv->sorted && gtk_tree_model_iter_children (GTK_TREE_MODEL (cv->store), &iter, parent)) { do { gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &iter, COL_CHAN, &ch, -1); if (ch->tag == 0 && cv->cb_compare (ch->userdata, ud) > 0) { gtk_tree_store_insert_before (cv->store, add_iter, parent, &iter); return; } } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &iter)); } gtk_tree_store_append (cv->store, add_iter, parent); } /* find a parent node with the same "family" pointer (i.e. the Server tab) */ static int chanview_find_parent (chanview *cv, void *family, GtkTreeIter *search_iter, chan *avoid) { chan *search_ch; /* find this new row's parent, if any */ if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cv->store), search_iter)) { do { gtk_tree_model_get (GTK_TREE_MODEL (cv->store), search_iter, COL_CHAN, &search_ch, -1); if (family == search_ch->family && search_ch != avoid /*&& gtk_tree_store_iter_depth (cv->store, search_iter) == 0*/) return TRUE; } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), search_iter)); } return FALSE; } static chan * chanview_add_real (chanview *cv, char *name, void *family, void *userdata, gboolean allow_closure, int tag, GdkPixbuf *icon, chan *ch, chan *avoid) { GtkTreeIter parent_iter; GtkTreeIter iter; gboolean has_parent = FALSE; if (chanview_find_parent (cv, family, &parent_iter, avoid)) { chanview_insert_sorted (cv, &iter, &parent_iter, userdata); has_parent = TRUE; } else { gtk_tree_store_append (cv->store, &iter, NULL); } if (!ch) { ch = calloc (1, sizeof (chan)); ch->userdata = userdata; ch->family = family; ch->cv = cv; ch->allow_closure = allow_closure; ch->tag = tag; ch->icon = icon; } memcpy (&(ch->iter), &iter, sizeof (iter)); gtk_tree_store_set (cv->store, &iter, COL_NAME, name, COL_CHAN, ch, COL_PIXBUF, icon, -1); cv->size++; if (!has_parent) ch->impl = cv->func_add (cv, ch, name, NULL); else ch->impl = cv->func_add (cv, ch, name, &parent_iter); return ch; } chan * chanview_add (chanview *cv, char *name, void *family, void *userdata, gboolean allow_closure, int tag, GdkPixbuf *icon) { char *new_name; chan *ret; new_name = truncate_tab_name (name, cv->trunc_len); ret = chanview_add_real (cv, new_name, family, userdata, allow_closure, tag, icon, NULL, NULL); if (new_name != name) free (new_name); return ret; } int chanview_get_size (chanview *cv) { return cv->size; } GtkWidget * chanview_get_box (chanview *cv) { return cv->box; } void chanview_move_focus (chanview *cv, gboolean relative, int num) { cv->func_move_focus (cv, relative, num); } GtkOrientation chanview_get_orientation (chanview *cv) { return (cv->vertical ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL); } void chanview_set_orientation (chanview *cv, gboolean vertical) { if (vertical != cv->vertical) { cv->vertical = vertical; cv->func_change_orientation (cv); } } int chan_get_tag (chan *ch) { return ch->tag; } void * chan_get_userdata (chan *ch) { return ch->userdata; } void chan_focus (chan *ch) { if (ch->cv->focused == ch) return; ch->cv->func_focus (ch); } void chan_move (chan *ch, int delta) { ch->cv->func_move (ch, delta); } void chan_move_family (chan *ch, int delta) { ch->cv->func_move_family (ch, delta); } void chan_set_color (chan *ch, PangoAttrList *list) { gtk_tree_store_set (ch->cv->store, &ch->iter, COL_ATTR, list, -1); ch->cv->func_set_color (ch, list); } void chan_rename (chan *ch, char *name, int trunc_len) { char *new_name; new_name = truncate_tab_name (name, trunc_len); gtk_tree_store_set (ch->cv->store, &ch->iter, COL_NAME, new_name, -1); ch->cv->func_rename (ch, new_name); ch->cv->trunc_len = trunc_len; if (new_name != name) free (new_name); } /* this thing is overly complicated */ static int cv_find_number_of_chan (chanview *cv, chan *find_ch) { GtkTreeIter iter, inner; chan *ch; int i = 0; if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cv->store), &iter)) { do { gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &iter, COL_CHAN, &ch, -1); if (ch == find_ch) return i; i++; if (gtk_tree_model_iter_children (GTK_TREE_MODEL (cv->store), &inner, &iter)) { do { gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &inner, COL_CHAN, &ch, -1); if (ch == find_ch) return i; i++; } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &inner)); } } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &iter)); } return 0; /* WARNING */ } /* this thing is overly complicated too */ static chan * cv_find_chan_by_number (chanview *cv, int num) { GtkTreeIter iter, inner; chan *ch; int i = 0; if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cv->store), &iter)) { do { if (i == num) { gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &iter, COL_CHAN, &ch, -1); return ch; } i++; if (gtk_tree_model_iter_children (GTK_TREE_MODEL (cv->store), &inner, &iter)) { do { if (i == num) { gtk_tree_model_get (GTK_TREE_MODEL (cv->store), &inner, COL_CHAN, &ch, -1); return ch; } i++; } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &inner)); } } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (cv->store), &iter)); } return NULL; } static void chan_emancipate_children (chan *ch) { char *name; chan *childch; GtkTreeIter childiter; PangoAttrList *attr; while (gtk_tree_model_iter_children (GTK_TREE_MODEL (ch->cv->store), &childiter, &ch->iter)) { /* remove and re-add all the children, but avoid using "ch" as parent */ gtk_tree_model_get (GTK_TREE_MODEL (ch->cv->store), &childiter, COL_NAME, &name, COL_CHAN, &childch, COL_ATTR, &attr, -1); ch->cv->func_remove (childch); gtk_tree_store_remove (ch->cv->store, &childiter); ch->cv->size--; chanview_add_real (childch->cv, name, childch->family, childch->userdata, childch->allow_closure, childch->tag, childch->icon, childch, ch); if (attr) { childch->cv->func_set_color (childch, attr); pango_attr_list_unref (attr); } g_free (name); } } gboolean chan_remove (chan *ch, gboolean force) { chan *new_ch; int i, num; extern int hexchat_is_quitting; if (hexchat_is_quitting) /* avoid lots of looping on exit */ return TRUE; /* is this ch allowed to be closed while still having children? */ if (!force && gtk_tree_model_iter_has_child (GTK_TREE_MODEL (ch->cv->store), &ch->iter) && !ch->allow_closure) return FALSE; chan_emancipate_children (ch); ch->cv->func_remove (ch); /* is it the focused one? */ if (ch->cv->focused == ch) { ch->cv->focused = NULL; /* try to move the focus to some other valid channel */ num = cv_find_number_of_chan (ch->cv, ch); /* move to the one left of the closing tab */ new_ch = cv_find_chan_by_number (ch->cv, num - 1); if (new_ch && new_ch != ch) { chan_focus (new_ch); /* this'll will set ch->cv->focused for us too */ } else { /* if it fails, try focus from tab 0 and up */ for (i = 0; i < ch->cv->size; i++) { new_ch = cv_find_chan_by_number (ch->cv, i); if (new_ch && new_ch != ch) { chan_focus (new_ch); /* this'll will set ch->cv->focused for us too */ break; } } } } ch->cv->size--; gtk_tree_store_remove (ch->cv->store, &ch->iter); free (ch); return TRUE; } gboolean chan_is_collapsed (chan *ch) { return ch->cv->func_is_collapsed (ch); } chan * chan_get_parent (chan *ch) { return ch->cv->func_get_parent (ch); }