This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmd-textmessage.go
98 lines (88 loc) · 2.04 KB
/
cmd-textmessage.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
package main
import (
"encoding/json"
"io"
"os"
"os/exec"
"layeh.com/murmur-cli/MurmurRPC"
)
func init() {
cmd := root.Add("textmessage")
cmd.Add("send", func(args Args) {
server := args.MustServer(0)
if len(args) <= 1 {
panic("not enough arguments")
}
text := args.MustString(len(args) - 1)
tm := &MurmurRPC.TextMessage{
Server: server,
Text: &text,
}
for i := 1; i < len(args)-1; i++ {
if session, ok := args.PrefixedUint32("sender:", i); ok && tm.Actor == nil {
tm.Actor = &MurmurRPC.User{
Server: server,
Session: &session,
}
} else if session, ok := args.PrefixedUint32("user:", i); ok {
tm.Users = append(tm.Users, &MurmurRPC.User{
Server: server,
Session: &session,
})
} else if id, ok := args.PrefixedUint32("channel:", i); ok {
tm.Channels = append(tm.Channels, &MurmurRPC.Channel{
Server: server,
Id: &id,
})
} else if id, ok := args.PrefixedUint32("tree:", i); ok {
tm.Trees = append(tm.Trees, &MurmurRPC.Channel{
Server: server,
Id: &id,
})
} else {
panic("unknown argument")
}
}
Output(client.TextMessageSend(ctx, tm))
})
cmd.Add("filter", func(args Args) {
server := args.MustServer(0)
program := args.MustString(1)
arguments := args[2:]
stream, err := client.TextMessageFilter(ctx)
if err != nil {
panic(err)
}
msg := MurmurRPC.TextMessage_Filter{
Server: server,
}
if err := stream.Send(&msg); err != nil {
panic(err)
}
for {
req, err := stream.Recv()
if err != nil {
if err != io.EOF {
panic(err)
}
return
}
var resp MurmurRPC.TextMessage_Filter
cmd := exec.Command(program, arguments...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
pipe, err := cmd.StdinPipe()
if err == nil {
encoder := json.NewEncoder(pipe)
go encoder.Encode(req.Message)
cmd.Run()
if cmd.ProcessState != nil {
if !cmd.ProcessState.Success() {
resp.Action = MurmurRPC.TextMessage_Filter_Reject.Enum()
}
}
}
stream.Send(&resp)
}
})
}