1 # include <iostream>
2 # include <memory>
3 # include <boost/algorithm/string/replace.hpp>
4 
5 # include "astroid.hh"
6 # include "db.hh"
7 # include "edit_message.hh"
8 # include "forward_message.hh"
9 
10 # include "actions/action_manager.hh"
11 # include "actions/onmessage.hh"
12 
13 # include "message_thread.hh"
14 # include "utils/address.hh"
15 # include "chunk.hh"
16 
17 namespace Astroid {
ForwardMessage(MainWindow * mw,refptr<Message> _msg,FwdDisposition disp)18   ForwardMessage::ForwardMessage (MainWindow * mw,
19       refptr<Message> _msg,
20       FwdDisposition disp) : EditMessage (mw, false)
21   {
22     using std::endl;
23     using std::string;
24 
25     msg = _msg;
26 
27     LOG (info) << "fwd: forwarding message " << msg->mid;
28 
29     /* set subject */
30     subject = ustring::compose ("Fwd: %1", msg->subject);
31 
32     if (subject.size () > 0) {
33       set_label ("New message: " + subject);
34     }
35 
36     if (disp == FwdDefault) {
37       disp = (astroid->config().get<string> ("mail.forward.disposition") == "attachment") ? FwdAttach : FwdInline;
38     }
39 
40     if (disp == FwdAttach) {
41 
42       add_attachment (new ComposeMessage::Attachment (msg));
43 
44     } else {
45 
46       /* quote original message */
47       std::ostringstream quoted;
48 
49       ustring quoting_a = ustring::compose (astroid->config ().get<string> ("mail.forward.quote_line"),
50           boost::replace_all_copy (string(Address(msg->sender.raw()).fail_safe_name()), "%", "%%"),
51           boost::replace_all_copy (string(msg->pretty_verbose_date()), "%", "%%"));
52 
53       /* date format */
54       Glib::DateTime dt = Glib::DateTime::create_now_local (msg->time);
55       quoting_a = dt.format (quoting_a);
56 
57       quoted  << quoting_a.raw ()
58               << endl;
59 
60       /* add forward header */
61       quoted << "From: " << msg->sender << endl;
62       quoted << "Date: " << msg->pretty_verbose_date() << endl;
63       quoted << "Subject: " << msg->subject << endl;
64       quoted << "To: " << AddressList(msg->to()).str () << endl;
65       auto cc = AddressList (msg->cc());
66       if (cc.addresses.size () > 0)
67         quoted << "Cc: " << AddressList(msg->cc()).str () << endl;
68       quoted << endl;
69 
70       string vt = msg->quote ();
71       quoted << vt;
72 
73       body = ustring(quoted.str());
74 
75       for (auto &c : msg->attachments ()) {
76         add_attachment (new ComposeMessage::Attachment (c));
77       }
78 
79       /* TODO: add non-text parts */
80     }
81 
82 
83     /* determine which account to use */
84     set_from (accounts->get_assosciated_account (msg));
85 
86     /* reload message */
87     prepare_message ();
88     read_edited_message ();
89 
90     /* sent signal */
91     message_sent_attempt().connect (
92         sigc::mem_fun (this, &ForwardMessage::on_message_sent_attempt_received));
93 
94     keys.title = "Forward mode";
95 
96     edit_when_ready ();
97   }
98 
on_message_sent_attempt_received(bool res)99   void ForwardMessage::on_message_sent_attempt_received (bool res) {
100     using std::endl;
101     if (res) {
102       LOG (info) << "fwd: message successfully sent, adding passed tag to original.";
103 
104       if (!msg->in_notmuch) {
105         LOG (warn) << "fwd: message not in notmuch.";
106         return;
107       }
108 
109       astroid->actions->doit (refptr<Action>(
110             new OnMessageAction (msg->mid, msg->tid,
111 
112               [] (Db * db, notmuch_message_t * msg) {
113 
114                 notmuch_message_add_tag (msg, "passed");
115 
116                 if (db->maildir_synchronize_flags){
117                   notmuch_message_tags_to_maildir_flags (msg);
118                 }
119 
120               })));
121     }
122   }
123 }
124 
125