Hackers

After i've set up a GSM-VoIP Gateway to forward calls via SIP, i wanted to see the caller name too instead just the calling number. I guess there are sure ways to import contacts into a PBX like Asterisk, that way you would need to keep it up-to-date by importing contacts over and over again what is simple but not practical. Maybe there are even modules available to work with Google, but i was lazy tinkering with Asterisk and just quickly wrote a bash script to do that job and even notifying me via mail.

Please note the script is in a proof-of-concept state, it basically works but needs remake as it is written quick and dirty.

It mails you the received text message or notifies you via mail about the caller and has a herold.at and addafix.com lookup too.

You need to set "GMail", "GPasswd" and "SendTo" variables to your Google account data.

#!/bin/bash

# google api requirements:
# https://developers.google.com/gdata/articles/using_cURL
# https://developers.google.com/gdata/faq#clientlogin
# https://developers.google.com/google-apps/contacts/v3/
# https://developers.google.com/google-apps/contacts/v3/reference
# json parser:
# https://github.com/dominictarr/JSON.sh#jsonsh
# recommendation(optional):
# https://developers.google.com/accounts/docs/OAuth2UserAgent

# -> Asterisk SMS storage is full:
# [Nov 22 01:45:10] ERROR[7068]: at_response.c:1420 at_response_smmemfull: [privatone] SMS storage is full
# [Nov 22 01:46:12] ERROR[7068]: at_response.c:1420 at_response_smmemfull: [privatone] SMS storage is full
# http://pastebin.com/Z8cd0Sr0
# delete SMS:
# asterisk -r
# mochile*CLI> dongle cmd privatone AT+CMGD=1,4
# [privatone] 'AT+CMGD=1,4' Command queued for execute
# [privatone] Got Response for user's command:'OK'
# [Nov 22 01:46:25] NOTICE[7068]: at_response.c:1714 at_response: [privatone] Got Response for user's command:'OK'

debug=0

# your google-login data
GMail="This email address is being protected from spambots. You need JavaScript enabled to view it."
GPasswd="yourpassword"
# where do we send the mail to? (seperate multiple addresses with comma)
SendTo="This email address is being protected from spambots. You need JavaScript enabled to view it."
# output format: atom (the default), rss, or json
# as this script uses a json-parser, we need json output
Format="json"

dir="/var/lib/asterisk"
jsonparser="JSON.sh"
tmpfile="gcontact"

# multiuser setup progress in work
if [ "$1" = "AnotherName" ]; then
  GMail=This email address is being protected from spambots. You need JavaScript enabled to view it.'
  GPasswd='anotherpassword'
  SendTo=This email address is being protected from spambots. You need JavaScript enabled to view it.'
fi

#if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - get external ip ..."; fi
#externalip=$(curl --silent http://ip6.me | grep 'Arial, Monospace' | awk -F ">|<" '{print $7}')

if [ "${2}" = "" ]; then
  # echo "Anonymous Caller"
  name="Anonymous"
  msg="
Date and timestamp:
$(date --rfc-3339=ns)

