summaryrefslogtreecommitdiff
path: root/imap4d/sync.c
blob: 961fad15b10d6e451bd01ccb622015e093ee3d6c (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2001, 2005, 2007, 2009-2012, 2014-2016 Free
   Software Foundation, Inc.

   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
   the Free Software Foundation; either version 3, or (at your option)
   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
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#include "imap4d.h"
#include <mailutils/observer.h>

/*

 */
static int *attr_table;
static size_t attr_table_count;
static size_t attr_table_max;
static int attr_table_valid;

static void
realloc_attributes (size_t total)
{
  if (total > attr_table_max)
    {
      attr_table = realloc (attr_table, sizeof (*attr_table) * total);
      if (!attr_table)
	imap4d_bye (ERR_NO_MEM);
      attr_table_max = total;
    }
  attr_table_count = total;
}

void
imap4d_sync_invalidate ()
{
  attr_table_valid = 0;
  attr_table_count = 0;
}

static void
reread_attributes (void)
{
  size_t total = 0;
  size_t i;

  mu_mailbox_messages_count (mbox, &total);
  realloc_attributes (total);
  for (i = 1; i <= total; i++)
    {
      mu_message_t msg = NULL;
      mu_attribute_t attr = NULL;

      mu_mailbox_get_message (mbox, i, &msg);
      mu_message_get_attribute (msg, &attr);
      mu_attribute_get_flags (attr, &attr_table[i-1]);
    }
  attr_table_valid = 1;
}

static void
notify (void)
{
  size_t total = 0;
  size_t recent = 0;
  
  mu_mailbox_messages_count (mbox, &total);
  mu_mailbox_messages_recent (mbox, &recent);

  if (!attr_table_valid)
    {
      reread_attributes ();
    }
  else 
    {
      size_t old_total = attr_table_count;
      size_t i;

      realloc_attributes (total);
      for (i = 1; i <= total; i++)
	{
	  mu_message_t msg = NULL;
	  mu_attribute_t nattr = NULL;
	  int nflags;
	  
	  mu_mailbox_get_message (mbox, i, &msg);
	  mu_message_get_attribute (msg, &nattr);
	  mu_attribute_get_flags (nattr, &nflags);

	  if (i <= old_total)
	    {
	      if (nflags != attr_table[i-1])
		{
		  io_sendf ("* %lu FETCH FLAGS (",  (unsigned long) i);
		  mu_imap_format_flags (iostream, nflags, 1);
		  io_sendf (")\n");
		  attr_table[i-1] = nflags;
		}
	    }
	  else
	    attr_table[i-1] = nflags;
	}
    }
  
  io_untagged_response (RESP_NONE, "%lu EXISTS", (unsigned long) total);
  io_untagged_response (RESP_NONE, "%lu RECENT", (unsigned long) recent);
}

size_t
uid_to_msgno (size_t uid)
{
  size_t msgno;
  mu_mailbox_translate (mbox, MU_MAILBOX_UID_TO_MSGNO, uid, &msgno);
  return msgno;
}

int
imap4d_sync_flags (size_t msgno)
{
  if (attr_table)
    {
      mu_message_t msg = NULL;
      mu_attribute_t attr = NULL;
      mu_mailbox_get_message (mbox, msgno, &msg);
      mu_message_get_attribute (msg, &attr);
      mu_attribute_get_flags (attr, &attr_table[msgno-1]);
    }
  return 0;
}

int silent_expunge;
/* When true, non-tagged EXPUNGE responses are suppressed. */

static int mailbox_corrupt;
/* When true, mailbox has been altered by another party. */

static int
action (mu_observer_t observer, size_t type, void *data, void *action_data)
{
  switch (type)
    {
    case MU_EVT_MAILBOX_CORRUPT:
      mailbox_corrupt = 1;
      break;

    case MU_EVT_MAILBOX_DESTROY:
      mailbox_corrupt = 0;
      break;

    case MU_EVT_MAILBOX_MESSAGE_EXPUNGE:
      /* The EXPUNGE response reports that the specified message sequence
	 number has been permanently removed from the mailbox.  The message
	 sequence number for each successive message in the mailbox is
	 immediately decremented by 1, and this decrement is reflected in
	 message sequence numbers in subsequent responses (including other
	 untagged EXPUNGE responses). */
      if (!silent_expunge)
	{
	  size_t *exp = data;
	  io_untagged_response (RESP_NONE, "%lu EXPUNGED",
				(unsigned long) (exp[0] - exp[1]));
	}
    }
  return 0;
}

void
imap4d_set_observer (mu_mailbox_t mbox)
{
  mu_observer_t observer;
  mu_observable_t observable;
      
  mu_observer_create (&observer, mbox);
  mu_observer_set_action (observer, action, mbox);
  mu_mailbox_get_observable (mbox, &observable);
  mu_observable_attach (observable,
			MU_EVT_MAILBOX_CORRUPT|
			MU_EVT_MAILBOX_DESTROY|
			MU_EVT_MAILBOX_MESSAGE_EXPUNGE,
			observer);
  mailbox_corrupt = 0;
}

int
imap4d_sync (void)
{
  manlock_touchlock (mbox);
  /* If mbox --> NULL, it means to free all the resources.
     It may be because of close or before select/examine a new mailbox.
     If it was a close we do not send any notification.  */
  if (mbox == NULL)
    imap4d_sync_invalidate ();
  else if (!attr_table_valid || !mu_mailbox_is_updated (mbox))
    {
      if (mailbox_corrupt)
	{
	  /* Some messages have been deleted from the mailbox by some other
	     party */
	  int status = mu_mailbox_close (mbox);
	  if (status)
	    imap4d_bye (ERR_MAILBOX_CORRUPTED);
	  status = mu_mailbox_open (mbox, MU_STREAM_RDWR);
	  if (status)
	    imap4d_bye (ERR_MAILBOX_CORRUPTED);
	  imap4d_set_observer (mbox);
	  imap4d_sync_invalidate ();
	  mailbox_corrupt = 0;
	  io_untagged_response (RESP_NONE,
		             "OK [ALERT] Mailbox modified by another program");
	}
      notify ();
    }
  else
    {
      size_t count = 0;
      mu_mailbox_messages_count (mbox, &count);
      if (count != attr_table_count)
	notify ();
    }
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.