imapmanager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * IMAP IO Manager
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @copyright 2009-2010 StatusNet, Inc.
  26. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  27. * @maintainer Craig Andrews <candrews@integralblue.com>
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. class ImapManager extends IoManager
  35. {
  36. protected $conn = null;
  37. function __construct($plugin)
  38. {
  39. $this->plugin = $plugin;
  40. }
  41. /**
  42. * Fetch the singleton manager for the current site.
  43. * @return mixed ImapManager, or false if unneeded
  44. */
  45. public static function get()
  46. {
  47. // TRANS: Exception thrown when the ImapManager is used incorrectly in the code.
  48. throw new Exception(_m('ImapManager should be created using its constructor, not using the static "get()" method.'));
  49. }
  50. /**
  51. * Lists the IM connection socket to allow i/o master to wake
  52. * when input comes in here as well as from the queue source.
  53. *
  54. * @return array of resources
  55. */
  56. public function getSockets()
  57. {
  58. return array();
  59. }
  60. /**
  61. * Tell the i/o master we need one instance globally.
  62. * Since this is a plugin manager, the plugin class itself will
  63. * create one instance per site. This prevents the IoMaster from
  64. * making more instances.
  65. */
  66. public static function multiSite()
  67. {
  68. return IoManager::GLOBAL_SINGLE_ONLY;
  69. }
  70. /**
  71. * Initialize connection to server.
  72. * @return boolean true on success
  73. */
  74. public function start($master)
  75. {
  76. if(parent::start($master))
  77. {
  78. $this->conn = $this->connect();
  79. return true;
  80. }else{
  81. return false;
  82. }
  83. }
  84. public function handleInput($socket)
  85. {
  86. $this->check_mailbox();
  87. return true;
  88. }
  89. public function poll()
  90. {
  91. return $this->check_mailbox() > 0;
  92. }
  93. function pollInterval()
  94. {
  95. return $this->plugin->poll_frequency;
  96. }
  97. protected function connect()
  98. {
  99. $this->conn = imap_open($this->plugin->mailbox, $this->plugin->user, $this->plugin->password);
  100. if($this->conn){
  101. common_log(LOG_INFO, "Connected");
  102. return $this->conn;
  103. }else{
  104. common_log(LOG_INFO, "Failed to connect: " . imap_last_error());
  105. return $this->conn;
  106. }
  107. }
  108. protected function check_mailbox()
  109. {
  110. imap_ping($this->conn);
  111. $count = imap_num_msg($this->conn);
  112. common_log(LOG_INFO, "Found $count messages");
  113. if($count > 0){
  114. $handler = new IMAPMailHandler();
  115. for($i=1; $i <= $count; $i++)
  116. {
  117. $rawmessage = imap_fetchheader($this->conn, $count, FT_PREFETCHTEXT) . imap_body($this->conn, $i);
  118. $handler->handle_message($rawmessage);
  119. imap_delete($this->conn, $i);
  120. }
  121. imap_expunge($this->conn);
  122. common_log(LOG_INFO, "Finished processing messages");
  123. }
  124. return $count;
  125. }
  126. }