Blame view

imap4d/idle.c 1.65 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
Sergey Poznyakoff authored
2
   Copyright (C) 2003, 2007-2012, 2014-2017 Free Software Foundation,
3
   Inc.
4 5 6

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
8 9 10 11 12 13 14 15
   any later version.

   GNU Mailutils 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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17 18 19 20

#include "imap4d.h"

int
21 22
imap4d_idle (struct imap4d_session *session,
             struct imap4d_command *command, imap4d_tokbuf_t tok)
23
{
24
  time_t start;
25 26
  char *token_str = NULL;
  size_t token_size = 0, token_len;
27
  
28
  if (imap4d_tokbuf_argc (tok) != 2)
29
    return io_completion_response (command, RESP_BAD, "Invalid arguments");
30

31 32
  if (io_wait_input (0) == -1)
    return io_completion_response (command, RESP_NO, "Cannot idle");
33

34 35
  io_sendf ("+ idling\n");
  io_flush ();
36

37
  start = time (NULL);
38 39
  while (1)
    {
40
      if (io_wait_input (5))
41
	{
42
          io_getline (&token_str, &token_size, &token_len); 	  
43
	  if (token_len == 4 && mu_c_strcasecmp (token_str, "done") == 0)
44 45
	    break;
	}
Sergey Poznyakoff authored
46
      else if (time (NULL) - start > idle_timeout)
47
	imap4d_bye (ERR_TIMEOUT);
48 49

      imap4d_sync ();
50
      io_flush ();
51
    }
52
  free (token_str);
53
  return io_completion_response (command, RESP_OK, "terminated");
54 55
}