jaromail

a commandline tool to easily and privately handle your e-mail
git clone git://parazyd.org/jaromail.git
Log | Files | Refs | Submodules | README

imap (3343B)


      1 #!/usr/bin/env zsh
      2 #
      3 # Jaro Mail, your humble and faithful electronic postman
      4 #
      5 # a tool to easily and privately handle your e-mail communication
      6 #
      7 # Copyleft (C) 2010-2016 Denis Roio <jaromil@dyne.org>
      8 #
      9 # This source  code is free  software; you can redistribute  it and/or
     10 # modify it under the terms of  the GNU Public License as published by
     11 # the Free  Software Foundation; either  version 3 of the  License, or
     12 # (at your option) any later version.
     13 #
     14 # This source code is distributed in  the hope that it will be useful,
     15 # but  WITHOUT ANY  WARRANTY;  without even  the  implied warranty  of
     16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     17 # Please refer to the GNU Public License for more details.
     18 #
     19 # You should have received a copy of the GNU Public License along with
     20 # this source code; if not, write to:
     21 # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     22 
     23 run_imap_query() {
     24     fn run_imap_query $*
     25 
     26     _trans=${transport:-plain}
     27     case $_trans in
     28         SSL*) _trans=ssl ;;
     29         TLS*) _trans=tls ;;
     30     esac
     31 
     32     if [ "$_trans" = "plain" ]; then
     33 	    func "running plain imap query via netcat, no encryption"
     34 	    nc ${imap} ${imap_port} -q 10 2>&1
     35 
     36     elif [ "$_trans" = "tls" ]; then
     37         func "running tls imap query via openssl, encrypted"
     38 	    openssl s_client -crlf -quiet -connect ${imap}:${imap_port} 2>&1
     39 
     40     elif [ "$_trans" = "ssl" ]; then
     41 	    func "running ssl imap query via openssl, encrypted"
     42 	    openssl s_client -starttls imap -quiet -connect ${imap}:${imap_port} 2>&1
     43 
     44     else
     45         error "unknown transport \"$_trans\" for a imap query"
     46     fi
     47 }
     48 
     49 check_imap() {
     50 	fn check_imap $*
     51 
     52     [[ $name == "" ]] &&  {
     53 	    error "get_imap_info() called with no account loaded"
     54 	    return 1 }
     55 
     56     [[ "$imap" == "" ]] && {
     57 	    error "get_imap_info() called on a non-imap account: $name on $imap"
     58 	return 1 }
     59 }
     60 
     61 imap_list_folders() {
     62 	fn imap_list_folders $*
     63     check_imap && return 1
     64 
     65     query="B00000 CAPABILITY
     66 B00001 LOGIN \"${login}\" \"${password}\"
     67 B00002 LIST \"\" *
     68 B00003 LOGOUT"
     69     response=`print $query | run_imap_query`
     70     folders=`print $response | awk '
     71 /^\* LIST/ {  gsub(/"/, "", $5); print $5 }' | tr -d '\r'`
     72     print $folders
     73 }
     74 
     75 imap_get_size() {
     76 	fn imap_get_size $*
     77 
     78     check_imap && return 1
     79 
     80     # skip getting size if Gmail
     81     # (imap there lacks support for RFC822.SIZE)
     82     { test "$host" = "imap.gmail.com" } && {
     83 	act "skipping IMAP examination on GMail account"
     84 	return 1 }
     85     func "Querying size on $imap:$imap_port"
     86 
     87     [[ "$1" == "" ]] && {
     88 	folders=(`imap_list_folders`)
     89     } || {
     90 	folders=("$1") }
     91 
     92     query="B00000 CAPABILITY
     93 B00001 LOGIN \"${login}\" \"${password}\""
     94 
     95     c=1
     96     for f in ${folders}; do
     97 	func "folder: $f"
     98 	[[ "$f" == "" ]] && continue
     99 	c=$(( $c + 1 ))
    100 
    101 	query="$query
    102 B0000$c SELECT \"${f}\""
    103 
    104 	c=$(( $c + 1 ))
    105         query="$query
    106 B0000$c FETCH 1:* (RFC822.SIZE)"
    107 
    108     done
    109     c=$(( $c + 1 ))
    110     query="$query
    111 B0000$c LOGOUT"
    112     # print $query
    113     # print "$query" | run_imap_query
    114     # return 0
    115 
    116     result=(`print "$query" | run_imap_query | awk '
    117 /^\*.*FETCH.*RFC822.SIZE/ { gsub(/\)/, "", $NF); print $NF }' | tr -d '\r'`)
    118 
    119     bytes_total=0
    120     typeset -A fsizes
    121     for i in $result; do
    122 	# TODO: list sizes per-folder
    123 	bytes_total=$(( $bytes_total + $i ))
    124     done
    125     print $bytes_total
    126 }