aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mail-remote/client.c
blob: 0db321242696a698587282ecbf2a9857ec58d9b3 (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

#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 <camel/camel-folder.h>

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

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) {
            printf("AddListener failed: %s\n", ev._id);
            CORBA_exception_free(&ev);
        }
    }

    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) {
        printf("getmessages failed: %s\n", ev._id);
        CORBA_exception_free(&ev);
        return;
    }

    do {
        Evolution_Mail_MessageInfos *msgs;
        int i;

        msgs = Evolution_Mail_MessageIterator_next(iter, 50, &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            printf("msgs.next(): %s\n", ev._id);
            CORBA_exception_free(&ev);
            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) {
                printf("changemessages failed: %s\n", ev._id);
                CORBA_exception_free(&ev);
                memset(&ev, 0, sizeof(ev));
            }
        }

        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);

    Evolution_Mail_MessageIterator_dispose(iter, &ev);
    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 = bonobo_stream_mem_create(msg, strlen(msg), TRUE, FALSE);
            
    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);
    }

    bonobo_object_unref(mem);
}

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) {
        printf("getStores failed: %s\n", ev._id);
        CORBA_exception_free(&ev);
        _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) {
            printf("getProperties failed\n");
            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 = bonobo_stream_mem_create(msg, strlen(msg), TRUE, FALSE);
            
            printf("attempt send mail to store\n");
            Evolution_Mail_Store_sendMessage(store, bonobo_object_corba_objref(mem), &ev);
            if (ev._major != CORBA_NO_EXCEPTION) {
                printf("sendmessage failed: %s\n", ev._id);
                CORBA_exception_free(&ev);
                CORBA_exception_init(&ev);
            }

            g_object_unref(mem);
        }
#endif

        folders = Evolution_Mail_Store_getFolders(store, "", bonobo_object_corba_objref((BonoboObject *)listener_folder), &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            printf("getfolders failed\n");
            /* FIXME: leaks ex data? */
            CORBA_exception_free(&ev);
        } 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;
}