imchannel.php 3.6 KB

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