From 046ec6380b7bb363e537ade7fd254b5505dde32d Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 16 Feb 2020 19:30:49 +0100 Subject: Some infrastructure --- .gitignore | 3 + appservice/account.go | 32 +++++++ appservice/db.go | 21 ++++ appservice/server.go | 72 ++++++++++++++ connector/irc/irc.go | 4 +- go.mod | 3 + go.sum | 29 ++++++ main.go | 261 +++++++++++++++++++++++++++++++------------------- mxlib/api.go | 18 ++++ mxlib/registration.go | 25 +++++ test/main.go | 146 ++++++++++++++++++++++++++++ 11 files changed, 516 insertions(+), 98 deletions(-) create mode 100644 appservice/account.go create mode 100644 appservice/db.go create mode 100644 appservice/server.go create mode 100644 mxlib/api.go create mode 100644 mxlib/registration.go create mode 100644 test/main.go diff --git a/.gitignore b/.gitignore index 90ed92e..f4f15a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ easybridge +config.json +registration.yaml +easybridge.db diff --git a/appservice/account.go b/appservice/account.go new file mode 100644 index 0000000..533e01e --- /dev/null +++ b/appservice/account.go @@ -0,0 +1,32 @@ +package appservice + +import ( + . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" +) + +type Account struct { + MatrixUser string + AccountName string + Protocol string + Conn Connector +} + +func (a *Account) Joined(roomId RoomID) { + // TODO +} + +func (a *Account) Left(roomId RoomID) { + // TODO +} + +func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) { + // TODO +} + +func (a *Account) RoomInfoUpdated(roomId RoomID, info *RoomInfo) { + // TODO +} + +func (a *Account) Event(event *Event) { + // TODO +} diff --git a/appservice/db.go b/appservice/db.go new file mode 100644 index 0000000..2c71312 --- /dev/null +++ b/appservice/db.go @@ -0,0 +1,21 @@ +package appservice + +import ( + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/mysql" + _ "github.com/jinzhu/gorm/dialects/postgres" + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + +var db *gorm.DB + +func InitDb() error { + var err error + + db, err = gorm.Open(config.DbType, config.DbPath) + if err != nil { + return err + } + + return nil +} diff --git a/appservice/server.go b/appservice/server.go new file mode 100644 index 0000000..395d383 --- /dev/null +++ b/appservice/server.go @@ -0,0 +1,72 @@ +package appservice + +import ( + "encoding/json" + "fmt" + "strings" + "log" + "net/http" + + "git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib" + "github.com/gorilla/mux" +) + +type Config struct { + HttpBindAddr string + Server string + DbType string + DbPath string +} + + +var registration *mxlib.Registration +var config *Config + +func Start(r *mxlib.Registration, c *Config) (chan error, error) { + registration = r + config = c + + err := InitDb() + if err != nil { + return nil, err + } + + router := mux.NewRouter() + router.HandleFunc("/_matrix/app/v1/transactions/{txnId}", handleTxn) + + errch := make(chan error) + go func() { + log.Printf("Starting HTTP server on %s", config.HttpBindAddr) + err := http.ListenAndServe(config.HttpBindAddr, checkTokenAndLog(router)) + if err != nil { + errch <- err + } + }() + + return errch, nil +} + +func checkTokenAndLog(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + if strings.Join(r.Form["access_token"], "") != registration.HsToken { + http.Error(w, "Wrong or no token provided", http.StatusUnauthorized) + return + } + + log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL) + handler.ServeHTTP(w, r) + }) +} + +func handleTxn(w http.ResponseWriter, r *http.Request) { + var txn mxlib.Transaction + err := json.NewDecoder(r.Body).Decode(&txn) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + log.Printf("Got transaction %#v\n", txn) + + fmt.Fprintf(w, "{}") +} diff --git a/connector/irc/irc.go b/connector/irc/irc.go index 671d19a..396b665 100644 --- a/connector/irc/irc.go +++ b/connector/irc/irc.go @@ -52,12 +52,12 @@ func (irc *IRC) Configure(c Configuration) error { return err } - port, err := c.GetInt("port", 6666) + port, err := c.GetInt("port", 6667) if err != nil { return err } - ssl, err := c.GetBool("ssl", true) + ssl, err := c.GetBool("ssl", false) if err != nil { return err } diff --git a/go.mod b/go.mod index 00f04ae..a2b6364 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,10 @@ module git.deuxfleurs.fr/Deuxfleurs/easybridge go 1.13 require ( + github.com/gorilla/mux v1.7.4 + github.com/jinzhu/gorm v1.9.12 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 + gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index f4be87f..7d91b95 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,35 @@ +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= +github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= +github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= +github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= +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/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= github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7/go.mod h1:liX5MxHPrwgHaKowoLkYGwbXfYABh1jbZ6FpElbGF1I= github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6 h1:GDh7egrbDEzP41mScMt7Q/uPM2nJENh9LNFXjUOGts8= github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6/go.mod h1:ECDRehsR9TYTKCAsRS8/wLeOk6UUqDydw47ln7wG41Q= +github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= +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= +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= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +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/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= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 9c5c015..f7a3e75 100644 --- a/main.go +++ b/main.go @@ -1,145 +1,214 @@ package main import ( - "strings" - "time" - "fmt" + "crypto/rand" + "encoding/hex" + "flag" + "io/ioutil" + "os" + _"strings" + _ "time" + _ "fmt" "log" + "encoding/json" + "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 TmpHandler struct{ - exit chan bool +type ConfigAccount struct { + Protocol string `json:"protocol"` + Rooms []string `json:"rooms"` + Config map[string]string `json:"config"` } -func (h *TmpHandler) Joined(roomId connector.RoomID) { - fmt.Printf("C Joined: %s\n", roomId) +type ConfigFile struct { + HttpBindAddr string`json:"http_bind_addr"` + Registration string `json:"registration"` + Server string `json:"homeserver_url"` + DbType string `json:"db_type"` + DbPath string `json:"db_path"` + Accounts map[string]map[string]ConfigAccount `json:"accounts"` } -func (h *TmpHandler) Left(roomId connector.RoomID) { - fmt.Printf("C Joined: %s\n", roomId) -} +var configFlag = flag.String("config", "./config.json", "Configuration file path") -func (h *TmpHandler) UserInfoUpdated(u connector.UserID, i *connector.UserInfo) { - fmt.Printf("C User info: %s => %#v\n", u, i) -} +var config *ConfigFile +var registration *mxlib.Registration -func (h *TmpHandler) RoomInfoUpdated(r connector.RoomID, i *connector.RoomInfo) { - fmt.Printf("C Room info: %s => %#v\n", r, i) -} -func (h *TmpHandler) Event(e *connector.Event) { - if e.Type == connector.EVENT_JOIN { - fmt.Printf("C E Join %s %s\n", e.Author, e.Room) - } else if e.Type == connector.EVENT_LEAVE { - fmt.Printf("C E Leave %s %s\n", e.Author, e.Room) - } else if e.Type == connector.EVENT_MESSAGE { - fmt.Printf("C E Message %s %s %s\n", e.Author, e.Room, e.Text) - if strings.Contains(e.Text, "ezbrexit") { - fmt.Printf("we have to exit\n") - h.exit <- true - } - } else if e.Type == connector.EVENT_ACTION { - fmt.Printf("C E Action %s %s %s\n", e.Author, e.Room, e.Text) +func readConfig() ConfigFile { + config_file := ConfigFile{ + HttpBindAddr: "0.0.0.0:8321", + Registration: "./registration.yaml", + Server: "http://localhost:8008", + DbType: "sqlite3", + DbPath: "easybridge.db", + Accounts: map[string]map[string]ConfigAccount{}, } -} -func testIrc() { - irc := &irc.IRC{} - h := TmpHandler{ - exit: make(chan bool), - } - irc.SetHandler(&h) + _, err := os.Stat(*configFlag) + if os.IsNotExist(err) { + // Generate default config file + log.Printf("Generating default config file as %s", *configFlag) - err := irc.Configure(connector.Configuration{ - "server": "irc.ulminfo.fr", - "port": "6666", - "nick": "ezbr", - }) - if err != nil { - log.Fatalf("Connect: %s", err) + bytes, err := json.MarshalIndent(&config_file, "", " ") + if err != nil { + log.Fatal(err) + } + + err = ioutil.WriteFile(*configFlag, bytes, 0644) + if err != nil { + log.Fatal(err) + } + + return config_file } - err = irc.Join(connector.RoomID("#ezbrtest@irc.ulminfo.fr")) if err != nil { - log.Fatalf("Join: %s", err) + log.Fatal(err) } - time.Sleep(time.Duration(1)*time.Second) - err = irc.Send(&connector.Event{ - Room: connector.RoomID("#ezbrtest@irc.ulminfo.fr"), - Type: connector.EVENT_MESSAGE, - Text: "EZBR TEST", - }) + bytes, err := ioutil.ReadFile(*configFlag) if err != nil { - log.Fatalf("Send: %s", err) + log.Fatal(err) } - time.Sleep(time.Duration(1)*time.Second) - err = irc.Send(&connector.Event{ - Recipient: connector.UserID("lx@irc.ulminfo.fr"), - Type: connector.EVENT_MESSAGE, - Text: "EZBR TEST direct message lol", - }) + err = json.Unmarshal(bytes, &config_file) if err != nil { - log.Fatalf("Send: %s", err) + log.Fatal(err) } - fmt.Printf("waiting exit signal\n") - <-h.exit - fmt.Printf("got exit signal\n") - irc.Close() + return config_file } -func testXmpp() { - xmpp := &xmpp.XMPP{} - h := TmpHandler{ - exit: make(chan bool), +func readRegistration(file string) mxlib.Registration { + rnd := make([]byte, 64) + n, err := rand.Read(rnd) + if err != nil || n != 64 { + log.Fatal(err) } - xmpp.SetHandler(&h) - err := xmpp.Configure(connector.Configuration{ - "server": "jabber.fr", - "jid": "ezbr@jabber.fr", - "password": "azerty1234", - }) - if err != nil { - log.Fatalf("Connect: %s", err) + reg := mxlib.Registration{ + Id: "Easybridge", + Url: "http://localhost:8321", + AsToken: hex.EncodeToString(rnd[:32]), + HsToken: hex.EncodeToString(rnd[32:]), + SenderLocalpart: "_ezbr", + Namespaces: mxlib.RegistrationNamespaceSet{ + Users: []mxlib.RegistrationNamespace{ + mxlib.RegistrationNamespace{ + Exclusive: true, + Regex: "@_ezbr_.*", + }, + }, + Aliases: []mxlib.RegistrationNamespace{ + mxlib.RegistrationNamespace{ + Exclusive: true, + Regex: "#_ezbr_.*", + }, + }, + Rooms: []mxlib.RegistrationNamespace{}, + }, + } + + _, err = os.Stat(file) + if os.IsNotExist(err) { + // Generate default config file + log.Printf("Generating default registration file as %s", file) + + bytes, err := yaml.Marshal(®) + if err != nil { + log.Fatal(err) + } + + err = ioutil.WriteFile(file, bytes, 0644) + if err != nil { + log.Fatal(err) + } + + return reg } - err = xmpp.Join(connector.RoomID("ezbrtest@muc.linkmauve.fr")) if err != nil { - log.Fatalf("Join: %s", err) + log.Fatal(err) } - time.Sleep(time.Duration(1)*time.Second) - err = xmpp.Send(&connector.Event{ - Room: connector.RoomID("ezbrtest@muc.linkmauve.fr"), - Type: connector.EVENT_MESSAGE, - Text: "EZBR TEST", - }) + bytes, err := ioutil.ReadFile(file) if err != nil { - log.Fatalf("Send: %s", err) + log.Fatal(err) } - time.Sleep(time.Duration(1)*time.Second) - err = xmpp.Send(&connector.Event{ - Recipient: connector.UserID("alexis211@jabber.fr"), - Type: connector.EVENT_MESSAGE, - Text: "EZBR TEST direct message lol", - }) + err = yaml.Unmarshal(bytes, ®) if err != nil { - log.Fatalf("Send: %s", err) + log.Fatal(err) } - fmt.Printf("waiting exit signal\n") - <-h.exit - fmt.Printf("got exit signal\n") - xmpp.Close() + return reg } func main() { - testXmpp() + flag.Parse() + + config_file := readConfig() + config = &config_file + + reg_file := readRegistration(config.Registration) + registration = ®_file + + as_config := &appservice.Config{ + HttpBindAddr: config.HttpBindAddr, + Server: config.Server, + DbType: config.DbType, + DbPath: config.DbPath, + } + + errch, err := appservice.Start(registration, as_config) + if err != nil { + log.Fatal(err) + } + + for user, accounts := range config.Accounts { + for name, params := range accounts { + var conn connector.Connector + switch params.Protocol { + case "irc": + conn = &irc.IRC{} + case "xmpp": + conn = &xmpp.XMPP{} + } + account := &appservice.Account{ + MatrixUser: user, + AccountName: name, + Protocol: params.Protocol, + Conn: conn, + } + conn.SetHandler(account) + go connectAndJoin(conn, params) + } + } + + err = <-errch + if err != nil { + log.Fatal(err) + } +} + +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) + } else { + log.Printf("Connected to %s, now joining %#v`n", params.Protocol, params.Rooms) + for _, room := range params.Rooms { + err := conn.Join(connector.RoomID(room)) + if err != nil { + log.Printf("Could not join %s: %s", room, err) + } + } + } } diff --git a/mxlib/api.go b/mxlib/api.go new file mode 100644 index 0000000..59e1267 --- /dev/null +++ b/mxlib/api.go @@ -0,0 +1,18 @@ +package mxlib + +import ( + _ "encoding/json" +) + +type Transaction struct { + Events []Event `json:"events"` +} + +type Event struct { + Content map[string]string `json:"content"` + Type string `json:"type"` + EventId string `json:"event_id"` + RoomId string `json:"room_id"` + Sender string `json:"sender"` + OriginServerTs int `json:"origin_server_ts"` +} diff --git a/mxlib/registration.go b/mxlib/registration.go new file mode 100644 index 0000000..d4b18dc --- /dev/null +++ b/mxlib/registration.go @@ -0,0 +1,25 @@ +package mxlib + +import ( + _ "gopkg.in/yaml.v2" +) + +type Registration struct { + Id string `yaml:"id"` + Url string `yaml:"url"` + AsToken string `yaml:"as_token"` + HsToken string `yaml:"hs_token"` + SenderLocalpart string `yaml:"sender_localpart"` + Namespaces RegistrationNamespaceSet `yaml:"namespaces"` +} + +type RegistrationNamespaceSet struct { + Users []RegistrationNamespace `yaml:"users"` + Aliases []RegistrationNamespace `yaml:"aliases"` + Rooms []RegistrationNamespace `yaml:"rooms"` +} + +type RegistrationNamespace struct { + Exclusive bool `yaml:"exclusive"` + Regex string `yaml:"regex"` +} diff --git a/test/main.go b/test/main.go new file mode 100644 index 0000000..60dfe5a --- /dev/null +++ b/test/main.go @@ -0,0 +1,146 @@ +package main + +import ( + "strings" + "time" + "fmt" + "log" + + "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" + "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc" + "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp" +) + +type TmpHandler struct{ + exit chan bool +} + +func (h *TmpHandler) Joined(roomId connector.RoomID) { + fmt.Printf("C Joined: %s\n", roomId) +} + +func (h *TmpHandler) Left(roomId connector.RoomID) { + fmt.Printf("C Joined: %s\n", roomId) +} + +func (h *TmpHandler) UserInfoUpdated(u connector.UserID, i *connector.UserInfo) { + fmt.Printf("C User info: %s => %#v\n", u, i) +} + +func (h *TmpHandler) RoomInfoUpdated(r connector.RoomID, i *connector.RoomInfo) { + fmt.Printf("C Room info: %s => %#v\n", r, i) +} +func (h *TmpHandler) Event(e *connector.Event) { + if e.Type == connector.EVENT_JOIN { + fmt.Printf("C E Join %s %s\n", e.Author, e.Room) + } else if e.Type == connector.EVENT_LEAVE { + fmt.Printf("C E Leave %s %s\n", e.Author, e.Room) + } else if e.Type == connector.EVENT_MESSAGE { + fmt.Printf("C E Message %s %s %s\n", e.Author, e.Room, e.Text) + if strings.Contains(e.Text, "ezbrexit") { + fmt.Printf("we have to exit\n") + h.exit <- true + } + } else if e.Type == connector.EVENT_ACTION { + fmt.Printf("C E Action %s %s %s\n", e.Author, e.Room, e.Text) + } +} + +func testIrc() { + irc := &irc.IRC{} + h := TmpHandler{ + exit: make(chan bool), + } + irc.SetHandler(&h) + + err := irc.Configure(connector.Configuration{ + "server": "irc.ulminfo.fr", + "port": "6666", + "ssl": "true", + "nick": "ezbr", + }) + if err != nil { + log.Fatalf("Connect: %s", err) + } + + err = irc.Join(connector.RoomID("#ezbrtest@irc.ulminfo.fr")) + if err != nil { + log.Fatalf("Join: %s", err) + } + + time.Sleep(time.Duration(1)*time.Second) + err = irc.Send(&connector.Event{ + Room: connector.RoomID("#ezbrtest@irc.ulminfo.fr"), + Type: connector.EVENT_MESSAGE, + Text: "EZBR TEST", + }) + if err != nil { + log.Fatalf("Send: %s", err) + } + + time.Sleep(time.Duration(1)*time.Second) + err = irc.Send(&connector.Event{ + Recipient: connector.UserID("lx@irc.ulminfo.fr"), + Type: connector.EVENT_MESSAGE, + Text: "EZBR TEST direct message lol", + }) + if err != nil { + log.Fatalf("Send: %s", err) + } + + fmt.Printf("waiting exit signal\n") + <-h.exit + fmt.Printf("got exit signal\n") + irc.Close() +} + +func testXmpp() { + xmpp := &xmpp.XMPP{} + h := TmpHandler{ + exit: make(chan bool), + } + xmpp.SetHandler(&h) + + err := xmpp.Configure(connector.Configuration{ + "server": "jabber.fr", + "jid": "ezbr@jabber.fr", + "password": "azerty1234", + }) + if err != nil { + log.Fatalf("Connect: %s", err) + } + + err = xmpp.Join(connector.RoomID("ezbrtest@muc.linkmauve.fr")) + if err != nil { + log.Fatalf("Join: %s", err) + } + + time.Sleep(time.Duration(1)*time.Second) + err = xmpp.Send(&connector.Event{ + Room: connector.RoomID("ezbrtest@muc.linkmauve.fr"), + Type: connector.EVENT_MESSAGE, + Text: "EZBR TEST", + }) + if err != nil { + log.Fatalf("Send: %s", err) + } + + time.Sleep(time.Duration(1)*time.Second) + err = xmpp.Send(&connector.Event{ + Recipient: connector.UserID("alexis211@jabber.fr"), + Type: connector.EVENT_MESSAGE, + Text: "EZBR TEST direct message lol", + }) + if err != nil { + log.Fatalf("Send: %s", err) + } + + fmt.Printf("waiting exit signal\n") + <-h.exit + fmt.Printf("got exit signal\n") + xmpp.Close() +} + +func main() { + testXmpp() +} -- cgit v1.2.3