Logging; fix xmpp a bit

This commit is contained in:
Alex 2020-02-17 09:41:08 +01:00
parent c3b941841e
commit 400c67f735
10 changed files with 73 additions and 23 deletions

View file

@ -2,7 +2,8 @@ package appservice
import (
"fmt"
"log"
log "github.com/sirupsen/logrus"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)

View file

@ -2,15 +2,15 @@ package appservice
import (
"fmt"
"log"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
log "github.com/sirupsen/logrus"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
var db *gorm.DB

View file

@ -3,12 +3,13 @@ package appservice
import (
"fmt"
"net/url"
"log"
"net/http"
"time"
"bytes"
"encoding/json"
log "github.com/sirupsen/logrus"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
)
@ -24,7 +25,7 @@ func init() {
}
func mxGetApiCall(endpoint string, response interface{}) error {
log.Printf("Matrix GET request: %s\n", endpoint)
log.Debugf("Matrix GET request: %s\n", endpoint)
req, err := http.NewRequest("GET", config.Server + endpoint, nil)
if err != nil {
@ -40,7 +41,7 @@ func mxPutApiCall(endpoint string, data interface{}, response interface{}) error
return err
}
log.Printf("Matrix PUT request: %s %s\n", endpoint, string(body))
log.Debugf("Matrix PUT request: %s %s\n", endpoint, string(body))
req, err := http.NewRequest("PUT", config.Server + endpoint, bytes.NewBuffer(body))
if err != nil {
@ -57,7 +58,7 @@ func mxPostApiCall(endpoint string, data interface{}, response interface{}) erro
return err
}
log.Printf("Matrix POST request: %s %s\n", endpoint, string(body))
log.Debugf("Matrix POST request: %s %s\n", endpoint, string(body))
req, err := http.NewRequest("POST", config.Server + endpoint, bytes.NewBuffer(body))
if err != nil {
@ -82,7 +83,7 @@ func mxDoAndParse(req *http.Request, response interface{}) error {
if err != nil {
return err
}
log.Printf("Response (%d): %#v\n", resp.StatusCode, e)
log.Debugf("Response (%d): %#v\n", resp.StatusCode, e)
return &e
}
@ -91,7 +92,7 @@ func mxDoAndParse(req *http.Request, response interface{}) error {
return err
}
log.Printf("Response: %#v\n", response)
log.Debugf("Response: %#v\n", response)
return nil
}

View file

@ -4,12 +4,13 @@ import (
"encoding/json"
"fmt"
"strings"
"log"
"net/http"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"github.com/gorilla/mux"
)
type Config struct {

View file

@ -6,9 +6,9 @@ import (
"strings"
"fmt"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"github.com/lrstanley/girc"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
// User id format: nickname@server

View file

@ -1,16 +1,16 @@
package xmpp
import (
"log"
"time"
//"os"
"strings"
"fmt"
"crypto/tls"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
log "github.com/sirupsen/logrus"
gxmpp "github.com/mattn/go-xmpp"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
// User id format: username@server (= JID)
@ -34,6 +34,8 @@ type XMPP struct {
nickname string
conn *gxmpp.Client
isMUC map[string]bool
}
func (xm *XMPP) SetHandler(h Handler) {
@ -87,6 +89,10 @@ func (xm *XMPP) Configure(c Configuration) error {
}
// Try to connect
if xm.isMUC == nil {
xm.isMUC = make(map[string]bool)
}
xm.connectorLoopNum += 1
go xm.connectLoop(xm.connectorLoopNum)
@ -171,10 +177,23 @@ func (xm *XMPP) handleXMPP() error {
return err
}
fmt.Printf("XMPP: %#v\n", m)
switch v := m.(type) {
case gxmpp.Chat:
log.Printf("== Receiving %#v\n", v)
if v.Text == "" && v.Type == "groupchat" && !strings.Contains(v.Remote, "/") {
// Empty message when we joined group chat
xm.handler.Joined(RoomID(v.Remote))
if v.Subject != "" {
xm.handler.RoomInfoUpdated(RoomID(v.Remote), &RoomInfo{
Description: v.Subject,
})
}
continue
}
if v.Text == "" || v.Remote == xm.jid {
continue
}
@ -212,6 +231,18 @@ func (xm *XMPP) handleXMPP() error {
xm.handler.Event(event)
}
case gxmpp.Presence:
remote := strings.Split(v.From, "/")
if muc, ok := xm.isMUC[remote[0]]; ok && muc && len(remote) == 2 {
event := &Event{
Type: EVENT_JOIN,
Room: RoomID(remote[0]),
Author: UserID(remote[1] + "@" + remote[0]),
}
if v.Type == "unavailable" {
event.Type = EVENT_LEAVE
}
xm.handler.Event(event)
}
// Do nothing.
}
}
@ -232,6 +263,8 @@ func (xm *XMPP) SetRoomInfo(roomId RoomID, info *RoomInfo) error {
}
func (xm *XMPP) Join(roomId RoomID) error {
xm.isMUC[string(roomId)] = true
fmt.Printf("Join %s with nick %s\n", roomId, xm.nickname)
_, err := xm.conn.JoinMUCNoHistory(string(roomId), xm.nickname)
return err

1
go.mod
View file

@ -8,5 +8,6 @@ require (
github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7
github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6
github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad
github.com/sirupsen/logrus v1.4.2
gopkg.in/yaml.v2 v2.2.8
)

9
go.sum
View file

@ -1,3 +1,4 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
@ -11,6 +12,7 @@ github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZ
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7 h1:BS9tqL0OCiOGuy/CYYk2gc33fxqaqh5/rhqMKu4tcYA=
@ -21,6 +23,11 @@ github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcB
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad h1:ntj2CDcRNjFht20llTwIwwguKa00u0UCLtF2J5+Gmxo=
github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad/go.mod h1:Cs5mF0OsrRRmhkyOod//ldNPOwJsrBvJ+1WRspv0xoc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@ -28,6 +35,8 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

11
main.go
View file

@ -10,15 +10,16 @@ import (
_"strings"
_ "time"
_ "fmt"
"log"
"encoding/json"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/appservice"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
"gopkg.in/yaml.v2"
)
type ConfigAccount struct {
@ -153,6 +154,8 @@ func readRegistration(file string) mxlib.Registration {
}
func main() {
log.SetLevel(log.DebugLevel)
flag.Parse()
config_file := readConfig()
@ -206,9 +209,9 @@ func connectAndJoin(conn connector.Connector, params ConfigAccount) {
log.Printf("Connecting to %s", params.Protocol)
err := conn.Configure(params.Config)
if err != nil {
log.Printf("Could not connect to %s: %s\n", params.Protocol, err)
log.Printf("Could not connect to %s: %s", params.Protocol, err)
} else {
log.Printf("Connected to %s, now joining %#v`n", params.Protocol, params.Rooms)
log.Printf("Connected to %s, now joining %#v", params.Protocol, params.Rooms)
for _, room := range params.Rooms {
err := conn.Join(connector.RoomID(room))
if err != nil {

View file

@ -4,7 +4,8 @@ import (
"strings"
"time"
"fmt"
"log"
log "github.com/sirupsen/logrus"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"