Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add get_thread() that gets all emails, belonging to a certain email #177

Closed
MoritzFago opened this issue Feb 27, 2018 · 5 comments
Closed

Comments

@MoritzFago
Copy link

I want to move all anwsered emails with a certain emailadress in cc to another folder, but there is no method to get the answer together with the original email.

@lefcha
Copy link
Owner

lefcha commented May 10, 2018

Well you need to write a custom filter function to do this, for example by storing the In-Reply-To header value from the reply, and then searching for a Message-ID that has the same value in your INBOX.

I think I mentioned a similar solution here, too: #174

@doronbehar
Copy link

@MoritzFago, I've encountered the need for such a function my self so I wrote this function in my config.lua:

-- {{{1 general function to fetch all messages in a thread
local function get_message_id(msg)
	return msg:fetch_field("Message-ID"):gsub("[Mm]essage%-[Ii][Dd]: ", "")
end
local function get_reply_id(msg)
	return msg:fetch_field("In-Reply-To"):gsub("[Ii]n%-[Rr]eply%-[Tt]o: ", "")
end
function thread_messages(msg, mbox, related_messages, examined_ids)
	-- initilize examined_ids in case it's the first call of this function
	if not examined_ids then examined_ids = {} end
	-- This message
	local this_msgid = get_message_id(msg)
	print("this message's id is " .. this_msgid)
	table.insert(examined_ids, this_msgid)
	print("our known examined_ids are: " .. inspect(examined_ids))
	local this_message = mbox:match_field("Message-ID", this_msgid)
	-- The messages directly replyed to our `msg`
	if related_messages then
		related_messages = related_messages + mbox:match_field("In-Reply-To", this_msgid)
	else
		related_messages = mbox:match_field("In-Reply-To", this_msgid)
	end
	print("found " .. #related_messages .. " messages replyed to it")
	-- The messages our `msg` was replyed to
	local prev_msgid = get_reply_id(msg)
	if string.len(prev_msgid) > 1 then
		print("this message was replied to " .. prev_msgid)
		related_messages = related_messages + mbox:match_field("Message-ID", prev_msgid)
	else
		print("it is not a reply to anything")
	end
	-- Recursively add up messages related in the same way to the related_messages
	for _, related_msg in ipairs(related_messages) do
		local mailbox_related, uid_related = table.unpack(related_msg)
		local related_msgid = get_message_id(mailbox_related[uid_related])
		print("will check for every known related message if the messages replyed to " .. related_msgid .. " have been examined")
		local skip = false
		for i=1,#examined_ids do
			if related_msgid == examined_ids[i] then
				skip = true
			end
		end
		if not skip then
			print("examining new message: " .. related_msgid)
			related_messages = related_messages + thread_messages(mailbox_related[uid_related], mbox, related_messages, examined_ids)
		end
	end
	return related_messages + this_message
end

Note that it is recursive - meaning it calls itself so I'm pretty proud about it you can take out all the print statements` if you want it to be quieter. You are supposed to call it like that:

for _, message in ipairs(account["mailbox"]:contain_somequery("search")) do
	local mailbox, uid = table.unpack(message)
	local all_related_messages = thread_messages(mailbox[uid], mbox)
end

Maybe @lefcha would be willing to make this function a standard method for messages? Anyway, I hope it helps.

@lefcha
Copy link
Owner

lefcha commented Jun 18, 2019

That's quite an interesting way to solve this.

But unfortunately, including this kind of functions would be out-of-scope for imapfilter, and falls more into the different recipes/ways to extend it.

@lefcha lefcha closed this as completed Jun 18, 2019
@doronbehar
Copy link

No objection @lefcha. Would you be willing to add a WiKi to the repo so I'll be able to add this function there? Or perhaps add a FAQ to the README and link to my comment? I'm suggesting because I do agree with @MoritzFago that this is kind of a basic functionality that is missing from the API.

@lefcha
Copy link
Owner

lefcha commented Jun 21, 2019

@doronbehar The Wiki is a good idea, since any GH user can contribute with config examples, custom functions, build instructions, etc. I enabled it, so feel free to add your pages there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants