-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_xmpp.go
98 lines (85 loc) · 2.36 KB
/
my_xmpp.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 (
"crypto/tls"
"flag"
"fmt"
"github.com/mattn/go-xmpp"
"log"
"os"
"strings"
)
var talk *xmpp.Client
type MyXMPP struct {
ServerAddess string //include port
Name string //include host/resource
Password string
SSL bool
}
type MyXMPPAction interface {
Connect() error
GetMessage() (string, string, error)
SendMessage(to string, msg string) error
}
func (my MyXMPP) Connect() error {
var server = flag.String("server", my.ServerAddess, "server")
var username = flag.String("username", my.Name, "username")
var password = flag.String("password", my.Password, "password")
var status = flag.String("status", "xa", "status")
var statusMessage = flag.String("status-msg", "I for one welcome our new codebot overlords.", "status message")
var notls = flag.Bool("notls", !my.SSL, "No TLS")
var debug = flag.Bool("debug", true, "debug output")
var session = flag.Bool("session", false, "use server session")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: example [options]\n")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
if *username == "" || *password == "" {
flag.Usage()
}
if !*notls {
xmpp.DefaultConfig = tls.Config{
ServerName: serverName(*server),
InsecureSkipVerify: true,
}
}
var err error
options := xmpp.Options{Host: *server,
User: *username,
Password: *password,
NoTLS: *notls,
Debug: *debug,
Session: *session,
Status: *status,
StatusMessage: *statusMessage,
InsecureAllowUnencryptedAuth: true,
}
talk, err = options.NewClient()
if err != nil {
log.Fatal(err)
return err
}
return nil
}
func (my MyXMPP) GetMessage() (string, string, error) {
chat, err := talk.Recv()
if err != nil {
log.Fatal(err)
}
switch v := chat.(type) {
case xmpp.Chat:
//fmt.Println(v.Remote, v.Text)
return v.Remote, v.Text, nil
case xmpp.Presence:
fmt.Printf("presence .. %s is %s \n", v.From, v.Show)
}
return "", "", nil
}
func (my MyXMPP) SendMessage(to string, msg string) error {
talk.Send(xmpp.Chat{Remote: to, Type: "chat", Text: msg})
return nil
}
func serverName(host string) string {
return strings.Split(host, ":")[0]
}