summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-06-28 17:02:26 +0530
committersinanmohd <sinan@firemail.cc>2023-06-30 12:25:46 +0530
commit51db82a2fca0becf4ff446b867f959d9cdead311 (patch)
tree4c7956227541da9bd4a913e8fe389b8abcc78e9e
repo: initial commit
-rw-r--r--.gitignore1
-rwxr-xr-xbin/vtono.sh78
-rwxr-xr-xzukbeam137
3 files changed, 216 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8fce603
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+data/
diff --git a/bin/vtono.sh b/bin/vtono.sh
new file mode 100755
index 0000000..dd4f8b6
--- /dev/null
+++ b/bin/vtono.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/zukbeam b/zukbeam
new file mode 100755
index 0000000..a715d1d
--- /dev/null
+++ b/zukbeam
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+mdtest="$(command -v mdtest)"
+data_dir="${PWD}/data"
+db_dir="${data_dir}/db"
+bin_dir="${PWD}/bin"
+
+mdpid="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/mdtest.pid"
+mdin="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/mdin" &&
+mdout="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/mdout" &&
+
+usage()
+{
+ cat <<- EOF
+ Usage: ${0##*/} [cmd] [options]
+ A whatsapp bot broadcast news
+ Commands:
+ kill-daemon kill the daemon if it is running
+ Options:
+ -m <path to mdtest binary>
+
+ EOF
+}
+
+die()
+{
+ : "${1:?}"
+
+ printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2
+ exit "${2:-1}"
+}
+
+note()
+{
+ : "${1:?}"
+ printf "\033[32;1m%b\033[0m\n" "$*" 1>&2
+}
+
+kill_daemon()
+{
+ kill "$(cat "$mdpid" 2> /dev/null)" 2> /dev/null &&
+ rm -f "$mdpid"
+ rm -f "$mdin"
+ rm -f "$mdout"
+}
+
+start_daemon()
+{
+ [ -p "$mdin" ] ||
+ mkfifo "$mdin"
+ [ -p "$mdout" ] ||
+ mkfifo "$mdout"
+ [ -p "$mdout" ] ||
+ mkfifo "$mdout"
+
+ cd "$db_dir" || exit 1
+ tail -f "$mdin" | "$mdtest" > "$mdout" 2>&1 &
+ echo $! > "$mdpid"
+ cd - > /dev/null || exit 1
+
+ trap kill_daemon EXIT
+}
+
+########
+# MAIN #
+########
+
+cmnd=
+infile=
+outfile=
+
+case "$1" in
+-*)
+ ;;
+*)
+ cmnd="$1"
+ [ "$#" -gt 0 ] &&
+ shift
+ ;;
+esac
+
+while getopts "m:d:i:o:" f; do
+ case "$f" in
+ m)
+ mdtest="$OPTARG"
+ ;;
+ d)
+ db_dir="$OPTARG"
+ ;;
+ i)
+ infile="$OPTARG"
+ ;;
+ o)
+ outfile="$OPTARG"
+ ;;
+ ?)
+ usage
+ exit 1
+ esac
+done
+
+[ ! -x "$mdtest" ] &&
+ die "invalid mdtest bin"
+[ -d "$db_dir" ] ||
+ mkdir -p "$db_dir"
+
+case "$PATH" in
+*"$bin_dir"*)
+ ;;
+*)
+ PATH="${PATH}:$bin_dir"
+ ;;
+esac
+
+case "$cmnd" in
+kill-daemon)
+ kill_daemon
+ exit
+ ;;
+vtono)
+ [ -z "$infile" ] &&
+ infile="${data_dir}/contacts.vcf"
+ [ -z "$outfile" ] &&
+ outfile="${data_dir}/contacts.txt"
+ [ ! -s "$infile" ] &&
+ die "vtono: ${infile}: no such file or directory"
+ vtono.sh "$infile" > "$outfile" &&
+ note "vtono: contacts saved to ${outfile}"
+ ;;
+"")
+ kill_daemon
+ start_daemon
+ read wip_remove_me
+ ;;
+*)
+ die "invalid command: ${cmnd}"
+esac