summaryrefslogtreecommitdiff
path: root/src/msglib/msglib.c
blob: 12b1a27139f3806ab2241654d5ac24e0db132547 (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
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
#include "../bbs.h"
#include "../mail_utils.h"
#include "msglib.h"
#include "msglib_jam.h"
#include "msglib_sq3.h"
#include "../../deps/jamlib/jam.h"

int msg_is_to(struct user_record *user, char *addressed_to, char *address, int type, int rn, struct mail_conference *mc) {
	char *myname;
	if (rn) {
		myname = str3dup(user->firstname, " ", user->lastname);
	} else {
		myname = strdup(user->loginname);
	}
	if (type == NETWORK_FIDO && address != NULL) {
		if (strcasecmp(myname, addressed_to) == 0) {
			struct fido_addr *dest = parse_fido_addr(address);
			if (mc->fidoaddr->zone == dest->zone &&
			    mc->fidoaddr->net == dest->net &&
			    mc->fidoaddr->node == dest->node &&
			    mc->fidoaddr->point == dest->point) {
				free(dest);
				free(myname);
				return 1;
			}
			free(dest);
		}
		free(myname);
		return 0;
	} else if (type == NETWORK_MAGI && address != NULL) {
		if (strcasecmp(myname, addressed_to) == 0) {
			int magi_dest = atoi(address);
			if (magi_dest == mc->maginode) {
				free(myname);
				return 1;
			}
		}
		free(myname);
		return 0;
	} else {
		if (strcasecmp(myname, addressed_to) == 0) {
			free(myname);
			return 1;
		}
		free(myname);
		return 0;
	}
}

int msg_is_from(struct user_record *user, char *addressed_from, char *address, int type, int rn, struct mail_conference *mc) {
	char *myname;

	if (rn) {
		myname = str3dup(user->firstname, " ", user->lastname);
	} else {
		myname = strdup(user->loginname);
	}

	if (type == NETWORK_FIDO && address != NULL) {
		if (strcasecmp(myname, addressed_from) == 0) {
			struct fido_addr *orig = parse_fido_addr(address);
			if (mc->fidoaddr->zone == orig->zone &&
			    mc->fidoaddr->net == orig->net &&
			    mc->fidoaddr->node == orig->node &&
			    mc->fidoaddr->point == orig->point) {
				free(orig);
				free(myname);
				return 1;
			}
			free(orig);
		}
		free(myname);
		return 0;
	} else if (type == NETWORK_MAGI && address != NULL) {
		if (strcasecmp(myname, addressed_from) == 0) {
			int magi_orig = atoi(address);
			if (magi_orig == mc->maginode) {
				free(myname);
				return 1;
			}
		}
		free(myname);
		return 0;
	} else {
		if (strcasecmp(myname, addressed_from) == 0) {
			free(myname);
			return 1;
		}
		free(myname);
		return 0;
	}
}

void free_message_hdr(struct msg_t *msg) {
    free(msg->msg_h);
	free(msg->from);
	free(msg->to);
	free(msg->subject);
	free(msg->oaddress);
	free(msg->daddress);
	free(msg->msgid);
	free(msg->replyid);
	free(msg->seenby);
	free(msg);
}

void free_message_headers(struct msg_headers *msghs) {
	int i;

	for (i = 0; i < msghs->msg_count; i++) {
        free_message_hdr(msghs->msgs[i]);
	}
	if (msghs->msg_count > 0) {
		free(msghs->msgs);
	}
	free(msghs);
}

struct msg_headers *read_message_headers(int msgconf, int msgarea, struct user_record *user, int personal) {
    struct mail_area *ma = get_area(msgconf, msgarea);

    switch (ma->base_type) {
        case BASE_TYPE_SQ3:
            return sq3_read_message_headers(msgconf, msgarea, user, personal);
        case BASE_TYPE_JAM:
        default:
            return jam_read_message_headers(msgconf, msgarea, user, personal);
    }
}

struct msg_base_t *open_message_base(int msgconf, int msgarea) {
    struct mail_area *ma = get_area(msgconf, msgarea);

    struct msg_base_t *ret = malloz(sizeof (struct msg_base_t));

    switch (ma->base_type) {
        case BASE_TYPE_SQ3:
            ret->base_type = BASE_TYPE_SQ3;
            ret->ma = ma;
            ret->mc = get_conf(msgconf);
            break;
        case BASE_TYPE_JAM:
        default:
            ret->base_type = BASE_TYPE_JAM;
            ret->ma = ma;
            ret->mc = get_conf(msgconf);
            ret->data.jam = jam_open_base(ma->path);
            if (ret->data.jam == NULL) {
                free(ret);
                return NULL;
            }
            break;
    }

    return ret;
}

int get_message_number(struct msg_headers *hdrs, int id) {
    switch (hdrs->base_type) {
        case BASE_TYPE_SQ3:
            return hdrs->msgs[id]->msg_no;
        case BASE_TYPE_JAM:
        default:
            return ((s_JamMsgHeader *)hdrs->msgs[id]->msg_h)->MsgNum;
    }
}

int get_message_lastread(struct msg_base_t *base, int userid) {
    switch (base->base_type) {
        case BASE_TYPE_SQ3:
            return sq3_message_lastread(base->ma->path, userid);
        case BASE_TYPE_JAM:
        default:
            return jam_message_lastread(base->data.jam, userid);
    }
}

int get_message_highread(struct msg_base_t *base, int userid) {
    switch (base->base_type) {
        case BASE_TYPE_SQ3:
            return sq3_message_highread(base->ma->path, userid);        
        case BASE_TYPE_JAM:
        default:
            return jam_message_highread(base->data.jam, userid);
    }
}

int get_message_issent(struct msg_headers *hdrs, int id) {
    switch (hdrs->base_type) {
        case BASE_TYPE_SQ3:
            return *(uint32_t *)hdrs->msgs[id]->msg_h & SQ3_MSG_SENT;
        case BASE_TYPE_JAM:
        default:
            return (((s_JamMsgHeader *)hdrs->msgs[id]->msg_h)->Attribute & JAM_MSG_SENT);
    }
}

int get_message_islocal(struct msg_headers *hdrs, int id) {
    switch (hdrs->base_type) {
        case BASE_TYPE_SQ3:
            return *(uint32_t *)hdrs->msgs[id]->msg_h & SQ3_MSG_LOCAL;
        case BASE_TYPE_JAM:
        default:
            return (((s_JamMsgHeader *)hdrs->msgs[id]->msg_h)->Attribute & JAM_MSG_LOCAL);
    }
}

int get_header_isprivate(struct msg_base_t *mb, struct msg_t *hdr) {
    switch (mb->base_type) {
        case BASE_TYPE_SQ3:
            return *(uint32_t *)hdr->msg_h & SQ3_MSG_PRIVATE;        
        case BASE_TYPE_JAM:
        default:
            return (((s_JamMsgHeader *)hdr->msg_h)->Attribute & JAM_MSG_PRIVATE);   
    }
}

char *load_message_text(struct msg_base_t *mb, struct msg_t *msg) {
    char *ret = NULL;

    switch (mb->base_type) {
        case BASE_TYPE_SQ3:
            return sq3_fetch_body(mb->ma->path, msg->msg_no);
        case BASE_TYPE_JAM:
        default:
            ret = (char *)malloz(((s_JamMsgHeader *)msg->msg_h)->TxtLen + 1);
	        JAM_ReadMsgText(mb->data.jam, ((s_JamMsgHeader *)msg->msg_h)->TxtOffset, ((s_JamMsgHeader *)msg->msg_h)->TxtLen, (char *)ret);
            break;
    }

    return ret;
}

void write_lasthighread(struct msg_base_t *mb, struct user_record *user, int lastread, int highread) {
    switch (mb->base_type) {
        case BASE_TYPE_SQ3:
            sq3_write_lasthighread(mb->ma->path, user, lastread, highread);
            break;
        case BASE_TYPE_JAM:
        default:
            jam_write_lasthighread(mb->data.jam, user, lastread, highread);
            break;
    }
}

void close_message_base(struct msg_base_t *mb) {
    switch (mb->base_type) {
        case BASE_TYPE_SQ3:
            free(mb);
            return;
        case BASE_TYPE_JAM:
        default: 
            JAM_CloseMB(mb->data.jam);
            free(mb->data.jam);
            free(mb);
            return;
    }
}

int write_message(struct msg_base_t *mb, const char *to, const char *from, const char *subj, const char *body, const char *destaddr, struct msg_t *inreplyto, time_t *dwritten, int dosem) {
    switch (mb->base_type) {
        case BASE_TYPE_SQ3:
            return sq3_write_message(mb, to, from, subj, body, destaddr, inreplyto, dwritten, dosem);
        case BASE_TYPE_JAM:
        default: 
            return jam_write_message(mb, to, from, subj, body, destaddr, inreplyto, dwritten, dosem);
    }
}

struct msg_t *load_message_hdr(struct msg_base_t *mb, int id) {
    switch (mb->base_type) {
        case BASE_TYPE_SQ3:
            return sq3_message_header(mb->ma->path, id);
        case BASE_TYPE_JAM:
        default:
            return jam_message_header(mb->data.jam, id);
    }
}

int new_message_count(struct msg_base_t *mb, struct user_record *user) {
	switch (mb->base_type) {
		case BASE_TYPE_SQ3:
			return sq3_new_message_count(mb, user);
		case BASE_TYPE_JAM:
		default:
			return jam_new_message_count(mb, user);
			
	}
}