-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
253 lines (211 loc) · 5.1 KB
/
main.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Evan Widloski - 2020-03-10
// thanks to https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/google/uuid"
)
var (
CONN_HOST = "0.0.0.0"
CONN_PORT = "9100"
LOG_LEVEL = "error"
XOCHITL_DIR = "/home/root/.local/share/remarkable/xochitl/"
)
const METADATA_TEMPLATE = `{
"deleted": false,
"lastModified": "%d000",
"lastOpened": "0",
"lastOpenedPage": 0,
"metadatamodified": true,
"modified": true,
"parent": "",
"pinned": false,
"synced": false,
"type": "DocumentType",
"version": 0,
"visibleName": "%v"
}
`
const CONTENT_TEMPLATE = "{}"
func main() {
// ----- Parse options -----
debug_flag := flag.Bool("debug", false, "enable debug output")
test := flag.Bool("test", false, "use /tmp as output dir")
restart := flag.Bool("restart", false, "restart xochitl after saving PDF")
CONN_HOST := flag.String("host", CONN_HOST, "override bind address")
CONN_PORT := flag.String("port", CONN_PORT, "override bind port")
flag.Parse()
if *debug_flag {
LOG_LEVEL = "debug"
debug("Debugging enabled")
}
if *test {
XOCHITL_DIR = "/tmp/"
}
// ----- Listen for connections -----
// Listen for incoming connections.
var l net.Listener
var err error
var isSocketActivated = os.Getenv("LISTEN_PID") == strconv.Itoa(os.Getpid())
if isSocketActivated {
l, err = net.FileListener(os.NewFile(3, "systemd-socket"))
fmt.Println("Listening on systemd-socket")
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
handleRequest(conn)
if *restart {
restartUISoftware()
}
} else {
l, err = net.Listen("tcp", *CONN_HOST+":"+*CONN_PORT)
fmt.Println("Listening on " + *CONN_HOST + ":" + *CONN_PORT)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
handleRequest(conn)
if *restart {
restartUISoftware()
}
}
}
check(err)
defer l.Close() // Close the listener when the application closes.
}
func debug(msg ...string) {
if LOG_LEVEL == "debug" {
for _, value := range msg {
fmt.Print(value)
fmt.Print(" ")
}
fmt.Println()
}
}
func check(e error) {
if e != nil {
panic(e)
}
}
// Handles incoming requests.
func handleRequest(conn net.Conn) {
u, _ := uuid.NewRandom()
pdf_path := XOCHITL_DIR + u.String() + ".pdf"
fmt.Println("Saving PDF to:", pdf_path)
// ----- Create .pdf -----
f, err := os.Create(pdf_path)
check(err)
reader := bufio.NewReader(conn)
// Default name of new document
title := "Printed"
// Read until start of PDF
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
fmt.Println("Couldn't find PDF start")
// Clean up
os.Remove(pdf_path)
os.Exit(1)
}
check(err)
debug(strings.TrimRight(line, "\n"))
// set print job name as file title
if strings.HasPrefix(line, "@PJL JOB NAME") {
title = strings.Split(line, "\"")[1]
debug("Setting title to:", title)
}
// PDF section started
if strings.HasPrefix(line, "%PDF-") {
debug("PDF begin")
_, err := f.WriteString(line)
check(err)
break
}
}
// Read until end of PDF
// ----------
// line: 116
// linelen: 5
// err: <nil>
// ----------
// line: %%EOF
// linelen: 7
// err: <nil>
// ----------
// line:
// linelen: 0
// err: EOF
// Couldn't find PDF end
// ----------
// line: 924148
// linelen: 7
// err: <nil>
// ----------
// line: %%EOF
// linelen: 5
// err: EOF
// Saving metadata to /tmp/49ea3977-acfa-4789-908e-fae620f6c617.metadata
// Saving content file to /tmp/49ea3977-acfa-4789-908e-fae620f6c617.content
last := ""
for {
line, err := reader.ReadString('\n')
// end of pdf file
if (err == io.EOF) {
if strings.HasPrefix(line, "%%EOF") || strings.HasPrefix(last, "%%EOF") {
_, err = f.WriteString(line)
f.Close()
break
} else {
debug("Couldn't find PDF end")
os.Remove(pdf_path)
os.Exit(1)
}
}
check(err)
_, err = f.WriteString(line)
if len(line) > 0 {
last = line
}
}
// ----- Create .metadata -----
meta_path := XOCHITL_DIR + u.String() + ".metadata"
fmt.Println("Saving metadata to", meta_path)
f, err = os.Create(meta_path)
f.WriteString(fmt.Sprintf(METADATA_TEMPLATE, time.Now().Unix(), title))
f.Close()
// ----- Create .content -----
cont_path := XOCHITL_DIR + u.String() + ".content"
fmt.Println("Saving content file to", cont_path)
f, err = os.Create(cont_path)
f.WriteString(fmt.Sprintf(CONTENT_TEMPLATE))
f.Close()
conn.Close()
}
// Restarts xochitl or other UI software
func restartUISoftware() {
services := []string{"xochitl", "remux", "tarnish", "draft"}
for _, service := range services {
_, exitcode := exec.Command("systemctl", "is-active", service).CombinedOutput()
if exitcode == nil {
fmt.Println("Restarting " + service)
stdout, err := exec.Command("systemctl", "restart", service).CombinedOutput()
if err != nil {
fmt.Println(service+" restart failed with message:", string(stdout))
}
}
}
}