forked from Evidlo/remarkable_printer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (140 loc) · 3.27 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
// Evan Widloski - 2020-03-10
// thanks to https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go
package main
import (
"fmt"
"net"
"os"
"os/exec"
"bufio"
"strings"
"io"
"flag"
"github.com/google/uuid"
"time"
)
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",
"metadatamodified": true,
"modified": true,
"parent": "",
"pinned": false,
"synced": false,
"type": "DocumentType",
"version": 0,
"visibleName": "%v"
}
`
func main() {
// ----- Parse options -----
debug := 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 {
LOG_LEVEL = "debug"
}
if *test {
XOCHITL_DIR = "/tmp/"
}
// ----- Listen for connections -----
// Listen for incoming connections.
l, err := net.Listen("tcp", *CONN_HOST + ":" + *CONN_PORT)
check(err)
defer l.Close()
// Close the listener when the application closes.
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)
// Restart xochitl
if *restart {
stdout, err := exec.Command("systemctl", "restart", "xochitl").CombinedOutput()
if err != nil {
fmt.Println("xochitl restart failed with message:", string(stdout))
}
}
}
}
func debug(msg ...string) {
if LOG_LEVEL == "debug" {
fmt.Println(msg)
}
}
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')
debug(line)
// 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
}
if err == io.EOF {
fmt.Println("Couldn't find PDF start")
os.Exit(1)
}
check(err)
}
// Read until end of PDF
for {
line, err := reader.ReadString('\n')
_, err = f.WriteString(line)
check(err)
// end of pdf file
if strings.HasPrefix(line, "%%EOF") {
f.Close()
break
}
if err == io.EOF {
debug(line)
debug("Couldn't find PDF end")
os.Exit(1)
}
check(err)
}
// ----- 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()
conn.Close()
}