Happy New Year
[squirrelmail.git] / plugins / listcommands / functions.php
index 7028ed2f010e591986aeaf1e82b7c56d0de76ba6..9ea9055c30aa221e43fc3f3772f01e50e3f24fba 100644 (file)
@@ -3,28 +3,77 @@
 /**
  * functions.php
  *
- * Copyright (c) 1999-2005 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
  * Implementation of RFC 2369 for SquirrelMail.
  * When viewing a message from a mailinglist complying with this RFC,
  * this plugin displays a menu which gives the user a choice of mailinglist
  * commands such as (un)subscribe, help and list archives.
  *
+ * @copyright 1999-2018 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package plugins
  * @subpackage listcommands
  */
 
+/**
+  * Get current list of subscribed non-RFC-compliant mailing lists for logged-in user
+  *
+  * @return array The list of mailing list addresses, keyed by integer index
+  */
+function get_non_rfc_lists() {
+    global $username, $data_dir;
+    $lists = getPref($data_dir, $username, 'non_rfc_lists', array());
+    $new_lists = array();
+    if (!empty($lists)) {
+        $lists = explode(':', $lists);
+        foreach ($lists as $list) {
+            list($index, $list_addr) = explode('_', $list);
+            if ((!empty($index) || $index === '0') && !empty($list_addr))
+                $new_lists[$index] = $list_addr;
+        }
+    }
+    $lists = $new_lists;
+    sort($lists);
+    return $lists;
+}
+
+/**
+  * Show mailing list management option section on options page
+  */
+function plugin_listcommands_optpage_register_block_do()
+{
+    global $optpage_blocks, $listcommands_allow_non_rfc_list_management;
+
+    // only allow management of non-RFC lists if admin deems necessary
+    //
+    @include_once(SM_PATH . 'plugins/listcommands/config.php');
+    if (!$listcommands_allow_non_rfc_list_management)
+        return;
+
+    $optpage_blocks[] = array(
+        'name' => _("Mailing Lists"),
+        'url'  => '../plugins/listcommands/options.php',
+        'desc' => _("Manage the (non-RFC-compliant) mailing lists that you are subscribed to for the purpose of providing one-click list replies when responding to list messages."),
+        'js'   => false
+    );
+
+}
+
+/**
+ * internal function that builds mailing list links
+ */
 function plugin_listcommands_menu_do() {
-    global $passed_id, $passed_ent_id, $color, $mailbox, $message, $startMessage;
+    global $passed_id, $passed_ent_id, $mailbox, $message, 
+           $startMessage, $oTemplate, $listcommands_allow_non_rfc_list_management;
+
+    @include_once(SM_PATH . 'plugins/listcommands/config.php');
 
     /**
      * Array of commands we can deal with from the header. The Reply option
      * is added later because we generate it using the Post information.
      */
     $fieldsdescr = listcommands_fieldsdescr();
-    $output = array();
+    $links = array();
 
     foreach ($message->rfc822_header->mlist as $cmd => $actions) {
 
@@ -34,20 +83,51 @@ function plugin_listcommands_menu_do() {
         }
 
         /* proto = {mailto,href} */
-        $proto = array_shift(array_keys($actions));
+        $aActions = array_keys($actions);
+        // note that we only use the first cmd/action, ignore the rest
+        $proto = array_shift($aActions);
         $act   = array_shift($actions);
 
         if ($proto == 'mailto') {
 
+            $identity = '';
+
             if (($cmd == 'post') || ($cmd == 'owner')) {
                 $url = 'src/compose.php?'.
                     (isset($startMessage)?'startMessage='.$startMessage.'&':'');
             } else {
                 $url = "plugins/listcommands/mailout.php?action=$cmd&";
+
+                // try to find which identity the mail should come from
+                include_once(SM_PATH . 'functions/identity.php');
+                $idents = get_identities();
+                // ripped from src/compose.php
+                $identities = array();
+                if (count($idents) > 1) {
+                    foreach($idents as $nr=>$data) {
+                        $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
+                        $identities[] = $enc_from_name;
+                    }
+
+                    $identity_match = $message->rfc822_header->findAddress($identities);
+                    if ($identity_match !== FALSE) {
+                        $identity = $identity_match;
+                    }
+                }
             }
-            $url .= 'send_to=' . strtr($act,'?','&');
 
-            $output[] = makeComposeLink($url, $fieldsdescr[$cmd]);
+            // if things like subject are given, peel them off and give
+            // them to src/compose.php as is (not encoded)
+            if (strpos($act, '?') > 0) {
+               list($act, $parameters) = explode('?', $act, 2);
+               $parameters = '&amp;identity=' . $identity . '&amp;' . $parameters;
+            } else {
+               $parameters = '&amp;identity=' . $identity;
+            }
+
+            $url .= 'send_to=' . urlencode($act) . $parameters;
+
+            $links[$cmd] = makeComposeLink($url, $fieldsdescr[$cmd]);
 
             if ($cmd == 'post') {
                 if (!isset($mailbox))
@@ -57,22 +137,72 @@ function plugin_listcommands_menu_do() {
                     (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'');
                 $url .= '&amp;smaction=reply';
 
-                $output[] = makeComposeLink($url, $fieldsdescr['reply']);
+                $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
             }
         } else if ($proto == 'href') {
-            $output[] = '<a href="' . $act . '" target="_blank">'
-                . $fieldsdescr[$cmd] . '</a>';
+            $links[$cmd] = create_hyperlink($act, $fieldsdescr[$cmd], '_blank');
         }
     }
 
-    if (count($output) > 0) {
-        echo '<tr>' .
-            html_tag('td', '<b>' . _("Mailing List") . ':&nbsp;&nbsp;</b>',
-                    'right', '', 'valign="middle" width="20%"') . "\n" .
-            html_tag('td', '<small>' . implode('&nbsp;|&nbsp;', $output) . '</small>',
-                    'left', $color[0], 'valign="middle" width="80%"') . "\n" .
-            '</tr>';
+
+    // allow non-rfc reply link if admin allows and message is from 
+    // non-rfc list the user has configured
+    //
+    if ($listcommands_allow_non_rfc_list_management) {
+
+        $non_rfc_lists = get_non_rfc_lists();
+
+        $recipients = formatRecipientString($message->rfc822_header->to, "to") . ' '
+                    . formatRecipientString($message->rfc822_header->cc, "cc") . ' '
+                    . formatRecipientString($message->rfc822_header->bcc, "bcc");
+
+        if (!in_array('post', array_keys($links))) {
+
+            foreach ($non_rfc_lists as $non_rfc_list) {
+                if (preg_match('/(^|,|<|\s)' . preg_quote($non_rfc_list) . '($|,|>|\s)/', $recipients)) {
+                    $url = 'src/compose.php?'
+                         . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
+                         . 'send_to=' . str_replace('?','&amp;', $non_rfc_list);
+
+                    $links['post'] = makeComposeLink($url, $fieldsdescr['post']);
+
+                    break;
+                }
+            }
+
+        }
+
+        if (!in_array('reply', array_keys($links))) {
+
+            foreach ($non_rfc_lists as $non_rfc_list) {
+                if (preg_match('/(^|,|\s)' . preg_quote($non_rfc_list) . '($|,|\s)/', $recipients)) {
+                    if (!isset($mailbox))
+                        $mailbox = 'INBOX';
+                    $url = 'src/compose.php?'
+                         . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
+                         . 'send_to=' . str_replace('?','&amp;', $non_rfc_list)
+                         . '&amp;passed_id='.$passed_id
+                         . '&amp;mailbox='.urlencode($mailbox)
+                         . (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'')
+                         . '&amp;smaction=reply';
+
+                    $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
+
+                    break;
+                }
+            }
+
+        }
+
     }
+
+
+    if (count($links) > 0) {
+        $oTemplate->assign('links', $links);
+        $output = $oTemplate->fetch('plugins/listcommands/read_body_header.tpl');
+        return array('read_body_header' => $output);
+    }
+
 }
 
 /**
@@ -89,4 +219,3 @@ function listcommands_fieldsdescr() {
             'help'        => _("Help"));
 }
 
-?>
\ No newline at end of file