==== //ccare/main/LiveJournal/htdocs/talkpost_do.bml#7 - /u0/jaffray/p4/LiveJournal/htdocs/talkpost_do.bml ====
--- /tmp/tmp.15385.0	Sat Jun  9 23:54:42 2001
+++ /u0/jaffray/p4/LiveJournal/htdocs/talkpost_do.bml	Sat Jun  9 23:51:52 2001
@@ -17,29 +17,38 @@
  require 'talklib.pl';
  require 'cleanhtml.pl';
 
- my $ret = "";
- my @errors = ();
+ # OK, we're going to get Pascal-ish and declare and comment lots of stuff
+ # up top, since this is a mess.
 
  my $DEBUG = $FORM{'debug'} ? 1 : 0;
+ my $rows;           # temporary variable to stick SELECT results in
+ my $ret = "";       # return value for this whole codeblock
+ my @errors = ();    # list of things wrong with submission - fatal if nonempty
+ my $item;           # hashref of data from log/logtext on the root item of this comment
+ my %logprops;       # log properties (music, nocomments, etc) associated with this item 
+ my $u;              # user hashref for journal
+ my $ujp;            # user hashref for poster
+ my $up;             # user hashref for user trying to comment, if not anonymous
+ my ($paruser, $paruserid, $parbody);  # author and body of parent comment, if any
+ 
+ # quoted/sanitized values for use in SQL statements
+ my ($qitemid, $quser, $qjpuser, $qparenttalkid);
 
  # start quoting shit to insert
- my $qitemid = $FORM{'itemid'}+0;
+ $qitemid = $FORM{'itemid'}+0;
  $sth = $dbh->prepare("SELECT l.ownerid, l.posterid, lt.event, l.eventtime, lt.subject, l.security, l.allowmask FROM log l, logtext lt WHERE lt.itemid=l.itemid AND l.itemid=$qitemid");
  $sth->execute;
- my $item = $sth->fetchrow_hashref;
- unless ($item) { push @errors, "No such entry."; }
+ $item = $sth->fetchrow_hashref
+   or return "No such entry.";
 
- my %logprops;
  &load_log_props([ $qitemid ], \%logprops);
 
- my $mod = $qitemid % 100;
-
  ### load users
