diff --git a/main.go b/main.go index 417d4b6..ef2e9c3 100644 --- a/main.go +++ b/main.go @@ -6,16 +6,32 @@ package main import ( "fmt" + "log" + "os" "net/http" -// "strings" "io/ioutil" "encoding/json" + "errors" + "gosrc.io/xmpp" + "gosrc.io/xmpp/stanza" ) const ( // APIEndpoint = "https://hodlhodl.com/api/v1" TestAPIEndpoint = "https://hhtestnet.com/api/v1" - APIKEY = "***REMOVED***" + APIkey = "***REMOVED***" +) + +var ( + CorrespChan = make(chan string, 1) + textChan = make(chan string, 5) + rawTextChan = make(chan string, 5) + killChan = make(chan error, 1) + errChan = make(chan error) + rosterChan = make(chan struct{}) + + logger *log.Logger + disconnectErr = errors.New("disconnecting client") ) // Notification export @@ -29,13 +45,20 @@ type Notification struct { } `json:"notifications"` } -func main() { - //body:= strings.NewReader(' -X POST -H "Authorization: Bearer ***REMOVED***" -H "Content-Type: application/json"`) +type config struct { + Server map[string]string `mapstructure:"server"` + Client map[string]string `mapstructure:"client"` + Contacts string `string:"contact"` + LogStanzas map[string]string `mapstructure:"logstanzas"` +} + +func gethdlNotif() { + req, err := http.NewRequest("POST", TestAPIEndpoint + "/notifications/read", nil) if err != nil { fmt.Println(err) } - req.Header.Add("Authorization", "Bearer " + APIKEY) + req.Header.Add("Authorization", "Bearer " + APIkey) req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) @@ -54,5 +77,119 @@ func main() { fmt.Println("RESULTAT", string(body)) } + defer resp.Body.Close() + +} + + +func handleMessage(s xmpp.Sender, p stanza.Packet) { + msg, ok := p.(stanza.Message) + if !ok { + _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p) + return + } + + _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From) + reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body} + _ = s.Send(reply) +} + +func startClient() { + + // Client Setup + clientCfg := &xmpp.Config { + TransportConfiguration: xmpp.TransportConfiguration{ + Address: "xmpp.sp4ke.xyz", + }, + Jid: "vicious@xmpp.sp4ke.xyz", + Credential: xmpp.Password("***REMOVED***"), + Insecure: true, + } + + + var err error + router := xmpp.NewRouter() + + handleMessage := func(s xmpp.Sender, p stanza.Packet) { + msg, ok := p.(stanza.Message) + if !ok { + _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p) + return + } + + _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From) + reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body} + _ = s.Send(reply) + } + + errorHandler:= func(err error) { + fmt.Println(err.Error()) + } + + router.HandleFunc("HELLO TEST", handleMessage) + + client, err := xmpp.NewClient(clientCfg, router, errorHandler) + if err != nil { + log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err)) + //log.Fatalf("%+v", err) + } else { + fmt.Println("Client running....") + } + + // Connection manager, reconect automatically + cm := xmpp.NewStreamManager(client, nil) + log.Fatal(cm.Run()) +} + +/*func startMessaging(client xmpp.Sender, config *config) { + var text string + var correspondent string + for { + select { + case err := <-killChan: + if err == disconnectErr { + sc := client.(xmpp.StreamClient) + sc.Disconnect() + } else { + logger.Println(err) + } + return + case text = <-textChan: + reply := stanza.Message{Attrs: stanza.Attrs{To: correspondent, Type: stanza.MessageTypeChat}, Body: text} + if logger != nil { + raw, _ := xml.Marshal(reply) + logger.Println(string(raw)) + } + err := client.Send(reply) + if err != nil { + fmt.Printf("There was a problem sending the message : %v", reply) + return + } + case text = <-rawTextChan: + if logger != nil { + logger.Println(text) + } + err := client.SendRaw(text) + if err != nil { + fmt.Printf("There was a problem sending the message : %v", text) + return + } + case crrsp := <-CorrespChan: + correspondent = crrsp + case <-rosterChan: + askForRoster(client, g, config) + } + + } +}*/ + + +func main() { + //body:= strings.NewReader(' -X POST -H "Authorization: Bearer ***REMOVED***" -H "Content-Type: application/json"`) + // Read configuration + + startClient() } + +