ContactName:
$name
"
else
  cd ${dir};
  if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - lookup calling number at google.com ..."; fi
  Auth=$(curl --silent https://www.google.com/accounts/ClientLogin --data-urlencode Email=${GMail} --data-urlencode Passwd=${GPasswd} -d accountType=GOOGLE -d source=Google-cURL-Example -d service=cp | grep Auth | cut -d= -f2)
  curl -o ${dir}/${tmpfile} --silent --header "Authorization: GoogleLogin auth=$Auth" "https://www.google.com/m8/feeds/contacts/$GMail/full?v=3.0&alt=$Format&q=$2"
  if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - code: $? - tmpfile ${tmpfile} saved. parsing file ..."; fi
  pos=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | egrep -m1 \"entry.*${2}\" | cut -d, -f 3)
  #if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - works? $? - pos: $pos ..."; fi
  shortinfo=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | grep ,${pos}, | grep -e 'title","$t' -e 'email.*address"]' -e 'phoneNumber.*$t"]')
  entry_name=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | egrep -m1 \"entry.*name\"\])
  entry_phone=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | egrep -m1 \"entry.*phoneNumber\"\])
  entry_email=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | egrep -m1 \"entry.*email\"\])
  entry_im=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | egrep -m1 \"entry.*im\"\])
  entry_birth=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | egrep -m1 \"entry.*birthday\"\])
  entry_address=$(${dir}/${jsonparser} < ${dir}/${tmpfile} | egrep -m1 \"entry.*structuredPostalAddress\")
  rm ${tmpfile}
  birthday=$(echo $entry_birth | awk -F "\"" '{print $10}')
  yearsold=$( DATE=$(date +%F); d1=$(date -d "$DATE" +%s); d2=$(date -d "$birthday" +%s); echo $(((d1-d2)/31104000)) )
  name=$(echo ${entry_name} | cut -d\" -f 12)
  namesource="google.com"
  addrsource="google.com"
  postaladdress=$(echo ${entry_address} | cut -d\" -f 12)
  number="00${2#+}"
  if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - lookup calling number at herold.at ..."; fi
  telefonbuch=$(curl --silent http://www.herold.at/telefonbuch/telefon_$number/ | grep '<div class="result-wrap">' | awk -F ">|<" '{print $131";"$139}')
  gelbeseiten=$(curl --silent http://www.herold.at/gelbe-seiten/telefon_$number/ | grep '<div class="result-wrap">' | awk -F ">|<" '{print $147";"$163}')
  if [ "${postaladdress}" = "" ]; then
    postaladdress=$(echo ${telefonbuch} | cut -d ";" -f 2)
    addrsource="herold.at/telefonbuch"
    if [ "${postaladdress}" = "" ]; then
      postaladdress=$(echo ${gelbeseiten} | cut -d ";" -f 2)
      addrsource="herold.at/gelbe-seiten"
      if [ "${postaladdress}" = "" ]; then
        addrsource=""
      fi
    fi
  fi
  if [ "${name}" = "" ]; then
    name=$(echo ${telefonbuch} | cut -d ";" -f 1)
    namesource="herold.at/telefonbuch"
    if [ "${name}" = "" ]; then
      name=$(echo ${gelbeseiten} | cut -d ";" -f 1)
      namesource="herold.at/gelbe-seiten"
      if [ "${name}" = "" ]; then
        if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - lookup calling number at adaffix.com ..."; fi
        adaffix=$(curl --silent http://srv1.adaffix.com/adaffix/wap/wic.jsp?number=$number | grep '<td><b>' | awk -F ">|<" '{print $5";"}')
        adaffix_result=$(echo ${adaffix} | cut -d ";" -f 1-)
        name=$(echo ${adaffix} | cut -d ";" -f 1)
        namesource="adaffix.com"
        if [ "${name}" = "" ]; then
          namesource=""
        fi
      fi
    fi
  fi
fi

if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - got name: $name ..."; fi

if [ "$3" = "" ]; then smstext="Call from $2 ($name)"; else smstext="$3"; fi
textmsg="###############################################################################
$smstext
###############################################################################
Date and timestamp:
$(date --rfc-3339=ns)

Contact:
Name: $name (source: $namesource)
PostalAddress: $postaladdress (source: $addrsource)
Birthday: $birthday (today: $yearsold years)

PhoneNumber:
$(for i in $(echo $entry_phone | awk -F "#" '{$1=""; print $0}'); do echo $i | awk -F "\"" '{ print "("$1") "$5" (tel:"$9")" }'; done)

E-Mail:
$(for i in $(echo $entry_email | awk -F "#" '{$1=""; print $0}'); do echo $i | awk -F "\"" '{ print "("$1") "$5 }'; done)

Instant Messaging:
$(for i in $(echo $entry_im | awk -F "{" '{$1=""; print $0}'); do echo $i | awk -F "\"" '{ print $8": "$4 }' | awk -F "#" '{print $2}'; done)

Herold.at Telefonbuch/Gelbe-Seiten: $number
PostalAddress: $postaladdress

Adaffix.com Results:
$adaffix_result
###############################################################################
($0 $1 $2 $3)
pid: $$
"
#IP: $externalip
#$(host $externalip)

htmlmsg="
<html>
<head><title></title></head>
<body>
<pre>
$textmsg
</pre>
</body>
</html>
"

#(cat <<EOCAT
#Subject: Call from $name ($2) to mobile "$1"
#MIME-Version: 1.0
#Content-Type: text/html
#Content-Disposition: inline
#EOCAT
#cat $dir/htmlmail.html) | sendmail $SendTo

#mail -s "Call from $name ($2) to mobile \"$1\"" "$SendTo" << MESSAGE
#mail -a "Content-Type: text/html" -s "Call from $name ($2) to mobile \"$1\"" "$SendTo" << MESSAGE
if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo -n " - sending mail to "; fi

# check if text was given, if not, notify about a call
if [ "$3" = "" ]; then
  # mail -s "Call from $name ($2) to mobile \"$1\"" "$SendTo" < msgfile
  if [ "$debug" = "1" ]; then echo "$SendTo ..."; fi
  #echo "$htmlmsg" | mail -a "Content-Type: text/html" -a "From: Asterisk PBX <This email address is being protected from spambots. You need JavaScript enabled to view it.>" -s "Call from $name ($2) to mobile \"$1\"" "$SendTo"
  echo "$textmsg" | mail -a "From: Asterisk PBX <This email address is being protected from spambots. You need JavaScript enabled to view it.>" -s "Call from $name ($2) to mobile \"$1\"" "$SendTo"
# some text was given, notify about a message
else
  # mail -s Text from $name ($2) to mobile \"$1\"" "$SendTo" < msgfile
  if [ "$debug" = "1" ]; then echo "$SendTo ..."; fi
  #echo "$htmlmsg" | mail -a "Content-Type: text/html" -a "From: Asterisk PBX <This email address is being protected from spambots. You need JavaScript enabled to view it.>" -s "Text from $name ($2) to mobile \"$1\"" "$SendTo"
  echo "$textmsg" | mail -a "From: Asterisk PBX <This email address is being protected from spambots. You need JavaScript enabled to view it.>" -s "Text from $name ($2) to mobile \"$1\"" "$SendTo"
fi

if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - mail sent: $?"; fi

echo $name
#return 0
exit 0

Feel free to use it (at your own risk). Maybe i'll update it some day to some more productive state...