about summary refs log blame commit diff stats
path: root/dwm.h
blob: 9b34210f596734053736ca090841ee309269513c (plain) (tree)
if err != nil { w.w.Logger.Printf("could not get message reader: %v", err) return err } w.w.PostMessage(&types.FullMessage{ Message: types.RespondTo(msg), Content: &models.FullMessage{ Uid: uid, Reader: r, }, }, nil) } w.done(msg) return nil } func (w *worker) handleReadMessages(msg *types.ReadMessages) error { for _, uid := range msg.Uids { m, err := w.msgFromUid(uid) if err != nil { w.w.Logger.Printf("could not get message: %v", err) w.err(msg, err) continue } if err := m.MarkRead(msg.Read); err != nil { w.w.Logger.Printf("could not mark message as read: %v", err) w.err(msg, err) continue } err = w.emitMessageInfo(m, msg) if err != nil { w.w.Logger.Printf(err.Error()) w.err(msg, err) continue } } w.done(msg) return nil } func (w *worker) handleSearchDirectory(msg *types.SearchDirectory) error { // the first item is the command (search / filter) s := strings.Join(msg.Argv[1:], " ") // we only want to search in the current query, so merge the two together search := fmt.Sprintf("(%v) and (%v)", w.query, s) uids, err := w.uidsFromQuery(search) if err != nil { return err } w.w.PostMessage(&types.SearchResults{ Message: types.RespondTo(msg), Uids: uids, }, nil) return nil } func (w *worker) handleModifyLabels(msg *types.ModifyLabels) error { for _, uid := range msg.Uids { m, err := w.msgFromUid(uid) if err != nil { return fmt.Errorf("could not get message from uid %v: %v", uid, err) } err = m.ModifyTags(msg.Add, msg.Remove) if err != nil { return fmt.Errorf("could not modify message tags: %v", err) } err = w.emitMessageInfo(m, msg) if err != nil { return err } } // tags changed, most probably some messages shifted to other folders // so we need to re-enumerate the query content err := w.emitDirectoryContents(msg) if err != nil { return err } // and update the list of possible tags w.emitLabelList() w.done(msg) return nil } func (w *worker) loadQueryMap(acctConfig *config.AccountConfig) error { raw, ok := acctConfig.Params["query-map"] if !ok { // nothing to do return nil } file, err := homedir.Expand(raw) if err != nil { return err } f, err := os.Open(file) if err != nil { return err } defer f.Close() w.nameQueryMap = make(map[string]string) scanner := bufio.NewScanner(f) for scanner.Scan() { line := scanner.Text() if line == "" || line[0] == '#' { continue } split := strings.SplitN(line, "=", 2) if len(split) != 2 { return fmt.Errorf("%v: invalid line %q, want name=query", file, line) } w.nameQueryMap[split[0]] = split[1] } return nil } func (w *worker) loadExcludeTags( acctConfig *config.AccountConfig) []string { raw, ok := acctConfig.Params["exclude-tags"] if !ok { // nothing to do return nil } excludedTags := strings.Split(raw, ",") return excludedTags } func (w *worker) emitDirectoryContents(parent types.WorkerMessage) error { uids, err := w.uidsFromQuery(w.query) if err != nil { return fmt.Errorf("could not fetch uids: %v", err) } sortedUids, err := w.sort(uids, w.currentSortCriteria) if err != nil { w.w.Logger.Printf("error sorting directory: %v", err) return err } w.w.PostMessage(&types.DirectoryContents{ Message: types.RespondTo(parent), Uids: sortedUids, }, nil) return nil } func (w *worker) emitMessageInfo(m *Message, parent types.WorkerMessage) error { info, err := m.MessageInfo() if err != nil { return fmt.Errorf("could not get MessageInfo: %v", err) } w.w.PostMessage(&types.MessageInfo{ Message: types.RespondTo(parent), Info: info, }, nil) return nil } func (w *worker) emitLabelList() { tags, err := w.db.ListTags() if err != nil { w.w.Logger.Printf("could not load tags: %v", err) return } w.w.PostMessage(&types.LabelList{Labels: tags}, nil) } func (w *worker) sort(uids []uint32, criteria []*types.SortCriterion) ([]uint32, error) { if len(criteria) == 0 { return uids, nil } var msgInfos []*models.MessageInfo for _, uid := range uids { m, err := w.msgFromUid(uid) if err != nil { w.w.Logger.Printf("could not get message: %v", err) continue } info, err := m.MessageInfo() if err != nil { w.w.Logger.Printf("could not get message info: %v", err) continue } msgInfos = append(msgInfos, info) } sortedUids, err := lib.Sort(msgInfos, criteria) if err != nil { w.w.Logger.Printf("could not sort the messages: %v", err) return nil, err } return sortedUids, nil }