-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
126 lines (97 loc) · 2.94 KB
/
handlers.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
package main
import (
"github.com/reiver/go-oi"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"unicode"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
// https://stackoverflow.com/a/26722698
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func newsHandler(stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser, args ...string) error {
var url string = "http://swapi.savla.su/news/krusty/"
// try URL
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
client := &http.Client{}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Token", os.Getenv("SWAPI_TOKEN"))
// get the raw Body
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// read Body
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print(err)
return err
}
// parse JSON stream into Go object
var newsStream News
if err := json.Unmarshal(data, &newsStream); err != nil {
log.Print(err)
return err
}
// loop over news items
for i, item := range newsStream.News {
// hardcoded paging limit for VTxxx terminals
if i > 4 {
break
}
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
title, _, _ := transform.String(t, item.Title)
perex, _, _ := transform.String(t, item.Perex)
oi.LongWriteString(stdout, fmt.Sprintf("%s\n\r", title))
oi.LongWriteString(stdout, fmt.Sprintf("[ %s / %s ]\n\r\n\r", item.PubDate, item.Server))
oi.LongWriteString(stdout, fmt.Sprintf("%s\n\r\n\r", perex))
oi.LongWriteString(stdout, fmt.Sprintf("--------------------------------------------\n\n"))
}
return nil
}
/*
* helpHandler()
* currently not used due to the circle/cycle reference
*/
func helpHandler(stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser, args ...string) error {
for _, cmd := range cmds {
oi.LongWriteString(stdout, "\n " + cmd.name + " - " + cmd.description)
}
oi.LongWriteString(stdout, "\r\n\r")
return nil
}
func versionHandler(stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser, args ...string) error {
oi.LongWriteString(stdout, "\n\rbbs-go telnet service")
oi.LongWriteString(stdout, "\n\rversion: " + version + "\n\r\r\n")
return nil
}
func fiveHandler(stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser, args ...string) error {
oi.LongWriteString(stdout, "The number FIVE looks like this: 5\r\n")
return nil
}
func danceHandler(stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser, args ...string) error {
for i:=0; i<20; i++ {
oi.LongWriteString(stdout, "\r/")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r-")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r\\")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r|")
time.Sleep(50*time.Millisecond)
}
oi.LongWriteString(stdout, "\r \r\n")
return nil
}