Skip to content

Commit

Permalink
add complete support for connecting and webirc
Browse files Browse the repository at this point in the history
  • Loading branch information
qaisjp committed Jun 25, 2017
1 parent 9a61c83 commit 9e7b7d4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 17 deletions.
5 changes: 3 additions & 2 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Options struct {
IRCServer string
IRCUseTLS bool
IRCListenerName string // i.e, "DiscordBot", required to listen for messages in all cases
WebIRCPass string
}

// A Bridge represents a bridging between an IRC server and channels in a Discord server
Expand Down Expand Up @@ -76,8 +77,8 @@ func New(opts Options) (*Bridge, error) {
}

discord, err := prepareDiscord(dib, opts.DiscordBotToken, opts.GuildID)
ircPrimary := prepareIRCListener(dib)
ircManager := prepareIRCManager(opts.IRCServer)
ircPrimary := prepareIRCListener(dib, opts.WebIRCPass)
ircManager := prepareIRCManager(opts.IRCServer, opts.WebIRCPass)

if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions bridge/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ func (d *discordBot) handleMemberUpdate(m *discordgo.Member) {
Nick: nickname,
Discriminator: m.User.Discriminator,
ID: m.User.ID,
Bot: m.User.Bot, // this should never change, we can't handle it when this changes, but it's ok
}
}
2 changes: 1 addition & 1 deletion bridge/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (h *home) loop() {
continue
}

h.ircManager.CreateConnection(user.ID, user.Discriminator, user.Nick)
h.ircManager.CreateConnection(user.ID, user.Discriminator, user.Nick, user.Bot)
// Done!
case <-h.done:
fmt.Println("Closing all connections!")
Expand Down
10 changes: 5 additions & 5 deletions bridge/irc_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type ircListener struct {
h *home
}

func prepareIRCListener(dib *Bridge) *ircListener {
irccon := irc.IRC(dib.ircPrimaryName, "BetterDiscordBot")
func prepareIRCListener(dib *Bridge, webIRCPass string) *ircListener {
irccon := irc.IRC(dib.ircPrimaryName, "github.com/qaisjp/go-discord-irc")
irc := &ircListener{irccon, nil}

setupIRCConnection(irccon)
// con.VerboseCallbackHandler = true
// con.Debug = true
setupIRCConnection(irccon, webIRCPass, "discord.", "fd75:f5f5:226f::")
// irccon.VerboseCallbackHandler = true
// irccon.Debug = true

// Welcome event
irccon.AddCallback("001", irc.OnWelcome)
Expand Down
25 changes: 20 additions & 5 deletions bridge/irc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
type ircManager struct {
ircConnections map[string]*ircConnection
ircServerAddress string
webIRCPass string

h *home
}

func prepareIRCManager(ircServerAddress string) *ircManager {
func prepareIRCManager(ircServerAddress, webIRCPass string) *ircManager {
return &ircManager{
ircConnections: make(map[string]*ircConnection),
ircServerAddress: ircServerAddress,
webIRCPass: webIRCPass,
}
}

Expand All @@ -31,7 +33,7 @@ func (m *ircManager) DisconnectAll() {
}
}

func (m *ircManager) CreateConnection(userID string, discriminator string, nick string) (*ircConnection, error) {
func (m *ircManager) CreateConnection(userID string, discriminator string, nick string, isBot bool) (*ircConnection, error) {
if con, ok := m.ircConnections[userID]; ok {
con.UpdateDetails(discriminator, nick)
return con, nil
Expand All @@ -41,8 +43,21 @@ func (m *ircManager) CreateConnection(userID string, discriminator string, nick

username := m.generateUsername(discriminator, nick)

innerCon := irc.IRC(username, "BetterDiscordBot")
setupIRCConnection(innerCon)
innerCon := irc.IRC(username, nick+"~"+discriminator)
innerCon.Debug = true

var ip string
{
baseip := "fd75:f5f5:226f:"
if isBot {
baseip += "2"
} else {
baseip += "1"
}
ip = SnowflakeToIP(baseip, userID)
}

setupIRCConnection(innerCon, m.webIRCPass, userID+".discord", ip)

con := &ircConnection{
Connection: innerCon,
Expand All @@ -69,7 +84,7 @@ func (m *ircManager) CreateConnection(userID string, discriminator string, nick

// TODO: Catch username changes, and cache UserID:Username mappings somewhere
func (m *ircManager) generateUsername(_ string, nick string) string {
return nick + "^d"
return nick + "~d"
// return fmt.Sprintf("[%s-%s]", username, discriminator), nil
}

Expand Down
1 change: 1 addition & 0 deletions bridge/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type DiscordUser struct {
Nick string // non-unique nickname
Discriminator string // locally unique ID
ID string // globally unique id
Bot bool // are they a bot?
}
8 changes: 6 additions & 2 deletions bridge/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bridge

import (
"crypto/tls"
"fmt"
"strconv"

"strings"
Expand All @@ -13,9 +14,12 @@ import (
)

// TOOD: Insecure TLS!
func setupIRCConnection(con *irc.Connection) {
func setupIRCConnection(con *irc.Connection, webIRCPass, hostname, ip string) {
con.UseTLS = true
con.TLSConfig = &tls.Config{InsecureSkipVerify: true} // TODO: REALLY, THIS IS NOT A VERIFIED CONNECTION!

// this requires a modification to thoj/go-ircevent
con.InitialCommand = fmt.Sprintf("WEBIRC %s discord %s %s", webIRCPass, hostname, ip)
}

// Leftpad is from github.com/douglarek/leftpad
Expand All @@ -35,7 +39,7 @@ func Leftpad(s string, length int, ch ...rune) string {
func SnowflakeToIP(base string, snowflake string) string {
num, err := strconv.ParseUint(snowflake, 10, 64)
if err != nil {
panic(errors.Wrap(err, "could not convert snowflake to uint"))
panic(errors.Wrapf(err, "could not convert snowflake to uint: %s", snowflake))
}

for i, c := range Leftpad(strconv.FormatUint(num, 16), 16, '0') {
Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
func main() {
discordBotToken := flag.String("discord_token", "", "Discord Bot User Token")
channelMappings := flag.String("channel_mappings", "", "Discord:IRC mappings in format '#discord1:#irc1,#discord2:#irc2,...'")
ircUsername := flag.String("irc_listener_name", "tilded", "Name for IRC-side bot, for listening to messages.")
ircUsername := flag.String("irc_listener_name", "~d", "Name for IRC-side bot, for listening to messages.")
ircServer := flag.String("irc_server", "", "Server address to use, example `irc.freenode.net:7000`.")
ircNoTLS := flag.Bool("no_irc_tls", false, "Disable TLS for IRC bots?")
guildID := flag.String("guild_id", "", "Guild to use")
webIRCPass := flag.String("webirc_pass", "", "Password for WEBIRC")

flag.Parse()

Expand All @@ -26,13 +27,18 @@ func main() {
return
}

if *webIRCPass == "" {
fmt.Println("Warning: webirc_pass is empty")
}

dib, err := bridge.New(bridge.Options{
DiscordBotToken: *discordBotToken,
GuildID: *guildID,
ChannelMappings: mappingsMap,
IRCListenerName: *ircUsername,
IRCServer: *ircServer,
IRCUseTLS: !*ircNoTLS,
IRCUseTLS: !*ircNoTLS, // exclamation mark is NOT a typo
WebIRCPass: *webIRCPass,
})

if err != nil {
Expand Down

0 comments on commit 9e7b7d4

Please sign in to comment.