- $sth = $dbh->prepare("SELECT * FROM user WHERE userid IN ($item->{'ownerid'}, $item->{'posterid'})");
- $sth->execute;
- my ($u, $ujp);  ## user owner and user journal poster
- my ($quser, $qjpuser);
- while ($_ = $sth->fetchrow_hashref) {
+ $rows = $dbh->selectall_hashref("SELECT *    " .
+                                 "FROM   user " .
+                                 "WHERE  userid IN ($item->{ownerid}, $item->{posterid})")
+   or return $dbh->errstr;
+ foreach (@$rows) {
      if ($_->{'userid'} == $item->{'ownerid'})  { 
 	 $u  = $_; 
 	 $quser = $dbh->quote($u->{'user'});
@@ -71,7 +80,6 @@
 
  my $userpost = lc($FORM{'userpost'});
  my $quserpost = $dbh->quote("");
- my $up;
 
  if ($FORM{'usertype'} eq "user") {
      if ($FORM{'userpost'}) {
@@ -146,11 +154,13 @@
      push @errors, "Your message was blank.  Please type at least something in the message field.";
  }
 
- my $max_length = 2000;
- my $length = length($FORM{'body'});
- if ($userpost) { $max_length = 4000; }
- if ($length > $max_length) {
-     push @errors, "Sorry, but your comment of length $length exceeds the maximum allowed length of $max_length.  Please go back, shorten it, and try posting it again.";
+ {
+     my $max_length = 2000;
+     my $length = length($FORM{'body'});
+     if ($userpost) { $max_length = 4000; }
+     if ($length > $max_length) {
+         push @errors, "Sorry, but your comment of length $length exceeds the maximum allowed length of $max_length.  Please go back, shorten it, and try posting it again.";
+     }
  }
 
  if (@errors)
@@ -171,6 +181,7 @@
  &LJ::CleanHTML::clean_subject_all(\$textsubject);
  my $cleansubject = $FORM{'subject'};
  &LJ::CleanHTML::clean_subject(\$cleansubject);
+ my $itemsubject = ($item->{subject} ? "Re: $item->{subject}" : "");
 
  ## preview
 
@@ -284,16 +295,14 @@
 	 }
      }
 
-     # check to see if parent post is from a registerd livejournal user, and 
-     # mail them the response
+     # if we have a parent post, get info on that post, maybe email response to the author
      my $parentcomment = "";
      my $parentmailed = "";  # who if anybody was just mailed
-
      if ($FORM{'parenttalkid'}) 
      {	
-	 $sth = $dbh->prepare("SELECT u.user, tt.body FROM talk t, talktext tt, user u WHERE t.talkid=$qpartid AND tt.talkid=$qpartid AND u.userid=t.posterid");
+	 $sth = $dbh->prepare("SELECT u.user, u.userid, tt.body FROM talk t, talktext tt, user u WHERE t.talkid=$qpartid AND tt.talkid=$qpartid AND u.userid=t.posterid");
 	 $sth->execute;
-	 my ($paruser, $parbody) = $sth->fetchrow_array;
+	 ($paruser, $paruserid, $parbody) = $sth->fetchrow_array;
 	 $parentcomment = $parbody;
 	 if ($paruser) {
 	     my $qparuser = $dbh->quote($paruser);
@@ -310,10 +319,12 @@
 		 }
 		 $parentmailed = $paru->{'email'};
 
+                 my $x_livejournal = make_x_livejournal_header($paruserid);
 		 my $fromname = $up->{'user'} ? "$up->{'user'} - LJ Comment" : "LiveJournal Comment";
 		 my $msg =  new MIME::Lite ('From' => "$LJ::BOGUS_EMAIL ($fromname)",
 					    'To' => $paru->{'email'},
-					    'Subject' => ($textsubject || "Reply to your comment..."),
+					    'Subject' => ($textsubject || $itemsubject || "Reply to your comment..."),
+                                            'X-LiveJournal' => $x_livejournal,
 					    'Type' => 'multipart/alternative');
 		 
 		 my $text = "";
@@ -380,10 +391,12 @@
 	 if ($DEBUG) { 
 	     $debug .= "send mail to journal owner. (to $ujp->{'email'})<br>\n";
 	 }
+         my $x_livejournal = make_x_livejournal_header($ujp->{userid});
 	 my $fromname = $up->{'user'} ? "$up->{'user'} - LJ Comment" : "LiveJournal Comment";
 	 my $msg =  new MIME::Lite ('From' => "$LJ::BOGUS_EMAIL ($fromname)",
 				    'To' => $ujp->{'email'},
-				    'Subject' => ($textsubject || "Reply to your post..."),
+				    'Subject' => ($textsubject || $itemsubject || "Reply to your post..."),
+                                    'X-LiveJournal' => $x_livejournal,
 				    'Type' => 'multipart/alternative');
 	 my $text = "";
 	 if ($parentcomment)  {
@@ -469,16 +482,48 @@
  }
  return $ret;
 
+ # make the X-LiveJournal header that appears in email notifications.
+ # takes userid as an argument; we need this to find friend relationships.
+ # example result: "itemid=133 talkid=38 journal=lusers:9 postby=joe commentby=jane:1"
+ # in this case, joe replied (talkid 38) to a post by jane (itemid 133) in lusers.
+ # the :\d+ suffixes indicate friends of $to_userid; the number is a groupmask.
+ sub make_x_livejournal_header {
+     my ($to_userid) = @_;
+     my %users;
+     $u        and $users{journal}    = $u;
+     $ujp      and $users{postby}     = $ujp;
+     $up       and $users{commentby}  = $up;
+     $paruser  and $users{parentby}   = {user => $paruser, userid => $paruserid};
+     my @userids = map {$users{$_}{userid}} keys %users;
+     my $rows = $dbh->selectall_hashref("SELECT friendid, groupmask " .
+                                        "FROM   friends             " .
+                                        "WHERE  userid = $to_userid " .
+                                        "AND    friendid IN (" . join(",", @userids) . ")")
+       or return $dbh->errstr;
+     my %friend_data = map {$_->{friendid} => $_->{groupmask}} @$rows;
+     
+     my $result = "itemid=$qitemid talkid=$insertid";
+     foreach my $key (keys %users) {
+         my $username = $users{$key}{user};
+         my $userid   = $users{$key}{userid};
+         $result .= " $key=$username";
+         if (exists $friend_data{$userid}) {
+             $result .= ":$friend_data{$userid}";
+         }
+     }
+     return $result;
+ }
+
  sub indent {
      my $a = shift;
      my $leadchar = shift || " ";
      return fill("$leadchar  ", "$leadchar  ", $a);
  }
 
-sub blockquote {
+ sub blockquote {
      my $a = shift;
      return "<BLOCKQUOTE style=\"BORDER-LEFT: #000040 2px solid; MARGIN-LEFT: 0px; MARGIN-RIGHT: 0px; PADDING-LEFT: 15px; PADDING-RIGHT: 0px\">$a</BLOCKQUOTE>";
-}
+ }
 
 _CODE=)
 

