#!/bin/bash #------------------------------------------------------------------------------ # /var/install/bin/capi2text-lookup-number - lookup phone number # # Copyright (c) 2008-2024 The Eisfair Team, team(at)eisfair(dot)org # # Creation : 2009-01-20 jed # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #------------------------------------------------------------------------------ # read eislib etc. . /var/install/include/eislib . /var/install/include/jedlib # activate debug output #debug_lookup_number=true if ${debug_lookup_number:-false} then exec 2>/tmp/$(basename ${0})-trace$$.log set -x ask_debug=true export ask_debug fi #------------------------------------------------------------------------------ # update local file # # input: $1 - file type, e.g. 'cache' or 'update:cache' # $2 - number # $3 - name # $4 - details # $5 - loginfo # # $1 - file type, e.g. 'calls' or 'update:calls' # $2 - nat. number # $3 - name # $4 - details # $5 - loginfo #------------------------------------------------------------------------------ write_to_file () { _w2f_file_type="$1" _w2f_number="$2" _w2f_name="$3" _w2f_details="$4" _w2f_loginfo="$5" _w2f_temp_file=`mktemp --tmpdir=/tmp -t $(basename ${0}).XXXXXXXXX` case ${_w2f_file_type} in *cache ) # cache (phonelist.txt) if [ "${CAPI2TEXT_LOCALCACHE}" = 'yes' -a -f "${CAPI2TEXT_LOCALCACHE_FILENAME}" ] then # update local cache if required ... grep -q "^${_w2f_number}" ${CAPI2TEXT_LOCALCACHE_FILENAME} if [ $? -eq 0 ] then if [ -n "${cache_nbr_int}" ] then # remove old entry cp ${CAPI2TEXT_LOCALCACHE_FILENAME} ${CAPI2TEXT_LOCALCACHE_FILENAME}.tmp grep -v "^${cache_nbr_int}" ${CAPI2TEXT_LOCALCACHE_FILENAME}.tmp > ${CAPI2TEXT_LOCALCACHE_FILENAME} rm -f ${CAPI2TEXT_LOCALCACHE_FILENAME}.tmp fi fi # encode ISO-8859-1 to UTF-8 printf "${_w2f_number}\t${_w2f_name} ${_w2f_loginfo}\t${_w2f_details}\n" >> ${_w2f_temp_file} /var/install/bin/convert-encoding ${_w2f_temp_file} cat ${_w2f_temp_file} >> ${CAPI2TEXT_LOCALCACHE_FILENAME} fi ;; *calls ) # calls (anrufe.txt) if [ "${CAPI2TEXT_LOG2FILE}" = 'yes' -a -n "${_w2f_number}" ] then _w2f_log_fpath="`dirname ${CAPI2TEXT_LOG2FILE_FILENAME}`" _w2f_log_fname="`basename ${CAPI2TEXT_LOG2FILE_FILENAME}`" # /public/anrufe.txt /public/anrufe.txt-2012-02 /public/anrufe.txt-2012-03 etc. _w2f_list="${CAPI2TEXT_LOG2FILE_FILENAME} `find ${_w2f_log_fpath} -maxdepth 1 -type f -name ${_w2f_log_fname}-????-??`" for _w2f_fname in ${_w2f_list} do # update logfile grep -q "${_w2f_number}" ${_w2f_fname} if [ $? -eq 0 ] then # DATE TIME CALL_NBR MSN NAME DETAILS DIR DURA # 2017-01-04\t13:10:26\t0216616114\t3848255\t0216616115\tKein Eintrag, , DE # 2017-01-04\t13:10:26\t0216616114\t3848255\t0216616115\tKein Eintrag, , DE\tIN\t0:00 sed -e "/${_w2f_number}/s/${_w2f_number}\([\t ][0-9]*[\t ]\).*[^0-9]$/${_w2f_number}\1${_w2f_name} ${_w2f_loginfo}\t${_w2f_details}/g" \ -e "/${_w2f_number}/s/${_w2f_number}\([\t ][0-9]*[\t ]\).*\([\t ][INOUT]*[\t ][0-9:]*\)$/${_w2f_number}\1${_w2f_name} ${_w2f_loginfo}\t${_w2f_details}\2/g" ${_w2f_fname} > ${_w2f_fname}.tmp cp ${_w2f_fname}.tmp ${_w2f_fname} rm -f ${_w2f_fname}.tmp chmod 0644 ${_w2f_fname} fi done fi ;; esac rm -f ${_w2f_temp_file} } #------------------------------------------------------------------------------ # update db table # # input: $1 - db table, e.g. 'cache' # $2 - number # $3 - name # $4 - details # $5 - loginfo # # $1 - db table, e.g. 'update:calls' # $2 - number # $3 - name # $4 - details # $5 - loginfo #------------------------------------------------------------------------------ write_to_db_table () { _w2dt_db_table="$1" _w2dt_number="$2" _w2dt_name="$3" _w2dt_details="$4" _w2dt_loginfo="`echo "$5" | tr -d ']['`" if [ -z "${_w2dt_loginfo}" ] then _w2dt_loginfo=' ' fi case ${CAPI2TEXT_DB_TYPE} in *mysql|*sqlite ) case ${_w2dt_db_table} in *cache ) # 'cache' _w2dt_data=`printf "${_w2dt_number}\t${_w2dt_name}\t${_w2dt_details}\t${_w2dt_loginfo}"` ;; *calls ) # 'update:calls' # KEEP 'update:calls' TABLE NAME!! # unrequired fields are replaced by fixed characters _w2dt_date='#UNSET#' _w2dt_time='#UNSET#' _w2dt_msn='#UNSET#' _w2dt_msn_replaced='#UNSET#' _w2dt_flag_file='#UNSET#' _w2dt_country_abbr='' _w2dt_country_name='' _w2dt_direction='#UNSET#' _w2dt_duration='#UNSET#' if [ -z "${_w2dt_country_abbr}" ] then _w2dt_tmp_country_abbr=`echo "${_w2dt_details}" | sed -e 's/^.*,//' -e 's/ *//g' | sed -n 's/^\(..\)$/\1/p'` if [ -n "${_w2dt_tmp_country_abbr}" ] then # possible country abbreviation found, strip it off _w2dt_details=`echo "${_w2dt_details}" | sed -e 's/^\(.*\),.*$/\1/' -e 's/ *$//'` # read country name grep -q "^[0-9]*#${_w2dt_tmp_country_abbr}#" ${ccode_file} if [ $? -eq 0 ] then _w2dt_country_abbr="${_w2dt_tmp_country_abbr}" _w2dt_country_name=`grep "^[0-9]*#${_w2dt_tmp_country_abbr}#" ${ccode_file} | cut -d'#' -f3` fi fi fi if [ -z "${_w2dt_country_name}" ] then _w2dt_tmp_country_name=`echo "${_w2dt_details}" | sed -e 's/^.*,//' -e 's/ *//g'` if [ -n "${_w2dt_tmp_country_name}" ] then # possible country name found, strip it off _w2dt_details=`echo "${_w2dt_details}" | sed -e 's/^\(.*\),.*$/\1/' -e 's/ *$//'` # read country abbreviation grep -q "^[0-9]*#[A-Z][A-Z]#${_w2dt_tmp_country_name}" ${ccode_file} if [ $? -eq 0 ] then _w2dt_country_abbr=`grep "^[0-9]*#[A-Z][A-Z]#${_w2dt_tmp_country_name}" ${ccode_file} | cut -d'#' -f2` _w2dt_country_name="${_w2dt_tmp_country_name}" fi fi fi if [ -z "${_w2dt_country_abbr}" ] then _w2dt_country_abbr=' ' fi if [ -z "${_w2dt_country_name}" ] then _w2dt_country_name=' ' fi _w2dt_data="${_w2dt_date}\t${_w2dt_time}\t${_w2dt_number}\t${_w2dt_msn}\t${_w2dt_msn_replaced}\t${_w2dt_name}\t${_w2dt_details}\t${_w2dt_loginfo}\t${_w2dt_flag_file}\t${_w2dt_country_abbr}\t${_w2dt_country_name}\t${_w2dt_direction}\t${_w2dt_duration}" ;; esac /var/install/config.d/capi2text.sh write-data-to-db-table "${_w2dt_db_table}" "${_w2dt_data}" ;; esac } #============================================================================== # main #============================================================================== configfile=/etc/config.d/capi2text ccode_file=/etc/capi2text-country-codes #CAPI2TEXT_DO_DEBUG="yes" if [ -f "${configfile}" ] then . ${configfile} if [ "${CAPI2TEXT_DO_DEBUG}" = 'yes' ] then debug="-d 1" else debug='' fi if [ -n "${CAPI2TEXT_DEFAULT_CCODE}" ] then # set individual country code ccode=${CAPI2TEXT_DEFAULT_CCODE} else # set default country code - 49 -> Germany CAPI2TEXT_DEFAULT_CCODE=49 ccode=49 fi nbr='' u_exit=0 until [ ${u_exit} -eq 1 ] do clrhome mecho --info "lookup name number" mecho mecho "Input example: 0221 112233" mecho " 0049 221 112233" mecho # PHONENBR=0221334455 # PHONENBR=0049221334455 /var/install/bin/ask "Enter phone number (e.g. format: [country-code] area-code number)[q]" "${nbr}" '+' > /tmp/ask.$$ rc=$? nbr=`cat /tmp/ask.$$ | sed -e 's/^+/00/' -e 's/[^0-9* qQ]//g'` rm -f /tmp/ask.$$ if [ ${rc} = 255 ] then exit 1 fi case "${nbr}" in [qQ] ) # quit u_exit=1 ;; * ) mecho # remove spaces and round brackets snbr="`echo ${nbr} | sed -e 's/[ )(]//g'`" # check for leading zeros or leading '**' (internal Fritzbox phone number) echo "${snbr}" | grep -E -q '^0|^\*\*[0-9]+' if [ $? -eq 0 ] then # invert CAPI2TEXT_NUMBER_TRIMLEADING and CAPI2TEXT_NUMBER_ADDCHARS # function at this stage because they will be applied later. # add number of leading zeros which will be automatically # removed again at a later stage of this function cmax=`echo "${CAPI2TEXT_NUMBER_TRIMLEADING}" | sed 's/^[zZ]//'` if [ -z "${cmax}" ] then cmax='0' fi cstr=`expr substr "00000000000000" 1 ${cmax}` snbr="${cstr}${snbr}" # remove number of leading zeros which will be automatically # added again at a later stage of this function # trim number sstart=`expr length "${CAPI2TEXT_NUMBER_ADDCHARS}"` snbr="${snbr:${sstart}}" # check if numeric value echo "${snbr}" | grep -q '^[0-9*]*$' if [ $? -eq 0 ] then # numeric value, go on ... # sresult=0049221334455|Tom Jones|[W]|Tom Street 5, 1234 Tomtown, USA|0221334455 search_result=`/usr/sbin/capi2text.sh --lookup-name-and-details ${snbr}` name=`echo "${search_result}" | cut -d'|' -f2` sinfo=`echo "${search_result}" | cut -d'|' -f3` echo "${sinfo}" | grep -i -q "\[C\]" if [ $? -eq 0 ] then # record already cached cached=1 cache_nbr_int=`echo "${search_result}" | cut -d'|' -f1` cache_name="${name}" cache_sinfo="${sinfo}" cache_details=`echo "${search_result}" | cut -d'|' -f4` cache_nbr_short=`echo "${search_result}" | cut -d'|' -f5` # details=Tom Street 5, 1234 Tomtown, USA cache_street=`echo "${cache_details}" | cut -d, -f1` cache_city=`echo "${cache_details}" | cut -d, -f2` cache_country=`echo "${cache_details}" | cut -d, -f3` cache_street=`trim_spaces "${cache_street}" both` cache_city=`trim_spaces "${cache_city}" both` cache_country=`trim_spaces "${cache_country}" both` else # record not cached cached=0 search_nbr_int=`echo "${search_result}" | cut -d'|' -f1` search_name="${name}" search_sinfo="${sinfo}" search_details=`echo "${search_result}" | cut -d'|' -f4` search_nbr_short=`echo "${search_result}" | cut -d'|' -f5` # details=Tom Street 5, 1234 Tomtown, USA search_street=`echo "${search_details}" | cut -d, -f1` search_city=`echo "${search_details}" | cut -d, -f2` search_country=`echo "${search_details}" | cut -d, -f3` search_street=`trim_spaces "${search_street}" both` search_city=`trim_spaces "${search_city}" both` search_country=`trim_spaces "${search_country}" both` fi mecho mecho -n "Number : ${nbr}" if [ -n "${name}" ] then diff_flag=0 diff_search_empty=0 if [ ${cached} -eq 1 ] then # already in cache echo "${snbr}" | grep -q '^\*\*[0-9]+' if [ $? -eq 0 ] then # internal Fritzbox phone number format search_nbr_short="${nbr}" search_name="${cache_name}" search_sinfo="${cache_sinfo}" search_street="${cache_street}" search_city="${cache_city}" search_country="${cache_country}" else search_result=`/usr/sbin/capi2text.sh --lookup-name-without-phonelist ${snbr}` search_nbr_int=`echo "${search_result}" | cut -d'|' -f1` search_name=`echo "${search_result}" | cut -d'|' -f2` search_sinfo=`echo "${search_result}" | cut -d'|' -f3` search_details=`echo "${search_result}" | cut -d'|' -f4` search_nbr_short=`echo "${search_result}" | cut -d'|' -f5` # details=Tom Street 5, 1234 Tomtown, USA search_street=`echo "${search_details}" | cut -d, -f1` search_city=`echo "${search_details}" | cut -d, -f2` search_country=`echo "${search_details}" | cut -d, -f3` search_street=`trim_spaces "${search_street}" both` search_city=`trim_spaces "${search_city}" both` search_country=`trim_spaces "${search_country}" both` diff_name='==' diff_street='==' diff_city='==' diff_country='==' if [ -z "${search_name}" ] then diff_search_empty=1 elif [ "${search_name}" != "${cache_name}" ] then diff_flag=1 diff_name='-warn ->' fi if [ -z "${search_street}" ] then diff_search_empty=1 elif [ "${search_street}" != "${cache_street}" ] then diff_flag=1 diff_street='-warn ->' fi if [ -z "${search_city}" ] then diff_search_empty=1 elif [ "${search_city}" != "${cache_city}" ] then diff_flag=1 diff_city='-warn ->' fi if [ -z "${search_country}" ] then diff_search_empty=1 elif [ "${search_country}" != "${cache_country}" ] then diff_flag=1 diff_country='-warn ->' fi fi fi # print search result if [ "`trim_spaces "${nbr}" all`" != "${search_nbr_short}" ] then mecho --warn " -> ${search_nbr_short} !!" else mecho fi mecho if [ ${diff_flag} -eq 1 ] then # results differ techo --begin 9 33 4 33 techo --info --row "" "Search result" "" "Cache content" techo --row techo --row "Name :" "${search_name} ${search_sinfo}" ${diff_name} "${cache_name} ${cache_sinfo}" techo --row techo --row "Details:" "${search_street}" ${diff_street} "${cache_street}" techo --row "" "${search_city}" ${diff_city} "${cache_city}" techo --row "" "${search_country}" ${diff_country} "${cache_country}" techo --end mecho mecho --warn "Local cache not up-to-date." mecho else techo --begin 9 33 if [ ${diff_search_empty} -eq 1 ] then # search result was empty, show cache entry techo --info --row "" "Cache result" techo --row techo --row "Name :" "${cache_name} ${cache_sinfo}" techo --row techo --row "Details:" "${cache_street}" techo --row "" "${cache_city}" techo --row "" "${cache_country}" else techo --info --row "" "Search result" techo --row techo --row "Name :" "${search_name} ${search_sinfo}" techo --row techo --row "Details:" "${search_street}" techo --row "" "${search_city}" techo --row "" "${search_country}" fi techo --end mecho if [ ${cached} -eq 0 ] then mecho --warn "Not in local cache." mecho fi fi if [ "${CAPI2TEXT_LOCALCACHE}" = 'yes' ] then # go on ... if [ ${cached} -eq 0 -o ${diff_flag} -eq 1 ] then # results differ if /var/install/bin/ask "Update local cache" 'no' then # update cache case ${CAPI2TEXT_DB_TYPE} in file* ) write_to_file 'update:cache' "${search_nbr_int}" "${search_name}" \ "${search_street}, ${search_zip} ${search_city}, ${search_country}" \ "${search_sinfo}" ;; esac case ${CAPI2TEXT_DB_TYPE} in *mysql|*sqlite ) write_to_db_table 'update:cache' "${search_nbr_int}" "${search_name}" \ "${search_street}, ${search_zip} ${search_city}, ${search_country}" \ "${search_sinfo}" ;; esac mecho mecho --info "Local cache updated." if [ "${CAPI2TEXT_LOG2FILE}" = "yes" ] then if /var/install/bin/ask "Update call log file" 'no' then # update call log file case ${CAPI2TEXT_DB_TYPE} in file* ) if [ "`trim_spaces "${nbr}" all`" != "${search_nbr_short}" ] then # update shortened number, if it exists write_to_file 'calls' "${search_nbr_short}" "${search_name}" \ "${search_street}, ${search_city}, ${search_country}" \ "${search_sinfo}" fi # update original number, if it exists write_to_file 'calls' "`trim_spaces "${nbr}" all`" "${search_name}" \ "${search_street}, ${search_city}, ${search_country}" \ "${search_sinfo}" ;; esac case ${CAPI2TEXT_DB_TYPE} in *mysql|*sqlite ) if [ "`trim_spaces "${nbr}" all`" != "${search_nbr_short}" ] then # update shortened number, if it exists write_to_db_table 'update:calls' "${search_nbr_short}" "${search_name}" \ "${search_street}, ${search_city}, ${search_country}" "${search_sinfo}" fi # update original number, if it exists write_to_db_table 'update:calls' "`trim_spaces "${nbr}" all`" "${search_name}" \ "${search_street}, ${search_city}, ${search_country}" "${search_sinfo}" ;; esac mecho mecho --info "Call log updated." fi fi fi else mecho --info "Local cache is up-to-date." fi else mecho --warn "Local cache disabled." fi else mecho mecho --info "No name found!" mecho if [ "${CAPI2TEXT_LOCALCACHE}" = 'yes' ] then # go on ... if /var/install/bin/ask "Do you want to enter it now" 'no' then # enter contact details cache_sinfo="[C]" search_result=`/usr/sbin/capi2text.sh --format-number ${snbr}` cache_nbr_int=`echo "${search_result}" | cut -d'|' -f1` cache_nbr_nat=`echo "${search_result}" | cut -d'|' -f2` mecho cache_name=$(/var/install/bin/ask "Name" "" "+") cache_sinfo="[C]" cache_street=$(/var/install/bin/ask "Street" "" "+") cache_zip=$(/var/install/bin/ask "Zip code" "" "+") cache_city=$(/var/install/bin/ask "City" "" "+") cache_country=$(/var/install/bin/ask "Country" "" "+") if /var/install/bin/ask "Do you want to save this data now" 'no' then case ${CAPI2TEXT_DB_TYPE} in file* ) write_to_file 'cache' "${cache_nbr_int}" "${cache_name}" \ "${cache_street}, ${cache_zip} ${cache_city}, ${cache_country}" \ "${cache_sinfo}" ;; esac case ${CAPI2TEXT_DB_TYPE} in *mysql|*sqlite ) write_to_db_table 'cache' "${cache_nbr_int}" "${cache_name}" \ "${cache_street}, ${cache_zip} ${cache_city}, ${cache_country}" \ "${cache_sinfo}" ;; esac mecho mecho --info "Local cache updated." if [ "${CAPI2TEXT_LOG2FILE}" = 'yes' ] then if /var/install/bin/ask "Update call log" 'no' then # update call log file - run command twice to catch all occurencies case ${CAPI2TEXT_DB_TYPE} in file* ) # 1. national number format write_to_file 'calls' "${cache_nbr_nat}" "${cache_name}" \ "${cache_street}, ${cache_zip} ${cache_city}, ${cache_country}" \ "${cache_sinfo}" # 2. international number format write_to_file 'calls' "${cache_nbr_int}" "${cache_name}" \ "${cache_street}, ${cache_zip} ${cache_city}, ${cache_country}" \ "${cache_sinfo}" ;; esac case ${CAPI2TEXT_DB_TYPE} in *mysql|*sqlite ) # 1. national number format write_to_db_table 'update:calls' "${cache_nbr_nat}" "${cache_name}" \ "${cache_street}, ${cache_zip} ${cache_city}, ${cache_country}" "${cache_sinfo}" # 2. international number format write_to_db_table 'update:calls' "${cache_nbr_int}" "${cache_name}" \ "${cache_street}, ${cache_zip} ${cache_city}, ${cache_country}" "${cache_sinfo}" ;; esac mecho mecho --info "Call log updated." fi fi else mecho mecho --warn "Local cache not updated." fi fi fi # if [ "${CAPI2TEXT_LOCALCACHE}" = 'yes' ] fi u_exit=1 else mecho --error "Only numeric value allowed!" fi else mecho --error "Invalid number format, leading zero(s) missing!" fi ;; esac mecho anykey done else echo "Error: No configuration file '${configfile}' found!" anykey fi #============================================================================== # end #============================================================================== exit 0