summaryrefslogtreecommitdiff
path: root/src/libicalcap/icalcap_utils.c
blob: 7308f8fb8266de6e22113adb61c6d351fc8d2660 (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
/*-
 * $Id: icalcap_utils.c,v 1.3 2008-01-02 20:07:38 dothebart Exp $
 *
 * See the file LICENSE for redistribution information. 
 *
 * Copyright (c) 2002 Andrea Campi <a.campi@inet.it>
 */

#include "config.h"

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "icalcap.h"

#define	CONTENT_TYPE	"Content-Type: text/calendar"


icalcomponent *
icalcap_component_new_from_string(const char *data) {

	icalcomponent  *ret = NULL;
	char	       *mtype;

	/* FIXME split the check */
	if (strncmp(data, CONTENT_TYPE, strlen(CONTENT_TYPE))) {
		return NULL;
	}

	mtype = (char *)data+strlen(CONTENT_TYPE);

	ret = icalcomponent_new_from_string(mtype);
	if (ret == NULL) {
		return NULL;
	}

#ifdef DEBUG
	g_message("icalcap_component_new_from_string icalcomponent_new_from_string = %p", ret);
#endif

	/* FIXME
	 * Validate here: should check at least the version
	 */
	if (icalcomponent_isa(ret) != ICAL_VCALENDAR_COMPONENT &&
	    icalcomponent_isa(ret) != ICAL_XROOT_COMPONENT) {
		icalcomponent_free(ret);

		return NULL;
	}

	return ret;
}

#if 0
RRCAPCmdArgs *
msg_parse(RRCAP *cap, icalcomponent *comp) {

	icalproperty   *prop;
	icalparameter  *param;
	icalvalue      *value;
	RRCAPCmdArgs   *ret = g_new0(RRCAPCmdArgs, 1);
	char *str;

	ret->comp = comp;

	/* Find the command */
	if ((prop = icalcomponent_get_first_property(comp, ICAL_CMD_PROPERTY)) == NULL) {
		rr_cap_send_error(cap, NULL, ICAL_9_0_UNRECOGNIZED_COMMAND,
			"No CMD sent", NULL);
		goto FAILED;
	}
	if ((value = icalproperty_get_value(prop)) == NULL) {
		str = icalproperty_as_ical_string_r(prop);
		rr_cap_send_error(cap, NULL, ICAL_9_0_UNRECOGNIZED_COMMAND,
			"CMD has no value", str);
		free(str);
		goto FAILED;
	}
	ret->cmd = icalvalue_get_cmd(value);

	/* Look for params */

	/* ID */
	if ((param = icalproperty_get_first_parameter(prop,
						ICAL_ID_PARAMETER)) != NULL) {
		if ((ret->id = icalparameter_get_id(param)) == NULL) {
			str = icalproperty_as_ical_string_r(prop);
			rr_cap_send_error(cap, NULL,
				ICAL_9_0_UNRECOGNIZED_COMMAND,
				"ID param is garbled",
				str);
			free(str);
			goto FAILED;
		}
	}

	/* LATENCY */
	if ((param = icalproperty_get_first_parameter(prop,
						ICAL_LATENCY_PARAMETER)) != NULL) {
		const char *tmp;
		if ((tmp = icalparameter_get_latency(param)) == NULL) {
			str = icalproperty_as_ical_string_r(prop);
			rr_cap_send_error(cap, NULL,
				ICAL_9_0_UNRECOGNIZED_COMMAND,
				"LATENCY is garbled",
				str);
			free(str);
			goto FAILED;
		}

		ret->latency = atoi(tmp);
	}

	/* ACTION */
	if ((param = icalproperty_get_first_parameter(prop,
						ICAL_ACTIONPARAM_PARAMETER)) != NULL) {
		if ((ret->action = icalparameter_get_actionparam(param))
		    == NULL) {
			str = icalproperty_as_ical_string_r(prop);
			rr_cap_send_error(cap, NULL,
				ICAL_9_0_UNRECOGNIZED_COMMAND,
				"ACTION is garbled",
				str);
			free(str);
			goto FAILED;
		}
	}

	if ((ret->latency >= 0) ^ (ret->action != ICAL_ACTIONPARAM_NONE)) {
		str = icalproperty_as_ical_string_r(prop);
		rr_cap_send_error(cap, NULL, ICAL_9_0_UNRECOGNIZED_COMMAND,
			"LATENCY and ACTION must be both present",
			str);
		free(str);
		goto FAILED;
	}

	return ret;

FAILED:
	g_free(ret);
	return NULL;
}
#endif