forked from kerphi/phpfreechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfcinfo.class.php
112 lines (96 loc) · 3.12 KB
/
pfcinfo.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
require_once dirname(__FILE__)."/pfcglobalconfig.class.php";
require_once dirname(__FILE__)."/pfci18n.class.php";
require_once dirname(__FILE__)."/commands/join.class.php";
class pfcInfo
{
var $c = null;
var $errors = array();
function pfcInfo( $serverid, $data_private_path = "" )
{
// check if the cache already exists
// if it doesn't exists, just stop the process
// because we can't initialize the chat from the external API
if ($data_private_path == "") $data_private_path = dirname(__FILE__)."/../data/private";
$cachefile = pfcGlobalConfig::_GetCacheFile( $serverid, $data_private_path );
if (!file_exists($cachefile))
{
$this->errors[] = _pfc("Error: the cached config file doesn't exists");
return;
}
// then intitialize the pfcglobalconfig
$params = array();
$params["serverid"] = $serverid;
$params["data_private_path"] = $data_private_path;
$this->c =& pfcGlobalConfig::Instance($params);
}
function free()
{
// free the pfcglobalconfig instance
pfcGlobalConfig::Instance(array(), true);
}
/**
* @return array(string) a list of errors
*/
function getErrors()
{
if ($this->c != null)
return array_merge($this->errors, $this->c->getErrors());
else
return $this->errors;
}
function hasErrors()
{
return count($this->getErrors()) > 0;
}
/**
* @param $channel the returned list is the list of nicknames present on the given channel (NULL for the whole server)
* @param $timeout is the time to wait before a nickname is considered offline
* @return array(string) a list of online nicknames
*/
function getOnlineNick($channel = NULL, $timeout = 20)
{
if ($this->hasErrors()) return array();
$ct =& pfcContainer::Instance();
if ($channel != NULL) $channel = pfcCommand_join::GetRecipient($channel);
$res = $ct->getOnlineNick($channel);
$users = array();
if (isset($res["nickid"]))
{
for($i = 0; $i < count($res["nickid"]); $i++)
{
if (time()-$timeout < $res["timestamp"][$i])
$users[] = $ct->getNickname($res["nickid"][$i]);
}
}
return $users;
}
/**
* Return the last $nb message from the $channel room.
* The messages format is very basic, it's raw data (it will certainly change in future)
*/
function getLastMsg($channel, $nb)
{
if ($this->hasErrors()) return array();
// to be sure the $nb params is a positive number
if ( !( $nb >= 0 ) ) $nb = 10;
// to get the channel recipient name
// @todo must use another function to get a private message last messages
$channel = pfcCommand_join::GetRecipient($channel);
$ct =& pfcContainer::Instance();
$lastmsg_id = $ct->getLastId($channel);
$lastmsg_raw = $ct->read($channel, $lastmsg_id-$nb);
return $lastmsg_raw;
}
/**
* Rehash the server config (same as /rehash command)
* Usefull to take into account new server's parameters
*/
function rehash()
{
if ($this->hasErrors()) return false;
$destroyed = $this->c->destroyCache();
return $destroyed;
}
}
?>