channel.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. /**
  21. * @todo Needs documentation.
  22. */
  23. class Channel
  24. {
  25. function on($user)
  26. {
  27. return false;
  28. }
  29. function off($user)
  30. {
  31. return false;
  32. }
  33. function output($user, $text)
  34. {
  35. return false;
  36. }
  37. function error($user, $text)
  38. {
  39. return false;
  40. }
  41. function source()
  42. {
  43. return null;
  44. }
  45. }
  46. class CLIChannel extends Channel
  47. {
  48. function source()
  49. {
  50. return 'cli';
  51. }
  52. function output($user, $text)
  53. {
  54. $site = common_config('site', 'name');
  55. print "[{$user->nickname}@{$site}] $text\n";
  56. }
  57. function error($user, $text)
  58. {
  59. $this->output($user, $text);
  60. }
  61. }
  62. class WebChannel extends Channel
  63. {
  64. var $out = null;
  65. function __construct($out=null)
  66. {
  67. $this->out = $out;
  68. }
  69. function source()
  70. {
  71. return 'web';
  72. }
  73. function on($user)
  74. {
  75. return false;
  76. }
  77. function off($user)
  78. {
  79. return false;
  80. }
  81. function output($user, $text)
  82. {
  83. // XXX: buffer all output and send it at the end
  84. // XXX: even better, redirect to appropriate page
  85. // depending on what command was run
  86. $this->out->startHTML();
  87. $this->out->elementStart('head');
  88. // TRANS: Title for command results.
  89. $this->out->element('title', null, _('Command results'));
  90. $this->out->elementEnd('head');
  91. $this->out->elementStart('body');
  92. $this->out->element('p', array('id' => 'command_result'), $text);
  93. $this->out->elementEnd('body');
  94. $this->out->endHTML();
  95. }
  96. function error($user, $text)
  97. {
  98. common_user_error($text);
  99. }
  100. }
  101. class AjaxWebChannel extends WebChannel
  102. {
  103. function output($user, $text)
  104. {
  105. $this->out->startHTML('text/xml;charset=utf-8');
  106. $this->out->elementStart('head');
  107. // TRANS: Title for command results.
  108. $this->out->element('title', null, _('Command results'));
  109. $this->out->elementEnd('head');
  110. $this->out->elementStart('body');
  111. $this->out->element('p', array('id' => 'command_result'), $text);
  112. $this->out->elementEnd('body');
  113. $this->out->endHTML();
  114. }
  115. function error($user, $text)
  116. {
  117. $this->out->startHTML('text/xml;charset=utf-8');
  118. $this->out->elementStart('head');
  119. // TRANS: Title for command results.
  120. $this->out->element('title', null, _('AJAX error'));
  121. $this->out->elementEnd('head');
  122. $this->out->elementStart('body');
  123. $this->out->element('p', array('id' => 'error'), $text);
  124. $this->out->elementEnd('body');
  125. $this->out->endHTML();
  126. }
  127. }
  128. class MailChannel extends Channel
  129. {
  130. var $addr = null;
  131. function source()
  132. {
  133. return 'mail';
  134. }
  135. function __construct($addr=null)
  136. {
  137. $this->addr = $addr;
  138. }
  139. function on($user)
  140. {
  141. return $this->setNotify($user, 1);
  142. }
  143. function off($user)
  144. {
  145. return $this->setNotify($user, 0);
  146. }
  147. function output($user, $text)
  148. {
  149. $headers['From'] = $user->incomingemail;
  150. $headers['To'] = $this->addr;
  151. // TRANS: E-mail subject when a command has completed.
  152. $headers['Subject'] = _('Command complete');
  153. return mail_send(array($this->addr), $headers, $text);
  154. }
  155. function error($user, $text)
  156. {
  157. $headers['From'] = $user->incomingemail;
  158. $headers['To'] = $this->addr;
  159. // TRANS: E-mail subject when a command has failed.
  160. $headers['Subject'] = _('Command failed');
  161. return mail_send(array($this->addr), $headers, $text);
  162. }
  163. function setNotify($user, $value)
  164. {
  165. $orig = clone($user);
  166. $user->smsnotify = $value;
  167. $result = $user->update($orig);
  168. if (!$result) {
  169. common_log_db_error($user, 'UPDATE', __FILE__);
  170. return false;
  171. }
  172. return true;
  173. }
  174. }