PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/laconica-0.5.0/maildaemon.php

#
PHP | 207 lines | 151 code | 29 blank | 27 comment | 30 complexity | 153b687abf7b28270961db44afc78d07 MD5 | raw file
Possible License(s): AGPL-3.0
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Laconica - a distributed open-source microblogging tool
  5. * Copyright (C) 2008, Controlez-Vous, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. # Abort if called from a web server
  21. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  22. print "This script must be run from the command line\n";
  23. exit();
  24. }
  25. define('INSTALLDIR', dirname(__FILE__));
  26. define('LACONICA', true);
  27. require_once(INSTALLDIR . '/lib/common.php');
  28. require_once(INSTALLDIR . '/lib/mail.php');
  29. require_once('Mail/mimeDecode.php');
  30. # FIXME: we use both Mail_mimeDecode and mailparse
  31. # Need to move everything to mailparse
  32. class MailerDaemon {
  33. function __construct() {
  34. }
  35. function handle_message($fname='php://stdin') {
  36. list($from, $to, $msg) = $this->parse_message($fname);
  37. if (!$from || !$to || !$msg) {
  38. $this->error(NULL, _t('Could not parse message.'));
  39. }
  40. common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
  41. $user = $this->user_from($from);
  42. if (!$user) {
  43. $this->error($from, _('Not a registered user.'));
  44. return false;
  45. }
  46. if (!$this->user_match_to($user, $to)) {
  47. $this->error($from, _('Sorry, that is not your incoming email address.'));
  48. return false;
  49. }
  50. if (!$user->emailpost) {
  51. $this->error($from, _('Sorry, no incoming email allowed.'));
  52. return false;
  53. }
  54. $response = $this->handle_command($user, $msg);
  55. if ($response) {
  56. $this->respond($from, $to, $response);
  57. return true;
  58. }
  59. $msg = $this->cleanup_msg($msg);
  60. $this->add_notice($user, $msg);
  61. }
  62. function error($from, $msg) {
  63. file_put_contents("php://stderr", $msg . "\n");
  64. exit(1);
  65. }
  66. function user_from($from_hdr) {
  67. $froms = mailparse_rfc822_parse_addresses($from_hdr);
  68. if (!$froms) {
  69. return NULL;
  70. }
  71. $from = $froms[0];
  72. $addr = common_canonical_email($from['address']);
  73. $user = User::staticGet('email', $addr);
  74. if (!$user) {
  75. $user = User::staticGet('smsemail', $addr);
  76. }
  77. return $user;
  78. }
  79. function user_match_to($user, $to_hdr) {
  80. $incoming = $user->incomingemail;
  81. $tos = mailparse_rfc822_parse_addresses($to_hdr);
  82. foreach ($tos as $to) {
  83. if (strcasecmp($incoming, $to['address']) == 0) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. function handle_command($user, $msg) {
  90. return false;
  91. }
  92. function respond($from, $to, $response) {
  93. $headers['From'] = $to;
  94. $headers['To'] = $from;
  95. $headers['Subject'] = "Command complete";
  96. return mail_send(array($from), $headers, $response);
  97. }
  98. function log($level, $msg) {
  99. common_log($level, 'MailDaemon: '.$msg);
  100. }
  101. function add_notice($user, $msg) {
  102. $notice = Notice::saveNew($user->id, $msg, 'mail');
  103. if (is_string($notice)) {
  104. $this->log(LOG_ERR, $notice);
  105. return;
  106. }
  107. common_broadcast_notice($notice);
  108. $this->log(LOG_INFO,
  109. 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
  110. }
  111. function parse_message($fname) {
  112. $contents = file_get_contents($fname);
  113. $parsed = Mail_mimeDecode::decode(array('input' => $contents,
  114. 'include_bodies' => true,
  115. 'decode_headers' => true,
  116. 'decode_bodies' => true));
  117. if (!$parsed) {
  118. return NULL;
  119. }
  120. $from = $parsed->headers['from'];
  121. $to = $parsed->headers['to'];
  122. $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
  123. if ($parsed->ctype_primary == 'multipart') {
  124. foreach ($parsed->parts as $part) {
  125. if ($part->ctype_primary == 'text' &&
  126. $part->ctype_secondary == 'plain') {
  127. $msg = $part->body;
  128. break;
  129. }
  130. }
  131. } else if ($type == 'text/plain') {
  132. $msg = $parsed->body;
  133. } else {
  134. $this->unsupported_type($type);
  135. }
  136. return array($from, $to, $msg);
  137. }
  138. function unsupported_type($type) {
  139. $this->error(NULL, "Unsupported message type: " . $type);
  140. }
  141. function cleanup_msg($msg) {
  142. $lines = explode("\n", $msg);
  143. $output = '';
  144. foreach ($lines as $line) {
  145. // skip quotes
  146. if (preg_match('/^\s*>.*$/', $line)) {
  147. continue;
  148. }
  149. // skip start of quote
  150. if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
  151. continue;
  152. }
  153. // probably interesting to someone, not us
  154. if (preg_match('/^\s*Sent via/', $line)) {
  155. continue;
  156. }
  157. // skip everything after a sig
  158. if (preg_match('/^\s*--+\s*$/', $line) ||
  159. preg_match('/^\s*__+\s*$/', $line))
  160. {
  161. break;
  162. }
  163. // skip everything after Outlook quote
  164. if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
  165. break;
  166. }
  167. // skip everything after weird forward
  168. if (preg_match('/^\s*Begin\s+forward/', $line)) {
  169. break;
  170. }
  171. $output .= ' ' . $line;
  172. }
  173. preg_replace('/\s+/', ' ', $output);
  174. return trim($output);
  175. }
  176. }
  177. $md = new MailerDaemon();
  178. $md->handle_message('php://stdin');