imchannel.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. class IMChannel extends Channel
  21. {
  22. var $imPlugin;
  23. function source()
  24. {
  25. return $imPlugin->transport;
  26. }
  27. function __construct($imPlugin)
  28. {
  29. $this->imPlugin = $imPlugin;
  30. }
  31. function on($user)
  32. {
  33. return $this->setNotify($user, 1);
  34. }
  35. function off($user)
  36. {
  37. return $this->setNotify($user, 0);
  38. }
  39. function output($user, $text)
  40. {
  41. $text = '['.common_config('site', 'name') . '] ' . $text;
  42. $this->imPlugin->sendMessage($this->imPlugin->getScreenname($user), $text);
  43. }
  44. function error($user, $text)
  45. {
  46. $text = '['.common_config('site', 'name') . '] ' . $text;
  47. $screenname = $this->imPlugin->getScreenname($user);
  48. if($screenname){
  49. $this->imPlugin->sendMessage($screenname, $text);
  50. return true;
  51. }else{
  52. common_log(LOG_ERR,
  53. 'Could not send error message to user ' . common_log_objstring($user) .
  54. ' on transport ' . $this->imPlugin->transport .' : user preference does not exist');
  55. return false;
  56. }
  57. }
  58. function setNotify($user, $notify)
  59. {
  60. global $_PEAR;
  61. $user_im_prefs = new User_im_prefs();
  62. $user_im_prefs->transport = $this->imPlugin->transport;
  63. $user_im_prefs->user_id = $user->id;
  64. if($user_im_prefs->find() && $user_im_prefs->fetch()){
  65. if($user_im_prefs->notify == $notify){
  66. //notify is already set the way they want
  67. return true;
  68. }else{
  69. $original = clone($user_im_prefs);
  70. $user_im_prefs->notify = $notify;
  71. $result = $user_im_prefs->update($original);
  72. if (!$result) {
  73. $last_error = &$_PEAR->getStaticProperty('DB_DataObject','lastError');
  74. common_log(LOG_ERR,
  75. 'Could not set notify flag to ' . $notify .
  76. ' for user ' . common_log_objstring($user) .
  77. ' on transport ' . $this->imPlugin->transport .' : ' . $last_error->message);
  78. return false;
  79. } else {
  80. common_log(LOG_INFO,
  81. 'User ' . $user->nickname . ' set notify flag to ' . $notify);
  82. return true;
  83. }
  84. }
  85. }else{
  86. common_log(LOG_ERR,
  87. 'Could not set notify flag to ' . $notify .
  88. ' for user ' . common_log_objstring($user) .
  89. ' on transport ' . $this->imPlugin->transport .' : user preference does not exist');
  90. return false;
  91. }
  92. }
  93. }