+class Delay_Manager
+{
+ var $delta;
+ var $lastckeck;
+ var $triglevel;
+
+ function Delay_Manager($triglevel)
+ {
+ $this->triglevel = $triglevel;
+ $this->delta = array();
+ $this->lastcheck = 0;
+ }
+
+ function delta_get($curtime)
+ {
+ // clean too old delta items
+ for ($i = 0 ; $i < count($this->delta) ; $i++) {
+ if ($this->delta[$i][0] < $curtime) {
+ array_splice($this->delta, $i, 1);
+ $i--;
+ }
+ }
+
+ // add new delta items if delay exceeded $this->triglevel sec
+ if ($curtime > $this->lastcheck + $this->triglevel && $curtime < $this->lastcheck + 1200.0) {
+ $delta = $curtime - $this->lastcheck - $this->triglevel;
+ array_push($this->delta, array($curtime + $delta , $delta));
+ // fprintf(STDERR, "DELTA: add new delta [%f] [%f] [%f]\n", $this->triglevel, $curtime + $delta, $delta);
+ }
+
+ // extract the maximum valid delta
+ $delta_max = 0.0;
+ for ($i = 0 ; $i < count($this->delta) ; $i++) {
+ $delta_cur = $this->delta[$i][1];
+ if ($delta_max < $delta_cur)
+ $delta_max = $delta_cur;
+ }
+
+ // fprintf(STDERR, "DELTA: status %d, delta_max: %f\n", count($this->delta), $delta_max);
+
+ return ($delta_max);
+ }
+
+ function lastcheck_set($curtime)
+ {
+ $this->lastcheck = $curtime;
+ }
+}
+
+class Client_prefs {
+ var $listen;
+ var $supp_comp;
+
+ function Client_prefs()
+ {
+ }
+
+ static function from_user($user)
+ {
+ $thiz = new Client_prefs();
+ $thiz->user_load($user);
+
+ return ($thiz);
+ }
+
+ static function from_json($json)
+ {
+ $thiz = new Client_prefs();
+ if ($thiz->json_load($json) == FALSE) {
+ unset($thiz);
+ return (FALSE);
+ }
+
+ return ($thiz);
+ }
+
+ function user_load($user)
+ {
+ fprintf(STDERR, "QQ %s: %x\n", __FUNCTION__, $user->flags);
+ $this->listen = ($user->flags & USER_FLAG_MAP_AUTH) >> 2;
+ if ($user->rec != FALSE) {
+ $this->supp_comp = $user->rec->supp_comp_get();
+ }
+ else {
+ $this->supp_comp = "000000000000";
+ }
+
+ fprintf(STDERR, "QQ %s: LISTEN: %d\n", __FUNCTION__, $this->listen);
+ }
+
+ function json_load($json_s)
+ {
+ $ret = FALSE;
+
+ do {
+ if (gettype($json_s) == "string") {
+ if (($json = json_decode($json_s)) == FALSE)
+ break;
+ }
+ else {
+ $json = $json_s;
+ }
+ if ($this->listen < 0 || $this->listen > 2)
+ break;
+ $this->listen = $json->listen;