[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Ideas to obtain text file list of emails in an IMAP folder?



David Wright wrote:
> [snip]
>
> How does your script deal with the users' IMAP passwords, or prevent
> the sudo-priveleged users from reading each others' emails?

It doesn't, because it isn't using IMAP at all.  The original intent
(and test case) was under the assumption that the person had priveleged
access to the mailserver itself.

The actual files / folders below /var/mail/[...] on the mailserver can
only be read by (in my case) the user 'mail', which (again, at least on
my system) is a non-login account.  One has to be root, or a sudoer in
order to access those directories (and files).

Anyone with the root password (or a sufficiently permissive 'sudo'
account) already *has* access to everyone's email.  If that is a concern
in your environment, then it's probably time to review who has the
access and why.

>
> e was interested in seeing how this problem would be tackled "through"
> IMAP. I should be able coerce mutt into writing a fresh cache of the
> headers in an INBOX [...]
>
> [snip]
>
> file says the resulting cache is a "Tokyo Cabinet (1.0:911)" and
> I have no idea how to list its contents. It would be neat to have
> a customised listing (with width unrestricted by the terminal) of
> my INBOX on the IMAP server to which I *don't* have privileged access.

Yeah, you can do this too, but my take on the question was that he
already had access to the mail server (as root / sudo).

That being said, if one has local copies of the messages -- such as in
$HOME/.mutt/cache/bodies/INBOX/ -- then you don't have to mess with the
header cache. A quick search says that the "Tokyo Cabinet" is some kind of
replacement / implementation of DBM.  Not sure how to read from / write
to that (yet), though I expect we'd need to leave the confines of 'bash'
and upgrade the script to python (maybe perl).  If we need something
heavier (e.g. C) to work with it, then we've kind of blown 'quick and
dirty' out of the water. :)

Here's a slightly modified version that will behave better (read: it'll
actually work) if a user doesn't have root permissions on a mailserver.
Note that you would need the mails cached somewhere on your local
machine, e.g. in $HOME/.cache or something; mutt allows for this fairly
easily through the 'set message_cachedir' directive. 

               < ----- cut here for script ----- >
#!/bin/bash
########################################################################
#
#
#                       script: mlhdr
#                           by: Dan Purgert
#                    copyright: 2016
#                      version: 0.2
#                         date: Sat Jun  4 10:13:31 EDT 2016
#                      purpose: Quick and dirty script to get email
#                             : header information from your mail cache
#                             : or the mailserver (if you have root).
#
#                      license: GPL v2 (only)
#                prerequisites: User running this MUST(RFC2119) either
#                             : have a local cache of the mails to be
#                             : parsed, or have root access to the mail
#                             : server itself.
#                 installation: chmod +x and run
#
#
########################################################################
if [[ $# = 0 ]] || [[ "$1" = "-h" ]] ; then
  printf "
  ------------
  mlhdr
  ------------

  This script is intended to grab the header information from emails in
  the listed directory.  If running on a mailserver (i.e. pointed at
  \"/var/mail/[...]\"), you will need to run this script as root.

  Usage:
    mlhdr /path/to/mail/box [/output/file]

  Options:
    -h Print this help and exit"
  exit 0
fi

spinner() {
  local pid=$1
  local del=0.5
  local sp='/-\|'
  local n=${#sp}
  printf ' ' >&2
  while [ $(ps a | awk '{print $1}' | grep $pid) ] ; do
        printf '\b%s' "${sp:i++%n:1}" >&2
        sleep $del
  done
}

if [[ "$2" = '' ]]; then
  :
else 
  exec 1>"$2"
fi 

dir="$1"
set -x
chk=$(echo "${1:0:4}")
if [[ "$chk" = "/var" ]]; then 
  chk=$(echo "${1:(-3)}")
  if ! [[ "$chk" = "cur" ]]; then 
    dir="$1/cur"
  fi
fi
set +x
cd "$dir"

(
  for f in *; do
    if [[ -f $f ]] ; then
      printf "Message: $f\n"
      egrep '^From:|^To:|^Date:|^Subject:' "$f"
      printf "\n=================\n\n"
    fi
  done ) & spinner $!
printf "\n"


-- 
|_|O|_| Registered Linux user #585947
|_|_|O| Github: https://github.com/dpurgert
|O|O|O| 


Reply to: