--- orig	2007-08-15 02:02:54.000000000 -0400
+++ new	2007-08-15 02:02:54.000000000 -0400
@@ -241,6 +241,19 @@ function privatemsg_cron() {
 }
 
 function _privatemsg_prune() {
+  /* If a user sends a private message, and then is deleted before the recipient has a chance to read or delete the message, then the recipient will receive the message "You have n new messages(s)" at the top of every page, with no recourse to deleting the unread message since it won't show up in the in box. The reason it won't show up is that the query that selects for new messages relies on the user existing (JOIN) whereas the queries that just checks for unread messages does not.
+  
+  This below added pruning task to be run during cron will check for message authors who no longer exist in the users table, and mark unread messages from such authors as read, and set them to eventually be archived. */
+  $result = db_query('SELECT DISTINCT author FROM {privatemsg} p LEFT JOIN users u ON p.author=u.uid WHERE u.uid IS NULL ORDER BY author DESC');
+  $deletedauthors = array();
+  while ($deleted = db_fetch_array($result)) {
+    $deletedauthors[] = $deleted['author'];
+  }
+  if (count($deletedauthors)>0) {
+    db_query('UPDATE {privatemsg} SET author_del=1, recipient_del=1, newmsg=0 WHERE author
+IN (%s)',implode(',',$deletedauthors));
+  }
+
   // move deleted message older than 1 month to archive table, and optimize table
   $result = db_query('SELECT * FROM {privatemsg} WHERE author_del = 1 AND recipient_del = 1 AND timestamp < %d', time() - 3600*24*30);
   while ($message = db_fetch_object($result)) {