[ previous ] [ next ] [ threads ]
 To :  yate@v...
 From :  Kenneth Sanislo <ken@i...>
 Subject :  external module for better CNAM support with Google Voice
 Date :  Fri, 30 Mar 2012 01:55:33 -0700
I wrote an external module to be used with Google Voice to clean up  
caller and callername so they provide more useful information to SIP  
phones and ATAs that support caller ID. This script will try to pull  
the lookup from your Google Voice account first, so you get the  
customized names from your cell phone, and failing that will try for a  
standard CNAM from http://www.opencnam.org/
Be sure you have the zend framework for PHP installed, and modify or  
remove the set_include_path() if necessary.

This module currently very USA centered, but could probably be adapted  
to provide a useful service elsewhere as well.

  And a big thanks to all the Yate developers for making such a  
fantastic program.

-Kenneth Sanislo

===============

#!/usr/bin/php -q
setMajorProtocolVersion(3);

       // perform query and get result feed
       $query = new Zend_Gdata_Query(
         'http://www.google.com/m8/feeds/contacts/default/full?max-results=9999') 
;
       $feed = $gdata->getFeed($query);

       $results = array();
       foreach($feed as $entry){
         $xml = simplexml_load_string($entry->getXML());
         $obj = new stdClass;
         $obj->name = (string) $entry->title;
         $obj->orgName = (string) $xml->organization->orgName;
         foreach ($xml->phoneNumber as $p) {
           $obj->phoneNumber[] = (string) $p;
         }
         if (isset($obj->phoneNumber) && @in_array($caller, $obj- 
 >phoneNumber)){
             if($obj->name != NULL) return ($obj->name);
         else
             if($obj->orgName != NULL) return ($obj->orgName);
         }
       }
     } catch (Exception $e) {
       Yate::Output("calleridfix.php:ERROR:" . $e->getMessage());
     }

     return FALSE;
}

function checkOpenCNAM($caller)
{
     $context = stream_context_create(array('http' => array('timeout'  
=> 2))); // HTTP Timeout at 2 sec.
     $callername = file_get_contents('https://api.opencnam.com/v1/phone/'.$caller 
, NULL, $context, NULL, 32);
     if ($callername == FALSE){ // Once more, with feeling...
         sleep(1); // OpenCNAM bug? First lookup on a new number  
always fails...
         $callername = file_get_contents('https://api.opencnam.com/v1/phone/'.$caller 
, NULL, $context, NULL, 32);
     }
     return $callername;
}

/* main loop */
for (;;) {
     $ev=Yate::GetEvent();
     /* If Yate disconnected us then exit cleanly */
     if ($ev === false)
         break;
     /* Empty events are normal in non-blocking operation.
        This is an opportunity to do idle tasks and check timers */
     if ($ev === true) {
         continue;
     }
     /* If we reached here we should have a valid object */
     switch ($ev->type) {
	case "incoming":
	    if ($ev->params['status'] == 'incoming') { // We've got an  
incoming call. Time to go to work.
		
		$ev->params['caller'] = preg_replace('/^\+1/', '', $ev- 
 >params['caller']); // Clean up the +1[number] bit.

		if ($ev->params['module'] == 'jingle')
		    $ev->params['callername'] = FALSE;

		if ($ev->params['callername'] == FALSE)
                     $ev->params['callername'] =  
checkGoogleContacts($ev->params['caller'], $ev->params['calleduri']);
                 if ($ev->params['callername'] == FALSE)
                     $ev->params['callername'] = checkOpenCNAM($ev- 
 >params['caller']);
                 if ($ev->params['callername'] == FALSE)
                     $ev->params['callername'] = 'Unknown Name';

             }

	    /* This is extremely important.
	       We MUST let messages return, handled or not */
	    $ev->Acknowledge();
	    break;
	case "answer":
	    break;
	case "installed":
	    break;
	case "uninstalled":
	    break;
	case "setlocal":
	    break;
	default:
     }
}

/* vi: set ts=8 sw=4 sts=4 noet: */
?>

















#!/usr/bin/php -q
setMajorProtocolVersion(3);

      // perform query and get result feed
      $query = new Zend_Gdata_Query(
        'http://www.google.com/m8/feeds/contacts/default/full?max-results=9999');
      $feed = $gdata->getFeed($query);

      $results = array();
      foreach($feed as $entry){
        $xml = simplexml_load_string($entry->getXML());
        $obj = new stdClass;
        $obj->name = (string) $entry->title;
        $obj->orgName = (string) $xml->organization->orgName;
        foreach ($xml->phoneNumber as $p) {
          $obj->phoneNumber[] = (string) $p;
        }
        if (isset($obj->phoneNumber) && @in_array($caller, $obj->phoneNumber)){
            if($obj->name != NULL) return ($obj->name);
        else
            if($obj->orgName != NULL) return ($obj->orgName);
        }
      }
    } catch (Exception $e) {
      Yate::Output("calleridfix.php:ERROR:" . $e->getMessage());
    }

    return FALSE;
}

function checkOpenCNAM($caller)
{
    $context = stream_context_create(array('http' => array('timeout' => 2))); // HTTP Timeout at 2 sec.
    $callername = file_get_contents('https://api.opencnam.com/v1/phone/'.$caller, NULL, $context, NULL, 32);
    if ($callername == FALSE){ // Once more, with feeling...
        sleep(1); // OpenCNAM bug? First lookup on a new number always fails...
        $callername = file_get_contents('https://api.opencnam.com/v1/phone/'.$caller, NULL, $context, NULL, 32);
    }
    return $callername;
} 

/* main loop */
for (;;) {
    $ev=Yate::GetEvent();
    /* If Yate disconnected us then exit cleanly */
    if ($ev === false)
        break;
    /* Empty events are normal in non-blocking operation.
       This is an opportunity to do idle tasks and check timers */
    if ($ev === true) {
        continue;
    }
    /* If we reached here we should have a valid object */
    switch ($ev->type) {
	case "incoming":
	    if ($ev->params['status'] == 'incoming') { // We've got an incoming call. Time to go to work.
		
		$ev->params['caller'] = preg_replace('/^\+1/', '', $ev->params['caller']); // Clean up the +1[number] bit.

		if ($ev->params['module'] == 'jingle')
		    $ev->params['callername'] = FALSE;

		if ($ev->params['callername'] == FALSE)
                    $ev->params['callername'] = checkGoogleContacts($ev->params['caller'], $ev->params['calleduri']);
                if ($ev->params['callername'] == FALSE)
                    $ev->params['callername'] = checkOpenCNAM($ev->params['caller']); 
                if ($ev->params['callername'] == FALSE)
                    $ev->params['callername'] = 'Unknown Name';

            }

	    /* This is extremely important.
	       We MUST let messages return, handled or not */
	    $ev->Acknowledge();
	    break;
	case "answer":
	    break;
	case "installed":
	    break;
	case "uninstalled":
	    break;
	case "setlocal":
	    break;
	default:
    }
}

/* vi: set ts=8 sw=4 sts=4 noet: */
?>