[Svn] r2358 - in branches/devel-api: plugins plugins/additional_message_headers plugins/show_additional_headers program/include program/lib program/steps/mail

trac at roundcube.net trac at roundcube.net
Tue Mar 17 22:40:44 CET 2009


Author: thomasb
Date: 2009-03-17 16:40:43 -0500 (Tue, 17 Mar 2009)
New Revision: 2358

Added:
   branches/devel-api/plugins/show_additional_headers/
   branches/devel-api/plugins/show_additional_headers/show_additional_headers.php
Modified:
   branches/devel-api/plugins/additional_message_headers/additional_message_headers.php
   branches/devel-api/program/include/rcube_imap.php
   branches/devel-api/program/include/rcube_plugin_api.php
   branches/devel-api/program/lib/imap.inc
   branches/devel-api/program/steps/mail/func.inc
Log:
Add hooks to fetch and display additional headers + use html wrapper functions

Modified: branches/devel-api/plugins/additional_message_headers/additional_message_headers.php
===================================================================
--- branches/devel-api/plugins/additional_message_headers/additional_message_headers.php	2009-03-17 19:21:48 UTC (rev 2357)
+++ branches/devel-api/plugins/additional_message_headers/additional_message_headers.php	2009-03-17 21:40:43 UTC (rev 2358)
@@ -22,7 +22,8 @@
  */
 class additional_message_headers extends rcube_plugin
 {
-
+    public $task = 'mail';
+    
     function init()
     {
         $this->add_hook('outgoing_message_headers', array($this, 'message_headers'));

Added: branches/devel-api/plugins/show_additional_headers/show_additional_headers.php
===================================================================
--- branches/devel-api/plugins/show_additional_headers/show_additional_headers.php	                        (rev 0)
+++ branches/devel-api/plugins/show_additional_headers/show_additional_headers.php	2009-03-17 21:40:43 UTC (rev 2358)
@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * Show additional message headers
+ *
+ * Proof-of-concept plugin which will fetch additional headers
+ * and display them in the message view.
+ *
+ * Enable the plugin in config/main.inc.php and add your desired headers:
+ *   $rcmail_config['show_additional_headers'] = array('User-Agent');
+ *
+ * @version 1.0
+ * @author Thomas Bruederli
+ * @website http://roundcube.net
+ */
+class show_additional_headers extends rcube_plugin
+{
+  public $task = 'mail';
+  
+  function init()
+  {
+    $rcmail = rcmail::get_instance();
+    if ($rcmail->action == 'show' || $rcmail->action == 'preview') {
+      $this->add_hook('imap_init', array($this, 'imap_init'));
+      $this->add_hook('message_headers_output', array($this, 'message_headers'));
+    }
+  }
+  
+  function imap_init($p)
+  {
+    $rcmail = rcmail::get_instance();
+    if ($add_headers = $rcmail->config->get('show_additional_headers', array()))
+      $p['fetch_headers'] = trim($p['fetch_headers'].' ' . strtoupper(join(' ', $add_headers)));
+
+    return $p;
+  }
+
+  function message_headers($p)
+  {
+console($p['headers']);
+    $rcmail = rcmail::get_instance();
+    foreach ($rcmail->config->get('show_additional_headers', array()) as $header) {
+      $key = strtolower($header);
+      if ($value = $p['headers']->others[$key])
+        $p['output'][$key] = array('title' => $header, 'value' => $value);
+    }
+
+    return $p;
+  }
+}

Modified: branches/devel-api/program/include/rcube_imap.php
===================================================================
--- branches/devel-api/program/include/rcube_imap.php	2009-03-17 19:21:48 UTC (rev 2357)
+++ branches/devel-api/program/include/rcube_imap.php	2009-03-17 21:40:43 UTC (rev 2358)
@@ -54,6 +54,7 @@
   var $default_charset = 'ISO-8859-1';
   var $default_folders = array('INBOX');
   var $default_folders_lc = array('inbox');
+  var $fetch_add_headers = '';
   var $cache = array();
   var $cache_keys = array();  
   var $cache_changes = array();
@@ -781,7 +782,7 @@
     $cache_index = $this->get_message_cache_index($cache_key);
     
     // fetch reuested headers from server
-    $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs);
+    $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs, false, $this->fetch_add_headers);
     $deleted_count = 0;
     
     if (!empty($a_header_index))
