summary refs log tree commit diff stats
path: root/plugins/upd/upd.c
blob: 1062c6e938753e952df28dbc70e0ef897bd2078e (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
/* XChat-WDK
 * Copyright (c) 2010 Berke Viktor.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <windows.h>
#include <wininet.h>

#include "xchat-plugin.h"

static xchat_plugin *ph;   /* plugin handle */

char*
check_version ()
{
	HINTERNET hINet, hFile;
	hINet = InternetOpen ("Update Checker", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	
	if (!hINet)
	{
		return "Unknown";
	}

	hFile = InternetOpenUrl (hINet, "http://xchat-wdk.googlecode.com/hg/version.txt", NULL, 0, 0, 0);
	
	if (hFile)
	{
		static char buffer[1024];
		DWORD dwRead;
		while (InternetReadFile (hFile, buffer, 1023, &dwRead))
		{
			if (dwRead == 0)
			{
				break;
			}
			buffer[dwRead] = 0;
		}
		
		return buffer;

		InternetCloseHandle (hFile);
	}
	
	InternetCloseHandle (hINet);

	return "Unknown";
}

void
print_version ()
{
	char *version = check_version ();

	if (strcmp (version, xchat_get_info (ph, "version")) == 0)
	{
		xchat_printf (ph, "You have the latest version of XChat-WDK installed!");
	}
	else if (strcmp (version, "Unknown") == 0)
	{
		xchat_printf (ph, "Unable to check for XChat-WDK updates!");
	}
	else
	{
		xchat_printf (ph, "An XChat-WDK update is available! You can download it from here:\nhttp://xchat-wdk.googlecode.com/files/XChat-WDK%%20%s.exe", version);
	}
}

int
xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
{
	ph = plugin_handle;

	*plugin_name = "Update Checker";
	*plugin_desc = "Plugin for checking for XChat-WDK updates";
	*plugin_version = "1.0";

	xchat_hook_command (ph, "UPDCHK", XCHAT_PRI_NORM, print_version, 0, 0);
	xchat_print (ph, "Update Checker plugin loaded\n");
	print_version ();

	return 1;       /* return 1 for success */
}

int
xchat_plugin_deinit (void)
{
	xchat_print (ph, "Update Checker plugin unloaded\n");
	return 1;
}
"mi">0; static int ignored_total = 0; /* ignore_exists (): * returns: struct ig, if this mask is in the ignore list already * NULL, otherwise */ struct ignore * ignore_exists (char *mask) { struct ignore *ig = 0; GSList *list; list = ignore_list; while (list) { ig = (struct ignore *) list->data; if (!rfc_casecmp (ig->mask, mask)) return ig; list = list->next; } return NULL; } /* ignore_add(...) * returns: * 0 fail * 1 success * 2 success (old ignore has been changed) */ int ignore_add (char *mask, int type) { struct ignore *ig = 0; int change_only = FALSE; /* first check if it's already ignored */ ig = ignore_exists (mask); if (ig) change_only = TRUE; if (!change_only) ig = malloc (sizeof (struct ignore)); if (!ig) return 0; ig->mask = strdup (mask); ig->type = type; if (!change_only) ignore_list = g_slist_prepend (ignore_list, ig); fe_ignore_update (1); if (change_only) return 2; return 1; } void ignore_showlist (session *sess) { struct ignore *ig; GSList *list = ignore_list; char tbuf[256]; int i = 0; EMIT_SIGNAL (XP_TE_IGNOREHEADER, sess, 0, 0, 0, 0, 0); while (list) { ig = list->data; i++; snprintf (tbuf, sizeof (tbuf), " %-25s ", ig->mask); if (ig->type & IG_PRIV) strcat (tbuf, _("YES ")); else strcat (tbuf, _("NO ")); if (ig->type & IG_NOTI) strcat (tbuf, _("YES ")); else strcat (tbuf, _("NO ")); if (ig->type & IG_CHAN) strcat (tbuf, _("YES ")); else strcat (tbuf, _("NO ")); if (ig->type & IG_CTCP) strcat (tbuf, _("YES ")); else strcat (tbuf, _("NO ")); if (ig->type & IG_DCC) strcat (tbuf, _("YES ")); else strcat (tbuf, _("NO ")); if (ig->type & IG_INVI) strcat (tbuf, _("YES ")); else strcat (tbuf, _("NO ")); if (ig->type & IG_UNIG) strcat (tbuf, _("YES ")); else strcat (tbuf, _("NO ")); strcat (tbuf, "\n"); PrintText (sess, tbuf); /*EMIT_SIGNAL (XP_TE_IGNORELIST, sess, ig->mask, 0, 0, 0, 0); */ /* use this later, when TE's support 7 args */ list = list->next; } if (!i) EMIT_SIGNAL (XP_TE_IGNOREEMPTY, sess, 0, 0, 0, 0, 0); EMIT_SIGNAL (XP_TE_IGNOREFOOTER, sess, 0, 0, 0, 0, 0); } /* ignore_del() * one of the args must be NULL, use mask OR *ig, not both * */ int ignore_del (char *mask, struct ignore *ig) { if (!ig) { GSList *list = ignore_list; while (list) { ig = (struct ignore *) list->data; if (!rfc_casecmp (ig->mask, mask)) break; list = list->next; ig = 0; } } if (ig) { ignore_list = g_slist_remove (ignore_list, ig); free (ig->mask); free (ig); fe_ignore_update (1); return TRUE; } return FALSE; } /* check if a msg should be ignored by browsing our ignore list */ int ignore_check (char *host, int type) { struct ignore *ig; GSList *list = ignore_list; /* check if there's an UNIGNORE first, they take precendance. */ while (list) { ig = (struct ignore *) list->data; if (ig->type & IG_UNIG) { if (ig->type & type) { if (match (ig->mask, host)) return FALSE; } } list = list->next; } list = ignore_list; while (list) { ig = (struct ignore *) list->data; if (ig->type & type) { if (match (ig->mask, host)) { ignored_total++; if (type & IG_PRIV) ignored_priv++; if (type & IG_NOTI) ignored_noti++; if (type & IG_CHAN) ignored_chan++; if (type & IG_CTCP) ignored_ctcp++; if (type & IG_INVI) ignored_invi++; fe_ignore_update (2); return TRUE; } } list = list->next; } return FALSE; } static char * ignore_read_next_entry (char *my_cfg, struct ignore *ignore) { char tbuf[1024]; /* Casting to char * done below just to satisfy compiler */ if (my_cfg) { my_cfg = cfg_get_str (my_cfg, "mask", tbuf, sizeof (tbuf)); if (!my_cfg) return NULL; ignore->mask = strdup (tbuf); } if (my_cfg) { my_cfg = cfg_get_str (my_cfg, "type", tbuf, sizeof (tbuf)); ignore->type = atoi (tbuf); } return my_cfg; } void ignore_load () { struct ignore *ignore; struct stat st; char *cfg, *my_cfg; int fh, i; fh = xchat_open_file ("ignore.conf", O_RDONLY, 0, 0); if (fh != -1) { fstat (fh, &st); if (st.st_size) { cfg = malloc (st.st_size + 1); cfg[0] = '\0'; i = read (fh, cfg, st.st_size); if (i >= 0) cfg[i] = '\0'; my_cfg = cfg; while (my_cfg) { ignore = malloc (sizeof (struct ignore)); memset (ignore, 0, sizeof (struct ignore)); if ((my_cfg = ignore_read_next_entry (my_cfg, ignore))) ignore_list = g_slist_prepend (ignore_list, ignore); else free (ignore); } free (cfg); } close (fh); } } void ignore_save () { char buf[1024]; int fh; GSList *temp = ignore_list; struct ignore *ig; fh = xchat_open_file ("ignore.conf", O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE); if (fh != -1) { while (temp) { ig = (struct ignore *) temp->data; if (!(ig->type & IG_NOSAVE)) { snprintf (buf, sizeof (buf), "mask = %s\ntype = %d\n\n", ig->mask, ig->type); write (fh, buf, strlen (buf)); } temp = temp->next; } close (fh); } } static gboolean flood_autodialog_timeout (gpointer data) { prefs.autodialog = 1; return FALSE; } int flood_check (char *nick, char *ip, server *serv, session *sess, int what) /*0=ctcp 1=priv */ { /* serv int ctcp_counter; time_t ctcp_last_time; prefs unsigned int ctcp_number_limit; unsigned int ctcp_time_limit; */ char buf[512]; char real_ip[132]; int i; time_t current_time; current_time = time (NULL); if (what == 0) { if (serv->ctcp_last_time == 0) /*first ctcp in this server */ { serv->ctcp_last_time = time (NULL); serv->ctcp_counter++; } else { if (difftime (current_time, serv->ctcp_last_time) < prefs.ctcp_time_limit) /*if we got the ctcp in the seconds limit */ { serv->ctcp_counter++; if (serv->ctcp_counter == prefs.ctcp_number_limit) /*if we reached the maximun numbers of ctcp in the seconds limits */ { serv->ctcp_last_time = current_time; /*we got the flood, restore all the vars for next one */ serv->ctcp_counter = 0; for (i = 0; i < 128; i++) if (ip[i] == '@') break; snprintf (real_ip, sizeof (real_ip), "*!*%s", &ip[i]); /*ignore_add (char *mask, int priv, int noti, int chan, int ctcp, int invi, int unignore, int no_save) */ snprintf (buf, sizeof (buf), _("You are being CTCP flooded from %s, ignoring %s\n"), nick, real_ip); PrintText (sess, buf); /*FIXME: only ignore ctcp or all?, its ignoring ctcps for now */ ignore_add (real_ip, IG_CTCP); return 0; } } } } else { if (serv->msg_last_time == 0) { serv->msg_last_time = time (NULL); serv->ctcp_counter++; } else { if (difftime (current_time, serv->msg_last_time) < prefs.msg_time_limit) { serv->msg_counter++; if (serv->msg_counter == prefs.msg_number_limit) /*if we reached the maximun numbers of ctcp in the seconds limits */ { snprintf (buf, sizeof (buf), _("You are being MSG flooded from %s, setting gui_auto_open_dialog OFF.\n"), ip); PrintText (sess, buf); serv->msg_last_time = current_time; /*we got the flood, restore all the vars for next one */ serv->msg_counter = 0; /*ignore_add (char *mask, int priv, int noti, int chan, int ctcp, int invi, int unignore, int no_save) */ if (prefs.autodialog) { /*FIXME: only ignore ctcp or all?, its ignoring ctcps for now */ prefs.autodialog = 0; /* turn it back on in 30 secs */ fe_timeout_add (30000, flood_autodialog_timeout, NULL); } return 0; } } } } return 1; }