]> git.jsancho.org Git - midgaard_bot.git/commitdiff
Open telnet connection to Merc MUD and receive welcome message
authorJavier Sancho <jsf@jsancho.org>
Tue, 12 Dec 2017 15:59:01 +0000 (16:59 +0100)
committerJavier Sancho <jsf@jsancho.org>
Tue, 12 Dec 2017 15:59:01 +0000 (16:59 +0100)
midgaard_bot.go
sessions.go

index ce92e73df009718e755192226d283ed5cc3d5bb0..10aab4b06a134f37768ac8a2df5302416de89b72 100644 (file)
@@ -49,7 +49,7 @@ func main() {
        ctx, cancel := context.WithCancel(context.Background())
        defer cancel()
 
        ctx, cancel := context.WithCancel(context.Background())
        defer cancel()
 
-       err = initSessions(ctx)
+       err = initSessions(Config.Merc.Host, ctx)
        if err != nil {
                log.Panic(err)
        }
        if err != nil {
                log.Panic(err)
        }
index a4f0c89a0e0ce4c7fcd10002118211407cf0ed09..17fa54555dcc062ec2a449e6f9700f4162070ec0 100644 (file)
@@ -20,9 +20,9 @@ package main
 
 import (
        "context"
 
 import (
        "context"
-       "log"
 
        "github.com/go-telegram-bot-api/telegram-bot-api"
 
        "github.com/go-telegram-bot-api/telegram-bot-api"
+       "github.com/reiver/go-telnet"
 )
 
 type Session struct {
 )
 
 type Session struct {
@@ -31,10 +31,12 @@ type Session struct {
 }
 
 var sessions map[int64]*Session
 }
 
 var sessions map[int64]*Session
+var mercHost string
 var sessionsCtx context.Context
 
 var sessionsCtx context.Context
 
-func initSessions(ctx context.Context) error {
+func initSessions(host string, ctx context.Context) error {
        sessions = make(map[int64]*Session)
        sessions = make(map[int64]*Session)
+       mercHost = host
        sessionsCtx = ctx
        return nil
 }
        sessionsCtx = ctx
        return nil
 }
@@ -56,19 +58,31 @@ func newSession(chat *tgbotapi.Chat) *Session {
 
 func startSession(session *Session) {
        ctx, _ := context.WithCancel(sessionsCtx)
 
 func startSession(session *Session) {
        ctx, _ := context.WithCancel(sessionsCtx)
+
        go func() {
        go func() {
-               for {
-                       select {
-                       case msg := <-session.Input:
-                               log.Printf("[%s] %s", msg.From.UserName, msg.Text)
-
-                               newMsg := tgbotapi.NewMessage(msg.Chat.ID, msg.Text)
-                               newMsg.ReplyToMessageID = msg.MessageID
-                               sendToTelegram(newMsg)
-                       case <-ctx.Done():
-                               return
-                       }
+               telnetInput, telnetOutput := make(chan string), make(chan string)
+               caller := TelnetCaller{
+                       Input: telnetInput,
+                       Output: telnetOutput,
                }
                }
+
+               go func() {
+                       for {
+                               select {
+                               case msg := <-session.Input:
+                                       if msg.Text != "/start" {
+                                               telnetInput <- msg.Text
+                                       }
+                               case body := <-telnetOutput:
+                                       newMsg := tgbotapi.NewMessage(session.Chat.ID, body)
+                                       sendToTelegram(newMsg)
+                               case <-ctx.Done():
+                                       return
+                               }
+                       }
+               }()
+
+               telnet.DialToAndCall(mercHost, caller)
        }()
 }
 
        }()
 }