This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
duke_nuked.php
92 lines (76 loc) · 2.85 KB
/
duke_nuked.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
<?
if (php_sapi_name() !== 'cli')
die('Must be run from the command line.');
require_once 'include/Global.php';
$lastMessageIdFilePath = '/mnt/websites/_private/duke_nuked_last_message_id.txt';
$passwordFilePath = '/mnt/websites/_private/duke_nuked_password.txt';
$threadIdFilePath = '/mnt/websites/_private/duke_nuked_thread.txt';
$slackTokenFilePath = '/mnt/websites/_private/slack_token.txt';
function buildQueryString($query)
{
$x = array();
foreach ($query as $key => $value)
{
$x[] = urlencode($key) . '=' . urlencode($value);
}
return implode('&', $x);
}
$lastMessageId = intval(trim(file_get_contents($lastMessageIdFilePath)));
$password = trim(file_get_contents($passwordFilePath));
$threadId = intval(trim(file_get_contents($threadIdFilePath)));
if ($threadId <= 0)
{
echo "No thread ID set!\n";
# This requires manual intervention. Inhibit the cycle from continuing. Admin has to Ctrl+C and fix it.
while (true) { sleep(60); }
}
$messagesPage = MessageParser()->getMessages('inbox', 'Duke Nuked', $password, 1, 0);
$messages = $messagesPage['messages'];
$maxId = 0;
foreach ($messages as $message)
{
$id = intval($message['id']);
$unread = $message['unread'] == 1;
if ($id > $maxId)
{
$maxId = $id;
file_put_contents($lastMessageIdFilePath, $maxId);
if (intval(trim(file_get_contents($lastMessageIdFilePath))) != $maxId)
{
echo "Failed to write to file!\n";
# This requires manual intervention. Inhibit the cycle from continuing. Admin has to Ctrl+C and fix it.
while (true) { sleep(60); }
}
}
if ($id > $lastMessageId && $unread)
{
MessageParser()->markMessageAsRead('Duke Nuked', $password, $id);
$body =
'From: y{' . $message['from'] . "}y\r\n" .
'Subject: ' . $message['subject'] . "\r\n" .
'Date: ' . date('D M j, Y H:i', strtotime($message['date'])) . "\r\n" .
"\r\n" .
strip_tags($message['body']);
echo "$body\n";
ChattyParser()->post('Duke Nuked', $password, $threadId, 0, $body, 99, 99);
if (file_exists($slackTokenFilePath))
{
# Also post on Slack.
$slackBody =
'From: ' . $message['from'] . "\r\n" .
'Subject: ' . $message['subject'] . "\r\n" .
"\r\n" .
strip_tags($message['body']);
$slackQueryString = buildQueryString(array(
'token' => trim(file_get_contents($slackTokenFilePath)),
'channel' => '#duke-nuked',
'text' => htmlspecialchars($slackBody, ENT_NOQUOTES),
'username' => 'duke nuked',
'parse' => 'none',
'link_names' => 0));
$slackUrl = 'https://slack.com/api/chat.postMessage?' . $slackQueryString;
$slackResult = file_get_contents($slackUrl);
print_r($slackResult);
}
}
}