summaryrefslogtreecommitdiff
path: root/libmailutils/msgset/aggr.c
blob: 641fb6490b2f450355de7e0eb6cab60e99ca0de0 (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2011-2012, 2014-2017 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 <config.h>
#include <stdlib.h>
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/msgset.h>
#include <mailutils/sys/msgset.h>

/* Comparator function for sorting the message set list. */
static int
compare_msgrange (const void *a, const void *b)
{
  struct mu_msgrange const *sa = a;
  struct mu_msgrange const *sb = b;
  
  if (sa->msg_end != sb->msg_end)
    {
      if (sa->msg_end == MU_MSGNO_LAST)
	return 1;
      if (sb->msg_end == MU_MSGNO_LAST)
	return -1;
    }
  
  if (sa->msg_beg < sb->msg_beg)
    return -1;
  if (sa->msg_beg > sb->msg_beg)
    return 1;

  if (sa->msg_end == sb->msg_end)
    return 0;

  if (sa->msg_end < sb->msg_end)
    return -1;
  return 1;
}

int
mu_msgset_aggregate (mu_msgset_t mset)
{
  int rc;
  mu_iterator_t itr;
  size_t count;
  struct mu_msgrange *prev = NULL, *mr;
  int dir;
  
  if (!mset)
    return EINVAL;

  if (mset->flags & _MU_MSGSET_AGGREGATED)
    return 0; /* nothing to do */
  
  rc = mu_list_count (mset->list, &count);
  if (rc)
    return rc;
  if (count < 2)
    return 0;
  
  mu_list_sort (mset->list, compare_msgrange);
  
  rc = mu_list_get_iterator (mset->list, &itr);
  if (rc)
    return rc;
  /* Set backward direction */
  dir = 1;
  rc = mu_iterator_ctl (itr, mu_itrctl_set_direction, &dir);
  if (rc)
    {
      mu_iterator_destroy (&itr);
      return rc;
    }

  mu_iterator_first (itr);
  mu_iterator_current (itr, (void **)&mr);
  
  if (mr->msg_end == MU_MSGNO_LAST)
    {
      struct mu_msgrange *last = mr;
      
      for (mu_iterator_next (itr);
	   rc == 0 && !mu_iterator_is_done (itr); mu_iterator_next (itr))
	{
	  mu_iterator_current (itr, (void **)&mr);
	  if (mr->msg_end == MU_MSGNO_LAST)
	    {
	      last->msg_beg = mr->msg_beg;
	      rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
	    }
	  else if (mr->msg_beg >= last->msg_beg)
	    rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
	  else if (mr->msg_end + 1 >= last->msg_beg)
	    {
	      last->msg_beg = mr->msg_beg;
	      rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
	    }
	  else
	    break;
	}
    }

  if (rc == 0)
    {
      dir = 0;
      rc = mu_iterator_ctl (itr, mu_itrctl_set_direction, &dir);
      if (rc)
	{
	  mu_iterator_destroy (&itr);
	  return rc;
	}

      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
	   mu_iterator_next (itr))
	{
	  mu_iterator_current (itr, (void **)&mr);
	  if (mr->msg_end == MU_MSGNO_LAST)
	    break;
	  if (prev)
	    {
	      if (prev->msg_beg <= mr->msg_beg && mr->msg_beg <= prev->msg_end)
		{
		  /* Possible cases:
		     1. mr lies fully within prev:
		     mr->msg_end <= prev->msg_end
		     Action: mr deleted
		     2. mr overlaps with prev:
		     mr->msg_end > prev->msg_end
		     Action: prev->msg_end is set to mr->msg_end; mr deleted
		  */
		  if (mr->msg_end > prev->msg_end)
		    prev->msg_end = mr->msg_end;
		  rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
		  if (rc)
		    break;
		  continue;
		}
	      else if (prev->msg_end + 1 == mr->msg_beg)
		{
		  prev->msg_end = mr->msg_end;
		  rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
		  if (rc)
		    break;
		  continue;
		}
	    }
	  prev = mr;
	}
    }
  mu_iterator_destroy (&itr);

  if (rc == 0)
    mset->flags |= _MU_MSGSET_AGGREGATED;
  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.