#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <syslog.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <signal.h>
#include <string.h>
#include "idle.h"
#include "idled.h"
#include "global.h"
const char *idle_method_desc = "no";
static idle_updateproc_t *idle_update = NULL;
static time_t idle_period = -1;
static int idle_started = 0;
static int notify_sock = -1;
static struct sockaddr_un idle_remote;
static int idle_remote_len = 0;
static int idle_send_msg(int msg, struct mailbox *mailbox)
{
idle_data_t idledata;
idledata.msg = msg;
idledata.pid = getpid();
strcpy(idledata.mboxname, mailbox ? mailbox->name : ".");
if (sendto(notify_sock, (void *) &idledata,
IDLEDATA_BASE_SIZE+strlen(idledata.mboxname)+1,
0, (struct sockaddr *) &idle_remote, idle_remote_len) == -1) {
syslog(LOG_ERR, "error sending to idled: %x", msg);
return 0;
}
return 1;
}
void idle_notify(struct mailbox *mailbox)
{
idle_send_msg(IDLE_NOTIFY, mailbox);
}
int idle_enabled(void)
{
if (idle_period == -1) {
int s;
int fdflags;
struct stat sbuf;
const char *idle_sock;
idle_period = config_getint(IMAPOPT_IMAPIDLEPOLL);
if (idle_period < 0) idle_period = 0;
if (!idle_period) return 0;
idle_method_desc = "poll";
if ((s = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
return idle_period;
}
idle_remote.sun_family = AF_UNIX;
idle_sock = config_getstring(IMAPOPT_IDLESOCKET);
if (idle_sock) {
strcpy(idle_remote.sun_path, idle_sock);
}
else {
strcpy(idle_remote.sun_path, config_dir);
strcat(idle_remote.sun_path, FNAME_IDLE_SOCK);
}
idle_remote_len = sizeof(idle_remote.sun_family) +
strlen(idle_remote.sun_path) + 1;
if (stat(idle_remote.sun_path, &sbuf) < 0) {
close(s);
return idle_period;
}
fdflags = fcntl(s, F_GETFD, 0);
if (fdflags != -1) fdflags = fcntl(s, F_SETFL, O_NONBLOCK | fdflags);
if (fdflags == -1) { close(s); return idle_period; }
notify_sock = s;
if (!idle_send_msg(IDLE_NOOP, NULL)) {
close(s);
notify_sock = -1;
return idle_period;
}
mailbox_set_updatenotifier(idle_notify);
idle_method_desc = "idled";
return 1;
}
else if (notify_sock != -1) {
return 1;
}
else {
return idle_period;
}
}
static void idle_handler(int sig)
{
if (!idle_started) return;
switch (sig) {
case SIGUSR1:
idle_update(IDLE_MAILBOX);
break;
case SIGUSR2:
idle_update(IDLE_ALERT);
break;
case SIGALRM:
idle_update(IDLE_MAILBOX|IDLE_ALERT);
alarm(idle_period);
break;
}
}
int idle_init(idle_updateproc_t *proc)
{
struct sigaction action;
idle_update = proc;
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, SIGUSR1);
sigaddset(&action.sa_mask, SIGUSR2);
action.sa_flags = 0;
#ifdef SA_RESTART
action.sa_flags |= SA_RESTART;
#endif
action.sa_handler = idle_handler;
if ((sigaction(SIGUSR1, &action, NULL) < 0) ||
(sigaction(SIGUSR2, &action, NULL) < 0) ||
(sigaction(SIGALRM, &action, NULL) < 0)) {
syslog(LOG_ERR, "sigaction: %m");
idle_done(NULL);
return 0;
}
return 1;
}
void idle_start(struct mailbox *mailbox)
{
idle_started = 1;
if (notify_sock == -1 || !idle_send_msg(IDLE_INIT, mailbox)) {
alarm(idle_period);
}
}
void idle_done(struct mailbox *mailbox)
{
if (notify_sock != -1) idle_send_msg(IDLE_DONE, mailbox);
alarm(0);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGALRM, SIG_IGN);
idle_update = NULL;
idle_started = 0;
}