forked from mbaeuerle/whatsapp-iphone-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
139 lines (129 loc) · 3.29 KB
/
dump.go
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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"path"
"strings"
)
func (app *App) DumpSessions(css []Session) {
// index.html
data := `
<frameset cols="25%,*">
<frame src="sessions/index.html">
<frame name="session">
</frameset>
`
filename := path.Join(app.DstDir, "index.html")
ioutil.WriteFile(filename, []byte(data), 0600)
// mkdir sessions
os.Mkdir(path.Join(app.DstDir, "sessions"), 0700)
// sessions.html
tpl := `
<h1>WhatsApp</h1>
{{range .}}
<a href="session_{{ .ID}}.html" target="session">{{ .Name}}</a><br>
{{end}}
`
t, err := template.New("foo").Parse(tpl)
check("DumpSessions template parsing", err)
out, err := os.Create(path.Join(app.DstDir, "sessions", "index.html"))
check("DumpSessions creating file", err)
defer out.Close()
err = t.Execute(out, css)
check("DumpSessions executing template", err)
}
func (app *App) DumpSession(session Session, messages []Message) {
tpl := `
<style><!--
body {
background:rgb(229,221,213);
}
.chat {
width:600px;
margin:auto;
}
.message {
margin: 0.4em;
padding: 0.5em;
min-height: 1.5em;
}
.message img { max-width: 100%; display: block; }
.message video { max-width: 100%; display: block; }
.incoming {
background:white;
}
.outgoing {
background:rgb(221,247,200);
text-align:right;
}
.name {
display: inline-block;
color: darkslategray;
font-size: 1.05em;
}
.date {
display: inline-block;
color: darkslategray;
padding-left: 0.5em;
font-size: 0.90em;
}
.content {
display: block;
padding-top: .5em;
}
--></style>
<h1>WhatsApp</h1>
<div class="chat">
{{range .}}
<p class="message {{if .JID}}incoming{{else}}outgoing{{end}}">
{{ if and .Name .JID }}
<span class="name">{{ .Name}}</span>
{{ end }}
<span class="date">{{ .Date }}</span>
<span class="content">
{{ nl2br .Text }}
{{ if eq .MediaExt ".jpg" }}
<img loading="lazy" src="../{{.Media}}">
{{ else if eq .MediaExt ".png" }}
<img loading="lazy" src="../{{.Media}}">
{{ else if eq .MediaExt ".webp" }}
<img loading="lazy" src="../{{.Media}}">
{{ else if eq .MediaExt ".mp4" }}
<video preload="metadata" controls>
<source src="../{{.Media}}" type="video/mp4">
</video>
{{ else if eq .MediaExt ".opus" }}
<audio preload="none" src="../{{.Media}}" controls />
Your browser does not support the audio element. Try Firefox.
</audio>
{{ else if eq .MediaExt ".mp3" }}
(MP3) <audio preload="none" src="../{{.Media}}" controls />
Your browser does not support the audio element. Try Firefox.
</audio>
{{ else if .MediaExt }}
<a href="../{{.Media}}" />FILE: {{.Media}}</a>
{{end}}
</span>
</p>
{{end}}
</div><!-- chat -->
`
funcs := template.FuncMap{
"nl2br": func(text string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
},
}
t, err := template.New("foo").Funcs(funcs).Parse(tpl)
check("DumpSession template parsing", err)
out, err := os.Create(path.Join(app.DstDir, "sessions", fmt.Sprintf("session_%d.html", session.ID)))
check("DumpSession creating file", err)
defer out.Close()
//optional: only dump last 30 messages per chat to HTML
//if len(messages) > 30 {
// messages = messages[len(messages)-30:]
//}
err = t.Execute(out, messages)
check("DumpSession executing template", err)
}