From 130ba2888643992943780962dd4efcca3c595735 Mon Sep 17 00:00:00 2001
From: Evan Prodromou <evan@controlyourself.ca>
Date: Wed, 17 Sep 2008 13:47:41 -0400
Subject: [PATCH] newmessage and showmessage

darcs-hash:20080917174741-5ed1f-c090055487bab0df52d25ad6550d3850ef5f7661.gz
---
 actions/emailsettings.php |  10 +++
 actions/newmessage.php    | 154 ++++++++++++++++++++++++++++++++++++++
 actions/showmessage.php   |  98 ++++++++++++++++++++++++
 classes/Message.php       |  30 ++++++++
 classes/User.php          |  22 ++++++
 classes/stoica.ini        |   1 +
 db/laconica.sql           |   1 +
 lib/mail.php              |  36 +++++++++
 8 files changed, 352 insertions(+)
 create mode 100644 actions/newmessage.php
 create mode 100644 actions/showmessage.php

diff --git a/actions/emailsettings.php b/actions/emailsettings.php
index c93cbfae0d..6e189a909d 100644
--- a/actions/emailsettings.php
+++ b/actions/emailsettings.php
@@ -88,6 +88,12 @@ class EmailsettingsAction extends SettingsAction {
 		common_checkbox('emailnotifysub',
 		                _('Send me notices of new subscriptions through email.'),
 		                $user->emailnotifysub);
+		common_checkbox('emailnotifyfav',
+		                _('Send me email when someone adds my notice as a favorite.'),
+		                $user->emailnotifyfav);
+		common_checkbox('emailnotifymsg',
+		                _('Send me email when someone sends me a private message.'),
+		                $user->emailnotifymsg);
 		common_checkbox('emailpost',
 						_('I want to post notices by email.'),
 						$user->emailpost);
@@ -142,6 +148,8 @@ class EmailsettingsAction extends SettingsAction {
 	function save_preferences() {
 
 		$emailnotifysub = $this->boolean('emailnotifysub');
+		$emailnotifyfav = $this->boolean('emailnotifyfav');
+		$emailnotifymsg = $this->boolean('emailnotifymsg');
 		$emailmicroid = $this->boolean('emailmicroid');
 		$emailpost = $this->boolean('emailpost');
 
@@ -154,6 +162,8 @@ class EmailsettingsAction extends SettingsAction {
 		$original = clone($user);
 
 		$user->emailnotifysub = $emailnotifysub;
+		$user->emailnotifyfav = $emailnotifyfav;
+		$user->emailnotifymsg = $emailnotifymsg;
 		$user->emailmicroid = $emailmicroid;
 		$user->emailpost = $emailpost;
 
diff --git a/actions/newmessage.php b/actions/newmessage.php
new file mode 100644
index 0000000000..95b98ec479
--- /dev/null
+++ b/actions/newmessage.php
@@ -0,0 +1,154 @@
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+if (!defined('LACONICA')) { exit(1); }
+
+class NewmessageAction extends Action {
+	
+	function handle($args) {
+		parent::handle($args);
+
+		if (!common_logged_in()) {
+			$this->client_error(_('Not logged in.'), 403);
+		} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+			$this->save_new_message();
+		} else {
+			$this->show_form();
+		}
+	}
+
+	function save_new_message() {
+
+		$user = common_current_user();
+		assert($user); # XXX: maybe an error instead...
+		$content = $this->trimmed('content');
+		$to = $this->trimmed('to');
+		
+		if (!$content) {
+			$this->show_form(_('No content!'));
+			return;
+		} else if (mb_strlen($content) > 140) {
+			common_debug("Content = '$content'", __FILE__);
+			common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
+			$this->show_form(_('That\'s too long. Max message size is 140 chars.'));
+			return;
+		}
+
+		$other = User::staticGet('id', $to);
+		
+		if (!$other) {
+			$this->show_form(_('No recipient specified.'));
+			return;
+		} else if (!$user->mutuallySubscribed($other)) {
+			$this->client_error(_('You can\'t send a message to this user.'), 404);
+			return;
+		}
+		
+		$message = Message::saveNew($user->id, $other->id, $content, 'web');
+		
+		if (is_string($message)) {
+			$this->show_form($message);
+			return;
+		}
+
+		$this->notify($user, $to, $message);
+
+		$url = common_local_url('showmessage',
+								array('message' => $message->id));
+
+		common_redirect($url, 303);
+	}
+
+	function show_top($params) {
+
+		list($content, $user, $to) = $params;
+		
+		assert(!is_null($user));
+		
+		common_element_start('form', array('id' => 'message_form',
+										   'method' => 'post',
+										   'action' => $this->self_url()));
+		
+		common_element_start('p');
+		
+		$mutual_users = $user->mutuallySubscribedUsers();
+		
+		$mutual = array();
+		
+		while ($mutual_users->fetch()) {
+			$mutual[$mutual_users->id] = $mutual_users->nickname;
+		}
+
+		$mutual_users->free();
+		unset($mutual_users);
+
+		common_dropdown('to', _('To'), $mutual,
+						_('User you want to send a message to'), FALSE,
+						$to->id);
+		
+		common_element('textarea', array('id' => 'content',
+										 'cols' => 60,
+										 'rows' => 3,
+										 'name' => 'content'),
+					   ($content) ? $content : '');
+						
+		common_element('input', array('id' => 'message_send',
+									  'name' => 'message_send',
+									  'type' => 'submit',
+									  'value' => _('Send')));
+		
+		common_element_end('p');
+		common_element_end('form');
+	}
+
+	function show_form($msg=NULL) {
+		
+		$content = $this->trimmed('content');
+		$user = common_current_user();
+
+		$to = common_canonical_nickname($this->trimmed('to'));
+		
+		$other = User::staticGet('nickname', $to);
+
+		if (!$other) {
+			$this->client_error(_('No such user'), 404);
+			return;
+		}
+
+		if (!$user->mutuallySubscribed($other)) {
+			$this->client_error(_('You can\'t send a message to this user.'), 404);
+			return;
+		}
+		
+		common_show_header(_('New message'), NULL,
+						   array($content, $user, $to),
+		                   array($this, 'show_top'));
+		
+		if ($msg) {
+			common_element('p', 'error', $msg);
+		}
+		
+		common_show_footer();
+	}
+	
+	function notify($from, $to, $message) {
+		mail_notify_message($message, $from, $to);
+		# XXX: Jabber, SMS notifications... probably queued
+	}
+}
diff --git a/actions/showmessage.php b/actions/showmessage.php
new file mode 100644
index 0000000000..137d6adf92
--- /dev/null
+++ b/actions/showmessage.php
@@ -0,0 +1,98 @@
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+if (!defined('LACONICA')) { exit(1); }
+
+require_once(INSTALLDIR.'/lib/mailbox.php');
+
+class ShowmessageAction extends MailboxAction {
+
+	function handle($args) {
+
+		parent::handle($args);
+
+		$id = $this->trimmed('id');
+
+		$message = Message::staticGet('id', $id);
+
+		if (!$message) {
+			$this->client_error(_('No such message.'), 404);
+			return;
+		}
+		
+		$cur = common_current_user();
+		
+		if (!$cur ||
+			$cur->id != $message->from_profile &&
+			$cur->id != $message->to_profile) 
+		{
+			$this->client_error(_('Only the sender and recipient may read this message.'), 404);
+			return;
+		}
+			
+		$this->show_page($cur, 1);
+	}
+	
+	function get_message() {
+		$id = $this->trimmed('id');
+		$message = Message::staticGet('id', $id);
+		return $message;
+	}
+	
+	function get_title($user, $page) {
+		$message = $this->get_message();
+		if (!$message) {
+			return NULL;
+		}
+		
+		if ($user->id == $message->from_profile) {
+			$to = $message->getTo();
+			$title = sprintf(_('Message to %1\$s on %2\$s'),
+							 $to->nickname,
+							 common_exact_date($message->created));
+		} else if ($user->id == $message->to_profile) {
+			$from = $message->getFrom();
+			$title = sprintf(_('Message from %1\$s on %2\$s'),
+							 $from->nickname,
+							 common_exact_date($message->created));
+		}
+		return $title;
+	}
+	
+	function get_messages($user, $page) {
+		return $this->get_message();
+	}
+	
+	function get_message_profile($message) {
+		$user = common_current_user();
+		if ($user->id == $message->from_profile) {
+			return $message->getTo();
+		} else if ($user->id == $message->to_profile) {
+			return $message->getFrom();
+		} else {
+			# This shouldn't happen
+			return NULL;
+		}
+	}
+	
+	function get_instructions() {
+		return '';
+	}
+}
+	
\ No newline at end of file
diff --git a/classes/Message.php b/classes/Message.php
index 93ec419d49..b713737e8b 100644
--- a/classes/Message.php
+++ b/classes/Message.php
@@ -34,4 +34,34 @@ class Message extends DB_DataObject
 	function getTo() {
 		return Profile::staticGet('id', $this->to_profile);
 	}
+	
+	static function saveNew($from, $to, $content, $source) {
+		
+		$msg = new Message();
+		
+		$msg->from_profile = $from;
+		$msg->to_profile = $to;
+		$msg->content = $content;
+		$msg->rendered = common_render_text($content);
+		$msg->source = $source;
+		
+		$result = $msg->insert();
+		
+		if (!$result) {
+			common_log_db_error($msg, 'INSERT', __FILE__);
+			return _('Could not insert message.');
+		}
+		
+		$orig = clone($msg);
+		$msg->uri = common_local_url('showmessage', array('message' => $message->id));
+		
+		$result = $msg->update($orig);
+		
+		if (!$result) {
+			common_log_db_error($msg, 'UPDATE', __FILE__);
+			return _('Could not update message with new URI.');
+		}
+		
+		return $msg;
+	}
 }
diff --git a/classes/User.php b/classes/User.php
index 65e01e799e..4305c15411 100644
--- a/classes/User.php
+++ b/classes/User.php
@@ -44,6 +44,7 @@ class User extends DB_DataObject
     public $incomingemail;                   // varchar(255)  unique_key
     public $emailnotifysub;                  // tinyint(1)   default_1
     public $emailnotifyfav;                  // tinyint(1)   default_1
+    public $emailnotifymsg;                  // tinyint(1)   default_1
     public $emailmicroid;                    // tinyint(1)   default_1
     public $language;                        // varchar(50)  
     public $timezone;                        // varchar(50)  
@@ -347,4 +348,25 @@ class User extends DB_DataObject
 		unset($fave);
 		return $result;
 	}
+	
+	function mutuallySubscribed($other) {
+		return $this->isSubscribed($other) &&
+		  $other->isSubscribed($this);
+	}
+	
+	function mutuallySubscribedUsers() {
+
+		# 3-way join; probably should get cached
+		
+		$qry = 'SELECT user.* ' .
+		  'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
+		  'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
+		  'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
+		  'ORDER BY user.nickname';
+		
+		$user = new User();
+		$user->query(sprintf($qry, $this->id, $this->id));
+
+		return $user;
+	}
 }
