Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Commit

Permalink
Switch to mautrix-go and add go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 22, 2018
1 parent edcf9d4 commit d79df1a
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 92 deletions.
38 changes: 19 additions & 19 deletions appservice.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package appservice

import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"

"gopkg.in/yaml.v2"

"errors"
"maunium.net/go/gomatrix"
"maunium.net/go/maulogger"
"net/http"
"regexp"
"strings"
"maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix"
)

// EventChannelSize is the size for the Events channel in Appservice instances.
Expand All @@ -24,7 +24,7 @@ var EventChannelSize = 64
func Create() *AppService {
return &AppService{
LogConfig: CreateLogConfig(),
clients: make(map[string]*gomatrix.Client),
clients: make(map[string]*mautrix.Client),
intents: make(map[string]*IntentAPI),
StateStore: NewBasicStateStore(),
}
Expand Down Expand Up @@ -71,14 +71,14 @@ type AppService struct {
Log maulogger.Logger `yaml:"-"`

lastProcessedTransaction string
Events chan *gomatrix.Event `yaml:"-"`
QueryHandler QueryHandler `yaml:"-"`
StateStore StateStore `yaml:"-"`
Events chan *mautrix.Event `yaml:"-"`
QueryHandler QueryHandler `yaml:"-"`
StateStore StateStore `yaml:"-"`

server *http.Server
botClient *gomatrix.Client
botClient *mautrix.Client
botIntent *IntentAPI
clients map[string]*gomatrix.Client
clients map[string]*mautrix.Client
intents map[string]*IntentAPI
}

Expand Down Expand Up @@ -147,11 +147,11 @@ func (as *AppService) BotIntent() *IntentAPI {
return as.botIntent
}

func (as *AppService) Client(userID string) *gomatrix.Client {
func (as *AppService) Client(userID string) *mautrix.Client {
client, ok := as.clients[userID]
if !ok {
var err error
client, err = gomatrix.NewClient(as.HomeserverURL, userID, as.Registration.AppToken)
client, err = mautrix.NewClient(as.HomeserverURL, userID, as.Registration.AppToken)
if err != nil {
as.Log.Fatalln("Failed to create gomatrix instance:", err)
return nil
Expand All @@ -165,10 +165,10 @@ func (as *AppService) Client(userID string) *gomatrix.Client {
return client
}

func (as *AppService) BotClient() *gomatrix.Client {
func (as *AppService) BotClient() *mautrix.Client {
if as.botClient == nil {
var err error
as.botClient, err = gomatrix.NewClient(as.HomeserverURL, as.BotMXID(), as.Registration.AppToken)
as.botClient, err = mautrix.NewClient(as.HomeserverURL, as.BotMXID(), as.Registration.AppToken)
if err != nil {
as.Log.Fatalln("Failed to create gomatrix instance:", err)
return nil
Expand All @@ -182,7 +182,7 @@ func (as *AppService) BotClient() *gomatrix.Client {

// Init initializes the logger and loads the registration of this appservice.
func (as *AppService) Init() (bool, error) {
as.Events = make(chan *gomatrix.Event, EventChannelSize)
as.Events = make(chan *mautrix.Event, EventChannelSize)
as.QueryHandler = &QueryHandlerStub{}

as.Log = maulogger.Create()
Expand Down Expand Up @@ -266,7 +266,7 @@ func CreateLogConfig() LogConfig {
}

type FileFormatData struct {
Date string
Date string
Index int
}

Expand All @@ -279,7 +279,7 @@ func (lc LogConfig) GetFileFormat() maulogger.LoggerFileFormat {
return func(now string, i int) string {
var buf strings.Builder
tpl.Execute(&buf, FileFormatData{
Date: now,
Date: now,
Index: i,
})
return buf.String()
Expand Down
12 changes: 6 additions & 6 deletions eventprocessor.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package appservice

import (
"maunium.net/go/gomatrix"
log "maunium.net/go/maulogger"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix"
)

type ExecMode uint8
Expand All @@ -19,7 +19,7 @@ type EventProcessor struct {
as *AppService
log log.Logger
stop chan struct{}
handlers map[gomatrix.EventType][]gomatrix.OnEventListener
handlers map[mautrix.EventType][]mautrix.OnEventListener
}

func NewEventProcessor(as *AppService) *EventProcessor {
Expand All @@ -28,14 +28,14 @@ func NewEventProcessor(as *AppService) *EventProcessor {
as: as,
log: as.Log.Sub("Events"),
stop: make(chan struct{}, 1),
handlers: make(map[gomatrix.EventType][]gomatrix.OnEventListener),
handlers: make(map[mautrix.EventType][]mautrix.OnEventListener),
}
}

func (ep *EventProcessor) On(evtType gomatrix.EventType, handler gomatrix.OnEventListener) {
func (ep *EventProcessor) On(evtType mautrix.EventType, handler mautrix.OnEventListener) {
handlers, ok := ep.handlers[evtType]
if !ok {
handlers = []gomatrix.OnEventListener{handler}
handlers = []mautrix.OnEventListener{handler}
} else {
handlers = append(handlers, handler)
}
Expand Down
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module maunium.net/go/mautrix-appservice

require (
github.com/fatih/color v1.7.0
github.com/gorilla/mux v1.6.2
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
gopkg.in/yaml.v2 v2.2.2
maunium.net/go/maulogger/v2 v2.0.0
maunium.net/go/mautrix v0.1.0-alpha.1
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
maunium.net/go/maulogger/v2 v2.0.0 h1:8PB95zf6e7Ddw8iOWqjrJjAjPcAI50LagA1X0Ur64os=
maunium.net/go/maulogger/v2 v2.0.0/go.mod h1:Hbbkq3NV6jvJodByZu1mgEF3fpT7Kz9z0MjEZ3/BusI=
maunium.net/go/mautrix v0.1.0-alpha.1 h1:s0AOe3bAuElTQeR2A12ISDFj99iaWzUqMVHSNdkoG7I=
maunium.net/go/mautrix v0.1.0-alpha.1/go.mod h1:Rdy+cP8SrJAtJmwyFrX8Lq02Ceb3JHfPgmboanjngls=
3 changes: 2 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package appservice
import (
"context"
"encoding/json"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
"time"

"github.com/gorilla/mux"
)

// Listen starts the HTTP server that listens for calls from the Matrix homeserver.
Expand Down
Loading

0 comments on commit d79df1a

Please sign in to comment.