+
+ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+ $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
+ return;
+ }
+
+ $user = $apidata['user'];
+ $source = $this->trimmed('source'); // Not supported by Twitter.
+
+ $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
+ if (!$source || in_array($source, $reserved_sources)) {
+ $source = 'api';
+ }
+
+ $content = $this->trimmed('text');
+
+ if (!$content) {
+ $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
+ } else {
+ $content_shortened = common_shorten_links($content);
+ if (mb_strlen($content_shortened) > 140) {
+ $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
+ $code = 406, $apidata['content-type']);
+ return;
+ }
+ }
+
+ $other = $this->get_user($this->trimmed('user'));
+
+ if (!$other) {
+ $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
+ return;
+ } else if (!$user->mutuallySubscribed($other)) {
+ $this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'),
+ $code = 403, $apidata['content-type']);
+ return;
+ } else if ($user->id == $other->id) {
+ // Sending msgs to yourself is allowed by Twitter
+ $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
+ $code = 403, $apidata['content-type']);
+ return;
+ }
+
+ $message = Message::saveNew($user->id, $other->id,
+ html_entity_decode($content, ENT_NOQUOTES, 'UTF-8'), $source);
+
+ if (is_string($message)) {
+ $this->server_error($message);
+ return;
+ }
+
+ $this->notify($user, $other, $message);
+
+ if ($apidata['content-type'] == 'xml') {
+ $this->show_single_xml_dmsg($message);
+ } elseif ($apidata['content-type'] == 'json') {
+ $this->show_single_json_dmsg($message);
+ }
+