示例#1
0
文件: read.go 项目: LA3QMA/wl2k-go
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])
		}
	}
}
示例#2
0
文件: http.go 项目: LA3QMA/wl2k-go
func readHandler(w http.ResponseWriter, r *http.Request) {
	var data struct{ Read bool }
	if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		log.Printf("%s %s: %s", r.Method, r.URL.Path, err)
		return
	}

	box, mid := mux.Vars(r)["box"], mux.Vars(r)["mid"]

	msg, err := mailbox.OpenMessage(path.Join(mbox.MBoxPath, box, mid))
	if err != nil {
		log.Printf("%s %s: %s", r.Method, r.URL.Path, err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if err := mailbox.SetUnread(msg, !data.Read); err != nil {
		log.Printf("%s %s: %s", r.Method, r.URL.Path, err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}