restructure configure so pkg-config derived SSL flags get used
[rofl0r-ixchat.git] / plugins / mailcheck / mailcheck.c
blob19011ab32843679907e87dfaee106b92ca028094
1 /* XCHAT 2.0 PLUGIN: Mail checker */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
10 #include "xchat-plugin.h"
13 static xchat_plugin *ph; /* plugin handle */
15 static int
16 mail_items(char *file)
18 FILE *fp;
19 int items;
20 char buf[512];
22 fp = fopen(file, "r");
23 if(!fp)
24 return 1;
26 items = 0;
27 while(fgets(buf, sizeof buf, fp))
29 if(!strncmp(buf, "From ", 5))
30 items++;
32 fclose(fp);
34 return items;
37 static void
38 xchat_mail_check (void)
40 static int last_size = -1;
41 int size;
42 struct stat st;
43 char buf[512];
44 char *maildir;
46 maildir = getenv("MAIL");
47 if(!maildir)
49 snprintf (buf, sizeof(buf), "/var/spool/mail/%s", getenv("USER"));
50 maildir = buf;
53 if(stat(maildir, &st) < 0)
54 return;
56 size = st.st_size;
58 if(last_size == -1)
60 last_size = size;
61 return;
64 if(size > last_size)
66 xchat_printf(ph,
67 "-\0033-\0039-\017\tYou have new mail (%d messages, %d bytes total).",
68 mail_items(maildir), size);
71 last_size = size;
74 static int timeout_cb(void *userdata)
76 xchat_mail_check();
78 return 1;
81 int xchat_plugin_init(xchat_plugin *plugin_handle,
82 char **plugin_name, char **plugin_desc, char **plugin_version,
83 char *arg)
85 ph = plugin_handle;
87 *plugin_name = "MailCheck";
88 *plugin_desc = "Checks your mailbox every 30 seconds";
89 *plugin_version = "0.1";
91 xchat_hook_timer(ph, 30000, timeout_cb, 0);
93 return 1;