pazz / alot

Terminal-based Mail User Agent
GNU General Public License v3.0
683 stars 163 forks source link

Documentation: Examples for :pipeto #1569

Closed robert-winkler closed 3 years ago

robert-winkler commented 3 years ago

Hi, I am migrating from sup to alot, and I like it a lot ;-).

I want to add some functions. The :pipeto command seems exactly what I need, but I cannot figure out, how it works.

For example, to move a mail to my SpamAssessin spam training folder, I would expect:

:pipeto --format=filepath xargs mv ~/Maildir/SpamAssessin/Spam/cur

However, this does not work, and there is no error message.

How would I implement this simple function?

pazz commented 3 years ago

Welcome :)

Quoting Robert Winkler (2021-04-14 16:33:21)

Hi, I am migrating from sup to alot, and I like it a lot ;-).

I want to add some functions. The :pipeto command seems exactly what I need, but I cannot figure out, how it works.

For example, to move a mail to my SpamAssessin spam training folder, I would expect:

:pipeto --format=filepath xargs mv ~/Maildir/SpamAssessin/Spam/cur

However, this does not work, and there is no error message.

How would I implement this simple function?

To be honest, this pipeto command has been neglected and could use some TLC. I think you are on the right path. Perhaps all you need is to put quotation marks around the actual shell commands?

You may be able to get a bit more verbose output by looking at the log file in debug mode (-d debug -l alot.log).

Other than that you can always define your own python hook (in ~/.config/alot/hooks.py) and use/bind the "call" command to call your python hook..

robert-winkler commented 3 years ago

Thanks a lot for the quick reply, pazz!

writing a hook, and "calling" it, is the way to go, then, I guess. Is there an example on how to do that?

I tried installing the https://github.com/pazz/alot/wiki/Contrib-Hooks#open-html-emails-in-external-browser and binding it in the conf by

[bindings]
        w = pipeto /home/rob/.config/alot/hooks/open-in-browser.py

This was obviously not correct! Pressing w in an HTML message just produces a cross on the screen, and I have to reboot to get my mouse back...

pazz commented 3 years ago

This is the part of the my config for that hook

[bindings]
  [[thread]]
    ', b' = "call hooks.open_in_browser(ui)"

It causes it to be called after pressing , then b. The parameter ui will access the global alot UI instance and can be used in the hook to trigger notifications or prompts. Please close the issue once it works for you, cheers!

robert-winkler commented 3 years ago

I faced additional problems: The alot version of Ubuntu 20.04 comes without the alot module (required by the hook). Installing 0.9.1 resulted in a series of problems; besides of leaving the Ubuntu matrix ;-). Thus, I used the scipt of Guillaume Chérel guillaumecherel (see https://github.com/pazz/alot/issues/789), and slightly modified it for adding the .html extension to the file (I my code to this issue). Works great!

fileending = ".html"
htmlfilename = "".join([f.name,fileending])
os.rename(f.name, htmlfilename)
webbrowser.open(htmlfilename)
#webbrowser.open(f.name)

in the config:

[bindings]
    w = pipeto /home/rob/.config/alot/input_to_browser.py 

actually, the advantage of this script is the independence from the alot module. I use alot now on 2 computers (a really potent gaming laptop and a Raspberry Pi Zero); thus, I stick to debian/ubuntu packages where possible.

For copying/moving files (training of SpamAssassin) I wrote two simple python scripts (I gave up on the shell command pipelines):;

move_to_spam.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import shutil
import os

filepath = sys.stdin.buffer.read()
basefilename = os.path.basename(filepath)
mailfile = basefilename.decode('utf-8')

destdir = r'/home/rob/Maildir/SpamAssassin/Spam/cur/'
destfile = "".join([destdir,mailfile])

shutil.move(filepath,destfile)

and copy_to_ham:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import shutil
import os

filepath = sys.stdin.buffer.read()
basefilename = os.path.basename(filepath)
mailfile = basefilename.decode('utf-8')

destdir = r'/home/rob/Maildir/SpamAssassin/Ham/cur/'
destfile = "".join([destdir,mailfile])

shutil.copyfile(filepath, destfile) 

in the config:

[bindings]
    v = pipeto --format=filepath /home/rob/.config/alot/move_to_spam.py
    z = pipeto --format=filepath /home/rob/.config/alot/copy_to_ham.py

Now, alot has all the important functions I need (well, some additional tweaking is always possible ;-)). Thanks for this great software, Patrick!