github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/service/emails.go (about) 1 package service 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/keybase/client/go/emails" 9 "github.com/keybase/client/go/gregor" 10 "github.com/keybase/client/go/libkb" 11 gregor1 "github.com/keybase/client/go/protocol/gregor1" 12 keybase1 "github.com/keybase/client/go/protocol/keybase1" 13 "github.com/keybase/go-framed-msgpack-rpc/rpc" 14 15 "golang.org/x/net/context" 16 ) 17 18 type EmailsHandler struct { 19 libkb.Contextified 20 *BaseHandler 21 } 22 23 func NewEmailsHandler(xp rpc.Transporter, g *libkb.GlobalContext) *EmailsHandler { 24 handler := &EmailsHandler{ 25 Contextified: libkb.NewContextified(g), 26 BaseHandler: NewBaseHandler(g, xp), 27 } 28 return handler 29 } 30 31 var _ keybase1.EmailsInterface = (*EmailsHandler)(nil) 32 33 func (h *EmailsHandler) AddEmail(ctx context.Context, arg keybase1.AddEmailArg) (err error) { 34 mctx := libkb.NewMetaContext(ctx, h.G()) 35 defer mctx.Trace("EmailsHandler#AddEmail", &err)() 36 return emails.AddEmail(mctx, arg.Email, arg.Visibility) 37 } 38 39 func (h *EmailsHandler) DeleteEmail(ctx context.Context, arg keybase1.DeleteEmailArg) (err error) { 40 mctx := libkb.NewMetaContext(ctx, h.G()) 41 defer mctx.Trace("EmailsHandler#DeleteEmail", &err)() 42 return emails.DeleteEmail(mctx, arg.Email) 43 } 44 45 func (h *EmailsHandler) SetPrimaryEmail(ctx context.Context, arg keybase1.SetPrimaryEmailArg) (err error) { 46 mctx := libkb.NewMetaContext(ctx, h.G()) 47 defer mctx.Trace("EmailsHandler#SetPrimaryEmail", &err)() 48 return emails.SetPrimaryEmail(mctx, arg.Email) 49 } 50 51 func (h *EmailsHandler) EditEmail(ctx context.Context, arg keybase1.EditEmailArg) (err error) { 52 mctx := libkb.NewMetaContext(ctx, h.G()) 53 defer mctx.Trace("EmailsHandler#EditEmail", &err)() 54 err = emails.DeleteEmail(mctx, arg.OldEmail) 55 if err != nil { 56 return err 57 } 58 return emails.AddEmail(mctx, arg.Email, arg.Visibility) 59 } 60 61 func (h *EmailsHandler) SendVerificationEmail(ctx context.Context, arg keybase1.SendVerificationEmailArg) (err error) { 62 mctx := libkb.NewMetaContext(ctx, h.G()) 63 defer mctx.Trace("EmailsHandler#SendVerificationEmail", &err)() 64 return emails.SendVerificationEmail(mctx, arg.Email) 65 } 66 67 func (h *EmailsHandler) SetVisibilityEmail(ctx context.Context, arg keybase1.SetVisibilityEmailArg) (err error) { 68 mctx := libkb.NewMetaContext(ctx, h.G()) 69 defer mctx.Trace("EmailsHandler#SetVisibilityEmailArg", &err)() 70 return emails.SetVisibilityEmail(mctx, arg.Email, arg.Visibility) 71 } 72 73 func (h *EmailsHandler) SetVisibilityAllEmail(ctx context.Context, arg keybase1.SetVisibilityAllEmailArg) (err error) { 74 mctx := libkb.NewMetaContext(ctx, h.G()) 75 defer mctx.Trace("EmailsHandler#SetVisibilityAllEmailArg", &err)() 76 return emails.SetVisibilityAllEmail(mctx, arg.Visibility) 77 } 78 79 func (h *EmailsHandler) GetEmails(ctx context.Context, sessionID int) (ret []keybase1.Email, err error) { 80 mctx := libkb.NewMetaContext(ctx, h.G()) 81 defer mctx.Trace("EmailsHandler#GetEmails", &err)() 82 return emails.GetEmails(mctx) 83 } 84 85 const emailsGregorHandlerName = "emailHandler" 86 87 type emailsGregorHandler struct { 88 libkb.Contextified 89 } 90 91 var _ libkb.GregorInBandMessageHandler = (*emailsGregorHandler)(nil) 92 93 func newEmailsGregorHandler(g *libkb.GlobalContext) *emailsGregorHandler { 94 return &emailsGregorHandler{ 95 Contextified: libkb.NewContextified(g), 96 } 97 } 98 99 func (r *emailsGregorHandler) Create(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) { 100 switch category { 101 case "email.added", "email.edited", "email.primary_changed", "email.deleted", "email.visibility_changed": 102 return true, r.handleEmailChangedMsg(ctx, cli, category, item) 103 case "email.verified": 104 return true, r.handleVerifiedMsg(ctx, cli, item) 105 case "email.unverified_count": 106 // handled in badgestate 107 return true, nil 108 default: 109 if strings.HasPrefix(category, "email.") { 110 return false, fmt.Errorf("unknown emailsGregorHandler category: %q", category) 111 } 112 return false, nil 113 } 114 } 115 116 func (r *emailsGregorHandler) Dismiss(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) { 117 return false, nil 118 } 119 120 func (r *emailsGregorHandler) IsAlive() bool { 121 return true 122 } 123 124 func (r *emailsGregorHandler) Name() string { 125 return emailsGregorHandlerName 126 } 127 128 func (r *emailsGregorHandler) handleVerifiedMsg(ctx context.Context, cli gregor1.IncomingInterface, item gregor.Item) error { 129 m := libkb.NewMetaContext(ctx, r.G()) 130 m.Debug("emailsGregorHandler: email.verified received") 131 var msg keybase1.EmailAddressVerifiedMsg 132 if err := json.Unmarshal(item.Body().Bytes(), &msg); err != nil { 133 m.Debug("error unmarshaling email.verified item: %s", err) 134 return err 135 } 136 m.Debug("email.verified unmarshaled: %+v", msg) 137 138 r.G().NotifyRouter.HandleEmailAddressVerified(ctx, msg.Email) 139 return r.G().GregorState.DismissItem(ctx, cli, item.Metadata().MsgID()) 140 } 141 142 func (r *emailsGregorHandler) handleEmailChangedMsg(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) error { 143 mctx := libkb.NewMetaContext(ctx, r.G()) 144 mctx.Debug("emailsGregorHandler: %s received", category) 145 var msg keybase1.EmailAddressChangedMsg 146 if err := json.Unmarshal(item.Body().Bytes(), &msg); err != nil { 147 mctx.Debug("error unmarshaling %s item: %s", category, err) 148 return err 149 } 150 mctx.Debug("%s unmarshaled: %+v", category, msg) 151 152 emailList, err := emails.GetEmails(mctx) 153 if err != nil { 154 mctx.Error("Could not get current phone number list during handleEmailChangedMsg: %s", err) 155 } else { 156 r.G().NotifyRouter.HandleEmailAddressChanged(ctx, emailList, category, msg.Email) 157 } 158 // Do not dismiss these notifications so other devices can see it and 159 // update their state as well. 160 return nil 161 }