#!/usr/bin/python # Copyright 2012 Jeff Epler # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import rfc822, sys, struct, fcntl, termios, re m = rfc822.Message(sys.stdin) def screen_width(): try: f = open("/dev/tty", "w") n = f.fileno() h, w = struct.unpack("hh", fcntl.ioctl(f.fileno(), termios.TIOCGWINSZ, '\0\0\0\0')) return w except (IOError,): return 80 width = screen_width() if m.unixfrom: sys.stdout.write(m.unixfrom) def headers(m): l = [] for line in m.headers: if not line[:1].isspace(): if l: yield what, "".join(l).strip() a, b = line.split(":", 1) what = a l = [b] else: l.append(line) if l: yield what, "".join(l).strip() for h, s in headers(m): if h.lower() in ('to', 'cc'): v = m.getaddrlist(h) s = re.sub("\n[ \t]+", " ", s) if len(s) > (width-4) and len(v) > 2: a = ", ".join(vi[0] or vi[1] for vi in v) s = "[%d recipients] %s" % (len(v), a) s = s[:width-7] + "..." lines = "%s: %s" % (h, s) for line in lines.split("\n"): # uncomment next line to truncate all header lines to 80 chars line = "%-.*s" % (width, line) print line print for line in sys.stdin: sys.stdout.write(line)