github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/teambot_handler.go (about)

     1  // Handlers for teambot-related gregor messages
     2  
     3  package service
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"strings"
     9  
    10  	"golang.org/x/net/context"
    11  
    12  	"github.com/keybase/client/go/gregor"
    13  	"github.com/keybase/client/go/libkb"
    14  	"github.com/keybase/client/go/protocol/gregor1"
    15  	"github.com/keybase/client/go/protocol/keybase1"
    16  	"github.com/keybase/client/go/teambot"
    17  )
    18  
    19  const teambotHandlerName = "teambotHandler"
    20  
    21  type teambotHandler struct {
    22  	libkb.Contextified
    23  }
    24  
    25  var _ libkb.GregorInBandMessageHandler = (*teambotHandler)(nil)
    26  
    27  func newTeambotHandler(g *libkb.GlobalContext) *teambotHandler {
    28  	return &teambotHandler{
    29  		Contextified: libkb.NewContextified(g),
    30  	}
    31  }
    32  
    33  func (r *teambotHandler) Create(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) {
    34  	switch category {
    35  	case "teambot.new_teambot_key":
    36  		return true, r.newTeambotKey(ctx, cli, item)
    37  	case "teambot.teambot_key_needed":
    38  		return true, r.teambotKeyNeeded(ctx, cli, item)
    39  	default:
    40  		if strings.HasPrefix(category, "teambot.") {
    41  			return false, fmt.Errorf("unknown teambotHandler category: %q", category)
    42  		}
    43  		return false, nil
    44  	}
    45  }
    46  
    47  func (r *teambotHandler) Dismiss(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) {
    48  	return false, nil
    49  }
    50  
    51  func (r *teambotHandler) IsAlive() bool {
    52  	return true
    53  }
    54  
    55  func (r *teambotHandler) Name() string {
    56  	return teambotHandlerName
    57  }
    58  
    59  func (r *teambotHandler) newTeambotKey(ctx context.Context, cli gregor1.IncomingInterface, item gregor.Item) error {
    60  	r.G().Log.CDebugf(ctx, "teambotHandler: teambot.new_teambot_key received")
    61  	var msg keybase1.NewTeambotKeyArg
    62  	if err := json.Unmarshal(item.Body().Bytes(), &msg); err != nil {
    63  		r.G().Log.CDebugf(ctx, "error unmarshaling teambot.new_teambot_key item: %s", err)
    64  		return err
    65  	}
    66  	r.G().Log.CDebugf(ctx, "teambot.new_teambot_key unmarshaled: %+v", msg)
    67  
    68  	if err := teambot.HandleNewTeambotKey(r.MetaContext(ctx), msg.Id, msg.Application, msg.Generation); err != nil {
    69  		return err
    70  	}
    71  
    72  	r.G().Log.CDebugf(ctx, "dismissing teambot.new_teambot_key item since action succeeded")
    73  	return r.G().GregorState.DismissItem(ctx, cli, item.Metadata().MsgID())
    74  }
    75  
    76  func (r *teambotHandler) teambotKeyNeeded(ctx context.Context, cli gregor1.IncomingInterface, item gregor.Item) error {
    77  	r.G().Log.CDebugf(ctx, "teambotHandler: teambot.teambot_key_needed received")
    78  	var msg keybase1.TeambotKeyNeededArg
    79  	if err := json.Unmarshal(item.Body().Bytes(), &msg); err != nil {
    80  		r.G().Log.CDebugf(ctx, "error unmarshaling teambot.teambot_key_needed item: %s", err)
    81  		return err
    82  	}
    83  	r.G().Log.CDebugf(ctx, "teambot.teambot_key_needed unmarshaled: %+v", msg)
    84  
    85  	if err := teambot.HandleTeambotKeyNeeded(r.MetaContext(ctx), msg.Id, msg.Uid, msg.Application, msg.Generation); err != nil {
    86  		r.G().Log.CDebugf(ctx, "teambot.teambot_key_needed unable to make new key: %v", err)
    87  	}
    88  
    89  	r.G().Log.CDebugf(ctx, "dismissing teambot.teambot_key_needed item")
    90  	return r.G().GregorState.DismissItem(ctx, cli, item.Metadata().MsgID())
    91  }