File: vnote.c

package info (click to toggle)
libcitadel 917-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,824 kB
  • sloc: ansic: 15,131; sh: 8,304; makefile: 301
file content (275 lines) | stat: -rw-r--r-- 6,992 bytes parent folder | download | duplicates (5)
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
/*
 * vNote implementation for Citadel
 *
 * Copyright (C) 1999-2007 by the citadel.org development team.
 *
 * This program is open source 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 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 <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <libcitadel.h>


struct vnote *vnote_new(void) {
	struct vnote *v;

	v = (struct vnote *) malloc(sizeof(struct vnote));
	if (v) {
		memset(v, 0, sizeof(struct vnote));
		v->magic = CTDL_VNOTE_MAGIC;
		v->pos_left = rand() % 256;
		v->pos_top = rand() % 256;
		v->pos_width = 200;
		v->pos_height = 150;
		v->color_red = 0xFF;
		v->color_green = 0xFF;
		v->color_blue = 0x00;
	}
	return v;
}

struct vnote *vnote_new_from_str(char *s) {
	struct vnote *v;
	char *ptr = s;
	char *nexteol;
	char *thisline;
	int thisline_len;
	char *encoded_value;
	char *decoded_value;
	int is_quoted_printable;
	int is_base64;

	v = vnote_new();
	if (!v) return NULL;

	while (*ptr) {		// keep going until we hit a null terminator
		thisline = NULL;
		nexteol = strchr(ptr, '\n');
		if (nexteol) {
			thisline_len = (nexteol-ptr);
			thisline = malloc(thisline_len + 2);
			memcpy(thisline, ptr, thisline_len);
			thisline[thisline_len] = '\0';
			ptr = nexteol + 1;
		}
		else {
			thisline = strdup(ptr);
			thisline_len = strlen(thisline);
			ptr += thisline_len;
		}

		if (thisline) {
			if (thisline_len > 1) {
				if (thisline[thisline_len - 1] == '\r') {
					thisline[thisline_len - 1] = 0;
					--thisline_len;
				}
			}

			/* locate the colon separator */
			encoded_value = strchr(thisline, ':');
			if (encoded_value) {
				*encoded_value++ = 0;

				/* any qualifiers?  (look for a semicolon) */
				is_base64 = bmstrcasestr(thisline, "encoding=base64") ? 1 : 0;
				is_quoted_printable = bmstrcasestr(thisline, "encoding=quoted-printable") ? 1 : 0;

				char *semicolon_pos = strchr(thisline, ';');
				if (semicolon_pos) {
					*semicolon_pos = 0;
				}

				decoded_value = malloc(thisline_len);
				if (is_base64) {
					CtdlDecodeBase64(decoded_value,
							encoded_value,
							strlen(encoded_value));
				}
				else if (is_quoted_printable) {
					CtdlDecodeQuotedPrintable(decoded_value,
							encoded_value,
							strlen(encoded_value));
				}
				else {
					strcpy(decoded_value, encoded_value);
				}

				if (!strcasecmp(thisline, "UID")) {
					if (v->uid) free(v->uid);
					v->uid = decoded_value;
				}
				else if (!strcasecmp(thisline, "SUMMARY")) {
					if (v->summary) free(v->summary);
					v->summary = decoded_value;
				}
				else if ( (!strcasecmp(thisline, "NOTE"))
				     || (!strcasecmp(thisline, "BODY")) ) {
					if (v->body) free(v->body);
					v->body = decoded_value;
				}
				else if (!strcasecmp(thisline, "X-OUTLOOK-WIDTH")) {
					v->pos_width = atoi(decoded_value);
					free(decoded_value);
				}
				else if (!strcasecmp(thisline, "X-OUTLOOK-HEIGHT")) {
					v->pos_height = atoi(decoded_value);
					free(decoded_value);
				}
				else if (!strcasecmp(thisline, "X-OUTLOOK-LEFT")) {
					v->pos_left = atoi(decoded_value);
					free(decoded_value);
				}
				else if (!strcasecmp(thisline, "X-OUTLOOK-TOP")) {
					v->pos_top = atoi(decoded_value);
					free(decoded_value);
				}
				else if ( (!strcasecmp(thisline, "X-OUTLOOK-COLOR"))
				     && (strlen(decoded_value) == 7)
				     && (decoded_value[0] == '#') ) {
					sscanf(&decoded_value[1], "%2x%2x%2x",
						&v->color_red,
						&v->color_green,
						&v->color_blue);
					free(decoded_value);
				}
				else {
					free(decoded_value);	// throw it away
				}

				/* FIXME still need to handle these:
				 * X-OUTLOOK-CREATE-TIME:20070611T204615Z
				 * REV:20070611T204621Z
				 */
			}
			free(thisline);
		}
	}

	return(v);
}

