aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mailing-list-actions/mailing-list-actions.c
blob: f8ae698a80e2534cb14ec8c823607bed79a2f984 (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
/*
 * Copyright (C) 2004 Meilof Veeningen <meilof@wanadoo.nl>
 *
 * This file is licensed under the GNU GPL v2 or later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <string.h>
#include <gconf/gconf-client.h>
#include <gtk/gtkcombobox.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtkcelllayout.h>
#include <gtk/gtktable.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtkdialog.h>
#include <libgnome/gnome-url.h>

#include "camel/camel-multipart.h"
#include "camel/camel-mime-part.h"
#include "camel/camel-exception.h"
#include "camel/camel-folder.h"
#include "composer/e-msg-composer.h"
#include "mail/em-composer-utils.h"
#include "mail/em-format-hook.h"
#include "mail/em-format.h"
#include "mail/em-menu.h"
#include "mail/em-config.h"
#include "mail/mail-ops.h"
#include "mail/mail-mt.h"
#include "mail/mail-config.h"
#include "e-util/e-error.h"

typedef enum {
    EMLA_ACTION_HELP,
    EMLA_ACTION_UNSUBSCRIBE,
    EMLA_ACTION_SUBSCRIBE,
    EMLA_ACTION_POST,
    EMLA_ACTION_OWNER,
    EMLA_ACTION_ARCHIVE
} EmlaAction;

typedef struct {
    EmlaAction action;    /* action enumeration */
    gboolean interactive; /* whether the user needs to edit a mailto: message (e.g. for post action) */
    const char* header;   /* header representing the action */
} EmlaActionHeader;

const EmlaActionHeader emla_action_headers[] = {
    { EMLA_ACTION_HELP,        FALSE, "List-Help" },
    { EMLA_ACTION_UNSUBSCRIBE, TRUE,  "List-Unsubscribe" },
    { EMLA_ACTION_SUBSCRIBE,   FALSE, "List-Subscribe" },
    { EMLA_ACTION_POST,        TRUE,  "List-Post" },
    { EMLA_ACTION_OWNER,       TRUE,  "List-Owner" },
    { EMLA_ACTION_ARCHIVE,     FALSE, "List-Archive" },
};

const int emla_n_action_headers = sizeof(emla_action_headers) / sizeof(EmlaActionHeader);

void emla_list_action (EPlugin *item, EMMenuTargetSelect* sel, EmlaAction action);
void emla_list_help (EPlugin *item, EMMenuTargetSelect* sel);
void emla_list_unsubscribe (EPlugin *item, EMMenuTargetSelect* sel);
void emla_list_subscribe (EPlugin *item, EMMenuTargetSelect* sel);
void emla_list_post (EPlugin *item, EMMenuTargetSelect* sel);
void emla_list_owner (EPlugin *item, EMMenuTargetSelect* sel);
void emla_list_archive (EPlugin *item, EMMenuTargetSelect* sel);

void emla_list_action_do (CamelFolder *folder, const char *uid, CamelMimeMessage *msg, void *data);

typedef struct {
    EmlaAction action;
    char* uri;
} emla_action_data;

void emla_list_action (EPlugin *item, EMMenuTargetSelect* sel, EmlaAction action)
{
    emla_action_data *data;

    g_return_if_fail (sel->uids->len == 1);

    data = (emla_action_data *) malloc (sizeof (emla_action_data));
    data->action = action;
    data->uri = strdup (sel->uri);

    mail_get_message (sel->folder, (const char*) g_ptr_array_index (sel->uids, 0),
                      emla_list_action_do, data, mail_thread_new);
}
    
void emla_list_action_do (CamelFolder *folder, const char *uid, CamelMimeMessage *msg, void *data)
{
    emla_action_data *action_data = (emla_action_data *) data;
    EmlaAction action = action_data->action;
    const char* header = NULL, *headerpos;
    char *end, *url = NULL;
    int t;
    EMsgComposer *composer;
    int send_message_response;
    EAccount *account;

    if (msg == NULL)
        return;

    for (t = 0; t < emla_n_action_headers; t++) {
        if (emla_action_headers[t].action == action &&
            (header = camel_medium_get_header (CAMEL_MEDIUM (msg), emla_action_headers[t].header)) != NULL)
            break;
    }

    if (!header) {
        /* there was no header matching the action */
        e_error_run (NULL, "org.gnome.mailing-list-actions:no-header", NULL);
        goto exit;
    }

    headerpos = header;

    if (action == EMLA_ACTION_POST) {
        while (*headerpos == ' ') headerpos++;
        if (g_ascii_strcasecmp (headerpos, "NO") == 0) {
            e_error_run (NULL, "org.gnome.mailing-list-actions:posting-not-allowed", NULL);
            goto exit;
        }
    }
    
    /* parse the action value */
    while (*headerpos) {
        /* skip whitespace */
        while (*headerpos == ' ') headerpos++;
        if (*headerpos != '<' || (end = strchr (headerpos++, '>')) == NULL) {
            e_error_run (NULL, "org.gnome.mailing-list-actions:malformed-header", emla_action_headers[t].header, header, NULL);
            goto exit;
        }
        
        /* get URL portion */
        url = g_strndup(headerpos, end - headerpos);

        if (strncmp (url, "mailto:", 6) == 0) {
            if (emla_action_headers[t].interactive)
                send_message_response = GTK_RESPONSE_NO;
            else
                send_message_response = e_error_run (NULL, "org.gnome.mailing-list-actions:ask-send-message", url, NULL);

            if (send_message_response == GTK_RESPONSE_YES) {
                /* directly send message */
                composer = e_msg_composer_new_from_url (url);
                if ((account = mail_config_get_account_by_source_url (action_data->uri)))
                    e_msg_composer_hdrs_set_from_account (e_msg_composer_get_hdrs(composer), account->name);
                em_utils_composer_send_cb (composer, NULL);
            } else if (send_message_response == GTK_RESPONSE_NO) {
                /* show composer */
                em_utils_compose_new_message_with_mailto (url, action_data->uri);
            }

            goto exit;
        } else {
            GError *err = NULL;
            gnome_url_show (url, &err);
            if (!err)
                goto exit;
            g_error_free (err);         
        }
        g_free (url);
        url = NULL;
        headerpos = end++;
        
        /* ignore everything 'till next comma */
        headerpos = strchr (headerpos, ',');
        if (!headerpos)
            break;
        headerpos++;
    }
    
    /* if we got here, there's no valid action */
    e_error_run (NULL, "org.gnome.mailing-list-actions:no-action", header, NULL);
    
exit:
    free (action_data->uri);
    free (action_data);
    g_free(url);
}

void emla_list_help (EPlugin *item, EMMenuTargetSelect* sel)
{
    emla_list_action (item, sel, EMLA_ACTION_HELP);
}

void emla_list_unsubscribe (EPlugin *item, EMMenuTargetSelect* sel)
{
    emla_list_action (item, sel, EMLA_ACTION_UNSUBSCRIBE);
}

void emla_list_subscribe (EPlugin *item, EMMenuTargetSelect* sel)
{
    emla_list_action (item, sel, EMLA_ACTION_SUBSCRIBE);
}

void emla_list_post (EPlugin *item, EMMenuTargetSelect* sel)
{
    emla_list_action (item, sel, EMLA_ACTION_POST);
}

void emla_list_owner (EPlugin *item, EMMenuTargetSelect* sel)
{
    emla_list_action (item, sel, EMLA_ACTION_OWNER);
}

void emla_list_archive (EPlugin *item, EMMenuTargetSelect* sel)
{
    emla_list_action (item, sel, EMLA_ACTION_ARCHIVE);
}