@@ -835,14 +836,14 @@
         if ($this->sort_field && $this->search_sort_field != $this->sort_field)
           $this->search('', $this->search_string, $this->search_charset, $this->sort_field);
 
-	if ($this->sort_order == 'DESC')
+        if ($this->sort_order == 'DESC')
           $this->cache[$key] = array_reverse($this->search_set);
-	else
-	  $this->cache[$key] = $this->search_set;
+        else
+          $this->cache[$key] = $this->search_set;
         }
       else
         {
-	$a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, join(',', $this->search_set), $this->sort_field);
+        $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, join(',', $this->search_set), $this->sort_field, false);
 
         if ($this->sort_order=="ASC")
           asort($a_index);
@@ -929,7 +930,7 @@
         
 
       // fetch complete headers and add to cache
-      $headers = iil_C_FetchHeader($this->conn, $mailbox, $id);
+      $headers = iil_C_FetchHeader($this->conn, $mailbox, $id, false, $this->fetch_add_headers);
       $this->add_message_cache($cache_key, $headers->id, $headers);
       }
 
@@ -1066,7 +1067,7 @@
     if ($uid && ($headers = &$this->get_cached_message($mailbox.'.msg', $uid)))
       return $headers;
 
-    $headers = iil_C_FetchHeader($this->conn, $mailbox, $id, $is_uid);
+    $headers = iil_C_FetchHeader($this->conn, $mailbox, $id, $is_uid, $this->fetch_add_headers);
 
     // write headers cache
     if ($headers)
@@ -2223,7 +2224,7 @@
     if ($cache_count==$msg_count)
       {
       // get highest index
-      $header = iil_C_FetchHeader($this->conn, $mailbox, "$msg_count");
+      $header = iil_C_FetchHeader($this->conn, $mailbox, "$msg_count", false, $this->fetch_add_headers);
       $cache_uid = array_pop($cache_index);
       
       // uids of highest message matches -> cache seems OK
@@ -2273,7 +2274,7 @@
         
         // featch headers if unserialize failed
         if (empty($this->cache[$cache_key][$uid]))
-          $this->cache[$cache_key][$uid] = iil_C_FetchHeader($this->conn, preg_replace('/.msg$/', '', $key), $uid, true);
+          $this->cache[$cache_key][$uid] = iil_C_FetchHeader($this->conn, preg_replace('/.msg$/', '', $key), $uid, true, $this->fetch_add_headers);
         }
       }
       

Modified: branches/devel-api/program/include/rcube_plugin_api.php
===================================================================
--- branches/devel-api/program/include/rcube_plugin_api.php	2009-03-17 19:21:48 UTC (rev 2357)
+++ branches/devel-api/program/include/rcube_plugin_api.php	2009-03-17 21:40:43 UTC (rev 2358)
@@ -32,7 +32,7 @@
   public $url = 'plugins/';
   public $output;
   
-  private $handlers = array();
+  public $handlers = array();
   private $plugins = array();
   private $actions = array();
   private $actionmap = array();
@@ -142,6 +142,15 @@
     $this->register_hook('template_container', array($this, 'template_container_hook'));
     
     // maybe also register a shudown function which triggers shutdown functions of all plugin objects
+    
+    
+    // call imap_init right now
+    // (should actually be done in rcmail::imap_init() but plugins are not initialized then)
+    if ($rcmail->imap) {
+      $hook = $this->exec_hook('imap_init', array('fetch_headers' => $rcmail->imap->fetch_add_headers));
+      if ($hook['fetch_headers'])
+        $rcmail->imap->fetch_add_headers = $hook['fetch_headers'];
+    }
   }
   
   

