Skip to content

Commit

Permalink
Added NATS initial support
Browse files Browse the repository at this point in the history
  • Loading branch information
xackery committed Jan 30, 2020
1 parent 87339c8 commit 11f226a
Show file tree
Hide file tree
Showing 18 changed files with 6,259 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
talkeq.conf
users.txt
guilds.txt
*.sql

bin/
vendor/*/
Expand Down
35 changes: 33 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
VERSION := v0.0.17
VERSION := v0.0.18
NAME := talkeq

.PHONY: build-all
Expand All @@ -18,4 +18,35 @@ build-all: sanitize
.PHONY: sanitize
sanitize:
@goimports -w .
@golint
@golint

PROTO_VERSION=3.8.0
GO_PLUGIN=bin/protoc-gen-go
GO_PROTOBUF_REPO=github.com/golang/protobuf
GO_PTYPES_ANY_PKG=$(GO_PROTOBUF_REPO)/ptypes/any
SWAGGER_PLUGIN=bin/protoc-gen-swagger
PROTO_FILES=$(shell find proto -name '*.proto')
PROTO_OUT=/src/pb/
$(GO_PLUGIN):
dep ensure -vendor-only
go install ./vendor/$(GO_PLUGIN_PKG)
go build -o $@ $(GO_PLUGIN_PKG) -ldflags="-s -w -X 'main.Version=${VERSION}'"
proto-clean:
@echo "removing generated contents..."
@rm -rf pb/*.pb.*go
-@rm -rf swagger/proto/*
@mkdir -p swagger/proto
.PHONY: proto
proto: proto-clean ## Generate protobuf files
@echo "proto > pb"
@(docker run --rm -v ${PWD}:/src xackery/protobuf:$(PROTO_VERSION) protoc \
-I/protobuf/src \
-I/src \
-I/grpc \
-I/grpc/third_party/googleapis \
$(PROTO_FILES) \
--grpc-gateway_out=logtostderr=true:$(PROTO_OUT) \
--swagger_out=logtostderr=true,allow_merge=true:swagger/ \
--go_out=plugins=grpc+retag:$(PROTO_OUT))
@(mv pb/proto/* pb/)
@(rm -rf pb/proto)
54 changes: 48 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/xackery/talkeq/database"
"github.com/xackery/talkeq/discord"
"github.com/xackery/talkeq/eqlog"
"github.com/xackery/talkeq/nats"
"github.com/xackery/talkeq/peqeditorsql"
"github.com/xackery/talkeq/telnet"
)
Expand All @@ -26,6 +27,7 @@ type Client struct {
discord *discord.Discord
telnet *telnet.Telnet
eqlog *eqlog.EQLog
nats *nats.Nats
peqeditorsql *peqeditorsql.PEQEditorSQL
}

Expand Down Expand Up @@ -54,7 +56,13 @@ func New(ctx context.Context) (*Client, error) {
if err != nil {
return nil, errors.Wrap(err, "usermanager")
}
c.discord, err = discord.New(ctx, c.config.Discord, userManager)

guildManager, err := database.NewGuildManager(ctx, c.config)
if err != nil {
return nil, errors.Wrap(err, "guildmanager")
}

c.discord, err = discord.New(ctx, c.config.Discord, userManager, guildManager)
if err != nil {
return nil, errors.Wrap(err, "discord")
}
Expand Down Expand Up @@ -93,6 +101,16 @@ func New(ctx context.Context) (*Client, error) {
if err != nil {
return nil, errors.Wrap(err, "peqeditorsql subscribe")
}

c.nats, err = nats.New(ctx, c.config.Nats, guildManager)
if err != nil {
return nil, errors.Wrap(err, "nats")
}

err = c.nats.Subscribe(ctx, c.onMessage)
if err != nil {
return nil, errors.Wrap(err, "nats subscribe")
}
return &c, nil
}

Expand Down Expand Up @@ -130,6 +148,14 @@ func (c *Client) Connect(ctx context.Context) error {
log.Warn().Err(err).Msg("peqeditorsql connect")
}

err = c.nats.Connect(ctx)
if err != nil {
if !c.config.IsKeepAliveEnabled {
return errors.Wrap(err, "nats connect")
}
log.Warn().Err(err).Msg("nats connect")
}

go c.loop(ctx)
return nil
}
Expand Down Expand Up @@ -189,7 +215,7 @@ func (c *Client) loop(ctx context.Context) {
}
}

func (c *Client) onMessage(source string, author string, channelID int, message string) {
func (c *Client) onMessage(source string, author string, channelID int, message string, optional string) {
var err error
endpoints := "none"
switch source {
Expand All @@ -198,7 +224,7 @@ func (c *Client) onMessage(source string, author string, channelID int, message
log.Info().Msgf("[%s->none] %s %s: %s", source, author, channel.ToString(channelID), message)
return
}
err = c.discord.Send(context.Background(), source, author, channelID, message)
err = c.discord.Send(context.Background(), source, author, channelID, message, "")
if err != nil {
log.Warn().Err(err).Msg("discord send")
} else {
Expand All @@ -214,7 +240,23 @@ func (c *Client) onMessage(source string, author string, channelID int, message
log.Info().Msgf("[%s->none] %s %s: %s", source, author, channel.ToString(channelID), message)
return
}
err = c.discord.Send(context.Background(), source, author, channelID, message)
err = c.discord.Send(context.Background(), source, author, channelID, message, "")
if err != nil {
log.Warn().Err(err).Msg("discord send")
} else {
if endpoints == "none" {
endpoints = "discord"
} else {
endpoints += ",discord"
}
}
log.Info().Msgf("[%s->%s] %s %s: %s", source, endpoints, author, channel.ToString(channelID), message)
case "nats":
if !c.config.Discord.IsEnabled {
log.Info().Msgf("[%s->none] %s %s: %s", source, author, channel.ToString(channelID), message)
return
}
err = c.discord.Send(context.Background(), source, author, channelID, message, "")
if err != nil {
log.Warn().Err(err).Msg("discord send")
} else {
Expand All @@ -230,7 +272,7 @@ func (c *Client) onMessage(source string, author string, channelID int, message
log.Info().Msgf("[%s->none] %s %s: %s", source, author, channel.ToString(channelID), message)
return
}
err = c.telnet.Send(context.Background(), source, author, channelID, message)
err = c.telnet.Send(context.Background(), source, author, channelID, message, "")
if err != nil {
log.Warn().Err(err).Msg("telnet send")
} else {
Expand All @@ -246,7 +288,7 @@ func (c *Client) onMessage(source string, author string, channelID int, message
log.Info().Msgf("[%s->none] %s %s: %s", source, author, channel.ToString(channelID), message)
return
}
err = c.discord.Send(context.Background(), source, author, channelID, message)
err = c.discord.Send(context.Background(), source, author, channelID, message, "")
if err != nil {
log.Warn().Err(err).Msg("discord send")
} else {
Expand Down
16 changes: 15 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ type Config struct {
Telnet Telnet
EQLog EQLog
PEQEditor PEQEditor `toml:"peq_editor"`
UsersDatabasePath string `toml:"users_database"`
Nats Nats
UsersDatabasePath string `toml:"users_database"`
GuildsDatabasePath string `toml:"guilds_database"`
}

// Discord represents config settings for discord
Expand Down Expand Up @@ -68,6 +70,14 @@ type Telnet struct {
IsOOCAuctionEnabled bool `toml:"convert_ooc_auction"`
}

// Nats represents config settings for NATS
type Nats struct {
IsEnabled bool `toml:"enabled"`
Host string
IsOOCAuctionEnabled bool `toml:"convert_ooc_auction"`
ItemURL string `toml:"item_url"`
}

// EQLog represents config settings for the EQ live eqlog file
type EQLog struct {
IsEnabled bool `toml:"enabled"`
Expand Down Expand Up @@ -153,5 +163,9 @@ func NewConfig(ctx context.Context) (*Config, error) {
cfg.UsersDatabasePath = "./users.txt"
}

if cfg.GuildsDatabasePath == "" {
cfg.GuildsDatabasePath = "./guilds.txt"
}

return &cfg, nil
}
31 changes: 28 additions & 3 deletions config/config_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ keep_alive = true
# default: 10s
keep_alive_retry = "10s"
# Users by IDs are mapped to their display names via the raw text file called users database
# Users by ID are mapped to their display names via the raw text file called users database
# If users database file does not exist, a new one is created
# This file is actively monitored. if you edit it while talkeq is running, it will reload the changes
# This file is actively monitored. if you edit it while talkeq is running, it will reload the changes instantly
# This file overrides the IGN: playerName role tags in discord
# If a user is found on this list, it will fall back to check for IGN tags
# If you do an API registration, it will update the users database
users_database = "./users.txt"
# ** Only supported by NATS **
# Guilds by ID are mapped to their database ID via the raw text file called guilds database
# If guilds database file does not exist, and NATS is enabled, a new one is created
# This file is actively monitored. if you edit it while talkeq is running, it will reload the changes instantly
guilds_database = "./guilds.txt"
[discord]
# Enable Discord
Expand Down Expand Up @@ -147,6 +152,26 @@ users_database = "./users.txt"
# Listen for /guild (guild messages)
listen_guild = true
# NATS is a custom alternative to telnet
# that a very limited number of eqemu
# servers utilize. Chances are, you can ignore.
[nats]
# Enable NATS (eqemu server owners)
enabled = false
# Specify where NATS is located.
# default 127.0.0.1:4222
host = "127.0.0.1:4222"
# if a OOC message uses prefix WTS or WTB, convert them into auction
convert_ooc_auction = true
# Optional. Converts item URLs to provided field. defaults to allakhazam. To disable, change to ""
# default: "http://everquest.allakhazam.com/db/item.html?item="
item_url = "http://everquest.allakhazam.com/db/item.html?item="
[peq_editor.sql]
# Enable PEQ Editor SQL log parsing
Expand Down
Loading

0 comments on commit 11f226a

Please sign in to comment.