1: <?php
2:
3: namespace Budabot\Core;
4:
5: 6: 7:
8: class BuddylistManager {
9:
10:
11: public $chatBot;
12:
13:
14: public $logger;
15:
16: public $buddyList = array();
17:
18: 19: 20: 21:
22: public function isOnline($name) {
23: if (strtolower($this->chatBot->vars['name']) == strtolower($name)) {
24: return 1;
25: } else {
26: $buddy = $this->getBuddy($name);
27: return ($buddy === null ? null : $buddy['online']);
28: }
29: }
30:
31: public function getBuddy($name) {
32: $uid = $this->chatBot->get_uid($name);
33: if ($uid === false || !isset($this->buddyList[$uid])) {
34: return null;
35: } else {
36: return $this->buddyList[$uid];
37: }
38: }
39:
40: public function add($name, $type) {
41: $uid = $this->chatBot->get_uid($name);
42: if ($uid === false || $type === null || $type == '') {
43: return false;
44: } else {
45: if (!isset($this->buddyList[$uid])) {
46: $this->logger->log('debug', "$name buddy added");
47: if ($this->chatBot->vars['use_proxy'] != 1 && count($this->buddyList) > 999) {
48: $this->logger->log('error', "Error adding '$name' to buddy list--buddy list is full");
49: }
50: $this->chatBot->buddy_add($uid);
51: }
52:
53: if (!isset($this->buddyList[$uid]['types'][$type])) {
54: $this->buddyList[$uid]['types'][$type] = 1;
55: $this->logger->log('debug', "$name buddy added (type: $type)");
56: }
57:
58: return true;
59: }
60: }
61:
62: public function remove($name, $type = '') {
63: $uid = $this->chatBot->get_uid($name);
64: if ($uid === false) {
65: return false;
66: } else if (isset($this->buddyList[$uid])) {
67: if (isset($this->buddyList[$uid]['types'][$type])) {
68: unset($this->buddyList[$uid]['types'][$type]);
69: $this->logger->log('debug', "$name buddy type removed (type: $type)");
70: }
71:
72: if (count($this->buddyList[$uid]['types']) == 0) {
73: $this->logger->log('debug', "$name buddy removed");
74: $this->chatBot->buddy_remove($uid);
75: }
76:
77: return true;
78: } else {
79: return false;
80: }
81: }
82:
83: public function update($args) {
84: $sender = $this->chatBot->lookup_user($args[0]);
85:
86:
87: list($bid, $bonline, $btype) = $args;
88: $this->buddyList[$bid]['uid'] = $bid;
89: $this->buddyList[$bid]['name'] = $sender;
90: $this->buddyList[$bid]['online'] = ($bonline ? 1 : 0);
91: $this->buddyList[$bid]['known'] = (ord($btype) ? 1 : 0);
92: }
93:
94: public function updateRemoved($args) {
95: $bid = $args[0];
96: unset($this->buddyList[$bid]);
97: }
98: }
99: