Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling for "message of the day" #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions c/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ char *call_write(wrapped_context_t *context, uint8_t *buffer, int length) {
return context->impl.write(context->clientCtx, buffer, length);
}

void call_notify_motd(wrapped_context_t* context) {
if (context->impl.notify_motd != NULL) {
return context->impl.notify_motd(context->clientCtx, context->motd);
}
}

read_result_t call_read(wrapped_context_t *context, uint8_t *buffer,
int length) {
return context->impl.read(context->clientCtx, buffer, length);
Expand Down Expand Up @@ -102,6 +108,10 @@ void free_wrapped_context(wrapped_context_t *wctx) {
free(wctx->config.transit_relay_url);
}

if (wctx->motd != NULL) {
free(wctx->motd);
}

free(wctx);
}
}
Expand Down
13 changes: 11 additions & 2 deletions c/client.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"unsafe"

"github.com/psanford/wormhole-william/rendezvous"
"github.com/psanford/wormhole-william/wormhole"
)

Expand Down Expand Up @@ -107,7 +108,11 @@ func sendFile(transfer PendingTransfer, fileName string) {
return
}

code, status, err := transfer.NewClient().SendFile(ctx, fileName, reader, NO_LISTEN, wormhole.WithProgress(transfer.UpdateProgress))
code, status, err := transfer.NewClient().SendFile(ctx, fileName, reader, NO_LISTEN,
wormhole.WithProgress(transfer.UpdateProgress),
wormhole.WithConnectInfoHandler(func(info *rendezvous.ConnectInfo) {
transfer.HandleMOTD(info.MOTD)
}))

