aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mailing-list-actions/mailing-list-actions.c
blob: 2ed27c3e59c2c309edd3cdb8d998d40b2e9e7fc0 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 * Copyright (C) 2004 Meilof Veeningen <meilof@wanadoo.nl>
 *
 */

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

#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gconf/gconf-client.h>

#include "composer/e-msg-composer.h"
#include "mail/e-mail-browser.h"
#include "mail/e-mail-reader.h"
#include "mail/em-composer-utils.h"
#include "mail/em-format-hook.h"
#include "mail/em-config.h"
#include "mail/em-utils.h"
#include "mail/mail-ops.h"
#include "mail/mail-mt.h"
#include "mail/mail-config.h"
#include "mail/message-list.h"
#include "e-util/e-util.h"
#include "e-util/e-alert-dialog.h"
#include "shell/e-shell-view.h"
#include "shell/e-shell-window.h"
#include "shell/e-shell-window-actions.h"

/* EAlert Message IDs */
#define MESSAGE_PREFIX          "org.gnome.mailing-list-actions:"
#define MESSAGE_NO_ACTION       MESSAGE_PREFIX "no-action"
#define MESSAGE_NO_HEADER       MESSAGE_PREFIX "no-header"
#define MESSAGE_ASK_SEND_MESSAGE    MESSAGE_PREFIX "ask-send-message"
#define MESSAGE_MALFORMED_HEADER    MESSAGE_PREFIX "malformed-header"
#define MESSAGE_POSTING_NOT_ALLOWED MESSAGE_PREFIX "posting-not-allowed"

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

typedef struct {
    /* action enumeration */
    EmlaAction action;

    /* whether the user needs to edit a mailto:
     * message (e.g. for post action) */
    gboolean interactive;

    /* header representing the action */
    const gchar *header;
} 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" },
};

gboolean    mail_browser_init       (GtkUIManager *ui_manager,
                         EMailBrowser *browser);
gboolean    mail_shell_view_init        (GtkUIManager *ui_manager,
                         EShellView *shell_view);

typedef struct {
    EMailReader *reader;
    EmlaAction action;
    gchar * uri;
} emla_action_data;

