Index: wp-includes/functions-post.php
===================================================================
--- wp-includes/functions-post.php	(revision 4663)
+++ wp-includes/functions-post.php	(working copy)
@@ -386,6 +386,29 @@
 	return $post;
 }
 
+function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
+	$post_id = (int) $post_id;
+
+	$data = get_post_meta( $post_id, '_wp_attachment_metadata', true );
+	if ( $unfiltered )
+		return $data;
+	return apply_filters( 'wp_get_attachment_metadata', $data, $post_id );
+}
+
+function wp_update_attachment_metadata( $post_id, $data ) {
+	if ( !get_post( $post_id ) )
+		return false;
+
+	$old_data = wp_get_attachment_metadata( $post_id, true );
+
+	$data = apply_filters( 'wp_update_attachment_metadata', $data, $post_id );
+
+	if ( $old_data )
+		return update_post_meta( $post_id, '_wp_attachment_metadata', $data, $old_data );
+	else
+		return add_post_meta( $post_id, '_wp_attachment_metadata', $data );
+}
+
 function wp_get_single_post($postid = 0, $mode = OBJECT) {
 	global $wpdb;
 
Index: xmlrpc.php
===================================================================
--- xmlrpc.php	(revision 4663)
+++ xmlrpc.php	(working copy)
@@ -32,6 +32,7 @@
 exit;
 }
 
+include_once(ABSPATH . 'wp-admin/admin-functions.php');
 include_once(ABSPATH . WPINC . '/class-IXR.php');
 
 // Turn off all warnings and errors.
@@ -396,6 +397,8 @@
 	    return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.');
 	  }
 
+ 	  $this->attach_uploads( $post_ID, $post_content );
+
 	  logIO('O', "Posted ! ID: $post_ID");
 
 	  return $post_ID;
@@ -445,6 +448,8 @@
 	  	return new IXR_Error(500, 'For some strange yet very annoying reason, this post could not be edited.');
 	  }
 
+	  $this->attach_uploads( $ID, $post_content );
+
 	  return true;
 	}
 
@@ -564,12 +569,13 @@
 	    return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.');
 	  }
 
+	  $this->attach_uploads( $post_ID, $post_content );
+
 	  logIO('O', "Posted ! ID: $post_ID");
 
 	  return strval($post_ID);
 	}
 
-
 	/* metaweblog.editPost ...edits a post */
 	function mw_editPost($args) {
 
@@ -644,6 +650,8 @@
 	    return new IXR_Error(500, 'Sorry, your entry could not be edited. Something wrong happened.');
 	  }
 
+	  $this->attach_uploads( $ID, $post_content );
+
 	  logIO('O',"(MW) Edited ! ID: $post_ID");
 
 	  return true;
@@ -833,15 +841,44 @@
 			return $this->error;
 		}
 
+		if ( $upload_err = apply_filters( "pre_upload_error", false ) )
+			return new IXR_Error(500, $upload_err);
+
 		$upload = wp_upload_bits($name, $type, $bits);
 		if ( ! empty($upload['error']) ) {
 			logIO('O', '(MW) Could not write file '.$name);
 			return new IXR_Error(500, 'Could not write file '.$name);
 		}
-		
-		return array('url' => $upload['url']);
+		// Construct the attachment array
+		// attach to post_id -1
+		$post_id = -1;
+		$attachment = array(
+			'post_title' => $name,
+			'post_content' => '',
+			'post_status' => 'attachment',
+			'post_parent' => $post_id,
+			'post_mime_type' => $type,
+			'guid' => $upload[ 'url' ]
+		);
+		// Save the data
+		$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
+		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
+
+		return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );
 	}
 
+	function attach_uploads( $post_ID, $post_content ) {
+	  global $wpdb;
+	  // find any unattached files
+	  $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '-1' AND post_status = 'attachment'" );
+	  if( is_array( $attachments ) ) {
+	    foreach( $attachments as $file ) {
+	      if( strpos( $post_content, $file->guid ) !== false ) {
+		$wpdb->query( "UPDATE {$wpdb->posts} SET post_parent = '$post_ID' WHERE ID = '{$file->ID}'" );
+	      }
+	    }
+	  }
+	}
 
 	/* MovableType API functions
 	 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
@@ -1215,8 +1252,8 @@
 
 		$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_content', 'comment_type');
 
-		wp_new_comment($commentdata);
-		do_action('pingback_post', $wpdb->insert_id);
+		$comment_ID = wp_new_comment($commentdata);
+		do_action('pingback_post', $comment_ID);
 		
 		return "Pingback from $pagelinkedfrom to $pagelinkedto registered. Keep the web talking! :-)";
 	}
Index: wp-admin/inline-uploading.php
===================================================================
--- wp-admin/inline-uploading.php	(revision 4663)
+++ wp-admin/inline-uploading.php	(working copy)
@@ -74,36 +74,8 @@
 
 // Save the data
 $id = wp_insert_attachment($attachment, $file, $post);
+wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
 
-if ( preg_match('!^image/!', $attachment['post_mime_type']) ) {
-	// Generate the attachment's postmeta.
-	$imagesize = getimagesize($file);
-	$imagedata['width'] = $imagesize['0'];
-	$imagedata['height'] = $imagesize['1'];
-	list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
-	$imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
-	$imagedata['file'] = $file;
-
-	add_post_meta($id, '_wp_attachment_metadata', $imagedata);
-
-	if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
-		if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
-			$thumb = wp_create_thumbnail($file, 128);
-		elseif ( $imagedata['height'] > 96 )
-			$thumb = wp_create_thumbnail($file, 96);
-
-		if ( @file_exists($thumb) ) {
-			$newdata = $imagedata;
-			$newdata['thumb'] = basename($thumb);
-			update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
-		} else {
-			$error = $thumb;
-		}
-	}
-} else {
-	add_post_meta($id, '_wp_attachment_metadata', array());
-}
-
 wp_redirect(basename(__FILE__) . "?post=$post&all=$all&action=view&start=0");
 die();
 
Index: wp-admin/admin-functions.php
===================================================================
--- wp-admin/admin-functions.php	(revision 4663)
+++ wp-admin/admin-functions.php	(working copy)
@@ -1888,4 +1888,29 @@
 		return array((int) ($width / $height * 96), 96);
 }
 
+function wp_generate_attachment_metadata( $attachment_id, $file ) {
+	$attachment = get_post( $attachment_id );
+
+	$metadata = array();
+	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
+		$imagesize = getimagesize($file);
+		$metadata['width'] = $imagesize['0'];
+		$metadata['height'] = $imagesize['1'];
+		list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']);
+		$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
+		$metadata['file'] = $file;
+
+		if ( $metadata['width'] * $metadata['height'] < 3 * 1024 * 1024 ) {
+			if ( $metadata['width'] > 128 && $metadata['width'] >= $metadata['height'] * 4 / 3 )
+				$thumb = wp_create_thumbnail($file, 128);
+			elseif ( $metadata['height'] > 96 )
+				$thumb = wp_create_thumbnail($file, 96);
+
+			if ( @file_exists($thumb) )
+				$metadata['thumb'] = basename($thumb);
+		}
+	}
+	return apply_filters( 'wp_generate_attachment_metadata', $metadata );
+}
+
 ?>
