aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mail-remote/client.c
blob: 8e97a1c618c032f104cabb9df9e077c748d28b62 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

#include <stdio.h>
#include <string.h>

#include <libbonobo.h>

#include "Evolution-DataServer-Mail.h"

#include "evolution-mail-sessionlistener.h"
#include "evolution-mail-storelistener.h"
#include "evolution-mail-folderlistener.h"
#include "evolution-mail-messagestream.h"

#include <camel/camel-folder.h>

static EvolutionMailSessionListener *listener_sess;
static EvolutionMailStoreListener *listener_store;
static EvolutionMailFolderListener *listener_folder;

#if 0
static char *
em_map_mail_ex(CORBA_Environment *ev, void *data)
{
    Evolution_Mail_MailException *x = CORBA_exception_value(ev);

    switch (x->id) {
    case Evolution_Mail_SYSTEM_ERROR:
        return g_strdup_printf(_("System error: %s"), x->desc);
    case Evolution_Mail_CAMEL_ERROR:
        return g_strdup_printf(_("Camel error: %s"), x->desc);
    default:
        return g_strdup(x->desc);
    }
}
#endif

static void e_mail_exception_dump(CORBA_Environment *ev, char *what)
{
#if 0
    static int init = 0;
    char *d;

    /* *shrug* this doesn't work */
    if (!init) {
        bonobo_exception_add_handler_fn(ex_Evolution_Mail_MailException, em_map_mail_ex, NULL, NULL);
        init = 1;
    }

    d = bonobo_exception_get_text(ev);

    if (d) {
        printf("Failed %s: %s\n", what, d);
        g_free(d);
    }
    CORBA_exception_free(ev);
#else
    const char *id = CORBA_exception_id(ev);

    switch (ev->_major) {
    case CORBA_USER_EXCEPTION:
        if (!strcmp(id, ex_Evolution_Mail_MailException)) {
            Evolution_Mail_MailException *x = CORBA_exception_value(ev);

            switch (x->id) {
            case Evolution_Mail_SYSTEM_ERROR:
                printf("Failed %s: System error %s\n", what, x->desc);
                break;
            case Evolution_Mail_CAMEL_ERROR:
                printf("Failed %s: Camel error %s\n", what, x->desc);
                break;
            default:
                printf("Failed %s: %s\n", what, x->desc);
                break;
            }
            break;
        }
    default:
        printf("Failed %s: %s\n", what, id);
        break;
    }

    CORBA_exception_free(ev);
#endif
}

static Evolution_Mail_Session
get_session(void)
{
    char *path, *ior;
    Evolution_Mail_Session sess = NULL;
    CORBA_Environment ev = { 0 };

    /* The new-improved bonobo-activation ... */

    path = g_build_filename(g_get_home_dir(), ".evolution-mail-remote.ior", NULL);
    if (g_file_get_contents(path, &ior, NULL, NULL)) {
        sess = CORBA_ORB_string_to_object(bonobo_orb(), ior, &ev);
        g_free(ior);
    }

    if (sess != CORBA_OBJECT_NIL) {
        listener_sess = evolution_mail_sessionlistener_new();
        listener_store = evolution_mail_storelistener_new();
        listener_folder = evolution_mail_folderlistener_new();
        Evolution_Mail_Session_addListener(sess, bonobo_object_corba_objref((BonoboObject *)listener_sess), &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            e_mail_exception_dump(&ev, "adding store listener");
        }
    }

    return sess;
}

static void
list_folder(Evolution_Mail_Folder folder)
{
    CORBA_Environment ev = { 0 };
    Evolution_Mail_MessageIterator iter;
    int more, total = 0;

    iter = Evolution_Mail_Folder_getMessages(folder, "", &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        e_mail_exception_dump(&ev, "getting mssages");
        return;
    }

    do {
        Evolution_Mail_MessageInfos *msgs;
        int i;

        msgs = Evolution_Mail_MessageIterator_next(iter, 50, &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            e_mail_exception_dump(&ev, "getting next messages");
            break;
        }

        /* NB: set the first 50 messages in private to unseen */
        if (total == 0) {
            Evolution_Mail_MessageInfoSets *changes;
            int j;

            changes = Evolution_Mail_MessageInfoSets__alloc();
            changes->_length = msgs->_length;
            changes->_maximum = msgs->_maximum;
            changes->_buffer = Evolution_Mail_MessageInfoSets_allocbuf(changes->_maximum);
            for (j=0;j<msgs->_length;j++) {
                changes->_buffer[j].uid = CORBA_string_dup(msgs->_buffer[j].uid);
                changes->_buffer[j].flagSet = 0;
                changes->_buffer[j].flagMask = CAMEL_MESSAGE_SEEN;
            }
            Evolution_Mail_Folder_changeMessages(folder, changes, &ev);
            if (ev._major != CORBA_NO_EXCEPTION)
                e_mail_exception_dump(&ev, "changing messages");
        }

        total += msgs->_length;
        more = msgs->_length == 50;
#if 0
        for (i=0;i<msgs->_length;i++) {
            printf("uid: %s  '%s'\n", msgs->_buffer[i].uid, msgs->_buffer[i].subject);
        }
#endif
        CORBA_free(msgs);
    } while (more);

    printf("calling dispose\n");
    Evolution_Mail_MessageIterator_dispose(iter, &ev);
    if (ev._major != CORBA_NO_EXCEPTION)
        e_mail_exception_dump(&ev, "disposing messageiterator");

    CORBA_Object_release(iter, &ev);

    printf("Got %d messages total\n", total);
}

