1: <?php
2:
3: namespace Budabot\Core;
4:
5: /*
6: * $Id: aochat.php,v 1.1 2006/12/08 15:17:54 genesiscl Exp $
7: *
8: * Modified to handle the recent problem with the integer overflow
9: *
10: * Copyright (C) 2002-2005 Oskari Saarenmaa <auno@auno.org>.
11: *
12: * AOChat, a PHP class for talking with the Anarchy Online chat servers.
13: * It requires the sockets extension (to connect to the chat server..)
14: * from PHP 4.2.0+ and either the GMP or BCMath extension (for generating
15: * and calculating the login keys) to work.
16: *
17: * A disassembly of the official java chat client[1] for Anarchy Online
18: * and Slicer's AO::Chat perl module[2] were used as a reference for this
19: * class.
20: *
21: * [1]: <http://www.anarchy-online.com/content/community/forumsandchat/>
22: * [2]: <http://www.hackersquest.org/ao/>
23: *
24: * Updates to this class can be found from the following web site:
25: * http://auno.org/dev/aochat.html
26: *
27: **************************************************************************
28: *
29: * This program is free software; you can redistribute it and/or modify
30: * it under the terms of the GNU General Public License as published by
31: * the Free Software Foundation; either version 2 of the License, or
32: * (at your option) any later version.
33: *
34: * This program is distributed in the hope that it will be useful, but
35: * WITHOUT ANY WARRANTY; without even the implied warranty of
36: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37: * General Public License for more details.
38: *
39: * You should have received a copy of the GNU General Public License
40: * along with this program; if not, write to the Free Software
41: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
42: * USA
43: *
44: */
45:
46: /* Prioritized chat message queue. */
47:
48: define('AOC_PRIORITY_HIGH', 1000);
49: define('AOC_PRIORITY_MED', 500);
50: define('AOC_PRIORITY_LOW', 100);
51:
52: class AOChatQueue {
53:
54: var $queue;
55: var $qsize; // the number of items in the queue for any priority
56: var $point; // the next time we can send a message
57: var $limit; // the amount of messages that can be sent before metering kicks in
58: var $increment; // the amount of time in seconds to wait after the limit has been reached
59:
60: function __construct($limit, $increment) {
61: $this->limit = $limit;
62: $this->increment = $increment;
63: $this->point = 0;
64: $this->queue = array();
65: $this->qsize = 0;
66: }
67:
68: function push($priority, $item) {
69: $now = time();
70:
71: if (isset($this->queue[$priority])) {
72: $this->queue[$priority][] = $item;
73: } else {
74: $this->queue[$priority] = array($item);
75: krsort($this->queue);
76: }
77: $this->qsize++;
78: }
79:
80: function getNext() {
81: if ($this->qsize === 0) {
82: return null;
83: }
84: $now = time();
85: if ($this->point > $now) {
86: return null;
87: }
88:
89: forEach (array_keys($this->queue) as $priority) {
90: while (true) {
91: $item = array_shift($this->queue[$priority]);
92: if ($item === null) {
93: unset($this->queue[$priority]);
94: break;
95: }
96:
97: // $limit specifies how much buffer we have
98: // this check makes sure we don't go beyond that buffer
99: if ($this->point < ($now - $this->limit)) {
100: $this->point = $now - $this->limit;
101: }
102:
103: $this->point += $this->increment;
104: $this->qsize--;
105: return $item;
106: }
107: }
108: return null;
109: }
110: }
111: