#!/bin/zsh
#AutoMailer
#ANTI-CAPITALIST SOFTWARE LICENSE (v 1.4)
#Copyright © 2020 Daniel Nash
#This is anti-capitalist software, released for free use by individuals and organizations that do not operate by capitalist principles. Permission is hereby granted, free of charge, to any person or organization (the "User") obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, modify, merge, distribute, and/or sell copies of the Software, subject to the following conditions:
#1. The above copyright notice and this permission notice shall be included in all copies or modified versions of the Software. 
#2. The User is one of the following:
#a. An individual person, laboring for themselves
#b. A non-profit organization
#c. An educational institution
#d. An organization that seeks shared profit for all of its members, and allows non-members to set the cost of their labor
#3. If the User is an organization with owners, then all owners are workers and all workers are owners with equal equity and/or equal vote.
#4. If the User is an organization, then the User is not law enforcement or military, or working for or under either. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
export HOME=/
export PATH=/bin:/sbin
export mydir=/mailer
export stats=$mydir/stats
if [ -d $mydir ];then
sleep .01
else
mkdir -p $mydir
fi
if [ -d $stats ];then
sleep .01
else
mkdir -p $stats
fi
cd $mydir
if [ -f $mydir/addresses ];then
sleep .01
else
echo enter email addresses to send messages to by default, one per line. When finished, press control d. For each message, you can override the default addresses by placing a file called addresses in the folder of the message. You can always change these default addresses, as well as per-message addresses, later by editting the addresses file in your mail root directory or any message specific folder.
cat >> $mydir/addresses
fi
if [ -f $mydir/mailcfg.template ];then
sleep .01
else
cat >> mailcfg.template<<EOF
user=myuser
pass=mypassword
myaddr=\$user
name="myname"
recaddr=\`cat addresses|tr \\\\\\\\n \\;\`
EOF
fi
if [ -f $mydir/mailcfg ];then
sleep .01
else
echo no configuration found
echo enter display name for outgoing messages
read name
echo enter gmail username
read username
echo enter gmail password, will not be echoed
read -s pass
sed "s|myuser|$username|g;s|mypassword|$pass|g;s|myname|$name|g" $mydir/mailcfg.template > $mydir/mailcfg
echo Configuration completed. Place messages, one per directory, in single file html format in $mydir. If sending messages to specific addresses, create a file called addresses, with one address per line, in the message\'s directory. If an addresses file exists for a given message, it will be used instead of the default address list.
fi
while true;do
IFS=$(echo -en \\n\\b)
for f in `find . -maxdepth 1 -type d|sed "s|.\/||g"`;do
if echo $f|grep -qw .;then
continue
fi
if echo $f|grep -qw `echo $stats|sed "s|$mydir\/||g"`;then
continue
fi
cd $mydir/$f
export mydate=`date +%j`
if [ -f $stats/tgt.$f ];then
sleep .01
else
echo how many days apart would you like to send $f\?
read reqtgt
echo -n $reqtgt > $stats/tgt.$f
echo send $f now\?
read sendanswer
case "$sendanswer" in
y)
echo $(($mydate-$reqtgt))> $stats/success.$f
;;
n)
echo $mydate > $stats/success.$f
;;
esac
fi
export tgt=`cat $stats/tgt.$f`
if [ -f $stats/success.$f ];then
sleep .01
else
echo 0 > $stats/success.$f
fi
export success=`cat $stats/success.$f`
if [ $(($mydate-$success)) -ge $tgt ];then
if [ -f ./mailcfg ];then
sleep .01
else
cp $mydir/mailcfg .
fi
if [ -f ./addresses ];then
sleep .01
else
cp $mydir/addresses .
fi
if send;then
rm mailcfg
date +%j > $stats/success.$f
else
continue
fi
fi
cd $mydir
done
done
