-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.php
More file actions
executable file
·194 lines (148 loc) · 5.31 KB
/
convert.php
File metadata and controls
executable file
·194 lines (148 loc) · 5.31 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /usr/bin/env php
<?php
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
require_once __DIR__ . "/vendor/autoload.php";
function getMessages(SplFileInfo $file)
{
// Trillian does not include a root element in the XML files
$xml = "<root>" . $file->getContents() . "</root>";
$dom = new DOMDocument;
$dom->loadXML($xml);
return $dom->getElementsByTagName("message");
}
function timestampToDate($timestamp)
{
$date = new DateTime;
$date->setTimestamp($timestamp / 1000);// Timestamp is in milliseconds
return $date;
}
function getMessageFromElement(DOMElement $element)
{
//<message time="1311612005103" type="outgoing_privateMessage" text="some%20message"/>
$date = timestampToDate($element->getAttribute("time"));
$type = $element->getAttribute("type");
$text = rawurldecode($element->getAttribute("text"));
$messageId = sha1(sprintf("%d/%s/%s", $date->getTimestamp(), $type, $text));
return array
(
$date,
$type,
$text,
$messageId
);
}
function getFilenameFromDate(DateTime $date)
{
//2011-07-25.184005+0200CEST
return $date->format("Y-m-d.HisOT") . ".html";
}
if (!isset($argv[3])) {
fwrite(STDERR, "Usage: " . $argv[0] . " <own username> <path to Trillian logs dir> <output dir> [<template>]\n");
exit(1);
}
$ownName = $argv[1];
$sourceDir = $argv[2];
$outputDir = $argv[3];
$template = $argv[4] ?? "bootstrap";
$loader = new Twig_Loader_Filesystem(__DIR__ . "/templates");
$twig = new Twig_Environment($loader);
$filesystem = new Filesystem;
$finder = new Finder;
$finder->in($sourceDir);
$finder->name("*.xml");
$finder->files();
$perContactFiles = array();
foreach ($finder as $file) {
if (!preg_match("/^(.*)-(.*).xml$/", $file->getFilename(), $matches)) {
fwrite(STDERR, sprintf("Can't parse contact name from filename: %s\n", $file->getFilename()));
continue;
}
$protocol = $matches[1];
$contactName = $matches[2];
if (!isset($perContactFiles[$contactName])) {
$perContactFiles[$contactName] = array();
}
$perContactFiles[$contactName][] = $file;
}
foreach ($perContactFiles as $contactName => $files) {
$perDayMessages = array();
foreach ($files as $file) {
foreach (getMessages($file) as $messageElement) {
/**
* @var $date DateTime
*/
list($date, $type, $text, $messageId) = getMessageFromElement($messageElement);
$day = $date->format("Y-m-d");
if (!isset($perDayMessages[$day])) {
$perDayMessages[$day] = array();
}
if (isset($perDayMessages[$day][$messageId])) {
fwrite(STDERR, sprintf("Skipping duplicate message: %s - %s\n", $date->format("r"), $text));
continue;
}
$perDayMessages[$day][$messageId] = array($date, $type, $text);
}
}
// Remove messageId key
$perDayMessages = array_map("array_values", $perDayMessages);
$perDayMessages = array_values($perDayMessages);
$previousLog = null;
foreach ($perDayMessages as $dayIndex => $dayMessages) {
$messages = array();
$firstDate = null;
foreach ($dayMessages as $message) {
list($date, $type, $text) = $message;
switch ($type) {
case "incoming_privateMessage":
$user = $contactName;
break;
case "outgoing_privateMessage":
$user = $ownName;
break;
default:
fwrite(STDERR, sprintf("Unsupported message type: %s\n", $type));
continue 2;
}
$text = str_replace("\n", "<br/>\n", $text);
// Inline images
$text = preg_replace("/(http|https):\/\/(ft|media).trillian.im\/([^ ]+)/", '<a href="${1}://${2}.trillian.im/${3}" target="_blank"><img src="${1}://${2}.trillian.im/${3}" width="200"/></a>', $text);
if ($firstDate === null) {
$firstDate = $date;
}
$messages[] = array
(
"date" => $date,
"user" => $user,
"type" => $type,
"text" => $text
);
}
if (empty($messages)) {
fwrite(STDERR, "No messages found, skipping\n");
continue;
}
if (isset($perDayMessages[$dayIndex + 1])) {
$nextDate = $perDayMessages[$dayIndex + 1][0][0];
$nextLog = getFilenameFromDate($nextDate);
} else {
$nextLog = null;
}
$outputFilename = getFilenameFromDate($firstDate);
try {
$html = $twig->render(sprintf("%s.twig", $template), array
(
"ownName" => $ownName,
"contactName" => $contactName,
"messages" => array_values($messages),
"previousLog" => $previousLog,
"nextLog" => $nextLog
));
$filesystem->dumpFile(sprintf("%s/%s/%s", $outputDir, $contactName, $outputFilename), $html);
} catch (Exception $exception) {
fwrite(STDERR, $exception->getTraceAsString());
}
$previousLog = $outputFilename;
}
}