rp

simple email tools
git clone https://git.parazyd.org/rp
Log | Files | Refs | README | LICENSE

commit a27f502661956817f84fd01f5929470a1d6442e3
parent dfce35670141565cda8555e6ca948eac1023c39a
Author: parazyd <parazyd@dyne.org>
Date:   Sat,  3 Mar 2018 03:39:54 +0100

Some code cleanup.

Diffstat:
MMakefile | 18+++++++++++++-----
Mbin/rpabook | 25+++++++++----------------
Mbin/rpcomp | 3++-
Mbin/rpgenc | 2+-
Mbin/rpgsign | 2+-
Mbin/rpinit | 3++-
Mbin/rppeek | 6------
Mbin/rpsend | 4++--
Mbin/rpsync | 24++++++++++++------------
Mconfig.mk | 2+-
Mnet.c | 17++++++++---------
Mrohrpost.c | 4++++
Mrplib/common.sh | 23++++-------------------
Mutil.c | 60+++++++++++++++++++++++++++++++++---------------------------
Mutil.h | 2+-
15 files changed, 93 insertions(+), 102 deletions(-)

diff --git a/Makefile b/Makefile @@ -31,16 +31,24 @@ install: all mkdir -p $(DESTDIR)$(PREFIX)/bin cd bin; \ for i in `ls -1`; do \ - cp -a $$i $(DESTDIR)$(PREFIX)/bin; \ - chmod 755 $(DESTDIR)$(PREFIX)/bin/$$i; \ + if [ -e $(DESTDIR)$(PREFIX)/bin/$$i ]; \ + then \ + rm -f $(DESTDIR)$(PREFIX)/bin/$$i; \ + fi; \ + \ + if [ ! -L $$i ]; \ + then \ + cp $$i $(DESTDIR)$(PREFIX)/bin; \ + chmod 755 $(DESTDIR)$(PREFIX)/bin/$$i ; \ + else \ + ln -sf $(DESTDIR)$(PREFIX)/bin/$(NAME) \ + $(DESTDIR)$(PREFIX)/bin/$$i; \ + fi \ done - cp $(NAME) $(DESTDIR)$(PREFIX)/bin - chmod 755 $(DESTDIR)$(PREFIX)/bin/$(NAME) sed "s,SHAREPREFIX,$(SHAREPREFIX),g" < bin/rpinit > $(DESTDIR)$(PREFIX)/bin/rpinit chmod 755 $(DESTDIR)$(PREFIX)/bin/rpinit mkdir -p $(DESTDIR)$(SHAREPREFIX) cp -r rplib mutt misc $(DESTDIR)$(SHAREPREFIX) - chmod 755 $(DESTDIR)$(SHAREPREFIX) uninstall: cd bin; \ diff --git a/bin/rpabook b/bin/rpabook @@ -3,35 +3,28 @@ profile="${RPPROFILE:-$HOME/.rp/default}" . "$HOME"/.rp/rplib/common.sh +ab="$profile/whitelist.abook" + usage() { edie "usage: $(basename $0) [-a] [-c] -a: add to abook -c: completion (used from mutt)" } -comp() { - einfo "searching for $1 in addressbook" - abook --datafile "$profile/whitelist.abook" --mutt-query "$1" || return 1 -} - -add() { - einfo "adding $1 to whitelist" - abook --datafile "$profile/whitelist.abook" --add-email-quiet - return $? -} - for i in $@; do case "$i" in -a) email="$(cat)" - addr="$(echo $email | rpheaders From)" - comp "$addr" || exit 0 - echo "$email" | add + addr="$(echo $email | rpheaders From | cut -d'<' -f2 | tr -d '<>')" + grep -q "^email=$addr" "$ab" && { + einfo "address already in abook" + exit 0 + } + echo "$email" | abook --datafile "$ab" --add-email-quiet exit $? ;; -c) - shift 1 - comp "$1" + abook --datafile "$ab" --mutt-query "$2" exit $? ;; *) diff --git a/bin/rpcomp b/bin/rpcomp @@ -1,6 +1,7 @@ #!/bin/sh profile="${RPPROFILE:-$HOME/.rp/default}" +. "$HOME"/.rp/rplib/common.sh content_head="Content-Type: text/plain; charset=\"utf-8\"" @@ -30,7 +31,7 @@ MIME-Version: 1.0 "$(LANG=C date "+%a, %d %b %Y %T %Z")" \ "$(rpheaders defaultfrom < $profile/config)" \ "$to" \ - "$(rputil genmsgid)" \ + "$(rputil -e genmsgid)" \ "$content_head" printf "\n\nText\n\n" diff --git a/bin/rpgenc b/bin/rpgenc @@ -1,6 +1,6 @@ #!/bin/sh -boundary="$(rputil genmimeb)" +boundary="$(rputil -e genmimeb)" email="$(cat)" diff --git a/bin/rpgsign b/bin/rpgsign @@ -1,6 +1,6 @@ #!/bin/sh -boundary="$(rputil genmimeb)" +boundary="$(rputil -e genmimeb)" email="$(cat)" diff --git a/bin/rpinit b/bin/rpinit @@ -142,6 +142,8 @@ set header_cache_backend = lmdb # add your customizations in the following file source ~/.rp/$profile/muttrc-custom EOF +echo "set imap_pass = $lpass" > ~/.rp/$profile/mutt/muttpass + touch $profile/muttrc-custom cat <<EOF > $profile/config @@ -157,7 +159,6 @@ EOF setupmutt "$profile" touch "$profile/whitelist.abook" -chmod 600 "$profile/whitelist.abook" [ -f "$profile/filters.txt" ] || { cp "$shareprefix/misc/filters.txt" "$profile" diff --git a/bin/rppeek b/bin/rppeek @@ -15,16 +15,10 @@ for i in $@; do *) folder="-f =${i}" einfo "requested folder '$i'" - shift 1 ;continue ;; esac done -printf "set imap_pass = $(rpheaders lpass < "$profile/config")" \ - > "$profile/mutt/muttpass" - -( sleep 2; printf "set imap_pass = a\n" > $profile/mutt/muttpass ) & - einfo "starting up mutt" neomutt -F $profile/muttrc $folder diff --git a/bin/rpsend b/bin/rpsend @@ -69,7 +69,7 @@ account default from $ruser user $ruser host $rimap -password $(pass show .rp/$(readlink $profile)/rimap) +password $(rpheaders rpass < $profile/config) port 587 auth plain tls on @@ -83,7 +83,7 @@ _dest="$(printf "$email" | rpheaders To)" einfo "sending using msmtp" einfo "subject: $_subj" einfo "to: $_dest" -einfo "sending $(rputil humansize $(printf "$email\n" | wc -c)) over the network" +einfo "sending $(rputil -e humansize $(printf "$email\n" | wc -c)) over the network" printf "$email\n" | \ diff --git a/bin/rpsync b/bin/rpsync @@ -9,17 +9,14 @@ usage() { -d: delete remote emails" } + for i in $@; do case "$i" in -n) dryrun="--dry" - shift 1 - continue ;; -d) delete="--delete1" - shift 1 - continue ;; *) usage @@ -27,14 +24,17 @@ for i in $@; do esac done -rnet="$(rpheaders rnet < "$profile/config")" +rnet="$(rpheaders rnet < ${profile}/config)" case "$rnet" in - tcps*) tls1="--tls1";; + tcps*) tls1="--tls1"; rport=993;; esac -lnet="$(rpheaders lnet < "$profile/config")" +[ -n "$tls1" ] || rport=143 + +lnet="$(rpheaders lnet < ${profile}/config)" case "$lnet" in - tcps*) tls2="--tls2";; + tcps*) tls2="--tls2"; rport=993;; esac +[ -n "$tls2" ] || lport=143 ruser="$(rpheaders ruser < "$profile/config")" rpass="$(rpheaders rpass < "$profile/config")" @@ -44,10 +44,10 @@ lpass="$(rpheaders lpass < "$profile/config")" echo "$rpass" > "$profile/tmp/rimap" echo "$lpass" > "$profile/tmp/limap" -rhost="$(echo $rnet | cut -d'!' -f2)" -lhost="$(echo $lnet | cut -d'!' -f2)" +rhost="$(echo "$rnet" | cut -d'!' -f2)" +lhost="$(echo "$lnet" | cut -d'!' -f2)" -ping -c 1 "$rhost" || edie "$rhost unreachable" +nc -w 1 "$rhost" "$rport" || edie "$rhost unreachable" einfo "starting imapsync" [ -n "$delete" ] && ewarn "remote deletion enabled" @@ -55,7 +55,7 @@ einfo "starting imapsync" imapsync \ --tmpdir "$profile/tmp" \ - --nolog \ #--logdir "$profile/tmp/rpsync_log" \ + --nolog \ --usecache \ --host1 "$rhost" $tls1 \ --user1 "$ruser" \ diff --git a/config.mk b/config.mk @@ -6,7 +6,7 @@ SHAREPREFIX = $(PREFIX)/share/rp MANPREFIX = $(PREFIX)/share/man CFLAGS = -O2 -Wall -pedantic -LDFLAGS = -static -s -lssl -lcrypto +LDFLAGS = -static -g -lssl -lcrypto .c.o: $(CC) $(CFLAGS) -c $< diff --git a/net.c b/net.c @@ -35,7 +35,7 @@ static char *netnames[] = { }; void netusage(void) { - die("usage: %s [-f host port]\n" + die("usage: %s [-f netspec]\n" " -f: fetch and print ssl certificate fingerprint\n", argv0); } @@ -143,27 +143,26 @@ sslfprerr: int netmain(int argc, char *argv[]) { - int fflag; + int fflag = 0; + + char *netspec; + + netspec = NULL; ARGBEGIN { case 'f': + netspec = EARGF(netusage()); fflag = 1; - argc++; argv--; break; default: netusage(); } ARGEND; if (fflag) { - if (argc < 2) - netusage(); - net_t *net; - - net = net_new(argv[1]); + net = net_new(netspec); if (sslfpr(net)) return 1; - return 0; } diff --git a/rohrpost.c b/rohrpost.c @@ -62,6 +62,10 @@ int main(int argc, char *argv[]) { if (cmds[i].flags & DOINSTALL) printf("%s\n", cmds[i].cmd); return 0; + case 'l': + for (i = 0; i < nelem(cmds); i++) + printf("%s\n", cmds[i].cmd); + return 0; default: usage(); } ARGEND; diff --git a/rplib/common.sh b/rplib/common.sh @@ -1,31 +1,16 @@ #!/bin/sh -# structure -reset="$(tput sgr0)" - -cyan="$(tput setaf 4)" -boldcyan="${green}$(tput bold)" - -green="$(tput setaf 2)" -boldgreen="${green}$(tput bold)" - -orange="$(tput setaf 3)" -boldorange="${orange}$(tput bold)" - -red="$(tput setaf 1)" -boldred="${red}$(tput bold)" -alertformat="${red}$(tput bold; tput blink)" - +export PATH="/usr/local/bin:/usr/bin:/bin" einfo() { - printf "${boldgreen} * ${reset}%s\n" "$*" >&2 + printf "$(tput setaf 2)$(tput bold) * $(tput sgr0)%s\n" "$*" >&2 } ewarn() { - printf "${boldorange} * ${reset}%s\n" "$*" >&2 + printf "$(tput setaf 3)$(tput bold) * $(tput sgr0)%s\n" "$*" >&2 } edie() { - printf "${boldred} * ${reset}%s\n" "$*" >&2 + printf "$(tput setaf 1)$(tput bold) * $(tput sgr0)%s\n" "$*" >&2 exit 1 } diff --git a/util.c b/util.c @@ -12,16 +12,19 @@ #include "ind.h" #include "util.h" +char *argv0; + char be[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; void utilusage(void) { - die("usage: %s [cmd] [args]\n" - " available commands:\n" + die("usage: %s [-e cmd [args]]\n" + "\t-e: run util\n\n" + " available utils:\n" "\t * genmsgid: generates a Message-ID header\n" "\t * genmimeb: generates a semi-random MIME boundary\n" "\t * humansize: calculates byte size into human readable " - "and prints it\n", "rputil"); + "and prints it\n", argv0); } int randint(int max) { @@ -63,42 +66,45 @@ int genmimeb(void) { return 0; } -int humansize(long long amnt) { - if (amnt > 1073741824) { - printf("%lld GiB", amnt >> 30); - return 0; - } - - if (amnt > 1048576) { - printf("%lld MiB", amnt >> 20); - return 0; - } - - if (amnt > 1024) { - printf("%lld KiB", amnt >> 10); - return 0; +int humansize(unsigned long size) { + int i = 0; + const char *units[] = {"Bytes", "KiB", "MiB", "GiB"}; + while (size > 1024) { + size /= 1024; + i++; } + printf("%ld %s", size, units[i]); - printf("%lld Bytes", amnt); return 0; } int utilmain(int argc, char *argv[]) { - /* TODO: do these checks in a better way */ - if (argc < 2) + char *runcmd, *cmdarg; + + runcmd = NULL; + cmdarg = NULL; + + ARGBEGIN { + case 'e': + runcmd = EARGF(utilusage()); + if (argc > 1) { + argc--; argv++; + cmdarg = EARGF(utilusage()); + } + break; + default: utilusage(); + } ARGEND; - if (!strncmp("genmsgid", argv[1], 8)) + if (!strncmp("genmsgid", runcmd, 8)) return genmsgid(); - if (!strncmp("genmimeb", argv[1], 8)) + if (!strncmp("genmimeb", runcmd, 8)) return genmimeb(); - if (argc < 2) - utilusage(); - - if (!strncmp("humansize", argv[1], 9)) - return humansize(atoll(argv[2])); + if (cmdarg) + if (!strncmp("humansize", runcmd, 9)) + return humansize(atol(cmdarg)); utilusage(); return 1; diff --git a/util.h b/util.h @@ -11,6 +11,6 @@ int randint(int max); char *randstring(char *str, size_t size); int genmsgid(void); int genmimeb(void); -int humansize(long long amnt); +int humansize(unsigned long amnt); #endif