1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use Smr\Database; |
4
|
|
|
use Smr\Irc\Exceptions\Timeout; |
5
|
|
|
|
6
|
|
|
function echo_r(string $message): void { |
7
|
|
|
echo date('Y-m-d H:i:s => ') . $message . EOL; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
// config file |
11
|
|
|
require_once(realpath(dirname(__FILE__)) . '/../../bootstrap.php'); |
12
|
|
|
|
13
|
|
|
// timer events |
14
|
|
|
$events = []; |
15
|
|
|
|
16
|
|
|
// supply/demand list |
17
|
|
|
$sds = []; |
18
|
|
|
|
19
|
|
|
$debugging = false; |
20
|
|
|
foreach ($argv as $arg) { |
21
|
|
|
if ($arg == '-debug') { |
22
|
|
|
$debugging = true; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
define('IRC_DEBUGGING', $debugging); |
26
|
|
|
|
27
|
|
|
require_once(TOOLS . 'irc/stream.php'); |
28
|
|
|
|
29
|
|
|
// after a timeout we start over |
30
|
|
|
while (true) { |
31
|
|
|
|
32
|
|
|
// delete all seen stats that appear to be on (we do not want to take |
33
|
|
|
// something for granted that happend while we were away) |
34
|
|
|
$db = Database::getInstance(); |
35
|
|
|
$db->write('DELETE from irc_seen WHERE signed_off = 0'); |
36
|
|
|
|
37
|
|
|
// Reset last ping each time we try connecting. |
38
|
|
|
$last_ping = time(); |
39
|
|
|
|
40
|
|
|
echo_r('Connecting to ' . IRC_BOT_SERVER_ADDRESS); |
41
|
|
|
$fp = fsockopen(IRC_BOT_SERVER_ADDRESS, IRC_BOT_SERVER_PORT); |
42
|
|
|
|
43
|
|
|
if (!$fp) { |
44
|
|
|
// network troubles |
45
|
|
|
echo_r('There was an error connecting to ' . IRC_BOT_SERVER_ADDRESS . '/' . IRC_BOT_SERVER_PORT); |
46
|
|
|
|
47
|
|
|
// sleep and try again! |
48
|
|
|
sleep(60); |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
echo_r('Socket ' . $fp . ' is connected...'); |
|
|
|
|
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
echo_r('Identifying...'); |
56
|
|
|
safefputs($fp, 'NICK ' . IRC_BOT_NICK . EOL); |
57
|
|
|
safefputs($fp, 'USER ' . IRC_BOT_USER . EOL); |
58
|
|
|
|
59
|
|
|
sleep(3); |
60
|
|
|
|
61
|
|
|
safefputs($fp, 'NICKSERV IDENTIFY ' . IRC_BOT_PASS . EOL); |
62
|
|
|
|
63
|
|
|
sleep(5); |
64
|
|
|
|
65
|
|
|
if (!IRC_DEBUGGING) { |
66
|
|
|
// join our public channels |
67
|
|
|
$joinChannels = ['#smr', '#smr-bar']; |
68
|
|
|
|
69
|
|
|
// join all alliance channels |
70
|
|
|
$dbResult = $db->read('SELECT channel |
71
|
|
|
FROM irc_alliance_has_channel |
72
|
|
|
JOIN game USING (game_id) |
73
|
|
|
WHERE join_time < ' . time() . ' |
74
|
|
|
AND end_time > ' . time()); |
75
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
76
|
|
|
$joinChannels[] = $dbRecord->getString('channel'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// now do the actual joining |
80
|
|
|
foreach ($joinChannels as $channel) { |
81
|
|
|
safefputs($fp, 'JOIN ' . $channel . EOL); |
82
|
|
|
sleep(1); |
83
|
|
|
safefputs($fp, 'WHO ' . $channel . EOL); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
stream_set_blocking($fp, true); |
88
|
|
|
while (!feof($fp)) { |
89
|
|
|
readFromStream($fp); |
90
|
|
|
// Close database connection between calls to avoid |
91
|
|
|
// stale or timed out server errors. |
92
|
|
|
Database::resetInstance(); |
93
|
|
|
} |
94
|
|
|
fclose($fp); // close socket |
95
|
|
|
|
96
|
|
|
} catch (Timeout) { |
97
|
|
|
// Ignore the timeout exception, we'll loop round and reconnect. |
98
|
|
|
} |
99
|
|
|
} // end of while running |
100
|
|
|
|