Skip to content

Commit

Permalink
Saving messages to folders
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed May 6, 2024
1 parent c2345cf commit 5d16331
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 75 deletions.
32 changes: 31 additions & 1 deletion js/core-mail-incoming.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2022 Michael Pozhidaev <[email protected]>
Copyright 2019-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
Expand All @@ -14,6 +14,36 @@
General Public License for more details.
*/

const PROPERTY_MAILING_LIST = "mailing-list";

Luwrain.addHook("luwrain.mail.incoming", (mail, msg) => {
var folder = null;
const headers = msg.getHeader("List-Id");
if (headers.length > 0) {
var listId = headers[0]
var personal = mail.getAddressPersonalName(listId);
if (!personal) {
const from= msg.getHeader("From");
if (from.length > 0)
personal = mail.getAddressPersonalName(from[0]);
}
listId = mail.getAddressWithoutPersonalName(listId);
if (!personal)
personal = listId;
folder = mail.getFolders().findFirstByProperty(PROPERTY_MAILING_LIST, listId);
if (!folder) {
folder = mail.getFolders().getDefaultMailingLists().newSubfolder();
folder.setName(personal);
folder.getProperties().setProperty(PROPERTY_MAILING_LIST, listId);
folder.update();
}
} else
folder = mail.getFolders().getDefaultIncoming();
if (!folder)
throw "No folder to save the mail";
folder.saveMessage(msg);
});

Luwrain.addHook("luwrain.pim.mail.save.new", function(mail, message){
const defaultIncoming = mail.getFolders().findByProp("defaultIncoming", "true")
if (!defaultIncoming) {
Expand Down
107 changes: 33 additions & 74 deletions js/core-mail-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@
General Public License for more details.
*/


Luwrain.addHook("luwrain.mail.summary", (mail, messages)=>{
const topics = [];
for(let m of messages){
var theme = m.getSubject();
if (!theme)
theme = "Без темы";
var re = theme.match(/Re:\s+(.*)$/);
if (re)
theme = re[1];
var topic = topics.find((t) => { return t.theme == theme;});
if (!topic) {
topic = {theme, messages: []};
topics.push(topic);
}
topic.messages.push(m);
}
topics.sort((a, b) => {
if (a.messages.length > b.messages.length)
return -1;
if (a.messages.length < b.messages.length)
return 1;
return 0;
});
const res = [];
for(let t of topics) {
res.push(t.theme);
for(let m of t.messages)
res.push(m);
}
return res;
});

Luwrain.addHook("luwrain.mail.reply", (message)=>{
const to = message.getTo().full;
const t = message.getTextAsArray();
Expand All @@ -29,77 +62,3 @@ Luwrain.addHook("luwrain.mail.reply", (message)=>{
Luwrain.launchApp("message", [JSON.stringify(arg)]);
return true;
});

function stripRe(text)
{
return text.replaceAll('^[Rr][Ee](\\[[0-9]+\\])*: ', '').trim();
}

function stripCommonBeginning(items)
{
if (items.length == 0)
return;
var commonTo = 0;
while(true)
{
if (commonTo >= items[0].subject.length)
break;
var ch = items[0].subject[commonTo];
//Looking for the item not matching the ch
var i;
for(i = 1;i < items.length;i++)
if (commonTo >= items[i].subject.length || items[i].subject[commonTo] != ch)
break;
if (i < items.length)
break;
commonTo++;
}
if (commonTo > 0)
for(var i = 0;i < items.length;i++)
items[i].subject = items[i].subject.substring(commonTo);
}

function divideOnGroups(items)
{
var groups = [];
for(var i = 0;i < items.length;i++)
{
var k = 0;
for(k = 0;k < groups.length;k++)
if (groups[k].subject == items[i].subject)
break;
if (k < groups.length)
groups[k].messages.push(items[i]); else
groups.push({subject: items[i].subject, messages: [items[i]]});
}
return groups;
}

Luwrain.addHook("luwrain.mail.summary.organize", function(messages){
return messages;
var res = [];
for(var i = 0;i < messages.length;i++)
res.push({
subject: stripRe(messages[i].subject),
source: messages[i]
});
stripCommonBeginning(res);
//Stripping RE: once more time
for(var i = 0;i < res.length;i++)
res[i].subject = stripRe(res[i].subject);
var groups = divideOnGroups(res);
res = [];
for(var i = 0;i < groups.length;i++)
if (groups[i].messages.length == 1)
res.push({message: groups[i].messages[0].source, title: groups[i].messages[0].source.from.personal + ' ' + groups[i].messages[0].source.subject});
for(var i = 0;i < groups.length;i++)
{
if (groups[i].messages.length < 2)
continue;
res.push("Тема " + groups[i].subject);
for(var j = 0;j < groups[i].messages.length;j++)
res.push({message: groups[i].messages[j].source, title: groups[i].messages[j].source.from.personal});
}
return res;
});

0 comments on commit 5d16331

Please sign in to comment.