diff --git a/classes/stoica.ini b/classes/stoica.ini
index 0d02da426c..c74cd9ccee 100644
--- a/classes/stoica.ini
+++ b/classes/stoica.ini
@@ -268,6 +268,7 @@ email = 2
 incomingemail = 2
 emailnotifysub = 17
 emailnotifyfav = 17
+emailnotifymsg = 17
 emailmicroid = 17
 language = 2
 timezone = 2
diff --git a/db/laconica.sql b/db/laconica.sql
index 8dba7e3797..82f9cecae5 100644
--- a/db/laconica.sql
+++ b/db/laconica.sql
@@ -48,6 +48,7 @@ create table user (
     incomingemail varchar(255) unique key comment 'email address for post-by-email',
     emailnotifysub tinyint default 1 comment 'Notify by email of subscriptions',
     emailnotifyfav tinyint default 1 comment 'Notify by email of favorites',
+    emailnotifymsg tinyint default 1 comment 'Notify by email of direct messages',
     emailmicroid tinyint default 1 comment 'whether to publish email microid',
     language varchar(50) comment 'preferred language',
     timezone varchar(50) comment 'timezone',
diff --git a/lib/mail.php b/lib/mail.php
index 162669eaaa..4788ccb917 100644
--- a/lib/mail.php
+++ b/lib/mail.php
@@ -231,3 +231,39 @@ function mail_confirm_sms($code, $nickname, $address) {
 
 	mail_send($recipients, $headers, $body);
 }
+
+function mail_notify_message($message, $from=NULL, $to=NULL) {
+
+	if (is_null($from)) {
+		$from = User::staticGet($message->from_profile);
+	}
+	
+	if (is_null($to)) {
+		$to = User::staticGet($message->to_profile);
+	}
+
+	if (!$to->email || !$to->emailnotifymsg) {
+		return true;
+	}
+	
+	$subject = sprintf(_('New private message from %s'), $from->nickname);
+
+	$from_profile = $from->getProfile();
+	
+	$body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
+					  "------------------------------------------------------\n".
+					  "%3\$s\n".
+					  "------------------------------------------------------\n\n".
+					  "You can reply to their message here:\n\n".
+					  "%4\$s\n\n".
+					  "Don't reply to this email; it won't get to them.\n\n".
+					  "With kind regards,\n",
+					  "%5\$s\n"),
+					$from_profile->getBestName(),
+					$from->nickname,
+					$message->content,
+					common_local_url('newmessage', array('to' => $from->id)),
+					common_config('site', 'name'));
+	
+	return mail_to_user($to, $subject, $body);
+}
-- 
2.39.5