Index: simplenews.module
===================================================================
--- simplenews.module	(revision 1019)
+++ simplenews.module	(working copy)
@@ -217,7 +217,7 @@
      * Always load simplenews_views.inc when views module is present
      */
     if (module_exists('views')) {
-      include_once(base_path() . drupal_get_path('module', 'simplenews') .'/simplenews_views.inc');
+      include_once(drupal_get_path('module', 'simplenews') .'/simplenews_views.inc');
     }
   }
   return $items;
@@ -521,9 +521,20 @@
   switch ($op) {
     case 'insert':
       if ($edit['mail']) {
-        $query = "SELECT snid FROM {simplenews_subscriptions} WHERE mail = '%s'";
-        if ($result = db_fetch_object(db_query($query, $edit['mail']))) {
-          db_query("UPDATE {simplenews_subscriptions} SET uid = %d WHERE snid = %d", $edit['uid'], $result->snid);
+        $mail = $edit['mail'];
+        
+        // Update existing anonymous subscription (if there is one)
+        // for this new user's email address to be for this user.  Do
+        // this before adding any registration subscriptions so we do
+        // not end up with multiple rows per email address.
+        db_query("UPDATE {simplenews_subscriptions} SET uid=%d WHERE uid=0 AND mail='%s'", $edit['uid'], $edit['mail']);
+
+        // Create subscriptions selected during registration.
+        if (isset($edit['newsletters']) && is_array($edit['newsletters'])) {
+          unset($edit['newsletters'][0]); // Remove key 0. This is not a valid newsletter
+          foreach (array_keys(array_filter($edit['newsletters'])) as $tid) {
+            simplenews_subscribe_user($mail, $tid, FALSE);
+          }
         }
       }
       break;
@@ -597,6 +608,33 @@
         return array(t('Newsletters') => $items);
       }
       break;
+    case 'register':
+      if (variable_get('simplenews_user_subscription', 0) && user_access('subscribe to newsletters')) {
+      $tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
+      if ($tree) {
+        $default = variable_get('simplenews_user_subscription_default', array());
+        $form['simplenews'] = array(
+          '#type' => 'fieldset',
+          '#title' => t('Newsletters'),
+          '#weight' => 1,
+        );
+        foreach( $tree as $newsletter ) {
+          $options[$newsletter->tid] = $newsletter->name;
+        }
+        $default = variable_get('simplenews_user_subscription_default', array());
+        // Remove 'nothing selected' flag
+        unset($default[0]);
+        $form['simplenews']['newsletters'] = array(
+          '#type' => 'checkboxes',
+          '#title' => t('Subscribe to these newsletters'),
+          '#default_value' => $default,
+          '#options' => $options,
+          '#description' => t('You always have the possibility to unsubscribe later.'),
+        );
+      }
+      return $form;
+      }
+      break;
   }
 }
 
@@ -1155,7 +1193,9 @@
     $term = taxonomy_get_term($nid->tid);
     $node = simplenews_node_prepare($nid->nid, $nid->tid);
 
-    $result2 = db_query('SELECT s.mail, s.snid FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid WHERE s.s_status = %d AND s.a_status = %d AND t.tid = %d ORDER BY s.snid ASC', 0, 1, $nid->tid);
+    // Select only users who have responded to simplenews confirmation mail,
+    // or are known to be active users.
+    $result2 = db_query('SELECT s.mail, s.snid FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid LEFT JOIN {users} u ON s.uid=u.uid WHERE s.s_status = %d AND s.a_status = %d AND t.tid = %d AND (u.uid IS NULL OR u.access != 0) ORDER BY s.snid ASC', 0, 1, $nid->tid);
     while ($mail = db_fetch_object($result2)) {
       $hash = _simplenews_generate_hash($mail->mail, $mail->snid, $nid->tid);
 
@@ -2184,6 +2224,27 @@
       '#options' => array(0 => t("Don't send now"), 2 => t('Send one test newsletter to the test address'), 1 => t('Send newsletter')),
       '#default_value' => variable_get('simplenews_send', 0),
     );
+    $form['simplenews_subscription_options'] = array('#type' => 'fieldset',
+      '#title' => t('Default subscription options'),
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+      '#description' => t('This is the default newsletter where a new user is subscribed to.'),
+    );
+    $form['simplenews_subscription_options']['simplenews_user_subscription'] = array('#type' => 'checkbox',
+      '#title' => t('Allow new users to subscribe to a newsletter on the subscription page. Anonymous user access is required for "subscribe to newsletters".'),
+      '#return_value' => 1,
+      '#default_value' => variable_get('simplenews_user_subscription', 0),
+    );
+    foreach (taxonomy_get_tree(variable_get('simplenews_vid', '')) as $newsletter) {
+      $options[$newsletter->tid] = $newsletter->name;
+    }
+    $default = variable_get('simplenews_user_subscription_default', array());
+    unset($default[0]); //Remove 'nothing selected' flag.
+    $form['simplenews_subscription_options']['simplenews_user_subscription_default'] = array('#type' => 'checkboxes',
+     '#title' => t('Default subscription newsletter'),
+     '#default_value' => $default,
+     '#options' => $options,
+    );
     $form['simplenews_test_address'] = array('#type' => 'fieldset',
       '#title' => t('Test addresses options'),
       '#collapsible' => TRUE,
