summary refs log tree commit diff
path: root/thread.c
blob: 6384a1b32c672dae9164929aa295e29f8523d41c (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
/* jwz-style threading
 * clean-room implementation of https://www.jwz.org/doc/threading.html
 * without looking at any code
 *
 * subject threading and sibling sorting is not done yet
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <wchar.h>

#include <search.h>


#include "blaze822.h"

struct container {
        char *mid;
	char *file;
	struct message *msg;
	struct container *parent;
	struct container *child;
	struct container *next;
};

static void *mids;

int
midorder(const void *a, const void *b)
{
        struct container *ia = (struct container *)a;
        struct container *ib = (struct container *)b;

	return strcmp(ia->mid, ib->mid);
}

char *
mid(struct message *msg)
{
	char *v;
	v = blaze822_hdr(msg, "message-id");
	// XXX intern mid?
	if (v) {
		char *m;

		m = strchr(v, '<');
		if (!m)
			return strdup(v);
		v = strchr(m, '>');
		if (!v)
			return strdup(m);
		return strndup(m+1, v-m-1);
	} else {
		// invent new message-id for internal tracking
		static long i;
		char buf[32];
		snprintf(buf, sizeof buf, "thread.%08d@localhost", ++i);
		return strdup(buf);
	}
}

struct container *
midcont(char *mid)
{
	struct container key, **result;
	key.mid = mid;

	if (!(result = tfind(&key, &mids, midorder))) {
		struct container *c = malloc(sizeof (struct container));
		c->mid = mid;
		c->file = 0;
		c->msg = 0;
		c->parent = c->child = c->next = 0;
		printf("ADD %s\n", mid);
		return *(struct container **)tsearch(c, &mids, midorder);
	} else {
		return *result;
	}
}

struct container *
store_id(char *file, struct message *msg)
{
	struct container *c;

	c = midcont(mid(msg));
	c->file = strdup(file);
	c->msg = msg;

	return c;
}

int
reachable(struct container *child, struct container *parent)
{
	if (strcmp(child->mid, parent->mid) == 0)
		return 1;
	else if (parent->child)
		return reachable(child, parent->child);
	else
		return 0;
}

void
thread(char *file)
{
	struct message *msg;

	msg = blaze822(file);
	if (!msg)
		return;

	struct container *c = store_id(file, msg);

	char *mid = "";

	char *v;
	struct container *parent = 0, *me = 0;

	v = blaze822_hdr(msg, "references");
	if (v) {
		char *m, *n;
		parent = 0;
		while (1) {
			m = strchr(v, '<');
			if (!m)
				break;
			v = strchr(m, '>');
			if (!v)
				break;
			mid = strndup(m+1, v-m-1);
			// XXX free?

			printf("ref |%s|\n", mid);

			me = midcont(mid);

			if (me == c)
				continue;

			if (parent && !me->parent &&
			    !reachable(me, parent) && !reachable(parent, me)) {
				me->parent = parent;
				me->next = parent->child;
				parent->child = me;
			}

			parent = me;
		}
	}
	
	v = blaze822_hdr(msg, "in-reply-to");
	char *irt;
	if (v) {
		char *m, *n;
		m = strchr(v, '<');
		if (!m)
			goto out;
		v = strchr(m, '>');
		if (!v)
			goto out;
		irt = strndup(m+1, v-m-1);
		printf("irt |%s|\n", irt);
		
		if (strcmp(irt, mid) != 0) {
			parent = midcont(irt);
		}
	}
out:

	if (parent && parent != c) {
		struct container *r;

		if (c->parent == parent) { // already correct
			goto out2;
		} else if (c->parent) {
			// if we already have a wrong parent, orphan us first

			for (r = c->parent->child; r; r = r->next) {
				if (r->next == c)
					r->next = c->next;
			}
		}

		c->parent = parent;

/*
  not needed with above checks?
		for (r = parent->child; r; r = r->next) {
			// check if we are already a child of the correct parent
			if (r == c)
				goto out2;
		}
*/
		c->next = parent->child;
		parent->child = c;

out2:
		// someone said our parent was our child, a lie
		if (c->child == c->parent) {
			c->child->parent = 0;
			c->child = 0;
		}
	}
}

struct container *top;
struct container *lastc;

void
find_root(const void *nodep, const VISIT which, const int depth)
{
        (void)depth;

        if (which == postorder || which == leaf) {
		struct container *c = *(struct container **)nodep;

		if (!c->parent) {
//			printf("%s\n", c->mid);
			printf("root %s\n", c->mid);
			lastc->next = c;
			c->next = 0;
			lastc = c;
		}
	}
}

void
find_roots()
{
	top = malloc (sizeof (struct container));
	top->msg = 0;
	top->next = top->child = top->parent = 0;
	top->mid = "(top)";

	lastc = top;

	twalk(mids, find_root);

	top->child = top->next;
	top->next = 0;
}

void
print_tree(struct container *c, int depth)
{
	do {
		if (depth >= 0) {
			int i;
			for (i = 0; i < depth; i++)
				printf(" ");
			if (c->file)
				printf("%s\n", c->file);
			else
				printf("<%s>\n", c->mid);
		}
	
		if (c->child)
			print_tree(c->child, depth+1);
	} while (c = c->next);
}

int
main(int argc, char *argv[])
{
	int i = blaze822_loop(argc-1, argv+1, thread);

	printf("%d mails scanned\n", i);
	
	find_roots();

	print_tree(top, -1);

	return 0;
}