This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
/
ligrevSim.php
150 lines (130 loc) · 4.01 KB
/
ligrevSim.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
138
139
140
141
142
143
144
145
146
147
148
149
150
<?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 'jwt/src/JWT.php';
require_once 'jwt/src/SignatureInvalidException.php';
require_once 'MDB2.php';
require_once 'ermarian-converters/markov/markov.inc';
require_once 'classes/ligrevCommand.php';
require_once 'classes/roster.php';
require_once 'classes/markov.php';
require_once 'classes/LCN.php';
l("[DB] Connecting to database...");
$db = & \MDB2::singleton($config['db']);
if (\PEAR::isError($db)) {
die($db->getMessage());
}
$db->loadModule('Extended', null, false);
l("Getting identity...");
if ($argc >= 2) {
$source = $argv[1];
$nick = $argv[2];
} else {
l("No identity!", L_AAAA);
die();
}
$config['sim_prefix'] .= $nick;
l("[JAXL] Loading JAXL and connecting...");
$client = new \JAXL($config['jaxl']);
$client->require_xep(array(
'0045', // MUC
'0203', // Delayed Delivery
'0199' // XMPP Ping
));
$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);
$ctrl = new \XMPPJid($config['LCN_control'] . '/' . $config['sim_prefix']);
l("[JAXL] Joining room " . $ctrl->to_string());
$client->xeps['0045']->join_room($ctrl);
l("[JAXL] Joined room " . $ctrl->to_string());
$rooms = new \XMPPJid($config['sim'] . '/' . $config['sim_prefix']);
l("[JAXL] Joining room " . $rooms->to_string());
$client->xeps['0045']->join_room($rooms);
l("[JAXL] Joined room " . $rooms->to_string());
});
$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) {
global $config, $client, $source, $nick;
if (LCN::isLWT($stanza->body)) {
$p = new LCN($stanza->body);
if (!array_key_exists('LCNerror', $p->res) && $p->res['aud'] == 'loungesim_' . $config['sim_prefix']) {
$m = new markov($source);
$client->xeps['0045']->send_groupchat($config['sim'], $m->markoved);
}
}
});
$client->start();