/
main.go
132 lines (114 loc) · 2.79 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
package main
import (
"cjones.org/hg/go-xmpp2.hg/xmpp"
"github.com/alyu/configparser"
"log"
"os"
"flag"
"crypto/tls"
"fmt"
"strings"
"regexp"
"os/exec"
"encoding/xml"
)
var (
statuses = map[xmpp.Status]string{
xmpp.StatusUnconnected : "StatusUnconnected",
xmpp.StatusConnected : "StatusConnected",
xmpp.StatusConnectedTls : "StatusConnectedTLS",
xmpp.StatusAuthenticated : "StatusAuthenticated",
xmpp.StatusBound : "StatusBound",
xmpp.StatusRunning : "StatusRunning",
xmpp.StatusShutdown : "StatusShutdown",
xmpp.StatusError : "StatusError",
}
)
func usage() {
fmt.Printf("usage: %s conf.ini\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
func process(msg *xmpp.Message, c *xmpp.Client) {
body := ""
if len(msg.Body) > 0 {
body = strings.ToLower(msg.Body[0].Chardata)
}
if body == "" {
return
}
pingRe, _ := regexp.Compile(`ping\s+(.*)`)
if pingRe.MatchString(body) {
re := pingRe.FindStringSubmatch(body)
log.Printf("ping %s for %s", re[1], msg.From.Bare())
out, _ := exec.Command("ping", "-c5", re[1]).CombinedOutput()
c.Send <- makeReplay(msg, string(out))
}
}
func makeReplay(msg *xmpp.Message, body string) *xmpp.Message {
reply := &xmpp.Message{}
reply.From = msg.To
reply.To = msg.From
reply.Id = xmpp.NextId()
reply.Type = "chat"
reply.Lang = "en"
if msg.Thread != nil {
reply.Thread = &xmpp.Data{XMLName: xml.Name{Local: "thread"}, Chardata: msg.Thread.Chardata}
}
reply.Body = []xmpp.Text{{XMLName: xml.Name{Local: "body"}, Chardata: body}}
return reply
}
func main() {
flag.Usage = usage
debug := flag.Bool("d", false, "debug")
conffile := flag.String("c", "config.ini", "config file")
flag.Parse()
if *debug {
xmpp.Debug = true
}
config, err := configparser.Read(*conffile)
if err != nil {
log.Fatal(*conffile, err)
}
section, err := config.Section("global")
if err != nil {
log.Fatal(err)
}
jid := xmpp.JID(section.ValueOf("jid"))
pass := section.ValueOf("pass")
status := make(chan xmpp.Status, 10)
go func() {
for s := range status {
log.Printf("connection status %s", statuses[s])
}
}()
jid = xmpp.JID(fmt.Sprintf("%s/%s", jid.Bare(), "bot"))
c, err := xmpp.NewClient(&jid, pass, tls.Config{InsecureSkipVerify: true}, nil, xmpp.Presence{}, status)
if err != nil {
log.Fatalf("NewClient(%v): %v", jid, err)
}
defer c.Close()
for {
select {
case s := <-status:
log.Printf("connection status %s", statuses[s])
if s.Fatal() {
return
}
case st, ok := <-c.Recv:
if !ok {
return
}
if m, ok := st.(*xmpp.Message); ok {
log.Printf("msg from %s", m.From.Bare())
process(m, c)
}
if p, ok := st.(*xmpp.Presence); ok {
log.Printf("presence from %s", p.From.Bare())
}
if iq, ok := st.(*xmpp.Iq); ok {
log.Printf("iq from %s", iq.From.Bare())
}
}
}
}