From 76f5872f5f739217caccc98ac185d8a5d24b1a95 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Fri, 30 Jun 2023 11:26:22 +0530 Subject: vtoc.sh: initial commit, convert vcf to csv, replaces vtono.sh --- bin/vtoc.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/vtono.sh | 78 ------------------------------------------------------------ zukbeam | 10 ++++---- 3 files changed, 83 insertions(+), 83 deletions(-) create mode 100755 bin/vtoc.sh delete mode 100755 bin/vtono.sh diff --git a/bin/vtoc.sh b/bin/vtoc.sh new file mode 100755 index 0000000..f2be2cb --- /dev/null +++ b/bin/vtoc.sh @@ -0,0 +1,78 @@ +#!/bin/sh + +die() +{ + : "${1:?}" + + printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2 + exit "${2:-1}" +} + +warn() +{ + : "${1:?}" + printf "\033[33;1mwarn: %b\033[0m\n" "$*" 1>&2 +} + +######## +# MAIN # +# ###### + +name= +no= +vcf= +nos= + +if [ -z "$1" ] || + [ "$(file --brief --dereference --mime-type "$1")" != "text/vcard" ]; then + die "no or invalid input" +else + vcf="$1" + shift +fi + +strings "$vcf" | while read -r line; do + case "$line" in + 'FN:'*) + name="${line##*:}" + ;; + 'TEL;TYPE='*':'*) + # avoid repetitions in same vacrd entry + no="${line##*:}" + case "$nos" in + *"$no"*) + continue + ;; + esac + nos="${nos}:${no}" + + # make sure $no is E.164-formatted + case "$no" in + *-*) + no="$(printf "%s" "$no" | tr -d '-')" + ;; + esac + case "$no" in + +[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) + # gotta go fast \+[0-9]{12} + ;; + +974[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) # qatar + # gotta go fast \+974{8} + ;; + *) + warn "illegal format: ${name}, ${no}" + continue; + ;; + esac + + [ "$1" = "-w" ] && + continue + + [ -n "$no" ] && + printf "%s, %s\n" "$no" "$name" + ;; + 'END:VCARD') + nos= + ;; + esac +done diff --git a/bin/vtono.sh b/bin/vtono.sh deleted file mode 100755 index dd4f8b6..0000000 --- a/bin/vtono.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/sh - -die() -{ - : "${1:?}" - - printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2 - exit "${2:-1}" -} - -warn() -{ - : "${1:?}" - printf "\033[33;1mwarn: %b\033[0m\n" "$*" 1>&2 -} - -######## -# MAIN # -# ###### - -name= -no= -vcf= -nos= - -if [ -z "$1" ] \ - || [ "$(file --brief --dereference --mime-type "$1")" != "text/vcard" ]; then - die "no or invalid input" -else - vcf="$1" - shift -fi - -strings "$vcf" | while read -r line; do - case "$line" in - 'FN:'*) - name="${line##*:}" - ;; - 'TEL;TYPE='*':'*) - # avoid repetitions in same vacrd entry - no="${line##*:}" - case "$nos" in - *"$no"*) - continue - ;; - esac - nos="${nos}:${no}" - - # make sure $no is E.164-formatted - case "$no" in - *-*) - no="$(printf "%s" "$no" | tr -d '-')" - ;; - esac - case "$no" in - +[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) - # gotta go fast \+[0-9]{12} - ;; - +974[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) # qatar - # gotta go fast \+974{8} - ;; - *) - warn "illegal format: ${name}, ${no}" - continue; - ;; - esac - - [ "$1" = "-w" ] && - continue - - [ -n "$no" ] && - printf "%s : %s\n" "$no" "$name" - ;; - 'END:VCARD') - nos= - ;; - esac -done diff --git a/zukbeam b/zukbeam index a715d1d..1cdd495 100755 --- a/zukbeam +++ b/zukbeam @@ -117,15 +117,15 @@ kill-daemon) kill_daemon exit ;; -vtono) +vtoc) [ -z "$infile" ] && infile="${data_dir}/contacts.vcf" [ -z "$outfile" ] && - outfile="${data_dir}/contacts.txt" + outfile="${data_dir}/contacts.csv" [ ! -s "$infile" ] && - die "vtono: ${infile}: no such file or directory" - vtono.sh "$infile" > "$outfile" && - note "vtono: contacts saved to ${outfile}" + die "vtoc: ${infile}: no such file or directory" + vtoc.sh "$infile" > "$outfile" && + note "vtoc: contacts saved to ${outfile}" ;; "") kill_daemon -- cgit v1.2.3