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