add hop info

add hop info creation

Save it to the db

format and fix clippy

replace `and_then` with simple check

cargo fmt

chage table to get info from
This commit is contained in:
Sebastian
2021-07-08 21:24:13 +02:00
parent 57870ec54a
commit 4fa0edcfbf
5 changed files with 67 additions and 4 deletions

View File

@@ -965,7 +965,7 @@ INSERT INTO msgs
txt, subject, txt_raw, param,
bytes, hidden, mime_headers, mime_in_reply_to,
mime_references, mime_modified, error, ephemeral_timer,
ephemeral_timestamp
ephemeral_timestamp, hop_info
)
VALUES (
?, ?, ?, ?,
@@ -974,7 +974,7 @@ INSERT INTO msgs
?, ?, ?, ?,
?, ?, ?, ?,
?, ?, ?, ?,
?
?, ?
);
"#,
)?;
@@ -1050,7 +1050,8 @@ INSERT INTO msgs
mime_modified,
part.error.take().unwrap_or_default(),
ephemeral_timer,
ephemeral_timestamp
ephemeral_timestamp,
mime_parser.hop_info
])?;
let row_id = conn.last_insert_rowid();

View File

@@ -5,6 +5,7 @@ use core::cmp::{max, min};
use std::borrow::Cow;
use std::fmt;
use std::io::Cursor;
use std::str::from_utf8;
use std::str::FromStr;
use std::time::{Duration, SystemTime};
@@ -14,6 +15,10 @@ use async_std::{fs, io};
use anyhow::{bail, Error};
use chrono::{Local, TimeZone};
use itertools::Itertools;
use mailparse::dateparse;
use mailparse::headers::Headers;
use mailparse::MailHeaderMap;
use rand::{thread_rng, Rng};
use crate::chat::{add_device_msg, add_device_msg_with_importance};
@@ -670,6 +675,47 @@ pub fn remove_subject_prefix(last_subject: &str) -> String {
.to_string()
}
// Types and methods to create hop-info for message-info
fn extract_address_from_receive_header<'a>(header: &'a str, start: &str) -> Option<&'a str> {
let header_len = header.len();
header.find(start).and_then(|mut begin| {
begin += start.len();
let end = header.get(begin..)?.find(' ').unwrap_or(header_len);
header.get(begin..begin + end)
})
}
pub(crate) fn parse_receive_header(header: &str) -> String {
let mut hop_info = String::from("Hop:\n");
if let Ok(date) = dateparse(header) {
let date_obj = Local.timestamp(date, 0);
hop_info.push_str(&format!("Date: {}\n", date_obj.to_rfc2822()));
};
if let Some(from) = extract_address_from_receive_header(header, "from ") {
hop_info.push_str(&format!("From: {}\n", from));
}
if let Some(by) = extract_address_from_receive_header(header, "by ") {
hop_info.push_str(&format!("By: {}\n", by));
}
hop_info
}
/// parses "receive"-headers
pub(crate) fn parse_receive_headers(headers: &Headers) -> String {
let headers = headers
.get_all_headers("Received")
.iter()
.filter_map(|header_map_item| from_utf8(header_map_item.get_value_raw()).ok())
.map(|header_value| parse_receive_header(header_value))
.collect::<Vec<_>>();
headers.iter().map(|a| a.to_string()).join("\n\n")
}
#[cfg(test)]
mod tests {
#![allow(clippy::indexing_slicing)]

View File

@@ -1263,6 +1263,14 @@ pub async fn get_msg_info(context: &Context, msg_id: MsgId) -> Result<String> {
ret += &format!("\nLast seen as: {}/{}", server_folder, msg.server_uid);
}
}
let hop_info: Option<String> = context
.sql
.query_get_value("SELECT hop_info FROM msgs WHERE id=?;", paramsv![msg_id])
.await?;
if hop_info.is_some() {
ret.push_str(&hop_info.unwrap_or_else(|| "No Hop info".to_owned()));
}
Ok(ret)
}

View File

@@ -13,7 +13,7 @@ use crate::blob::BlobObject;
use crate::constants::{Viewtype, DC_DESIRED_TEXT_LEN, DC_ELLIPSE};
use crate::contact::addr_normalize;
use crate::context::Context;
use crate::dc_tools::{dc_get_filemeta, dc_truncate};
use crate::dc_tools::{dc_get_filemeta, dc_truncate, parse_receive_headers};
use crate::dehtml::dehtml;
use crate::e2ee;
use crate::events::EventType;
@@ -76,6 +76,8 @@ pub struct MimeMessage {
/// This is non-empty only if the message was actually encrypted. It is used
/// for e.g. late-parsing HTML.
pub decoded_data: Vec<u8>,
pub(crate) hop_info: String,
}
#[derive(Debug, PartialEq)]
@@ -271,6 +273,8 @@ impl MimeMessage {
footer: None,
is_mime_modified: false,
decoded_data: Vec::new(),
hop_info: parse_receive_headers(&mail.get_headers()),
};
parser.parse_mime_recursive(context, &mail, false).await?;
parser.maybe_remove_bad_parts();

View File

@@ -473,6 +473,10 @@ paramsv![]
// this way, the app looks familiar after the contact request upgrade.
info!(context, "[migration] v78");
sql.execute_migration("UPDATE chats SET archived=1 WHERE blocked=2;", 78)
}
if dbversion < 79 {
info!(context, "[migration] v79");
sql.execute_migration("ALTER TABLE msgs ADD COLUMN hop_info TEXT DEFAULT '';", 79)
.await?;
}