function handle_message($rawmessage) { list($from, $to, $msg, $attachments) = $this->parse_message($rawmessage); if (!$from || !$to || !$msg) { // TRANS: Error message in incoming mail handler used when an incoming e-mail cannot be processed. $this->error(null, _('Could not parse message.')); } common_log(LOG_INFO, "Mail from {$from} to {$to} with " . count($attachments) . ' attachment(s): ' . substr($msg, 0, 20)); $user = $this->user_from_header($from); if (!$user) { // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a registered user. $this->error($from, _('Not a registered user.')); return false; } if (!$this->user_match_to($user, $to)) { // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a user's incoming e-mail address. $this->error($from, _('Sorry, that is not your incoming email address.')); return false; } if (!$user->emailpost) { // TRANS: Error message in incoming mail handler used when no incoming e-mail is allowed. $this->error($from, _('Sorry, no incoming email allowed.')); return false; } $response = $this->handle_command($user, $from, $msg); if ($response) { return true; } $msg = $this->cleanup_msg($msg); $msg = $user->shortenLinks($msg); if (Notice::contentTooLong($msg)) { // TRANS: Error message in incoming mail handler used when an incoming e-mail contains too many characters. $this->error($from, sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent())); } $mediafiles = array(); foreach ($attachments as $attachment) { $mf = null; try { $mf = MediaFile::fromFileHandle($attachment, $user); } catch (ClientException $ce) { $this->error($from, $ce->getMessage()); } $msg .= ' ' . $mf->shortUrl(); array_push($mediafiles, $mf); fclose($attachment); } $err = $this->add_notice($user, $msg, $mediafiles); if (is_string($err)) { $this->error($from, $err); return false; } else { return true; } }
/** * Fetch an attachment from Yammer and save it into our system. * Unlike avatars, the attachment URLs are guarded by authentication, * so we need to run the HTTP hit through our OAuth API client. * * @param string $url * @param User $user * @return MediaFile * * @throws Exception on low-level network or HTTP error */ private function saveAttachment($url, User $user) { // Fetch the attachment... // WARNING: file must fit in memory here :( $body = $this->client->fetchUrl($url); // Save to a temporary file and shove it into our file-attachment space... $temp = tmpfile(); fwrite($temp, $body); try { $upload = MediaFile::fromFileHandle($temp, $user); fclose($temp); return $upload; } catch (Exception $e) { fclose($temp); throw $e; } }