summary refs log tree commit diff stats
path: root/plugins/mailcheck
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/mailcheck')
-rw-r--r--plugins/mailcheck/Makefile.am25
-rw-r--r--plugins/mailcheck/mailcheck.c94
2 files changed, 119 insertions, 0 deletions
diff --git a/plugins/mailcheck/Makefile.am b/plugins/mailcheck/Makefile.am
new file mode 100644
index 00000000..bda04229
--- /dev/null
+++ b/plugins/mailcheck/Makefile.am
@@ -0,0 +1,25 @@
+## Process with automake to produce Makefile.in
+
+# To create a standalone tarball of your plugin run the plugin_dist
+# target.  Note: you must configure the main source tree with
+# --enable-maintainer-mode
+
+# These two must be defined.
+PLUGIN=mailcheck
+PLUGIN_VERSION=0.1
+
+# This file must be in the form PLUGIN-config.h.in, it can be empty but
+# must exist
+EXTRA_DIST = mailcheck-config.h.in
+
+# Remember to include this line in your Makefile.am
+include @top_srcdir@/plugins/Make.plugin
+
+libdir = $(xchatdir)/plugins
+
+lib_LTLIBRARIES = mailcheck.la
+mailcheck_la_SOURCES = mailcheck.c
+mailcheck_la_LDFLAGS = -avoid-version -module 
+INCLUDES = $(COMMON_CFLAGS) $(PLUGIN_INCLUDES)
+
+DISTCLEANFILES = pg_dir/*
diff --git a/plugins/mailcheck/mailcheck.c b/plugins/mailcheck/mailcheck.c
new file mode 100644
index 00000000..19011ab3
--- /dev/null
+++ b/plugins/mailcheck/mailcheck.c
@@ -0,0 +1,94 @@
+/* XCHAT 2.0 PLUGIN: Mail checker */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "xchat-plugin.h"
+
+
+static xchat_plugin *ph;	/* plugin handle */
+
+static int
+mail_items(char *file)
+{
+	FILE *fp;
+	int items;
+	char buf[512];
+
+	fp = fopen(file, "r");
+	if(!fp)
+		return 1;
+
+	items = 0;
+	while(fgets(buf, sizeof buf, fp))
+	{
+		if(!strncmp(buf, "From ", 5))
+			items++;
+	}
+	fclose(fp);
+
+	return items;
+}
+
+static void
+xchat_mail_check (void)
+{
+	static int last_size = -1;
+	int size;
+	struct stat st;
+	char buf[512];
+	char *maildir;
+
+	maildir = getenv("MAIL");
+	if(!maildir)
+	{
+		snprintf (buf, sizeof(buf), "/var/spool/mail/%s", getenv("USER"));
+		maildir = buf;
+	}
+
+	if(stat(maildir, &st) < 0)
+		return;
+
+	size = st.st_size;
+
+	if(last_size == -1)
+	{
+		last_size = size;
+		return;
+	}
+
+	if(size > last_size)
+	{
+		xchat_printf(ph,
+	"-\0033-\0039-\017\tYou have new mail (%d messages, %d bytes total).",
+				mail_items(maildir), size);
+	}
+
+	last_size = size;
+}
+
+static int timeout_cb(void *userdata)
+{
+	xchat_mail_check();
+
+	return 1;
+}
+
+int xchat_plugin_init(xchat_plugin *plugin_handle,
+				char **plugin_name, char **plugin_desc, char **plugin_version,
+				char *arg)
+{
+	ph = plugin_handle;
+
+	*plugin_name = "MailCheck";
+	*plugin_desc = "Checks your mailbox every 30 seconds";
+	*plugin_version = "0.1";
+
+	xchat_hook_timer(ph, 30000, timeout_cb, 0);
+
+	return 1;
+}