aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libicalcap/client.c
blob: 399568ac798894e337818d169251b4b535402c23 (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
/*-
 * $Id$
 *
 * See the file LICENSE for redistribution information. 
 * If you have not received a copy of the license, please contact CodeFactory
 * by email at info@codefactory.se, or on the web at http://www.codefactory.se/
 * You may also write to: CodeFactory AB, SE-903 47, Umeå, Sweden.
 *
 * Copyright (c) 2002 Jonas Borgström <jonas@codefactory.se>
 * Copyright (c) 2002 Daniel Lundin   <daniel@codefactory.se>
 * Copyright (c) 2002 CodeFactory AB.  All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>

#include <librr/rr.h>

#include "icalcap.h"
#include "icalcap_session.h"

#define CLIENT_CAPABILITY \
"Content-Type: text/html\n\n\
BEGIN:VCALENDAR\n\
VERSION:2.0\n\
PRODIR:rrcap-client\n\
CMD:REPLY\n\
BEGIN:VREPLY\n\
CAP-VERSION:1.0\n\
PRODID:rrcap-client\n\
QUERY-LEVEL:CAL-QL-NONE\n\
CAR-LEVEL:CAR-FULL-NONE\n\
DATE-MAX:99991231T235959Z\n\
DATE-MIN:00000101T000000Z\n\
MAX-COMPONENT-SIZE:0\n\
COMPONENTS:VCALENDAR,VTODO,VJOURNAL,VEVENT,VCAR,\n\
 VALARM,VFREEBUSY,VTIMEZONE,STANDARD,DAYLIGHT,VREPLY\n\
ITIP-VERSION:2447\n\
RECUR-ACCEPTED:TRUE\n\
RECUR-EXPAND:TRUE\n\
RECUR-LIMIT:0\n\
STORES-EXPANDED:FALSE\n\
END:VREPLY\n\
END:VCALENDAR\n"


icalcomponent *
icalcap_send_cmd(const icalcap *cap, const gchar *cmd, const gchar *id, int timeout) {

    icalcap_message*capmsg;
    icalcomponent  *comp, *ret = NULL;
    icalproperty   *prop;

    prop = icalproperty_vanew_cmd(
        icalproperty_string_to_enum(cmd),
        icalparameter_new_id(id),
        0);

    if (timeout > 0) {
        char buf[16];

        snprintf(buf, 16, "%d", timeout);
        icalproperty_add_parameter(prop,
            icalparameter_new_latency(buf));

        icalproperty_add_parameter(prop,
            icalparameter_new_actionparam(ICAL_ACTIONPARAM_ABORT));
    }

    comp = icalcomponent_vanew(
        ICAL_VCALENDAR_COMPONENT,
        icalproperty_new_version("2.0"),
        icalproperty_new_prodid("-//I.Net spa//NONSGML//EN"),
        prop,
        0);

    capmsg = icalcap_message_new(cap, comp);
    ret = icalcap_message_sync_send(capmsg, timeout);
    icalcap_message_free(capmsg);
    icalcomponent_free(comp);

    return ret;
}

icalcomponent *
icalcap_new_reply_component(const char *id, const icalcomponent *comp) {

    if (comp == NULL)
        return NULL;

    return icalcomponent_vanew(
        ICAL_VCALENDAR_COMPONENT,
        icalproperty_new_version("2.0"),
        icalproperty_new_prodid("-//I.Net spa//NONSGML//EN"),
        icalproperty_vanew_cmd(
            ICAL_CMD_REPLY,
/*          icalparameter_new_id(id), */
            0),
        comp,
        0);
}


static int
msg_handler(const icalcap_message *capmsg) {

    icalcomponent *reply;

    g_message("Got: %s", icalcomponent_as_ical_string(capmsg->comp));

    /* FIXME Check it's a GET-CAPABILITY */

    reply = icalcap_new_reply_component(NULL, capmsg->comp);
    if (reply == NULL) {
        return FALSE;
    }

    icalcomponent_add_property(reply,
        icalproperty_new_prodid("client"));

    icalcomponent_free(reply);
    return TRUE;
}

int
main (gint argc, gchar **argv)
{
    icalcap_session*conn;
    icalcap        *cap;
    icalcomponent  *comp;

    int     i, n;
    int     verbose = 0;

    if ((conn = icalcap_session_new()) == NULL) {
        fprintf(stderr, "Init failed\n");
        exit(0);
    }

    if (!icalcap_session_connect(conn, "gundam.inet.it", 0)) {
        fprintf(stderr, "Connect failed\n");
        exit(0);
    }

    if (!icalcap_session_login(conn, "user@example.com", "user@example.com", "password")) {
        fprintf(stderr, "Login failed\n");
        exit(0);
    }

    if ((cap = icalcap_session_start(conn, msg_handler)) == NULL) {
        fprintf(stderr, "Start failed\n");
        exit(0);
    }

    if (argc > 1 && *argv[1] == '1')
        n = 100;
    else
        n = 1;

    for (i=0; i<n; i++) {
        g_print("Test 1: %d\n", i);

        if ((comp = icalcap_send_cmd(cap, "GET-CAPABILITY", "zero",15))
            == NULL) {
            fprintf(stderr, "Send failed\n");
            exit(0);
        }

        if (verbose)
            g_print("Got %s\n", icalcomponent_as_ical_string(comp));

        if (comp)
            icalcomponent_free(comp);
    }

    if (!icalcap_stop(cap)) {
        fprintf(stderr, "Stop failed\n");
        exit(0);
    }

    if (!icalcap_session_disconnect(conn)) {
        fprintf(stderr, "Disconnect failed\n");
        exit(0);
    }

    return 0;
}