#!/usr/bin/ruby

# $Id: addFromIfMissing,v 1.6 2018/08/31 11:21:54 gilles Exp gilles $

# Credits: Emiliano Heyns

# https://github.com/imapsync/imapsync/issues/54
# https://github.com/mikel/mail

# Test example in command line:
# { echo  "Date: Fri, 31 Aug 2018 12:58:59 +0000"  ; echo ;  echo "Hello World!" ; } | ./addFromIfMissing

# Add mail gem with the command:
#   gem install mail

require 'mail'

data = STDIN.read

exit(0) if data.strip == ''

message = Mail.read_from_string(data)
if message.from && message.from.length > 0
  puts data
else
  message.from = 'emiliano.heyns@han.nl'
  puts message.to_s
end