if err != nil {
transfer.NotifyCodeGenerationFailure(C.CodeGenerationFailed, err.Error())
Expand Down Expand Up @@ -180,7 +185,11 @@ func recvFile(transfer PendingTransfer, code string) {
addPendingTransfer(transfer.Reference(), cancelFunc)
downloadId := transfer.Reference()

msg, err := transfer.NewClient().Receive(ctx, code, NO_LISTEN, wormhole.WithProgress(transfer.UpdateProgress))
msg, err := transfer.NewClient().Receive(ctx, code, NO_LISTEN,
wormhole.WithProgress(transfer.UpdateProgress),
wormhole.WithConnectInfoHandler(func(info *rendezvous.ConnectInfo) {
transfer.HandleMOTD(info.MOTD)
}))

if err != nil {
transfer.NotifyError(C.ReceiveFileError, err.Error())
Expand Down
4 changes: 4 additions & 0 deletions c/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ typedef struct {

typedef void (*notifyf)(void *context, result_t *result);
typedef void (*notifycodegenf)(void *context, codegen_result_t *result);
typedef void (*notifymotd)(void* context, char* motd);
typedef void (*update_progressf)(void *context, progress_t *progress);

typedef void (*update_metadataf)(void *context, file_metadata_t *metadata);
Expand All @@ -95,6 +96,7 @@ typedef struct {
readf read;
seekf seek;
update_progressf update_progress;
notifymotd notify_motd;
notifyf notify;
notifycodegenf notify_codegen;
update_metadataf update_metadata;
Expand All @@ -111,6 +113,7 @@ typedef struct _wrapped_context_t {
client_config_t config;

progress_t progress;
char* motd;
result_t result;
codegen_result_t codegen_result;
file_metadata_t metadata;
Expand All @@ -120,6 +123,7 @@ void call_notify(wrapped_context_t *context);
void call_notify_codegen(wrapped_context_t *context);
void call_update_progress(wrapped_context_t *context);
void call_update_metadata(wrapped_context_t *context);
void call_notify_motd(wrapped_context_t* context);

read_result_t call_read(wrapped_context_t *context, uint8_t *buffer,
int32_t length);
Expand Down
7 changes: 7 additions & 0 deletions c/wrapped_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
type PendingTransfer interface {
Log(message string, args ...interface{})
UpdateProgress(done int64, total int64)
HandleMOTD(motd string)
NotifyError(result C.result_type_t, errorMessage string)
UpdateMetadata(fileName string, length int64)
Write(bytes unsafe.Pointer, length int) error
Expand Down Expand Up @@ -242,3 +243,9 @@ func (wctx *C.wrapped_context_t) Malloc(size int) (unsafe.Pointer, error) {

return block, nil
}

func (wctx *C.wrapped_context_t) HandleMOTD(motd string) {
wctx.Log("Mesage of the day:\n%s\n", motd)
wctx.motd = C.CString(motd)
C.call_notify_motd(wctx)
}
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
transitHelper string
verify bool
hideProgressBar bool
hideMOTD bool
disableListener bool
)

Expand Down
12 changes: 11 additions & 1 deletion cmd/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/cheggaaa/pb/v3"
"github.com/klauspost/compress/zip"
"github.com/psanford/wormhole-william/rendezvous"
"github.com/psanford/wormhole-william/wormhole"
"github.com/spf13/cobra"
)
Expand All @@ -30,6 +31,7 @@ func recvCommand() *cobra.Command {

cmd.Flags().BoolVarP(&verify, "verify", "v", false, "display verification string (and wait for approval)")
cmd.Flags().BoolVar(&hideProgressBar, "hide-progress", false, "suppress progress-bar display")
cmd.Flags().BoolVar(&hideMOTD, "hide-motd", false, "suppress message of the day")

cmd.ValidArgsFunction = recvCodeCompletion

Expand Down Expand Up @@ -65,7 +67,15 @@ func recvAction(cmd *cobra.Command, args []string) {
}
}

msg, err := c.Receive(ctx, code, disableListener)
opts := []wormhole.TransferOption{}

if !hideMOTD {
opts = append(opts, wormhole.WithConnectInfoHandler(func(info *rendezvous.ConnectInfo) {
fmt.Println(info)
}))
}

msg, err := c.Receive(ctx, code, disableListener, opts...)
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/cheggaaa/pb/v3"
qrterminal "github.com/mdp/qrterminal/v3"
"github.com/psanford/wormhole-william/rendezvous"
"github.com/psanford/wormhole-william/wormhole"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -59,6 +60,7 @@ func sendCommand() *cobra.Command {
cmd.Flags().StringVar(&codeFlag, "code", "", "human-generated code phrase")
cmd.Flags().StringVar(&sendTextFlag, "text", "", "text message to send, instead of a file.\nUse '-' to read from stdin")
cmd.Flags().BoolVar(&hideProgressBar, "hide-progress", false, "suppress progress-bar display")
cmd.Flags().BoolVar(&hideMOTD, "hide-motd", false, "suppress message of the day")
cmd.Flags().BoolVar(&showQRCode, "qr", false, "display code as QR code (experimental)")

return &cmd
Expand Down Expand Up @@ -144,6 +146,12 @@ func sendFile(filename string) {
}))
}

if !hideMOTD {
args = append(args, wormhole.WithConnectInfoHandler(func(info *rendezvous.ConnectInfo) {
fmt.Println(info.MOTD)
}))
}

code, status, err := c.SendFile(ctx, filepath.Base(filename), f, disableListener, args...)
if err != nil {
bail("Error sending message: %s", err)
Expand Down
22 changes: 20 additions & 2 deletions wormhole/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package wormhole

import "github.com/psanford/wormhole-william/rendezvous"

type transferOptions struct {
code string
progressFunc progressFunc
code string
progressFunc progressFunc
connectInfoFunc connectInfoFunc
}

type TransferOption interface {
Expand Down Expand Up @@ -50,3 +53,18 @@ func (o progressTransferOption) setOption(opts *transferOptions) error {
func WithProgress(f func(sentBytes int64, totalBytes int64)) TransferOption {
return progressTransferOption{f}
}

type connectInfoFunc func(*rendezvous.ConnectInfo)

type connectInfoHandlerOption struct {
connectInfoFunc connectInfoFunc
}

func (o connectInfoHandlerOption) setOption(opts *transferOptions) error {
opts.connectInfoFunc = o.connectInfoFunc
return nil
}

func WithConnectInfoHandler(f connectInfoFunc) TransferOption {
return connectInfoHandlerOption{f}
}
6 changes: 5 additions & 1 deletion wormhole/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Client) Receive(ctx context.Context, code string, disableListener bool,
rc.Close(ctx, mood)
}()

_, err := rc.Connect(ctx)
info, err := rc.Connect(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,6 +128,10 @@ func (c *Client) Receive(ctx context.Context, code string, disableListener bool,
}
}

if fr.options.connectInfoFunc != nil {
fr.options.connectInfoFunc(info)
}

if offer.Message != nil {
answer := genericMessage{
Answer: &answerMsg{
Expand Down
5 changes: 4 additions & 1 deletion wormhole/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ func (c *Client) sendFileDirectory(ctx context.Context, offer *offerMsg, r io.Re
appID := c.AppID
rc := rendezvous.NewClient(c.RendezvousURL, sideID, appID)

_, err := rc.Connect(ctx)
info, err := rc.Connect(ctx)
if options.connectInfoFunc != nil {
options.connectInfoFunc(info)
}
if err != nil {
return "", nil, err
}
Expand Down