aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-movemail.c
blob: 679e71ec7a2b605c707197e645bc6df753022b04 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#include <camel.h>
#include <e-util/e-setup.h>

static char *
auth_callback (char *prompt, gboolean secret, CamelService *service,
           char *item, CamelException *ex)
{
    char buf[80];

    printf ("%s\n", prompt);
    if (secret)
        printf ("(Warning: your input will be displayed)\n");
    if (fgets (buf, sizeof (buf), stdin) == NULL) {
        camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
                     "User cancelled input.");
        return NULL;
    }
    return g_strdup (buf);
}

extern char *evolution_folders_dir;

int main (int argc, char **argv)
{
    CamelSession *session;
    CamelException *ex;
    CamelStore *store, *outstore;
    CamelFolder *folder, *outfolder;
    int nmsgs, i;
    CamelMimeMessage *msg;
    char *url;
    gboolean delete = FALSE;

    gtk_init (&argc, &argv);
    camel_init ();

    if (argc == 3) {
        if (!strcmp (argv[1], "--delete") ||
            !strcmp (argv[1], "-d")) {
            delete = TRUE;
            argc--;
            argv++;
        }
    }
    if (argc != 2) {
        fprintf (stderr, "Usage: test-movemail [--delete] url\n");
        exit (1);
    }
    e_setup_base_dir ();
    camel_provider_scan ();
    session = camel_session_new (auth_callback);

    ex = camel_exception_new ();
    store = camel_session_get_store (session, argv[1], ex);
    if (!store) {
        fprintf(stderr, "Could not open store %s:\n%s\n", argv[1],
            camel_exception_get_description (ex));
        exit (1);
    }
    camel_service_connect_with_url (CAMEL_SERVICE (store), argv[1], ex);
    if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
        printf ("Couldn't connect to %s:\n%s\n", argv[1],
            camel_exception_get_description (ex));
        exit (1);
    }

    folder = camel_store_get_folder (store, "inbox", ex);
    if (!folder) {
        fprintf(stderr, "Could not get inbox:\n%s\n",
            camel_exception_get_description (ex));
        exit (1);
    }
    camel_folder_open (folder, FOLDER_OPEN_READ, ex);
    if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
        printf ("Couldn't open folder: %s\n",
            camel_exception_get_description (ex));
        exit (1);
    }

    nmsgs = camel_folder_get_message_count (folder, ex);
    if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
        printf ("Couldn't get message count: %s\n",
            camel_exception_get_description (ex));
        exit (1);
    }
    printf ("Inbox contains %d messages.\n", nmsgs);

#ifdef DISPLAY_ONLY
    stdout_stream = camel_stream_fs_new_with_fd (1);
#else
    url = g_strdup_printf ("mbox://%s", evolution_folders_dir);
    outstore = camel_session_get_store (session, url, ex);
    g_free (url);
    if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
        printf ("Couldn't open output store: %s\n",
            camel_exception_get_description (ex));
        exit (1);
    }
    outfolder = camel_store_get_folder (outstore, "inbox", ex);
    if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
        printf ("Couldn't make output folder: %s\n",
            camel_exception_get_description (ex));
        exit (1);
    }
    camel_folder_open (outfolder, FOLDER_OPEN_WRITE, ex);
    if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
        printf ("Couldn't open output folder: %s\n",
            camel_exception_get_description (ex));
        exit (1);
    }
#endif

    for (i = 1; i <= nmsgs; i++) {
        msg = camel_folder_get_message_by_number (folder, i, ex);
        if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
            printf ("Couldn't get message: %s\n",
                camel_exception_get_description (ex));
            exit (1);
        }

#ifdef DISPLAY_ONLY
        camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (msg),
                            stdout_stream);
#else
        camel_folder_append_message (outfolder, msg, ex);
        if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
            printf ("Couldn't write message: %s\n",
                camel_exception_get_description (ex));
            exit (1);
        }

        if (delete) {
            camel_folder_delete_message_by_number (folder, i, ex);
            if (camel_exception_get_id (ex) !=
                CAMEL_EXCEPTION_NONE) {
                printf ("Couldn't delete message: %s\n",
                    camel_exception_get_description (ex));
                exit (1);
            }
        }
#endif
    }

#ifndef DISPLAY_ONLY
    camel_folder_close (outfolder, FALSE, ex);
#endif
    camel_folder_close (folder, TRUE, ex);

    camel_service_disconnect (CAMEL_SERVICE (store), ex);
    if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
        printf ("Couldn't disconnect: %s\n",
            camel_exception_get_description (ex));
        exit (1);
    }

    return 0;
}

void
gratuitous_dependency_generator()
{
    xmlSetProp();
}