<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -2,13 +2,13 @@ NabazTwit is a script which try to provide a better way to combine twitter and t
 
 h1. Why?
 
-The Violet twitter modules takes all your friends twits, and send them in TTS (Text to speech) to your Nabaztag.
+The Violet twitter modules takes all your friends twits and send them in TTS (Text to speech) to your Nabaztag.
 It can be really too much noise as you can have a lot of friends, and also, the nabaztag is not good at reading urls or smileys
 
 
 h1. What do this script do?
 
-This script can read only the replies made to you (i.e. with a @your_nickname in the text) 
+This script can read only the replies made to you (i.e. with a @your_nickname in the text) and private message received
 Before sending thoses twits to your nabaztag, the script remove all smileys and urls.
 And the script also check if your nabaztag is awake (otherwise it don't send message to prevent mailbox overload)
 At the end you receive less noise and more undestandable TTS messages
@@ -37,6 +37,10 @@ You can activate debug mode, by changing $debug to true near the top of the scri
 
 Feel free to enhance, adapt, evolve it for your own needs.
 
+REMARK:
+
+This script consume 2 API request per run (1 to retrieve your replies and 1 to retrieve your direct messages)
+
 
 h1. known issues
 </diff>
      <filename>README.textile</filename>
    </modified>
    <modified>
      <diff>@@ -6,7 +6,7 @@ Retrieve your twitter replies of friends timeline and process it to be more
 &quot;Nabaztag&quot; compatible, by removing some stuff that the rabbit
 can't read properly 
 
-Version:0.1
+Version:0.2
 Author: Olivier Ruffin (http://www.veilleperso.com)
 
 Remarks:
@@ -25,12 +25,15 @@ header('Content-type: text/xml; charset=UTF-8');
 $params = @parse_ini_file(&quot;config.ini&quot;, TRUE);
 if (empty($params)) die(&quot;Unable to find conf.ini file\n&quot;);
 
-$twitter_url = &quot;http://&quot;.$params[&quot;twitter&quot;][&quot;login&quot;].&quot;:&quot;.$params[&quot;twitter&quot;][&quot;password&quot;].&quot;@twitter.com/statuses/&quot;.$params[&quot;twitter&quot;][&quot;check&quot;].&quot;.xml?count=10&quot;;
+$status_url = &quot;http://&quot;.$params[&quot;twitter&quot;][&quot;login&quot;].&quot;:&quot;.$params[&quot;twitter&quot;][&quot;password&quot;] .&quot;@twitter.com/statuses/&quot;.$params[&quot;twitter&quot;][&quot;check&quot;].&quot;.xml&quot;;
+$private_url = &quot;http://&quot;.$params[&quot;twitter&quot;][&quot;login&quot;].&quot;:&quot;.$params[&quot;twitter&quot;][&quot;password&quot;] .&quot;@twitter.com/direct_messages.xml&quot;;
+
+
 $api_url = &quot;http://api.nabaztag.com/vl/FR/api.jsp?sn=&quot;.$params[&quot;violet&quot;][&quot;sn&quot;].&quot;&amp;token=&quot;.$params[&quot;violet&quot;][&quot;token&quot;];
 if (empty($params[&quot;twitter&quot;][&quot;cache&quot;])) $localfile = &quot;/tmp/last_read.twitter&quot;;
 else $localfile = $params[&quot;twitter&quot;][&quot;cache&quot;];
 
-$debug = false;
+$debug = true;
 $sleep = 10; //time to sleep beetween 2 messages to make sure each message is delivered
 //$debug_with = &quot;/tmp/twitter.debug&quot;; // a twitter XML file to allow local debug
 
@@ -44,6 +47,7 @@ function debug_log($msg) {
 // EMOTICONS
 $smileys = array(
 ':-)', 
+'xD',
 ':)', 
 ':-D',
 ':D', // very happy
@@ -127,7 +131,11 @@ $smileys = array(
 $translate = array(
   'lol' =&gt; 'ha ha ha ha',
   'ptdr' =&gt; 'ha ha ha ha',
-  'pr' =&gt; 'pour'
+  'pr' =&gt; 'pour',
+  'rt' =&gt; 'retwit',
+  'tx' =&gt; 'merci',
+  'thx' =&gt; 'merci',
+  'slt' =&gt; 'salut'
 );
 
 // -------------------------------------------------------------------
@@ -144,32 +152,67 @@ function delete_emoticons($txt) {
     $short_smiley = str_replace(&quot;:-&quot;, &quot;;&quot;, $smiley);
     if ($short_smiley != $smiley) $txt = str_replace($short_smiley, &quot;&quot;, $txt);
   }
+  
+  $txt = &quot;$txt &quot;;
   foreach ($translate as $key =&gt; $value) {
-    $txt = str_replace(&quot; $key &quot;, $value, $txt);
+    $txt = preg_replace(&quot;| $key |is&quot;, &quot; $value &quot;, $txt);
   }
-  return $txt;
+  return trim($txt);
 }
 
 // -------------------------------------------------------------------
-// Process message and send new messages to nabztag
+// What is the last twit id retrieved?
+// -------------------------------------------------------------------
+function previous_id($type) {
+  return (int) @file_get_contents(cache_filename($type));
+}
+
+// -------------------------------------------------------------------
+// What is the file cache ?
+// -------------------------------------------------------------------
+function cache_filename($type) {
+  global $localfile;
+  return &quot;${localfile}.${type}&quot;;
+}
+
 // -------------------------------------------------------------------
-function send_twits_to_nabaztag($str) {
-  global $api_url, $localfile, $sleep, $params;
-  $previous_id = (int) @file_get_contents($localfile);
+// What is the url to check ?
+// -------------------------------------------------------------------
+function url_for($type) {
+  global $status_url, $private_url;
+  if ($type == 'status') $url = $status_url;
+  else $url = $private_url;
+  $since_id = previous_id($type);
+  if ($since_id&gt;0) {
+    if (strpos($url, &quot;?&quot;)) $url .= &quot;&amp;since_id=${since_id}&quot;;
+    else $url .= &quot;?since_id=${since_id}&quot;;
+  }
+  return $url;
+}
 
+// -------------------------------------------------------------------
+// Process message and send new messages to nabztag
+// -------------------------------------------------------------------
+function send_twits_to_nabaztag($str, $type) {
+  global $api_url, $sleep, $params;
+  $previous_id = previous_id($type);
   debug_log(&quot;SINCE: $previous_id\n&quot;);
   debug_log(&quot;XML: $str&quot;);
   $xml =  simplexml_load_string($str);
   
   $last_id = 0;
-  foreach ($xml-&gt;status as $item) {
+  if ($type == 'status') $list = $xml-&gt;status;
+  else $list = $xml-&gt;direct_message;
+  
+  foreach ($list as $item) {
     if ($item-&gt;id &gt; $last_id) $last_id = $item-&gt;id;
   }
+
   // memorise the new last id
-  save_last_id($last_id);
+  save_last_id($last_id, $type);
 
   $to_send = array();
-  foreach ($xml-&gt;status as $item) {
+  foreach ($list as $item) {
     $id = (int)$item-&gt;id;
     if ($id&gt;$previous_id) {
       $txt = preg_replace(&quot;/http(s)*:\/\/[^\s]+/is&quot;, &quot;&quot;, html_entity_decode(utf8_decode($item-&gt;text), ENT_QUOTES));
@@ -179,7 +222,9 @@ function send_twits_to_nabaztag($str) {
       $txt = trim(preg_replace(&quot;/([\s\_\/-]+)/is&quot;, &quot; &quot;, $txt));
 
       if ($txt != &quot;&quot;) {
-        $from = preg_replace(&quot;/([\s\_-]+)/is&quot;, &quot; &quot;, $item-&gt;user-&gt;name);
+        if ($type == 'status') $from = $item-&gt;user-&gt;name;
+        else $from = $item-&gt;sender-&gt;name;
+        $from = preg_replace(&quot;/([\s\_-]+)/is&quot;, &quot; &quot;, $from);
         $txt = $params[&quot;violet&quot;][&quot;from&quot;].&quot; $from , &quot;.utf8_encode($txt);
         $to_send[&quot;$id&quot;] = $txt;
       }
@@ -187,9 +232,8 @@ function send_twits_to_nabaztag($str) {
     else {
       debug_log(&quot;Skipping $id - Already sent&quot;);
     }
-    
   }
-  
+
   if (sizeof($to_send)&gt;0) {
     ksort($to_send, SORT_NUMERIC);
     foreach ($to_send as $id =&gt; $txt) {
@@ -199,6 +243,7 @@ function send_twits_to_nabaztag($str) {
       sleep($sleep); // prevent spam-like attitude !
     } 
   }
+
   return &quot;&quot;;
 }
 
@@ -206,10 +251,10 @@ function send_twits_to_nabaztag($str) {
 // get the lasts message from twitter friends timeline
 // -------------------------------------------------------------------
 
-function fetch_xml(){
-  global $twitter_url, $localfile, $debug_with;
-  if ($debug_with) $twitter_url = $debug_with;
-  $content = @file_get_contents($twitter_url);
+function fetch_xml($url){
+  global $debug_with;
+  if ($debug_with) $url = $debug_with;
+  $content = @file_get_contents($url);
   return $content;
 }
 
@@ -228,27 +273,38 @@ function is_sleeping() {
 // -------------------------------------------------------------------
 // memorise the id of the last message read
 // -------------------------------------------------------------------
-function save_last_id($last_id) {
-  global $localfile;
-  debug_log(&quot;SAVE LAST ID : $last_id&quot;);
-  $fp=fopen($localfile, &quot;w&quot;);
-  fwrite($fp, $last_id); //write contents of feed to cache file
-  fclose($fp);
+function save_last_id($last_id, $type) {
+  if ((int)$last_id&gt;0) {
+    debug_log(&quot;SAVE LAST ID : $last_id&quot;);
+    $fp = fopen(cache_filename($type), &quot;w&quot;);
+    fwrite($fp, $last_id); //write contents of feed to cache file
+    fclose($fp);
+  }
 }
 
 // -------------------------------------------------------------------
-// process new twits
+// memorise the id of the last message read
 // -------------------------------------------------------------------
 
-function process_incoming_twits(){
-  global $localfile;
-  if (!file_exists($localfile)) { // if cache file doesn't exist
-    touch($localfile); //create it
-    chmod($localfile, 0666); 
+function process_incoming_twits_for($type) {
+  $file = cache_filename($type);
+  if (!file_exists($file)) { // if cache file doesn't exist
+    touch($file); //create it
+    chmod($file, 0666); 
   }
-  $xml = fetch_xml(); //then populate cache file with contents of RSS feed
-  if ($xml != &quot;&quot;) send_twits_to_nabaztag($xml);
+  $url = url_for($type);
+  debug_log(&quot;URL: ${url}&quot;);
+  $xml = fetch_xml($url); //then populate cache file with contents of RSS feed
+  if ($xml != &quot;&quot;) send_twits_to_nabaztag($xml, $type);
   else debug_log(&quot;EMPTY XML!&quot;);
+  
+}
+// -------------------------------------------------------------------
+// process new twits
+// -------------------------------------------------------------------
+function process_incoming_twits(){
+  process_incoming_twits_for('status');
+  process_incoming_twits_for('private');
 }
 
 if (!is_sleeping()) process_incoming_twits();</diff>
      <filename>nabaztag.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>1b34de58f0d04cca2ce0bf71484f18ab47789b87</id>
    </parent>
  </parents>
  <author>
    <name>Olivier Ruffin</name>
    <email>olivier@veilleperso.com</email>
  </author>
  <url>http://github.com/veilleperso/nabaztwit/commit/e344a312c7398a153a30518e83fecb0a55c0c45b</url>
  <id>e344a312c7398a153a30518e83fecb0a55c0c45b</id>
  <committed-date>2009-03-05T06:55:01-08:00</committed-date>
  <authored-date>2009-03-05T06:55:01-08:00</authored-date>
  <message>The script now retrieve public twits but also direct messages in TTS</message>
  <tree>fbffaf046f88f888bd04d7ba8c6a3eb145942164</tree>
  <committer>
    <name>Olivier Ruffin</name>
    <email>olivier@veilleperso.com</email>
  </committer>
</commit>
