Erstellt: 28. Juli 2013 | Kategorie: Software | Tags: BaSH, Google, Kontakte, Scripting, Software

Nachdem ich ein GSM-VoIP Gateway eingerichtet hatte, um Anrufe über SIP weiterzuleiten, wollte ich auch den Namen des Anrufers sehen, anstatt nur die Rufnummer. Ich denke, es gibt sicher Möglichkeiten, Kontakte in eine PBX wie Asterisk zu importieren, aber dann müsste man sie durch wiederholtes Importieren von Kontakten auf dem neuesten Stand halten, was einfach, aber nicht praktisch ist. Vielleicht gibt es sogar Module, die mit Google arbeiten, aber ich war zu faul, mit Asterisk herumzubasteln und schrieb schnell ein Bash-Skript für diese Aufgabe, das mich sogar per E-Mail benachrichtigt.

Bitte beachten Sie, dass sich das Skript im Proof-of-Concept-Status befindet, es funktioniert grundsätzlich, benötigt aber eine Überarbeitung, da es schnell und schmutzig geschrieben wurde.

Es schickt Ihnen die empfangene Textnachricht per E-Mail oder benachrichtigt Sie per E-Mail über den Anrufer und hat auch eine herold.at und addafix.com Suche.

Sie müssen die Variablen "GMail", "GPasswd" und "SendTo" auf Ihre Google-Kontodaten setzen.

#!/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="yourname@gmail.com"
GPasswd="yourpassword"
# where do we send the mail to? (seperate multiple addresses with comma)
SendTo="yourname@example.org"
# 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='anothername@gmail.com'
  GPasswd='anotherpassword'
  SendTo='c.brajkovic@gmail.com'
fi

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)
  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 '
' | awk -F ">|<" '{print $131";"$139}') gelbeseiten=$(curl --silent http://www.herold.at/gelbe-seiten/telefon_$number/ | grep '
' | 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 '' | 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: $$ " htmlmsg="
$textmsg
" 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 if [ "$debug" = "1" ]; then echo "$SendTo ..."; fi echo "$htmlmsg" | mail -a "Content-Type: text/html" -a "From: Asterisk PBX " -s "Call from $name ($2) to mobile \"$1\"" "$SendTo" else if [ "$debug" = "1" ]; then echo "$SendTo ..."; fi echo "$htmlmsg" | mail -a "Content-Type: text/html" -a "From: Asterisk PBX " -s "SMS from $name ($2) to mobile \"$1\"" "$SendTo" fi if [ "$debug" = "1" ]; then echo -n $(date --rfc-3339="ns"); echo " - mail sent."; fi