Skip to content

Instantly share code, notes, and snippets.

@betsalel-williamson
Last active June 24, 2018 11:52
Show Gist options
  • Save betsalel-williamson/8a493ac61864cb4577f5ade610c01460 to your computer and use it in GitHub Desktop.
Save betsalel-williamson/8a493ac61864cb4577f5ade610c01460 to your computer and use it in GitHub Desktop.
Bash script to create blocked contact. Recently in America, the number of spam calls with numbers similar to your own number is out of hand. This will create a contact with all of those numbers for you to be able to block with your phone.
#! /bin/bash
#
# Copyright 2018 Betsalel "Saul" Williamson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 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.
#
##
usage() { echo "Usage: $0 -n <first 6 digits of your phone number>" 1>&2; exit 1; }
while getopts ":n:" o; do
case "${o}" in
n)
n=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${n}" ]; then
usage
fi
# Create a unique file name for the contact
time_stamp=$(date -d "today" +"%Y%m%d%H%M" 2>/dev/null)
if [ -z "${time_stamp}" ]; then
time_stamp=$(date +"%Y%m%d%H%M" 2>/dev/null)
fi
# if the timestamp didn't work use a UUID
cmd_output=$(python -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().urn[9:])' 2>&1)
if [ -z "${time_stamp}" ]; then
time_stamp=$cmd_output
fi
file_number=1
file_name="blocked-contact-${n}-${time_stamp}-${file_number}.vcf"
echo Creating card ${file_name}...
# VCF doesn't work very well with large cards...
maximum_size=$((128000))
echo "BEGIN:VCARD
VERSION:3.0
PRODID:Betsalel Williamson's Handy Block Number Script
N:Contact;Blocked;${n}-${file_number};;
FN:Blocked Contact ${n}-${file_number}" >> ${file_name}
offset=0
for i in {0..9999}
do
printf "item%u.TEL;type=pref:%s%04u\n" $(($i+1-$offset)) $n $i >> ${file_name}
printf "item%u.X-ABLabel:blocked number\n" $(($i+1-$offset)) >> ${file_name}
actual_size=$(wc -c <"$file_name")
if [ $actual_size -ge $maximum_size ]; then
echo Size is over $maximum_size bytes.
echo Ending current card...
cmd_output=$(python -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().urn)' 2>&1)
printf "UID:%s\n" $cmd_output >> ${file_name}
echo "END:VCARD" >> ${file_name}
file_number=$(($file_number+1))
file_name="blocked-contact-${n}-${time_stamp}-${file_number}.vcf"
offset=$(($i+1))
echo Creating another card ${file_name}...
echo "BEGIN:VCARD
VERSION:3.0
PRODID:Betsalel Williamson's Handy Block Number Script
N:Contact;Blocked;${n}-${file_number};;
FN:Blocked Contact ${n}-${file_number}" >> ${file_name}
fi
done
if grep -Fxq "END:VCARD" $file_name
then
# code if found do nothing
echo
else
# code if not found
cmd_output=$(python -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().urn)' 2>&1)
printf "UID:%s\n" $cmd_output >> ${file_name}
echo "END:VCARD" >> ${file_name}
fi
echo Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment