aboutsummaryrefslogtreecommitdiffstats
path: root/mail/message-thread.c
blob: c8ed152e71acc1f733a62d8ec66d665a05788404 (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Copyright (C) 2000 Helix Code Inc.
 *
 *  Authors: Michael Zucchi <notzed@helixcode.com>
 *
 *  This program 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 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA
 */

#include "camel/camel.h"
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glib.h>
#include <ctype.h>

#include "message-thread.h"
#include "mail-tools.h"
#include "mail-threads.h"

#define d(x)
/*#define LEAKDEBUG*/

/* **************************************** */
/* mem leak debug stuff */

#ifdef LEAKDEBUG

static GHashTable *allocedht = NULL;

#define EXISTS (1 << 0)
#define WALKED (1 << 1)
#define FREED  (1 << 2)
#define AL_401 (1 << 3)
#define AL_541 (1 << 4)
#define AL_546 (1 << 5)
#define AL_569 (1 << 6)
#define LINKED (1 << 7)
#define EXCUSED (1 << 8)

#define GITP(x) GINT_TO_POINTER(x)
#define GPTI(x) GPOINTER_TO_INT(x)

static struct _container *
alloc_container (int where)
{
    struct _container *c;

    c = g_new0 (struct _container, 1);

    if (!allocedht)
        allocedht = g_hash_table_new (g_direct_hash, g_direct_equal);

    g_hash_table_insert (allocedht, c, GITP(EXISTS|where));
    return c;
}

static void
free_container (struct _container **c)
{
    gpointer flags;

    memset ((*c), 0, sizeof (struct _container));
    if ((flags = g_hash_table_lookup (allocedht, (*c))) == NULL)
        printf ("** threading mem debug: freeing unalloced entry %p?\n", (*c));
    g_hash_table_insert (allocedht, (*c), GITP(GPTI(flags)|FREED));
    g_free ((*c));
    (*c) = NULL;
}

static void 
cont_print (gpointer key, gpointer value, gpointer user)
{
    struct _container *c = (struct _container *) key;
    char *line;

    if (GPTI(value) & FREED)
        return;

    if (GPTI(value) & AL_401)
        line = "401";
    else if (GPTI(value) & AL_541)
        line = "541";
    else if (GPTI(value) & AL_546)
        line = "546";
    else if (GPTI(value) & AL_569)
        line = "569";
    else
        line = "???";

    printf ("   %p : %s %s %s %s %s",
        c,
        GPTI(value) & FREED ? "freed" : "unfrd",
        GPTI(value) & WALKED ? "walked" : "unwlkd",
        line,
        GPTI(value) & LINKED ? "linked" : "unlnkd",
        GPTI(value) & EXCUSED ? "excused" : "unexcsd");

    if ((GPTI(value) & FREED) == 0) {
        gpointer oth_flags;

        printf (" : %p %p %p : \"%s\" \"%s\" %d %d", 
            c->next, c->parent, c->child,
            c->message ? c->message->subject : "(null message)",
            c->root_subject ? c->root_subject : "(null root-subject)",
            c->re, c->order);

        if (c->next) {
            oth_flags = g_hash_table_lookup (allocedht, c->next);

            printf ("\n     next : %p : %s %s %s",
                c->next,
                GPTI(oth_flags) & WALKED ? "walked" : "unwlkd",
                GPTI(oth_flags) & LINKED ? "linked" : "unlnkd",
                GPTI(oth_flags) & EXCUSED ? "excused" : "unexcsd");
        }

        if (c->parent) {
            oth_flags = g_hash_table_lookup (allocedht, c->parent);

            printf ("\n     prnt : %p : %s %s %s",
                c->parent,
                GPTI(oth_flags) & WALKED ? "walked" : "unwlkd",
                GPTI(oth_flags) & LINKED ? "linked" : "unlnkd",
                GPTI(oth_flags) & EXCUSED ? "excused" : "unexcsd");
        }

        if (c->child) {
            oth_flags = g_hash_table_lookup (allocedht, c->child);

            printf ("\n     chld : %p : %s %s %s",
                c->child,
                GPTI(oth_flags) & WALKED ? "walked" : "unwlkd",
                GPTI(oth_flags) & LINKED ? "linked" : "unlnkd",
                GPTI(oth_flags) & EXCUSED ? "excused" : "unexcsd");
        }
        
    }

    printf ("\n");
              
}

static void 
make_excuses (gpointer key, gpointer value, gpointer user)
{
    struct _container *c;
    gpointer chldflags;
    gpointer nextflags;

    if (GPTI(value) & FREED)
        return;

    c = (struct _container *) key;

    if (c->next) {
        nextflags = g_hash_table_lookup (allocedht, c->next);

        if ((GPTI(nextflags) & EXCUSED) == 0) {
            g_hash_table_insert (allocedht, c->next, GITP(GPTI(nextflags)|EXCUSED));
            ((gint *)user) = 1;
        }
    }

    if (c->child) {
        chldflags = g_hash_table_lookup (allocedht, c->child);

        if ((GPTI(chldflags) & EXCUSED) == 0) {
            g_hash_table_insert (allocedht, c->child, GITP(GPTI(chldflags)|EXCUSED));
            ((gint *)user) = 1;
        }
    }
}

static void 
print_containers (void)
{
    gint hit;

    do {
        hit = 0;
        g_hash_table_foreach (allocedht, make_excuses, &hit);
    } while (hit);

    printf ("List of container stats:\n");
    g_hash_table_foreach (allocedht, cont_print, NULL);
    printf ("End of list.\n");
}

static void
walk_containers (struct _container *head)
{
    gpointer flags;

    while (head) {
        if (head->child)
            walk_containers (head->child);
        if ((flags = g_hash_table_lookup (allocedht, head)) == NULL) {
            printf ("*** walk_containers : bad pointer %p\n", head);
        } else {
            g_hash_table_insert (allocedht, head, GITP(GPTI(flags)|WALKED));
        }
        
        head = head->next;
    }
}

static void
link_container (struct _container *c)
{
    gpointer flags;

    if ((flags = g_hash_table_lookup (allocedht, c)) == NULL)
        printf ("** threading mem debug: linking unalloced entry %p?\n", c);
    g_hash_table_insert (allocedht, c, GITP(GPTI(flags)|LINKED));
}

#else
#define alloc_container(w) (g_new0 (struct _container, 1))
#define free_container(c) g_free (*(c))
#define print_containers()
#define walk_containers(c)
#define link_container(c)
#endif

/* **************************************** */

static struct _container *thread_messages(CamelFolder *folder, GPtrArray *uids);
static void thread_messages_free(struct _container *);

/* for debug only */
int dump_tree(struct _container *c, int depth);

static void
container_add_child(struct _container *node, struct _container *child)
{
    d(printf("\nAdding child %p to parent %p \n", child, node));
    child->next = node->child;
    node->child = child;
    child->parent = node;
}

#if 0
static void
container_unparent_child(struct _container *child)
{
    struct _container *c, *node;

    /* are we unparented? */
    if (child->parent == NULL) {
        return;
    }

    /* else remove child from its existing parent, and reparent */
    node = child->parent;
    c = (struct _container *)&node->child;
    d(printf("scanning children:\n"));
    while (c->next) {
        d(printf(" %p\n", c));
            if (c->next==child) {
            d(printf("found node %p\n", child));
            c->next = c->next->next;
            child->parent = NULL;
            return;
        }
        c = c->next;
    }

    printf("DAMN, we shouldn't  be here!\n");
}
#endif

static void
container_parent_child(struct _container *parent, struct _container *child)
{
    struct _container *c, *node, **prev;

    /* are we already the right parent? */
    if (child->parent == parent)
        return;

    /* are we unparented? */
    if (child->parent == NULL) {
        container_add_child(parent, child);
        return;
    }

    /* check for trying to make my child my parent */
    for (c = parent; c; c = c->parent) {
        if (c == child) {
            d(printf("AIIE: trying to lop off hunk of nodes!\n"));
            return;
        }
    }

    /* else remove child from its existing parent, and reparent */
    node = child->parent;

    /*  c = (struct _container *)&node->child;
     *d(printf("scanning children:\n"));
     *while (c->next) {
     *  d(printf(" %p\n", c));
     *       if (c->next==child) {
     *      d(printf("found node %p\n", child));
     *      c->next = c->next->next;
     *      child->parent = NULL;
     *      container_add_child(parent, child);
     *      return;
     *  }
     *  c = c->next;
     *}
     */

    d(printf("PKGW deparent child"));
    c = node->child;
    prev = &(node->child);
    while (c) {
        d(printf ("   %p\n", c));

        if (c == child) {
            d(printf ("    hit child %p\n", child));
            (*prev) = c->next;
            c->next = NULL;
            c->parent = NULL;
            container_add_child (parent, child);
            return;
        }

        prev = &(c->next);
        c = c->next;
    }

    printf("DAMN, we shouldn't  be here!\n");
}

static void
prune_empty(struct _container **cp)
{
    struct _container *child, *next, *c, *lastc;

    /* yes, this is intentional */
    lastc = (struct _container *)cp;
    while (lastc->next) {
        c = lastc->next;

        d(printf("checking message %p %p (%s)\n", c,
             c->message, c->message?c->message->message_id:"<empty>"));
        if (c->message == NULL) {
            if (c->child == NULL) {
                d(printf("removing empty node\n"));
                lastc->next = c->next;
                free_container (&c);
                continue;
            }
            if (c->parent || c->child->next==0) {
                d(printf("promoting child\n"));
                lastc->next = c->next; /* remove us */
                child = c->child;
                while (child) {
                    next = child->next;

                    child->parent = c->parent;
                    child->next = lastc->next;
                    lastc->next = child;

                    child = next;
                }
                free_container (&c);
                continue;
            }
        }
        prune_empty(&c->child);
        lastc = c;
    }
}

static void
hashloop(void *key, void *value, void *data)
{
    struct _container *c = value;
    struct _container *tail = data;

    if (c->parent == NULL) {
        c->next = tail->next;
        tail->next = c;
        link_container(c);
    }
}

static char *
get_root_subject(struct _container *c, int *re)
{
    char *s, *p;
    struct _container *scan;
    
    s = NULL;
    *re = FALSE;
    if (c->message)
        s = c->message->subject;
    else {
        /* one of the children will always have a message */
        scan = c->child;
        while (scan) {
            if (scan->message) {
                s = scan->message->subject;
                break;
            }
            scan = scan->next;
        }
    }
    if (s != NULL) {
        while (*s) {
            while (isspace(*s))
                s++;
            if (s[0] == 0)
                break;
            if ((s[0] == 'r' || s[0]=='R')
                && (s[1] == 'e' || s[1]=='E')) {
                p = s+2;
                while (isdigit(*p) || (ispunct(*p) && (*p != ':')))
                    p++;
                if (*p==':') {
                    *re = TRUE;
                    s = p+1;
                } else
                    break;
            } else
                break;
        }
        if (*s)
            return s;
    }
    return NULL;
}

/* this is pretty slow, but not used often */
static void
remove_node(struct _container **list, struct _container *node, struct _container **clast)
{
    struct _container *c;

    /* this is intentional, even if it looks funny */
    c = (struct _container *)list;
    while (c->next) {
        if (c->next == node) {
            if (clast && *clast == c->next)
                *clast = c;
            c->next = c->next->next;
            break;
        }
        c = c->next;
    }
}

static void
group_root_set(struct _container **cp)
{
    GHashTable *subject_table = g_hash_table_new(g_str_hash, g_str_equal);
    struct _container *c, *clast, *scan, *container;

    /* gather subject lines */ 
    d(printf("gathering subject lines\n"));
    clast = (struct _container *)cp;
    c = clast->next;
    while (c) {
        c->root_subject = get_root_subject(c, &c->re);
        if (c->root_subject) {
            container = g_hash_table_lookup(subject_table, c->root_subject);
            if (container == NULL
                || (container->message == NULL && c->message)
                || (container->re == TRUE && !c->re)) {
                g_hash_table_insert(subject_table, c->root_subject, c);
            }
        }
        c = c->next;
    }

    /* merge common subjects? */
    clast = (struct _container *)cp;
    while (clast->next) {
        c = clast->next;
        d(printf("checking %p %s\n", c, c->root_subject));
        if (c->root_subject
            && (container = g_hash_table_lookup(subject_table, c->root_subject))
            && (container != c)) {
            d(printf(" matching %p %s\n", container, container->root_subject));
            if (c->message == NULL && container->message == NULL) {
                d(printf("merge containers children\n"));
                /* steal the children from c onto container, and unlink c */
                scan = (struct _container *)&container->child;
                while (scan->next)
                    scan = scan->next;
                scan->next = c->child;
                clast->next = c->next;
                continue;
            } if (c->message == NULL && container->message != NULL) {
                d(printf("container is non-empty parent\n"));
                remove_node(cp, container, &clast);
                container_add_child(c, container);
            } else if (c->message != NULL && container->message == NULL) {
                d(printf("container is empty child\n"));
                clast->next = c->next;
                container_add_child(container, c);
                continue;
            } else if (c->re && !container->re) {
                d(printf("container is re\n"));
                clast->next = c->next;
                container_add_child(container, c);
                continue;
            } else if (!c->re && container->re) {
                d(printf("container is not re\n"));
                remove_node(cp, container, &clast);
                container_add_child(c, container);
            } else if (c->re && container->re) {
                d(printf("subjects are common %p and %p\n", c, container));

                remove_node(cp, container, &clast);
                remove_node(cp, c, &clast);

                scan = alloc_container(AL_401);
                scan->root_subject = c->root_subject;
                scan->re = c->re && container->re;
                scan->next = c->next;
                clast->next = scan;
                container_add_child(scan, c);
                container_add_child(scan, container);
                clast = scan;
                g_hash_table_insert(subject_table, scan->root_subject, scan);
                continue;
            }
        }
        clast = c;
    }
    g_hash_table_destroy(subject_table);
}

int
dump_tree(struct _container *c, int depth)
{
    char *p;
    int count=0;

    p = alloca(depth*2+1);
    memset(p, ' ', depth*2);
    p[depth*2] = 0;

    while (c) {
        if (c->message) {
            printf("%s %p Subject: %s <%s>\n", p, c, c->message->subject, c->message->message_id);
            count += 1;
        } else {
            printf("%s %p <empty>\n", p, c);
        }
        if (c->child)
            count += dump_tree(c->child, depth+1);
        c = c->next;
    }
    return count;
}

static void thread_messages_free(struct _container *c)
{
    struct _container *n;

    /* FIXME: ok, for some reason this doesn't work .. investigate later ... */

    while (c) {
        n = c->next;
        if (c->child)
            thread_messages_free(c->child); /* free's children first */
        free_container (&c);
        c = n;
    }
}

static int
sort_node(const void *a, const void *b)
{
    const struct _container *a1 = ((struct _container **)a)[0];
    const struct _container *b1 = ((struct _container **)b)[0];

    /* if we have no message, it must be a dummy node, which 
       also means it must have a child, just use that as the
       sort data (close enough?) */
    if (a1->message == NULL)
        a1 = a1->child;
    if (b1->message == NULL)
        b1 = b1->child;
    if (a1->order == b1->order)
        return 0;
    if (a1->order < b1->order)
        return 1;
    else
        return -1;
}

static void
sort_thread(struct _container **cp)
{
    struct _container *c, *head, **carray;
    int size=0;

    c = *cp;
    while (c) {
        /* sort the children while we're at it */
        if (c->child)
            sort_thread(&c->child);
        size++;
        c = c->next;
    }
    if (size<2)
        return;
    carray = alloca(size*sizeof(struct _container *));
    c = *cp;
    size=0;
    while (c) {
        carray[size] = c;
        c = c->next;
        size++;
    }
    qsort(carray, size, sizeof(struct _container *), sort_node);
    size--;
    head = carray[size];
    head->next = NULL;
    size--;
    do {
        c = carray[size];
        c->next = head;
        head = c;
        size--;
    } while (size>=0);
    *cp = head;
}

static struct _container *
thread_messages(CamelFolder *folder, GPtrArray *uids)
{
    GHashTable *id_table, *no_id_table;
    int i;
    struct _container *c, *p, *child, *head, *container;
    struct _header_references *ref;

    id_table = g_hash_table_new(g_str_hash, g_str_equal);
    no_id_table = g_hash_table_new(NULL, NULL);
    for (i=0;i<uids->len;i++) {
        const CamelMessageInfo *mi;
        mail_tool_camel_lock_up ();
        mi = camel_folder_get_message_info (folder, uids->pdata[i]);
        mail_tool_camel_lock_down ();

        if (mi == NULL) {
            g_warning("Folder doesn't contain uid %s", (char *)uids->pdata[i]);
            continue;
        }

        if (mi->message_id) {
            d(printf("doing : %s\n", mi->message_id));
            c = g_hash_table_lookup(id_table, mi->message_id);
            if (!c) {
                c = alloc_container(AL_541);
                g_hash_table_insert(id_table, mi->message_id, c);
            }
        } else {
            d(printf("doing : (no message id)\n"));
            c = alloc_container(AL_546);
            g_hash_table_insert(no_id_table, (void *)mi, c);
        }

        c->message = mi;
        c->order = i;
        container = c;
        ref = mi->references;
        p = NULL;
        child = container;
        head = NULL;
        d(printf("references:\n"));
        while (ref) {
            if (ref->id == NULL) {
                printf("ref missing id!?\n");
                ref = ref->next;
                continue;
            }

            d(printf("looking up reference: %s\n", ref->id));
            c = g_hash_table_lookup(id_table, ref->id);
            if (c == NULL) {
                d(printf("not found\n"));
                c = alloc_container(AL_569);
                g_hash_table_insert(id_table, ref->id, c);
            }
            if (c!=child)
                container_parent_child(c, child);
            child = c;
            if (head == NULL)
                head = c;
            ref = ref->next;
        }
    }

    d(printf("\n\n"));
    /* build a list of root messages (no parent) */
    head = NULL;
    g_hash_table_foreach(id_table, hashloop, &head);
    g_hash_table_foreach(no_id_table, hashloop, &head);

    g_hash_table_destroy(id_table);
    g_hash_table_destroy(no_id_table);

    /* remove empty parent nodes */
    prune_empty(&head);

    /* find any siblings which missed out */
    group_root_set(&head);

#if 0
    printf("finished\n");
    i = dump_tree(head, 0);
    printf("%d count, %d items in tree\n", uids->len, i);
#endif

    sort_thread(&head);
    return head;
}

/* ** THREAD MESSAGES ***************************************************** */

typedef struct thread_messages_input_s {
    MessageList *ml;
    GPtrArray *uids;
    gboolean use_camel_uidfree;
    void (*build) (MessageList *, struct _container *);
} thread_messages_input_t;

typedef struct thread_messages_data_s {
    struct _container *container;
} thread_messages_data_t;

static gchar *describe_thread_messages (gpointer in_data, gboolean gerund);
static void setup_thread_messages   (gpointer in_data, gpointer op_data, CamelException *ex);
static void do_thread_messages      (gpointer in_data, gpointer op_data, CamelException *ex);
static void cleanup_thread_messages (gpointer in_data, gpointer op_data, CamelException *ex);

static gchar *describe_thread_messages (gpointer in_data, gboolean gerund)
{
    if (gerund)
        return g_strdup ("Threading message list");
    else
        return g_strdup ("Thread message list");
}

static void setup_thread_messages (gpointer in_data, gpointer op_data, CamelException *ex)
{
    thread_messages_input_t *input = (thread_messages_input_t *) in_data;

    if (!IS_MESSAGE_LIST (input->ml)) {
        camel_exception_set (ex, CAMEL_EXCEPTION_INVALID_PARAM,
                     "No messagelist to thread was provided to thread_messages");
        return;
    }

    if (!input->uids) {
        camel_exception_set (ex, CAMEL_EXCEPTION_INVALID_PARAM,
                     "No uids were provided to thread_messages");
        return;
    }

    if (!input->build) {
        camel_exception_set (ex, CAMEL_EXCEPTION_INVALID_PARAM,
                     "No build callback provided to thread_messages");
        return;
    }

    gtk_object_ref (GTK_OBJECT (input->ml));
}

static void do_thread_messages (gpointer in_data, gpointer op_data, CamelException *ex)
{
    thread_messages_input_t *input = (thread_messages_input_t *) in_data;
    thread_messages_data_t *data = (thread_messages_data_t *) op_data;

    data->container = thread_messages (input->ml->folder, input->uids);
}

static void cleanup_thread_messages (gpointer in_data, gpointer op_data, CamelException *ex)
{
    thread_messages_input_t *input = (thread_messages_input_t *) in_data;
    thread_messages_data_t *data = (thread_messages_data_t *) op_data;

    (input->build) (input->ml, data->container);
    walk_containers (data->container);
    thread_messages_free (data->container);

    print_containers();

    if (input->use_camel_uidfree) {
        mail_tool_camel_lock_up ();
        camel_folder_free_uids (input->ml->folder, input->uids);
        mail_tool_camel_lock_down ();
    } else {
        g_ptr_array_add (input->uids, NULL);
        g_strfreev ((char **)input->uids->pdata);
        g_ptr_array_free (input->uids, FALSE);
    }

    gtk_object_unref (GTK_OBJECT (input->ml));
}

static const mail_operation_spec op_thread_messages =
{
    describe_thread_messages,
    sizeof (thread_messages_data_t),
    setup_thread_messages,
    do_thread_messages,
    cleanup_thread_messages
};

void mail_do_thread_messages (MessageList *ml, GPtrArray *uids, 
                  gboolean use_camel_uidfree,
                  void (*build) (MessageList *,
                         struct _container *))
{
    thread_messages_input_t *input;

    input = g_new (thread_messages_input_t, 1);
    input->ml = ml;
    input->uids = uids;
    input->use_camel_uidfree = use_camel_uidfree;
    input->build = build;

    mail_operation_queue (&op_thread_messages, input, TRUE);
}

/* ************************************************************************ */

#ifdef STANDALONE

static char *
auth_callback(char *prompt, gboolean secret,
          CamelService *service, char *item,
          CamelException *ex)
{
    printf ("auth_callback called: %s\n", prompt);
    return NULL;
}

int
main (int argc, char**argv)
{
    CamelSession *session;
    CamelException *ex;
    CamelStore *store;
    gchar *store_url = "mbox:///home/notzed/evolution/local/Inbox";
    CamelFolder *folder;
    CamelMimeMessage *message;
    GList *uid_list;
    GPtrArray *summary;

    gtk_init (&argc, &argv);
    camel_init ();      
    ex = camel_exception_new ();
    
    session = camel_session_new (auth_callback);
    store = camel_session_get_store (session, store_url, ex);
    if (camel_exception_get_id (ex)) {
        printf ("Exception caught in camel_session_get_store\n"
            "Full description : %s\n", camel_exception_get_description (ex));
        return -1;
    }

    folder = camel_store_get_folder (store, "mbox", TRUE, ex);
    if (camel_exception_get_id (ex)) {
        printf ("Exception caught in camel_store_get_folder\n"
            "Full description : %s\n", camel_exception_get_description (ex));
        return -1;
    }

#if 0
    camel_folder_open (folder, FOLDER_OPEN_RW, ex);
    if (camel_exception_get_id (ex)) {
        printf ("Exception caught when trying to open the folder\n"
            "Full description : %s\n", camel_exception_get_description (ex));
        return -1;
    }
#endif

    summary = camel_folder_get_summary(folder);
    thread_messages((CamelMessageInfo **)summary->pdata, summary->len);

    return 0;
}

#endif

/*

  msgid: d
  references: a b c

  msgid: f
  references: c d

  msgid: e
  references: c

  a
   \
    b
     \
      c
       \
        d
        |\
        e f
 */
/*
  lookup d
    create new node d
  child = d
  loop on c b a
    lookup node?
    if no node, create node
    add child to node
    child = node
  endloop

 */