コード例 #1
0
ファイル: command.go プロジェクト: danielkrainas/web-cipher
func run(ctx context.Context, args []string) error {
	msg := context.GetStringValue(ctx, "flags.message")
	if readFromIn, ok := ctx.Value("flags.in").(bool); ok {
		if readFromIn && msg != "" {
			return errors.New("cannot specify a message flag and reading from input")
		} else if readFromIn {
			msg = readMessageFromStdin()
		}
	}

	if msg == "" {
		return fmt.Errorf("you must specify a message to encode")
	}

	var urls []string
	var err error
	urlListFile := context.GetStringValue(ctx, "flags.urls")
	if urlListFile != "" {
		urls, err = readUrlList(urlListFile)
		if err != nil {
			return err
		}
	}

	var references []*cipher.PageReference
	i := uint16(0)
	for _, url := range urls {
		refs, err := cipher.GetReferences(url, i)
		if err != nil {
			return fmt.Errorf("error getting references: %v\n", err)
		}

		references = append(references, refs...)
		i++
	}

	var used []*cipher.EncodedReference
	buf := []byte(msg)
	for _, b := range buf {
		encoded := cipher.NextReference(b, used, references)
		if encoded == nil {
			fmt.Printf("Couldn't find anything for %x\n", b)
		} else {
			fmt.Printf("%s/", encoded.Base77())
			used = append(used, encoded)
		}
	}

	fmt.Print("\n\n")
	return nil
}
コード例 #2
0
ファイル: command.go プロジェクト: danielkrainas/web-cipher
func run(ctx context.Context, args []string) error {
	encodedMsg := context.GetStringValue(ctx, "flags.message")
	if readFromIn, ok := ctx.Value("flags.in").(bool); ok {
		if encodedMsg != "" && readFromIn {
			return errors.New("cannot specify a message flag and reading from input")
		} else if readFromIn {
			encodedMsg = readMessageFromStdin()
		}
	}

	if encodedMsg == "" {
		return fmt.Errorf("you must specify a message to decode")
	}

	var urls []string
	var err error
	urlListFile := context.GetStringValue(ctx, "flags.urls")
	if urlListFile != "" {
		urls, err = readUrlList(urlListFile)
		if err != nil {
			return err
		}
	}

	var references []*cipher.PageReference
	i := uint16(0)
	for _, url := range urls {
		refs, err := cipher.GetReferences(url, i)
		if err != nil {
			return fmt.Errorf("error getting references: %v\n", err)
		}

		references = append(references, refs...)
		i++
	}

	for _, glyph := range strings.Split(encodedMsg, "/") {
		e := cipher.FromBase77(glyph)
		r := cipher.Lookup(e.Reference.Index, e.Reference.Level, e.Reference.Url, references)
		if r == nil || uint16(len(r.Text)) < e.CharIndex {
			fmt.Print("#")
		} else {
			fmt.Print(r.Text[e.CharIndex])
		}
	}

	fmt.Print("\n\n")
	return nil
}