void vnote_free(struct vnote *v) {
	if (!v) return;
	if (v->magic != CTDL_VNOTE_MAGIC) return;

	if (v->uid) free(v->uid);
	if (v->summary) free(v->summary);
	if (v->body) free(v->body);
	
	memset(v, 0, sizeof(struct vnote));
	free(v);
}


/* helper function for vnote_serialize() */
void vnote_serialize_output_field(char *append_to, char *field, char *label) {

	char *mydup;
	int output_len = 0;
	int is_qp = 0;
	char *ptr = field;
	unsigned char ch;
	int pos = 0;

	if (!append_to) return;
	if (!field) return;
	if (!label) return;

	mydup = malloc((strlen(field) * 3) + 1);
	if (!mydup) return;
	*mydup = '\0';

	while (ptr[pos] != 0) {
		ch = (unsigned char)(ptr[pos++]);

		if (ch == 9) {
			mydup[output_len++] = ch;
		}
		else if ( (ch >= 32) && (ch <= 60) ) {
			mydup[output_len++] = ch;
		}
		else if ( (ch >= 62) && (ch <= 126) ) {
			mydup[output_len++] = ch;
		}
		else {
			sprintf((char *)&mydup[output_len], "=%02X", ch);
			output_len += 3;
			is_qp = 1;
		}
	}
	mydup[output_len] = 0;

	sprintf(&append_to[strlen(append_to)], "%s%s:%s\r\n",
		label,
		(is_qp ? ";ENCODING=QUOTED-PRINTABLE" : ""),
		mydup);
	free(mydup);
}


char *vnote_serialize(struct vnote *v) {
	char *s;
	int bytes_needed = 0;

	if (!v) return NULL;
	if (v->magic != CTDL_VNOTE_MAGIC) return NULL;

	bytes_needed = 1024;
	if (v->summary) bytes_needed += strlen(v->summary);
	if (v->body) bytes_needed += strlen(v->body);
	s = malloc(bytes_needed);
	if (!s) return NULL;

	*s = '\0';
	vnote_serialize_output_field(s, "vnote", "BEGIN");
	vnote_serialize_output_field(s, "//Citadel//vNote handler library//EN", "PRODID");
	vnote_serialize_output_field(s, "1.1", "VERSION");
	vnote_serialize_output_field(s, "PUBLIC", "CLASS");
	vnote_serialize_output_field(s, v->uid, "UID");
	vnote_serialize_output_field(s, v->summary, "SUMMARY");
	vnote_serialize_output_field(s, v->body, "BODY");
	vnote_serialize_output_field(s, v->body, "NOTE");
	sprintf(&s[strlen(s)], "X-OUTLOOK-COLOR:#%02X%02X%02X\r\n",
		v->color_red, v->color_green, v->color_blue);
	sprintf(&s[strlen(s)], "X-OUTLOOK-LEFT:%d\r\n", v->pos_left);
	sprintf(&s[strlen(s)], "X-OUTLOOK-TOP:%d\r\n", v->pos_top);
	sprintf(&s[strlen(s)], "X-OUTLOOK-WIDTH:%d\r\n", v->pos_width);
	sprintf(&s[strlen(s)], "X-OUTLOOK-HEIGHT:%d\r\n", v->pos_height);
	vnote_serialize_output_field(s, "vnote", "END");
	return(s);
}