From 7793c4da458137a4a8810cc7836a2b75b03da1b7 Mon Sep 17 00:00:00 2001
From: Jake Bell <jake@theunraveler.com>
Date: Mon, 23 Jan 2012 12:40:03 -0600
Subject: [PATCH 1/2] Decoupling mail address from server/website.

---
 femail.drush.inc |   15 ++++++++++++++-
 femail.install   |   19 +++++++++++++++++--
 femail.module    |   18 +++++++++++++++---
 femail.pages.inc |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 99 insertions(+), 7 deletions(-)

diff --git a/femail.drush.inc b/femail.drush.inc
index d9b988f..e3a9d08 100755
--- a/femail.drush.inc
+++ b/femail.drush.inc
@@ -224,7 +224,20 @@ function _femail_post_comment($msg, $user, $status){
 function _femail_post_forum_topic($msg, $user, $status){
   // Get the tid of the forum that this post will go into
   $emails = variable_get('femail_emails', array());
-  $tid = array_search($msg['to'], $emails);
+  foreach ($emails as $fid => $items) {
+    if (is_array($items) && array_search($msg['to'], $items) !== FALSE) {
+      $tid = $fid;
+      break;
+    }
+  }
+
+  // In case we couldn't find a proper forum.
+  if (!isset($tid)) {
+    watchdog('femail', t('Could not find a forum correspond to the e-mail address %email.', array(
+      '%email' => $msg['to'],
+    )));
+    return;
+  }
   $term = taxonomy_term_load($tid);
   $node = new stdClass();
   $node->uid = $user->uid;
diff --git a/femail.install b/femail.install
index b2b4aff..241c363 100755
--- a/femail.install
+++ b/femail.install
@@ -40,7 +40,7 @@ function femail_install(){
       }
       $email = $email . "_" . $i;
     }
-    $emails[$row->tid] = $email . '@' . $parts['host'];
+    $emails[$row->tid] = array($email . '@' . $parts['host']);
   }
   variable_set('femail_emails', $emails);
   // Set the install time for use in hashes
@@ -347,4 +347,19 @@ function femail_uninstall(){
   // Delete variables
   variable_del('femail_emails');
   variable_del('femail_install_time');
-}
\ No newline at end of file
+}
+
+/**
+ * Change femail_emails to be an associative array.
+ */
+function femail_update_7101() {
+  $emails = variable_get('femail_emails', array());
+
+  foreach ($emails as $fid => $email) {
+    if (!is_array($email)) {
+      $emails[$fid] = array($emails[$fid]);
+    }
+  }
+
+  variable_set('femail_emails', $emails);
+}
diff --git a/femail.module b/femail.module
index d6d2876..6170e9c 100755
--- a/femail.module
+++ b/femail.module
@@ -5,6 +5,7 @@
  * 
  * femail.module
  */
