Example #1
0
func printMessages(w io.Writer, msgs []*wl2k.Message) {
	rows := make([][]string, len(msgs))
	for i, msg := range msgs {
		to := msg.To()[0].Addr
		if len(msg.To()) > 1 {
			to = to + ", ..."
		}

		var flags string
		if mailbox.IsUnread(msg) {
			flags += "N" // New
		}

		rows[i] = []string{
			fmt.Sprintf("%2d", i),
			flags,
			msg.Subject(),
			msg.From().Addr,
			msg.Date().String(),
			to,
		}
	}
	t := gotabulate.Create(rows)
	t.SetHeaders([]string{"i", "Flags", "Subject", "From", "Date", "To"})
	t.SetAlign("left")
	t.SetWrapStrings(true)
	t.SetMaxCellSize(60)
	fmt.Fprintln(w, t.Render("simple"))
}
Example #2
0
func readMail() {
	w := os.Stdout
	rd := bufio.NewReader(os.Stdin)

	// Query user for mailbox to list
	printMailboxes(w)
	fmt.Fprintf(w, "\nChoose mailbox [0]: ")
	mailboxIdx := readInt(rd)
	if mailboxIdx+1 > len(mailboxes) {
		fmt.Fprintln(w, "Invalid mailbox number")
		return
	}

	for {
		// Fetch messages
		msgs, err := mailbox.LoadMessageDir(path.Join(mailbox.UserPath(fOptions.MailboxPath, fOptions.MyCall), mailboxes[mailboxIdx]))
		if err != nil {
			log.Fatal(err)
		} else if len(msgs) == 0 {
			fmt.Fprintf(w, "(empty)\n")
			break
		}

		// Print messages (sorted by date)
		sort.Sort(wl2k.ByDate(msgs))
		printMessages(w, msgs)

		// Query user for message to print
		fmt.Fprintf(w, "Choose message [n]: ")
		msgIdx := readInt(rd)
		if msgIdx+1 > len(msgs) {
			fmt.Fprintf(w, "invalid message number\n")
			continue
		}
		printMsg(w, msgs[msgIdx])

		// Mark as read?
		if mailbox.IsUnread(msgs[msgIdx]) {
			fmt.Fprintf(w, "Mark as read? [Y/n]: ")
			ans, _ := rd.ReadString('\n')
			if ans == "\n" || ans[0] == 'Y' || ans[0] == 'y' {
				mailbox.SetUnread(msgs[msgIdx], false)
			}
		}

		// Reply?
		fmt.Fprintf(w, "Reply (ctrl+c to quit) [y/N]: ")
		ans, _ := rd.ReadString('\n')
		if ans[0] == 'y' {
			composeMessage(msgs[msgIdx])
		}
	}
}
Example #3
0
func (m JSONMessage) MarshalJSON() ([]byte, error) {
	body, _ := m.Body()
	msg := struct {
		MID     string
		Date    time.Time
		From    wl2k.Address
		To      []wl2k.Address
		Cc      []wl2k.Address
		Subject string
		Body    string
		Files   []*wl2k.File
		P2POnly bool
		Unread  bool
	}{
		m.MID(), m.Date(), m.From(), m.To(), m.Cc(), m.Subject(), body, m.Files(), m.Header.Get("X-P2POnly") == "true", mailbox.IsUnread(m.Message),
	}

	return json.Marshal(msg)
}