--- listhandler.module.orig	2009-04-03 13:28:11.000000000 -0400
+++ listhandler.module	2009-04-03 13:49:40.000000000 -0400
@@ -66,6 +66,7 @@
  */
 function listhandler_admin_settings() {
   $form['#submit'] = array('listhandler_admin_settings_submit' => array());
+
   $form['listhandler_from'] = array(
     '#type' => 'textfield',
     '#title' => t("'Anonymous user' email address"),
@@ -85,6 +86,11 @@
     '#title' => t('Attachments as link'),
     '#default_value' => variable_get('listhandler_attachments_as_link', 0),
     '#description' => t('Send attachments as links instead of MIME attachments. It only affects mails generated by forum posts.'));
+ 	$form['listhandler_post_mail_attachments'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Post mail attachments'),
+    '#default_value' => variable_get('listhandler_post_mail_attachments', 0),
+    '#description' => t('If an incoming email has an attachment, check this box to have the attachment posted to the forum.')); 
   $form['listhandler_accountstatus'] = array(
     '#type' => 'select',
     '#title' => t('Account status'),
@@ -424,7 +430,10 @@
         $node->title = truncate_utf8(decode_entities(strip_tags(check_markup($node->body, 1))), 29, TRUE);
       }
       $node = listhandler_find_parent($node, $header);
-
+			if(variable_get('listhandler_post_mail_attachments',0)) {
+				$files = listhandler_get_files_for_mimeparts($node->mimeparts);
+				if(count($files > 0)) listhandler_attach_files_to_node($node, $files); 
+			}
       $node->message_id = $header->message_id;
       $node->sentbylisthandler = true;
 
@@ -607,3 +616,76 @@
   $tests = file_scan_directory($dir, '\.test$');
   return array_keys($tests);
 }
+
+
+/**
+ * mailhandler will create node->mimeparts with all the
+ * mimeparts of the email message. This function saves the
+ * file mime part and  returns an array 
+ * acceptable to be added as $node->files.
+ */
+function listhandler_get_files_for_mimeparts($mimeparts) {
+	$ret = array();
+	$allowed_types = listhandler_get_allowed_mimetypes();
+	while(list(,$part) = each($mimeparts)) {
+		if(!in_array(strtolower($part->filemime),$allowed_types)) {
+			watchdog('listhandler',"Ignoring dis-allowed mimetype: " . $part->filemime);
+			continue;
+		}
+		// Save the attachment as a file on the server filesystem
+		$temp = file_directory_temp();
+		$file = tempnam(realpath($temp), 'file');
+		if (!$fp = fopen($file, 'wb')) {
+			watchdog('listhandler',t('The attachment could not be created.'));
+			continue;
+		}
+		fwrite($fp, $part->data);
+		fclose($fp);
+		$ret[] = array(
+			'source' => "$file",
+			'list' => 1,
+			'filename' => $part->filename,
+			'filepath' => $file,
+			'filesize' => filesize($file),
+			'filemime' => $part->filemime,
+			'fid' => 'upload'
+		);
+	}
+	return $ret;
+}
+
+/**
+  * Return list of allowed mime types
+	*
+  * Fixme: should be user configurable
+  */
+
+function listhandler_get_allowed_mimetypes() {
+	return array(
+		'application/vnd.oasis.opendocument.text',
+		'application/vnd.oasis.opendocument.spreadsheet',
+		'application/vnd.oasis.opendocument.presentation',
+		'application/vnd.ms-excel',
+		'application/msword',
+		'application/vnd.ms-powerpoint',
+		'application/pdf',
+		'application/vnd.wordperfect',
+		'application/vnd.wordperfect5.1',
+	);
+}
+
+/**
+	* Attach files to either passed node  or parent node (if it's a comment)	
+	*
+ */
+function listhandler_attach_files_to_node(&$node, $files) {
+	if($node->type != 'comment') {
+		$node->files = $files;
+	} else {
+		$pnode = node_load($node->nid);
+		$pnode->files = array_merge($pnode->files,$files);
+		node_save($pnode);
+	}
+}
+
+