+
 /**
  * Implementation of hook_permission
  * 
@@ -48,7 +49,7 @@ function femail_menu_local_tasks_alter(&$data, $router_item, $root_path){
                   'title' => t('Add new @node_type by email', array(
                     '@node_type' => node_type_get_name($type)
                   )),
-                  'href' => 'mailto:' . $emails[$tid]
+                  'href' => 'mailto:' . $emails[$tid][0],
                 ),
                 '#weight' => 100
               );
@@ -111,7 +112,18 @@ function femail_menu(){
       ),
       'type' => MENU_CALLBACK,
       'file' => 'femail.pages.inc'
-    )
+    ),
+    'admin/structure/forum/femail' => array(
+      'title' => t('E-mail integration'),
+      'description' => t('Settings for forum/e-mail integration.'),
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('femail_settings_form'),
+      'access callback' => 'user_access',
+      'access arguments' => array('administer forums'),
+      'type' => MENU_LOCAL_TASK,
+      'file' => 'femail.pages.inc',
+      'weight' => 10,
+    ),
   );
 }
 
@@ -268,7 +280,7 @@ function femail_taxonomy_term_insert($term){
       $email = $email . "_" . $i;
     }
     // Set it and save it
-    $emails[$term->tid] = $email . '@' . $parts['host'];
+    $emails[$term->tid] = array($email . '@' . $parts['host']);
     variable_set('femail_emails', $emails);
   }
 }
diff --git a/femail.pages.inc b/femail.pages.inc
index 07536c2..5d5b341 100755
--- a/femail.pages.inc
+++ b/femail.pages.inc
@@ -243,4 +243,56 @@ function femail_user_add_validate($form, &$form_state){
   if($mails){
     form_set_error('femail_email', t('Email is already in use on this site.'));
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Page callback for the main settings form.
+ */
+function femail_settings_form($form, $form_state) {
+  $containers = variable_get('forum_containers', array());
+  $forum_vocab = variable_get('forum_nav_vocabulary', NULL);
+  $emails = variable_get('femail_emails', array());
+
+  $query = db_select('taxonomy_term_data', 't')
+    ->fields('t', array('tid', 'name'))
+    ->condition('vid', $forum_vocab);
+  if (!empty($containers)) {
+    $query->condition('tid', $containers, 'NOT IN');
+  }
+  $forums = $query->execute()->fetchAllKeyed();
+
+  $form['femail_emails'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Accepted emails'),
+    '#description' => t('Enter a list of valid email addresses for each forum, separated by commas.'),
+    '#tree' => TRUE,
+  );
+  foreach ($forums as $fid => $forum) {
+    $form['femail_emails'][$fid] = array(
+      '#type' => 'textfield',
+      '#title' => $forum,
+      '#default_value' => isset($emails[$fid]) ? implode(', ', $emails[$fid]) : '',
+    );
+  }
+
+  $form['actions']['#type'] = 'actions';
+  $form['actions']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save configuration'),
+  );
+  $form['#theme'] = 'system_settings_form';
+
+  return $form;
+}
+
+/**
+ * Submit callback for femail settings form.
+ */
+function femail_settings_form_submit($form, &$form_state) {
+  $emails = array();
+  foreach ($form_state['values']['femail_emails'] as $fid => $items) {
+    $items = str_replace(', ',',', $items);
+    $emails[$fid] = explode(',', $items);
+  }
+  variable_set('femail_emails', $emails);
+}
-- 
1.7.8.4


From cea2b3e38122b748dcc6e0c10f695b026876969a Mon Sep 17 00:00:00 2001
From: Jake Bell <jake@theunraveler.com>
Date: Mon, 23 Jan 2012 12:41:21 -0600
Subject: [PATCH 2/2] Fixing errors when sending mail.

---
 femail.mail.inc |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/femail.mail.inc b/femail.mail.inc
index bcea540..9eebf81 100755
--- a/femail.mail.inc
+++ b/femail.mail.inc
@@ -63,14 +63,14 @@ function _femail_send_message($tid, $subject, $body, $nid, $files = array(), $ci
       // How fucking ridiculous is this that we can no longer send a string as 
       // the body for a mail message!
       'body' => drupal_wrap_mail(strip_tags($body)),
-      'to' => $from_emails[$tid],
+      'to' => $from_emails[$tid][0],
       'headers' => array(
-        'Return-path' => $from_emails[$tid],
+        'Return-path' => $from_emails[$tid][0],
         'From' => $from,
         'Message-id' => $msgid,
-        'Reply-to' => $from_emails[$tid],
-        'List-id' => check_plain($forum_term->name) . ' <' . str_replace("@", ".", $from_emails[$tid]) . '>',
-        'List-post' => '<mailto:' . $from_emails[$tid] . '>',
+        'Reply-to' => $from_emails[$tid][0],
+        'List-id' => check_plain($forum_term->name) . ' <' . str_replace("@", ".", $from_emails[$tid][0]) . '>',
+        'List-post' => '<mailto:' . $from_emails[$tid][0] . '>',
         'List-archive' => '<' . url('forum/' . $tid, array(
           'absolute' => TRUE
         )) . '>',
@@ -87,4 +87,4 @@ function _femail_send_message($tid, $subject, $body, $nid, $files = array(), $ci
     $system = drupal_mail_system('femail', $msgid);
     $system->mail($message);
   }
-}
\ No newline at end of file
+}
-- 
1.7.8.4

