mailhandler.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. require_once(INSTALLDIR . '/lib/mail.php');
  20. require_once('Mail/mimeDecode.php');
  21. // @todo FIXME: we use both Mail_mimeDecode and mailparse
  22. // Need to move everything to mailparse
  23. class MailHandler
  24. {
  25. function __construct()
  26. {
  27. }
  28. function handle_message($rawmessage)
  29. {
  30. list($from, $to, $msg, $attachments) = $this->parse_message($rawmessage);
  31. if (!$from || !$to || !$msg) {
  32. // TRANS: Error message in incoming mail handler used when an incoming e-mail cannot be processed.
  33. $this->error(null, _('Could not parse message.'));
  34. }
  35. common_log(LOG_INFO, "Mail from $from to $to with ".count($attachments) .' attachment(s): ' .substr($msg, 0, 20));
  36. $user = $this->user_from_header($from);
  37. if (!$user) {
  38. // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a registered user.
  39. $this->error($from, _('Not a registered user.'));
  40. return false;
  41. }
  42. if (!$this->user_match_to($user, $to)) {
  43. // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a user's incoming e-mail address.
  44. $this->error($from, _('Sorry, that is not your incoming email address.'));
  45. return false;
  46. }
  47. if (!$user->emailpost) {
  48. // TRANS: Error message in incoming mail handler used when no incoming e-mail is allowed.
  49. $this->error($from, _('Sorry, no incoming email allowed.'));
  50. return false;
  51. }
  52. $response = $this->handle_command($user, $from, $msg);
  53. if ($response) {
  54. return true;
  55. }
  56. $msg = $this->cleanup_msg($msg);
  57. $msg = $user->shortenLinks($msg);
  58. if (Notice::contentTooLong($msg)) {
  59. // TRANS: Error message in incoming mail handler used when an incoming e-mail contains too many characters.
  60. $this->error($from, sprintf(_m('That\'s too long. Maximum notice size is %d character.',
  61. 'That\'s too long. Maximum notice size is %d characters.',
  62. Notice::maxContent()),
  63. Notice::maxContent()));
  64. }
  65. $mediafiles = array();
  66. foreach($attachments as $attachment){
  67. $mf = null;
  68. try {
  69. $mf = MediaFile::fromFilehandle($attachment, $user->getProfile());
  70. } catch(ClientException $ce) {
  71. $this->error($from, $ce->getMessage());
  72. }
  73. $msg .= ' ' . $mf->shortUrl();
  74. array_push($mediafiles, $mf);
  75. fclose($attachment);
  76. }
  77. $err = $this->add_notice($user, $msg, $mediafiles);
  78. if (is_string($err)) {
  79. $this->error($from, $err);
  80. return false;
  81. } else {
  82. return true;
  83. }
  84. }
  85. function error($from, $msg)
  86. {
  87. file_put_contents("php://stderr", $msg . "\n");
  88. exit(1);
  89. }
  90. function user_from_header($from_hdr)
  91. {
  92. $froms = mailparse_rfc822_parse_addresses($from_hdr);
  93. if (!$froms) {
  94. return null;
  95. }
  96. $from = $froms[0];
  97. $addr = common_canonical_email($from['address']);
  98. $user = User::getKV('email', $addr);
  99. if (!$user) {
  100. $user = User::getKV('smsemail', $addr);
  101. }
  102. return $user;
  103. }
  104. function user_match_to($user, $to_hdr)
  105. {
  106. $incoming = $user->incomingemail;
  107. $tos = mailparse_rfc822_parse_addresses($to_hdr);
  108. foreach ($tos as $to) {
  109. if (strcasecmp($incoming, $to['address']) == 0) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. function handle_command($user, $from, $msg)
  116. {
  117. $inter = new CommandInterpreter();
  118. $cmd = $inter->handle_command($user, $msg);
  119. if ($cmd) {
  120. $cmd->execute(new MailChannel($from));
  121. return true;
  122. }
  123. return false;
  124. }
  125. function respond($from, $to, $response)
  126. {
  127. $headers['From'] = $to;
  128. $headers['To'] = $from;
  129. // TRANS: E-mail subject for reply to an e-mail command.
  130. $headers['Subject'] = _('Command complete');
  131. return mail_send(array($from), $headers, $response);
  132. }
  133. function log($level, $msg)
  134. {
  135. common_log($level, 'MailDaemon: '.$msg);
  136. }
  137. function add_notice($user, $msg, $mediafiles)
  138. {
  139. try {
  140. $notice = Notice::saveNew($user->id, $msg, 'mail');
  141. } catch (Exception $e) {
  142. $this->log(LOG_ERR, $e->getMessage());
  143. return $e->getMessage();
  144. }
  145. foreach($mediafiles as $mf){
  146. $mf->attachToNotice($notice);
  147. }
  148. $this->log(LOG_INFO,
  149. 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
  150. return true;
  151. }
  152. function parse_message($contents)
  153. {
  154. $parsed = Mail_mimeDecode::decode(array('input' => $contents,
  155. 'include_bodies' => true,
  156. 'decode_headers' => true,
  157. 'decode_bodies' => true));
  158. if (!$parsed) {
  159. return null;
  160. }
  161. $from = $parsed->headers['from'];
  162. $to = $parsed->headers['to'];
  163. $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
  164. $attachments = array();
  165. $this->extract_part($parsed,$msg,$attachments);
  166. return array($from, $to, $msg, $attachments);
  167. }
  168. function extract_part($parsed,&$msg,&$attachments){
  169. if ($parsed->ctype_primary == 'multipart') {
  170. if($parsed->ctype_secondary == 'alternative'){
  171. $altmsg = $this->extract_msg_from_multipart_alternative_part($parsed);
  172. if(!empty($altmsg)) $msg = $altmsg;
  173. }else{
  174. foreach($parsed->parts as $part){
  175. $this->extract_part($part,$msg,$attachments);
  176. }
  177. }
  178. } else if ($parsed->ctype_primary == 'text'
  179. && $parsed->ctype_secondary=='plain') {
  180. $msg = $parsed->body;
  181. if(strtolower($parsed->ctype_parameters['charset']) != "utf-8"){
  182. $msg = utf8_encode($msg);
  183. }
  184. }else if(!empty($parsed->body)){
  185. if(common_config('attachments', 'uploads')){
  186. //only save attachments if uploads are enabled
  187. $attachment = tmpfile();
  188. fwrite($attachment, $parsed->body);
  189. $attachments[] = $attachment;
  190. }
  191. }
  192. }
  193. function extract_msg_from_multipart_alternative_part($parsed){
  194. foreach ($parsed->parts as $part) {
  195. $this->extract_part($part,$msg,$attachments);
  196. }
  197. //we don't want any attachments that are a result of this parsing
  198. return $msg;
  199. }
  200. function unsupported_type($type)
  201. {
  202. // TRANS: Error message in incoming mail handler used when an incoming e-mail is of an unsupported type.
  203. // TRANS: %s is the unsupported type.
  204. $this->error(null, sprintf(_('Unsupported message type: %s.'), $type));
  205. }
  206. function cleanup_msg($msg)
  207. {
  208. $lines = explode("\n", $msg);
  209. $output = '';
  210. foreach ($lines as $line) {
  211. // skip quotes
  212. if (preg_match('/^\s*>.*$/', $line)) {
  213. continue;
  214. }
  215. // skip start of quote
  216. if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
  217. continue;
  218. }
  219. // probably interesting to someone, not us
  220. if (preg_match('/^\s*Sent via/', $line)) {
  221. continue;
  222. }
  223. if (preg_match('/^\s*Sent from my/', $line)) {
  224. continue;
  225. }
  226. // skip everything after a sig
  227. if (preg_match('/^\s*--+\s*$/', $line) ||
  228. preg_match('/^\s*__+\s*$/', $line))
  229. {
  230. break;
  231. }
  232. // skip everything after Outlook quote
  233. if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
  234. break;
  235. }
  236. // skip everything after weird forward
  237. if (preg_match('/^\s*Begin\s+forward/', $line)) {
  238. break;
  239. }
  240. // skip everything after a blank line if we already have content
  241. if ($output !== '' && $line === '') {
  242. break;
  243. }
  244. $output .= ' ' . $line;
  245. }
  246. preg_replace('/\s+/', ' ', $output);
  247. return trim($output);
  248. }
  249. }