This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dietLigrev.php
137 lines (117 loc) · 3.47 KB
/
dietLigrev.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Ligrev;
define("L_DEBUG", 0);
define("L_INFO", 1);
define("L_CAUT", 2);
define("L_WARN", 3);
define("L_AAAA", 4);
// Default error reporting level
define("L_REPORT", L_DEBUG);
// Take over PHP's error handling, since it's a picky whore sometimes.
function php_error_handler($no, $str, $file, $line) {
switch ($no) {
case E_ERROR:
case E_RECOVERABLE_ERROR:
l("[PHP] " . $str . " at " . $file . ":" . $line, L_AAAA);
die();
break;
case E_WARNING:
case E_PARSE:
l("[PHP] " . $str . " at " . $file . ":" . $line, L_WARN);
break;
case E_NOTICE:
l("[PHP] " . $str . " at " . $file . ":" . $line, L_CAUT);
break;
case E_DEPRECATED:
case E_STRICT:
l("[PHP] " . $str . " at " . $file . ":" . $line, L_DEBUG);
break;
default:
l("[PHP] " . $str . " at " . $file . ":" . $line, L_INFO);
break;
}
return true;
}
// Function to log/echo to the console. Includes timestamp and what-not
function l($text, $level = L_INFO) {
// get current log time
$time = date("H:i:s");
switch ($level) {
case L_DEBUG:
$tag = "[\033[0;36mDBUG\033[0m]";
break;
case L_INFO:
$tag = "[\033[0;37mINFO\033[0m]";
default:
break;
case L_CAUT:
$tag = "[\033[0;33mCAUT\033[0m]";
break;
case L_WARN:
$tag = "[\033[0;31mWARN\033[0m]";
break;
case L_AAAA:
$tag = "[\033[41mAAAA\033[0m]";
break;
}
if ($level >= L_REPORT) {
echo "[" . $time . "] " . $tag . " " . html_entity_decode($text) . PHP_EOL;
}
}
set_error_handler("Ligrev\\php_error_handler");
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . DIRECTORY_SEPARATOR . 'lib');
// Hey, let's load some things
l("Reading config.php...");
require_once 'config.php';
l("Loading libraries...");
require_once 'qp.php'; // don't fall for that 2.x crap.
require_once 'JAXL/jaxl.php';
require_once 'MDB2.php';
require_once 'classes/ligrevCommand.php';
require_once 'classes/roster.php';
l("[DB] Connecting to database...");
$db = & \MDB2::singleton($config['db']);
if (\PEAR::isError($db)) {
die($db->getMessage());
}
$db->loadModule('Extended', null, false);
l("[JAXL] Loading JAXL and connecting...");
$client = new \JAXL($config['jaxl']);
$client->require_xep(array(
'0045', // MUC
'0203', // Delayed Delivery
'0199' // XMPP Ping
));
$rooms = array();
$client->add_cb('on_auth_success', function() {
global $client, $config, $rooms;
l("[JAXL] Connected with jid " . $client->full_jid->to_string());
$client->get_vcard();
$client->get_roster();
$client->set_status("", "chat", 10);
foreach ($config['rooms'] as $id => $jid) {
$rooms[$id] = new \XMPPJid($jid . '/' . $config['botname']);
l("[JAXL] Joining room " . $rooms[$id]->to_string());
$client->xeps['0045']->join_room($rooms[$id]);
l("[JAXL] Joined room " . $rooms[$id]->to_string());
}
});
$roster = new roster();
$decks = array();
$client->add_cb('on_auth_failure', function($reason) {
global $client;
$client->send_end_stream();
l("[JAXL] Auth failure: " . $reason, L_WARN);
});
// Where the magic happens. "Magic" "Happens". I dunno why I type this either.
$client->add_cb('on_groupchat_message', function($stanza) {
new ligrevCommand($stanza, "groupchat");
});
$client->add_cb('on_chat_message', function($stanza) {
new ligrevCommand($stanza, "chat");
});
$client->add_cb('on_presence_stanza', function($stanza) {
global $roster;
$roster->ingest($stanza);
});
$client->start();