static void
emla_list_action_do (CamelFolder *folder,
                     const gchar *uid,
                     CamelMimeMessage *msg,
                     gpointer data)
{
    emla_action_data *action_data = (emla_action_data *) data;
    EmlaAction action = action_data->action;
    const gchar * header = NULL, *headerpos;
    gchar *end, *url = NULL;
    gint t;
    EMsgComposer *composer;
    gint send_message_response;
    EAccount *account;
    GtkWindow *window;

    if (msg == NULL)
        return;

    window = e_mail_reader_get_window (action_data->reader);

    for (t = 0; t < G_N_ELEMENTS (emla_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_alert_run_dialog_for_args (window, MESSAGE_NO_HEADER, NULL);
        goto exit;
    }

    headerpos = header;

    if (action == EMLA_ACTION_POST) {
        while (*headerpos == ' ') headerpos++;
        if (g_ascii_strcasecmp (headerpos, "NO") == 0) {
            e_alert_run_dialog_for_args (
                window, MESSAGE_POSTING_NOT_ALLOWED, NULL);
            goto exit;
        }
    }

    /* parse the action value */
    while (*headerpos) {
        /* skip whitespace */
        while (*headerpos == ' ') headerpos++;
        if (*headerpos != '<' || (end = strchr (headerpos++, '>')) == NULL) {
            e_alert_run_dialog_for_args (
                window, MESSAGE_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_alert_run_dialog_for_args (
                    window, MESSAGE_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_composer_header_table_set_account (
                        e_msg_composer_get_header_table (composer),
                        account);
                e_msg_composer_send (composer);
            } else if (send_message_response == GTK_RESPONSE_NO) {
                /* show composer */
                em_utils_compose_new_message_with_mailto (url, action_data->uri);
            }

            goto exit;
        } else {
            e_show_uri (window, url);
            goto exit;
        }
        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_alert_run_dialog_for_args (window, MESSAGE_NO_ACTION, header, NULL);

exit:
    g_object_unref (action_data->reader);
    g_free (action_data->uri);
    g_free (action_data);
    g_free (url);
}

static void
emla_list_action (EMailReader *reader,
                  EmlaAction action)
{
    CamelFolder *folder;
    GPtrArray *uids;
    emla_action_data *data;
    const gchar *folder_uri;

    folder = e_mail_reader_get_folder (reader);
    folder_uri = e_mail_reader_get_folder_uri (reader);
    uids = e_mail_reader_get_selected_uids (reader);

    g_return_if_fail (uids->len == 1);

    data = g_malloc (sizeof (emla_action_data));
    data->reader = g_object_ref (reader);
    data->action = action;
    data->uri = g_strdup (folder_uri);

    mail_get_message (
        folder, uids->pdata[0],
        emla_list_action_do, data,
        mail_msg_unordered_push);

    em_utils_uids_free (uids);
}

static void
action_mailing_list_archive_cb (GtkAction *action,
                                EMailReader *reader)
{
    emla_list_action (reader, EMLA_ACTION_ARCHIVE);
}

static void
action_mailing_list_help_cb (GtkAction *action,
                             EMailReader *reader)
{
    emla_list_action (reader, EMLA_ACTION_HELP);
}

static void
action_mailing_list_owner_cb (GtkAction *action,
                              EMailReader *reader)
{
    emla_list_action (reader, EMLA_ACTION_OWNER);
}

static void
action_mailing_list_post_cb (GtkAction *action,
                             EMailReader *reader)
{
    emla_list_action (reader, EMLA_ACTION_POST);
}

static void
action_mailing_list_subscribe_cb (GtkAction *action,
                                  EMailReader *reader)
{
    emla_list_action (reader, EMLA_ACTION_SUBSCRIBE);
}

static void
action_mailing_list_unsubscribe_cb (GtkAction *action,
                                    EMailReader *reader)
{
    emla_list_action (reader, EMLA_ACTION_UNSUBSCRIBE);
}

static GtkActionEntry mailing_list_entries[] = {

    { "mailing-list-archive",
      NULL,
      N_("Get List _Archive"),
      NULL,
      N_("Get an archive of the list this message belongs to"),
      G_CALLBACK (action_mailing_list_archive_cb) },

    { "mailing-list-help",
      NULL,
      N_("Get List _Usage Information"),
      NULL,
      N_("Get information about the usage of the list this message belongs to"),
      G_CALLBACK (action_mailing_list_help_cb) },

    { "mailing-list-owner",
      NULL,
      N_("Contact List _Owner"),
      NULL,
      N_("Contact the owner of the mailing list this message belongs to"),
      G_CALLBACK (action_mailing_list_owner_cb) },

    { "mailing-list-post",
      NULL,
      N_("_Post Message to List"),
      NULL,
      N_("Post a message to the mailing list this message belongs to"),
      G_CALLBACK (action_mailing_list_post_cb) },

    { "mailing-list-subscribe",
      NULL,
      N_("_Subscribe to List"),
      NULL,
      N_("Subscribe to the mailing list this message belongs to"),
      G_CALLBACK (action_mailing_list_subscribe_cb) },

    { "mailing-list-unsubscribe",
      NULL,
      N_("_Unsubscribe from List"),
      NULL,
      N_("Unsubscribe from the mailing list this message belongs to"),
      G_CALLBACK (action_mailing_list_unsubscribe_cb) },

    /*** Menus ***/

    { "mailing-list-menu",
      NULL,
      N_("Mailing _List"),
      NULL,
      NULL,
      NULL }
};

static void
update_actions_cb (EMailReader *reader,
                   GtkActionGroup *action_group)
{
    gboolean sensitive;
    guint32 state;

    state = e_mail_reader_check_state (reader);
    sensitive = (state & E_MAIL_READER_SELECTION_IS_MAILING_LIST);
    gtk_action_group_set_sensitive (action_group, sensitive);
}

static void
setup_actions (EMailReader *reader,
               GtkUIManager *ui_manager)
{
    GtkActionGroup *action_group;
    const gchar *domain = GETTEXT_PACKAGE;

    action_group = gtk_action_group_new ("mailing-list");
    gtk_action_group_set_translation_domain (action_group, domain);
    gtk_action_group_add_actions (
        action_group, mailing_list_entries,
        G_N_ELEMENTS (mailing_list_entries), reader);
    gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
    g_object_unref (action_group);

    /* GtkUIManager now owns the action group reference.
     * The signal we're connecting to will only be emitted
     * during the GtkUIManager's lifetime, so the action
     * group will not disappear on us. */

    g_signal_connect (
        reader, "update-actions",
        G_CALLBACK (update_actions_cb), action_group);
}

gboolean
mail_browser_init (GtkUIManager *ui_manager,
                   EMailBrowser *browser)
{
    setup_actions (E_MAIL_READER (browser), ui_manager);

    return TRUE;
}

gboolean
mail_shell_view_init (GtkUIManager *ui_manager,
                      EShellView *shell_view)
{
    EShellContent *shell_content;

    shell_content = e_shell_view_get_shell_content (shell_view);

    setup_actions (E_MAIL_READER (shell_content), ui_manager);

    return TRUE;
}