Some infrastructure
This commit is contained in:
parent
225fc84f09
commit
046ec6380b
11 changed files with 530 additions and 112 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,4 @@
|
||||||
easybridge
|
easybridge
|
||||||
|
config.json
|
||||||
|
registration.yaml
|
||||||
|
easybridge.db
|
||||||
|
|
32
appservice/account.go
Normal file
32
appservice/account.go
Normal file
|
@ -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
|
||||||
|
}
|
21
appservice/db.go
Normal file
21
appservice/db.go
Normal file
|
@ -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
|
||||||
|
}
|
72
appservice/server.go
Normal file
72
appservice/server.go
Normal file
|
@ -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, "{}")
|
||||||
|
}
|
|
@ -52,12 +52,12 @@ func (irc *IRC) Configure(c Configuration) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
port, err := c.GetInt("port", 6666)
|
port, err := c.GetInt("port", 6667)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ssl, err := c.GetBool("ssl", true)
|
ssl, err := c.GetBool("ssl", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
3
go.mod
3
go.mod
|
@ -3,7 +3,10 @@ module git.deuxfleurs.fr/Deuxfleurs/easybridge
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
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/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7
|
||||||
github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6
|
github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6
|
||||||
github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad
|
github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad
|
||||||
|
gopkg.in/yaml.v2 v2.2.8
|
||||||
)
|
)
|
||||||
|
|
29
go.sum
29
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 h1:BS9tqL0OCiOGuy/CYYk2gc33fxqaqh5/rhqMKu4tcYA=
|
||||||
github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7/go.mod h1:liX5MxHPrwgHaKowoLkYGwbXfYABh1jbZ6FpElbGF1I=
|
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 h1:GDh7egrbDEzP41mScMt7Q/uPM2nJENh9LNFXjUOGts8=
|
||||||
github.com/matterbridge/go-xmpp v0.0.0-20180131083630-7ec2b8b7def6/go.mod h1:ECDRehsR9TYTKCAsRS8/wLeOk6UUqDydw47ln7wG41Q=
|
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 h1:ntj2CDcRNjFht20llTwIwwguKa00u0UCLtF2J5+Gmxo=
|
||||||
github.com/mattn/go-xmpp v0.0.0-20200128155807-a86b6abcb3ad/go.mod h1:Cs5mF0OsrRRmhkyOod//ldNPOwJsrBvJ+1WRspv0xoc=
|
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=
|
||||||
|
|
281
main.go
281
main.go
|
@ -1,145 +1,214 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"crypto/rand"
|
||||||
"time"
|
"encoding/hex"
|
||||||
"fmt"
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
_"strings"
|
||||||
|
_ "time"
|
||||||
|
_ "fmt"
|
||||||
"log"
|
"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"
|
||||||
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"
|
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"
|
||||||
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
|
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TmpHandler struct{
|
type ConfigAccount struct {
|
||||||
exit chan bool
|
Protocol string `json:"protocol"`
|
||||||
|
Rooms []string `json:"rooms"`
|
||||||
|
Config map[string]string `json:"config"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TmpHandler) Joined(roomId connector.RoomID) {
|
type ConfigFile struct {
|
||||||
fmt.Printf("C Joined: %s\n", roomId)
|
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) {
|
var configFlag = flag.String("config", "./config.json", "Configuration file path")
|
||||||
fmt.Printf("C Joined: %s\n", roomId)
|
|
||||||
|
var config *ConfigFile
|
||||||
|
var registration *mxlib.Registration
|
||||||
|
|
||||||
|
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{},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := os.Stat(*configFlag)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// Generate default config file
|
||||||
|
log.Printf("Generating default config file as %s", *configFlag)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := ioutil.ReadFile(*configFlag)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(bytes, &config_file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return config_file
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TmpHandler) UserInfoUpdated(u connector.UserID, i *connector.UserInfo) {
|
func readRegistration(file string) mxlib.Registration {
|
||||||
fmt.Printf("C User info: %s => %#v\n", u, i)
|
rnd := make([]byte, 64)
|
||||||
}
|
n, err := rand.Read(rnd)
|
||||||
|
if err != nil || n != 64 {
|
||||||
func (h *TmpHandler) RoomInfoUpdated(r connector.RoomID, i *connector.RoomInfo) {
|
log.Fatal(err)
|
||||||
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() {
|
reg := mxlib.Registration{
|
||||||
irc := &irc.IRC{}
|
Id: "Easybridge",
|
||||||
h := TmpHandler{
|
Url: "http://localhost:8321",
|
||||||
exit: make(chan bool),
|
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{},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
irc.SetHandler(&h)
|
|
||||||
|
|
||||||
err := irc.Configure(connector.Configuration{
|
_, err = os.Stat(file)
|
||||||
"server": "irc.ulminfo.fr",
|
if os.IsNotExist(err) {
|
||||||
"port": "6666",
|
// Generate default config file
|
||||||
"nick": "ezbr",
|
log.Printf("Generating default registration file as %s", file)
|
||||||
})
|
|
||||||
|
bytes, err := yaml.Marshal(®)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Connect: %s", err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = irc.Join(connector.RoomID("#ezbrtest@irc.ulminfo.fr"))
|
err = ioutil.WriteFile(file, bytes, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Join: %s", err)
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return reg
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
log.Fatalf("Send: %s", err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Duration(1)*time.Second)
|
bytes, err := ioutil.ReadFile(file)
|
||||||
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 {
|
if err != nil {
|
||||||
log.Fatalf("Send: %s", err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("waiting exit signal\n")
|
err = yaml.Unmarshal(bytes, ®)
|
||||||
<-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 {
|
if err != nil {
|
||||||
log.Fatalf("Connect: %s", err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = xmpp.Join(connector.RoomID("ezbrtest@muc.linkmauve.fr"))
|
return reg
|
||||||
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() {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
18
mxlib/api.go
Normal file
18
mxlib/api.go
Normal file
|
@ -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"`
|
||||||
|
}
|
25
mxlib/registration.go
Normal file
25
mxlib/registration.go
Normal file
|
@ -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"`
|
||||||
|
}
|
146
test/main.go
Normal file
146
test/main.go
Normal file
|
@ -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()
|
||||||
|
}
|
Loading…
Reference in a new issue