Skip to content

Commit

Permalink
reply, forward: use lynx (or configurable program) to convert HTML pa…
Browse files Browse the repository at this point in the history
…rts to text when quoting
  • Loading branch information
gauteh committed Apr 3, 2019
1 parent 48ec1e2 commit 0b188f5
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/compose_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace Astroid {
sf << s.rdbuf ();
s.close ();
if (account->signature_separate) {
md_body_content += "-- \n";
md_body_content += "-- \n";
}
md_body_content += sf.str ();
}
Expand Down Expand Up @@ -228,6 +228,7 @@ namespace Astroid {
contentStream = g_mime_stream_mem_new_with_buffer(_html.c_str(), _html.size());
}

g_spawn_close_pid (pid);
} catch (Glib::SpawnError &ex) {
LOG (error) << "cm: md: failed to spawn markdown processor: " << ex.what ();

Expand Down
2 changes: 2 additions & 0 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ namespace Astroid {
default_config.put ("editor.markdown_processor", "cmark");
default_config.put ("editor.markdown_on", false); // default

default_config.put ("mail.reply.quote_processor", "lynx -dump -stdin"); // e.g. lynx -dump

/* mail composition */
default_config.put ("mail.reply.quote_line", "Excerpts from %1's message of %2:"); // %1 = author, %2 = pretty_verbose_date
default_config.put ("mail.reply.mailinglist_reply_to_sender", true);
Expand Down
64 changes: 64 additions & 0 deletions src/message_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# include "message_thread.hh"
# include "chunk.hh"
# include "utils/utils.hh"
# include "utils/cmd.hh"
# include "utils/date_utils.hh"
# include "utils/address.hh"
# include "utils/ustring_utils.hh"
Expand Down Expand Up @@ -367,6 +368,69 @@ namespace Astroid {
return body;
}

ustring Message::quote () {
if (missing_content) {
LOG (warn) << "message: missing content, no text.";
return "";
}

ustring body;

function< void (refptr<Chunk>) > app_body =
[&] (refptr<Chunk> c)
{
/* check if we're the preferred sibling */
bool use = false;

if (c->siblings.size() >= 1) {
if (c->is_content_type ("text", "plain") || c->is_content_type ("text", "html")) {
use = true;
} else {
/* check if there are any other preferred */
if (all_of (c->siblings.begin (),
c->siblings.end (),
[](refptr<Chunk> c) { return !(c->is_content_type ("text", "plain") || c->is_content_type("text", "html")); })) {
use = true; // no
} else {
use = false;
}
}
} else {
use = true;
}

if (use) {
if (c->viewable && (c->is_content_type ("text", "plain") || c->is_content_type ("text", "html"))) {
/* will output html if HTML part */
if (c->is_content_type ("text", "html")) {
ustring quote_cmd = astroid->config ().get<string>("mail.reply.quote_processor");

if (!quote_cmd.empty()) {
ustring h = c->viewable_text (false);
ustring _stdout, _stderr;
Cmd::pipe (quote_cmd, h, _stdout, _stderr);

LOG (debug) << "got: " << _stdout;

body += _stdout;
}

} else {
body += c->viewable_text (false);
}
}

for_each (c->kids.begin(),
c->kids.end (),
app_body);
}
};

app_body (root);

return body;
}

vector<refptr<Chunk>> Message::attachments () {
/* return a flat vector of attachments */

Expand Down
1 change: 1 addition & 0 deletions src/message_thread.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace Astroid {
std::vector<ustring> tags;

ustring plain_text (bool fallback_html = false);
ustring quote ();
std::vector<refptr<Chunk>> attachments ();
refptr<Chunk> get_chunk_by_id (int id);

Expand Down
2 changes: 1 addition & 1 deletion src/modes/edit_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ namespace Astroid {

if (tmpfile.fail()) {
LOG (error) << "em: error: could not create tmpfile!";
throw runtime_error ("em: coult not create tmpfile!");
throw runtime_error ("em: could not create tmpfile!");
}

tmpfile.close ();
Expand Down
2 changes: 1 addition & 1 deletion src/modes/forward_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace Astroid {
quoted << "Cc: " << AddressList(msg->cc()).str () << endl;
quoted << endl;

string vt = msg->plain_text (false);
string vt = msg->quote ();
quoted << vt;

body = ustring(quoted.str());
Expand Down
2 changes: 1 addition & 1 deletion src/modes/reply_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Astroid {
quoted << quoting_a.raw ()
<< endl;

string vt = msg->plain_text (false);
string vt = msg->quote ();
stringstream sstr (vt);
while (sstr.good()) {
string line;
Expand Down
61 changes: 59 additions & 2 deletions src/utils/cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using std::endl;
using std::string;

namespace bfs = boost::filesystem;

namespace Astroid {
Expand Down Expand Up @@ -44,9 +45,8 @@ namespace Astroid {
ustring c = (!undo ? cmd : undo_cmd);

LOG (info) << "cmd: running: " << c;
string _stdout;
string _stderr;
int exit;
string _stdout, _stderr;

string _cmd = c;
try {
Expand All @@ -71,6 +71,7 @@ namespace Astroid {
return (exit == 0);
}


ustring Cmd::substitute (const ustring _cmd) {
ustring ncmd = _cmd;

Expand All @@ -82,6 +83,62 @@ namespace Astroid {

return ncmd;
}

bool Cmd::pipe (ustring cmd, const ustring& _stdin, ustring& _stdout, ustring &_stderr) {
LOG (info) << "cmd: running: " << cmd;

try {
int pid;
int stdin;
int stdout;
int stderr;
std::vector<std::string> args = Glib::shell_parse_argv (cmd);

Glib::spawn_async_with_pipes ("",
args,
Glib::SPAWN_DO_NOT_REAP_CHILD |
Glib::SPAWN_SEARCH_PATH,
sigc::slot <void> (),
&pid,
&stdin,
&stdout,
&stderr
);

refptr<Glib::IOChannel> ch_stdin;
refptr<Glib::IOChannel> ch_stdout;
refptr<Glib::IOChannel> ch_stderr;
ch_stdin = Glib::IOChannel::create_from_fd (stdin);
ch_stdout = Glib::IOChannel::create_from_fd (stdout);
ch_stderr = Glib::IOChannel::create_from_fd (stderr);

LOG (debug) << "cmd: writing: " << _stdin;

ch_stdin->write (_stdin);
ch_stdin->close ();

ch_stderr->read_to_end (_stderr);
ch_stderr->close ();

if (!_stderr.empty ()) {
for (auto &l : VectorUtils::split_and_trim (_stderr, "\n")) {
if (!l.empty ()) LOG (error) << "cmd: " << l;
}
}

ch_stdout->read_to_end (_stdout);
ch_stdout->close ();

g_spawn_close_pid (pid);

} catch (Glib::SpawnError &ex) {
LOG (error) << "cmd: failed to execute: '" << cmd << "': " << ex.what ();
return false;
}


return true;
}
}


10 changes: 5 additions & 5 deletions src/utils/cmd.hh
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# pragma once

# include "astroid.hh"

# include <mutex>
# include <chrono>
# include <glibmm/threads.h>
# include <glibmm/iochannel.h>
# include <string>

namespace Astroid {
class Cmd {
Expand All @@ -25,8 +21,12 @@ namespace Astroid {
ustring undo_cmd;

int execute (bool undo); /* currently only in sync */
int execute (bool undo, std::string& _stdout, std::string& _stderr); /* currently only in sync */

ustring substitute (ustring);

public:
static bool pipe (ustring cmd, const ustring& _stdin, ustring& _stdout, ustring& _stderr);
};
}

0 comments on commit 0b188f5

Please sign in to comment.