Modified: branches/devel-api/program/lib/imap.inc
===================================================================
--- branches/devel-api/program/lib/imap.inc	2009-03-17 19:21:48 UTC (rev 2357)
+++ branches/devel-api/program/lib/imap.inc	2009-03-17 21:40:43 UTC (rev 2358)
@@ -176,6 +176,7 @@
 	var $forwarded = false;
 	var $junk = false;
 	var $flagged = false;
+	var $others = array();
 }
 
 /**
@@ -1617,7 +1618,7 @@
 	return $t_index;
 }
 
-function iil_C_FetchHeaders(&$conn, $mailbox, $message_set, $uidfetch=false)
+function iil_C_FetchHeaders(&$conn, $mailbox, $message_set, $uidfetch=false, $add='')
 {
 	global $IMAP_USE_INTERNAL_DATE;
 	
@@ -1664,7 +1665,7 @@
 	$request .= "(UID RFC822.SIZE FLAGS INTERNALDATE BODY.PEEK[HEADER.FIELDS ";
 	$request .= "(DATE FROM TO SUBJECT REPLY-TO IN-REPLY-TO CC BCC ";
 	$request .= "CONTENT-TRANSFER-ENCODING CONTENT-TYPE MESSAGE-ID ";
-	$request .= "REFERENCES DISPOSITION-NOTIFICATION-TO X-PRIORITY)])";
+	$request .= "REFERENCES DISPOSITION-NOTIFICATION-TO X-PRIORITY ".strtoupper($add).")])";
 
 	if (!iil_PutLine($fp, $request)) {
 		return false;
@@ -1812,7 +1813,7 @@
 					list($field, $string) = iil_SplitHeaderLine($str);
 					
 					$field  = strtolower($field);
-                                        $string = ereg_replace("\n[[:space:]]*"," ",$string); 
+					$string = ereg_replace("\n[[:space:]]*"," ",$string);
 					
 					switch ($field) {
 					case 'date';
@@ -1870,6 +1871,10 @@
 						if (preg_match('/^(\d+)/', $string, $matches))
 							$result[$id]->priority = intval($matches[1]);
 						break;
+					default:
+						if (strlen($field) > 2)
+							$result[$id]->others[$field] = $string;
+						break;
 					} // end switch ()
 				} // end while ()
 		
@@ -1887,9 +1892,9 @@
 	return $result;
 }
 
-function iil_C_FetchHeader(&$conn, $mailbox, $id, $uidfetch=false) {
+function iil_C_FetchHeader(&$conn, $mailbox, $id, $uidfetch=false, $add='') {
 
-	$a  = iil_C_FetchHeaders($conn, $mailbox, $id, $uidfetch);
+	$a  = iil_C_FetchHeaders($conn, $mailbox, $id, $uidfetch, $add);
 	if (is_array($a)) {
 		return array_shift($a);
 	}

Modified: branches/devel-api/program/steps/mail/func.inc
===================================================================
--- branches/devel-api/program/steps/mail/func.inc	2009-03-17 19:21:48 UTC (rev 2357)
+++ branches/devel-api/program/steps/mail/func.inc	2009-03-17 21:40:43 UTC (rev 2358)
@@ -814,7 +814,7 @@
  */
 function rcmail_message_headers($attrib, $headers=NULL)
   {
-  global $IMAP, $OUTPUT, $MESSAGE, $PRINT_MODE, $CONFIG;
+  global $IMAP, $OUTPUT, $MESSAGE, $PRINT_MODE, $RCMAIL;
   static $sa_attrib;
   
   // keep header table attrib
@@ -823,7 +823,6 @@
   else if (!is_array($attrib) && is_array($sa_attrib))
     $attrib = $sa_attrib;
   
-  
   if (!isset($MESSAGE))
     return FALSE;
 
@@ -831,58 +830,55 @@
   if (!$headers)
     $headers = is_object($MESSAGE->headers) ? get_object_vars($MESSAGE->headers) : $MESSAGE->headers;
     
-  $header_count = 0;
-  
-  // allow the following attributes to be added to the <table> tag
-  $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
-  $out = '<table' . $attrib_str . ">\n";
-
   // show these headers
   $standard_headers = array('subject', 'from', 'to', 'cc', 'bcc', 'replyto', 'date');
+  $output_headers = array();
 
-  foreach ($standard_headers as $hkey)
-    {
+  foreach ($standard_headers as $hkey) {
     if (!$headers[$hkey])
       continue;
 
-    if ($hkey == 'date')
-      {
+    if ($hkey == 'date') {
       if ($PRINT_MODE)
-        $header_value = format_date($headers[$hkey], $CONFIG['date_long'] ? $CONFIG['date_long'] : 'x');
+        $header_value = format_date($headers[$hkey], $RCMAIL->config->get('date_long', 'x'));
       else
         $header_value = format_date($headers[$hkey]);
-      }
-    else if ($hkey == 'replyto')
-      {
+    }
+    else if ($hkey == 'replyto') {
       if ($headers['replyto'] != $headers['from'])
-        $header_value = Q(rcmail_address_string($headers['replyto'], null, true, $attrib['addicon']), 'show');
+        $header_value = rcmail_address_string($headers['replyto'], null, true, $attrib['addicon']);
       else
         continue;
-      }
+    }
     else if (in_array($hkey, array('from', 'to', 'cc', 'bcc')))
-      $header_value = Q(rcmail_address_string($headers[$hkey], null, true, $attrib['addicon']), 'show');
+      $header_value = rcmail_address_string($headers[$hkey], null, true, $attrib['addicon']);
     else if ($hkey == 'subject' && empty($headers[$hkey]))
-      $header_value = Q(rcube_label('nosubject'));
+      $header_value = rcube_label('nosubject');
     else
-      $header_value = Q(trim($IMAP->decode_header($headers[$hkey])));
+      $header_value = trim($IMAP->decode_header($headers[$hkey]));
+      
+    $output_headers[$hkey] = array('title' => rcube_label($hkey), 'value' => $header_value, 'raw' => $headers[$hkey]);
+  }
+    
+  $plugin = $RCMAIL->plugins->exec_hook('message_headers_output', array('output' => $output_headers, 'headers' => $MESSAGE->headers));
+  
+  // compose html table
+  $table = new html_table(array('cols' => 2));
+  
+  foreach ($plugin['output'] as $hkey => $row) {
+    $table->add(array('class' => 'header-title'), Q($row['title']));
+    $table->add(array('class' => $hkey, 'width' => "90%"), Q($row['value'], ($hkey == 'subject' ? 'strict' : 'show')));
+  }
 
-    $out .= "\n<tr>\n";
-    $out .= '<td class="header-title">'.Q(rcube_label($hkey)).": </td>\n";
-    $out .= '<td class="'.$hkey.'" width="90%">'.$header_value."</td>\n</tr>";
-    $header_count++;
-    }
-
   // all headers division
-  $out .= "\n".'<tr><td colspan="2" class="more-headers show-headers"
-	onclick="return '.JS_OBJECT_NAME.'.command(\'load-headers\', \'\', this)"></td></tr>';
-  $out .= "\n".'<tr id="all-headers"><td colspan="2" class="all"><div id="headers-source"></div></td></tr>';
-
+  $table->add(array('colspan' => 2, 'class' => "more-headers show-headers", 'onclick' => "return ".JS_OBJECT_NAME.".command('load-headers','',this)"), '');
+  $table->add_row(array('id' => "all-headers"));
+  $table->add(array('colspan' => 2, 'class' => "all"), html::div(array('id' => 'headers-sour