static void
add_message(Evolution_Mail_Folder folder, const char *msg)
{
    BonoboObject *mem;
    CORBA_Environment ev = { 0 };
    Evolution_Mail_MessageInfoSet mis = { 0 };

    mis.uid = "";
    mis.flagSet = CAMEL_MESSAGE_SEEN;
    mis.flagMask = CAMEL_MESSAGE_SEEN;

    mem = (BonoboObject *)evolution_mail_messagestream_new_buffer(msg, strlen(msg));
            
    printf("attempt send mail to store\n");
    Evolution_Mail_Folder_appendMessage(folder, &mis, bonobo_object_corba_objref(mem), &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        printf("appendmessage failed: %s\n", ev._id);
        CORBA_exception_free(&ev);
        CORBA_exception_init(&ev);
    }
}

static int domain(void *data)
{
    Evolution_Mail_Session sess;
    Evolution_Mail_StoreInfos *stores;
    Evolution_Mail_FolderInfos *folders;
    CORBA_Environment ev = { 0 };
    int i, j, f;

    sess = get_session();

    stores = Evolution_Mail_Session_getStores(sess, "", bonobo_object_corba_objref((BonoboObject *)listener_store), &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        e_mail_exception_dump(&ev, "getting stores");
        _exit(1);
        return 0;
    }

    printf("Got %d stores\n", stores->_length);
    for (i=0;i<stores->_length;i++) {
#if 0
        Evolution_Mail_PropertyName namesarray[] = {
            "name", "uid"
        };
        Evolution_Mail_PropertyNames names = {
            2, 2,
            namesarray,
            FALSE,
        };
        Evolution_Mail_Properties *props;
#endif
        Evolution_Mail_Store store = stores->_buffer[i].store;
        
        printf("store %p '%s' uid '%s'\n", store, stores->_buffer[i].name, stores->_buffer[i].uid);
            
#if 0
        Evolution_Mail_Store_getProperties(store, &names, &props, &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            e_mail_exception_dump(&ev, "getting store properties");
            return 1;
        }

        for (j=0;j<props->_length;j++) {
            printf(" %s = (%s)", props->_buffer[j].name, (char *)ORBit_tk_to_name(props->_buffer[j].value._type->kind));
            if (props->_buffer[j].value._type == TC_CORBA_string) {
                printf(" '%s'\n", (char *)props->_buffer[j].value._value);
            } else {
                printf(" '%s' ", BONOBO_ARG_GET_STRING(&props->_buffer[j].value));
                printf(" <unknonw type>\n");
            }
        }

        CORBA_free(props);
#endif

#if 1
        {
            char *msg = "To: notzed@novell.com\r\n"
                "Subject: This is a test from auto-send\r\n"
                "\r\n" 
                "Blah blah, test message!\r\n";
            BonoboObject *mem;

            mem = (BonoboObject *)evolution_mail_messagestream_new_buffer(msg, strlen(msg));
            
            printf("attempt send mail to store\n");
            Evolution_Mail_Store_sendMessage(store, bonobo_object_corba_objref(mem), &ev);
            if (ev._major != CORBA_NO_EXCEPTION)
                e_mail_exception_dump(&ev, "sending message to store");
            /* If we get a system exception, do we have to dispose it ourselves?? */
        }
#endif

        folders = Evolution_Mail_Store_getFolders(store, "", bonobo_object_corba_objref((BonoboObject *)listener_folder), &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            e_mail_exception_dump(&ev, "getting folders");
        } else {
            for (f = 0; f<folders->_length;f++) {
                printf("folder %p full:'%s' name:'%s'\n", folders->_buffer[f].folder, folders->_buffer[f].full_name, folders->_buffer[f].name);
            }

            for (f = 0; f<folders->_length;f++) {
                if (!strcmp(folders->_buffer[f].full_name, "Private")) {
                    const char *msg = "To: notzed@novell.com\r\n"
                        "Subject: This is a test append from client\r\n"
                        "\r\n" 
                        "Blah blah, test appended message!\r\n";

                    list_folder(folders->_buffer[f].folder);
                    add_message(folders->_buffer[f].folder, msg);
                }
            }

        }
        CORBA_free(folders);
    }

    CORBA_free(stores);

    return 0;
}

int main(int argc, char **argv)
{
    bonobo_init(&argc, argv);

    g_idle_add(domain, NULL);

    bonobo_main();

    return 0;
}