Example #1
0
func (ctx *Context) PerformLogin(host, name, pass string) (*imap.Client, error) {
	client, err := imap.NewClient(imap.TLS, host, name, pass, "")
	if err != nil {
		return nil, err
	}
	ctx.Session.Values["username"] = name
	ctx.Session.Values["pass"] = pass
	ctx.Session.Values["host"] = host

	clientKey := name + "@" + host
	ctx.Session.Values["client"] = clientKey
	clients[clientKey] = client
	return client, err
}
Example #2
0
func (ctx *Context) GetClient() (*imap.Client, error) {
	clientKey, ok := ctx.Session.Values["client"].(string)
	if !ok {
		return nil, NoClientPresent
	}
	client, ok := clients[clientKey]
	if !ok {
		user, ok := ctx.Session.Values["username"].(string)
		if !ok {
			return nil, NoClientPresent
		}
		pass, ok := ctx.Session.Values["pass"].(string)
		if !ok {
			return nil, NoClientPresent
		}
		host, ok := ctx.Session.Values["host"].(string)
		if !ok {
			return nil, NoClientPresent
		}
		return imap.NewClient(imap.TLS, host, user, pass, "")
	}
	return client, nil
}
Example #3
0
File: gmail.go Project: 0x7cc/rsc
func main() {
	flag.BoolVar(&imap.Debug, "imapdebug", false, "imap debugging trace")
	flag.Parse()

	acct = google.Acct(*acctName)

	if args := flag.Args(); len(args) > 0 {
		for i := range args {
			args[i] = "-to=" + args[i]
		}
		cmd := exec.Command("gmailsend", append([]string{"-a", acct.Email, "-i"}, args...)...)
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		if err := cmd.Run(); err != nil {
			fmt.Fprintf(os.Stderr, "!%s\n", err)
			os.Exit(1)
		}
		return
	}

	c, err := imap.NewClient(imap.TLS, "imap.gmail.com", acct.Email, acct.Password, "")
	if err != nil {
		log.Fatal(err)
	}
	isGmail = c.IsGmail()
	threaded = isGmail

	if *search != "" {
		b, err := c.GmailSearch(*search)
		if err != nil {
			log.Fatal(err)
		}
		inbox = b
	} else {
		inbox = c.Inbox()
		if err := inbox.Check(); err != nil {
			log.Fatal(err)
		}
	}

	msgs = inbox.Msgs()
	maxfrom = 12
	for i, m := range msgs {
		msgNum[m] = i
		if n := len(from(m.Hdr)); n > maxfrom {
			maxfrom = n
		}
	}
	if maxfrom > 20 {
		maxfrom = 20
	}
	subjlen = 80 - maxfrom

	rethread()

	interrupts := make(chan os.Signal, 1)
	signal.Notify(interrupts, syscall.SIGINT)

	go func() {
		for _ = range interrupts {
			fmt.Fprintf(os.Stderr, "!interrupt\n")
			interrupted = true
		}
	}()

	for {
		if dot != nil {
			fmt.Fprintf(bout, "%d", msgNum[dot.Msg]+1)
			if dot != &dot.Msg.Root {
				fmt.Fprintf(bout, ".%s", dot.ID)
			}
		}
		fmt.Fprintf(bout, ": ")
		bout.Flush()

		line, err := bin.ReadString('\n')
		if err != nil {
			break
		}

		cmd, err := parsecmd(line)
		if err != nil {
			fmt.Fprintf(bout, "!%s\n", err)
			continue
		}

		if cmd.Targ != nil || cmd.Targs == nil && cmd.A2 == 0 {
			x := cmd.F(cmd, cmd.Targ)
			if x != nil {
				dot = x
			}
		} else {
			targs := cmd.Targs
			if targs == nil {
				delta := +1
				if cmd.A1 > cmd.A2 {
					delta = -1
				}
				for i := cmd.A1; i <= cmd.A2; i += delta {
					if i < 1 || i > len(msgs) {
						continue
					}
					targs = append(targs, msgs[i-1])
				}
			}
			if cmd.Thread {
				if !isGmail {
					fmt.Fprintf(bout, "!need gmail for threaded command\n")
					continue
				}
				byThread := make(map[uint64][]*imap.Msg)
				for _, m := range msgs {
					t := m.GmailThread
					byThread[t] = append(byThread[t], m)
				}
				for _, m := range targs {
					t := m.GmailThread
					if byThread[t] != nil {
						if cmd.TF != nil {
							if x := cmd.TF(cmd, byThread[t]); x != nil {
								dot = x
							}
						} else {
							for _, mm := range byThread[t] {
								x := cmd.F(cmd, &mm.Root)
								if x != nil {
									dot = x
								}
							}
						}
					}
					delete(byThread, t)
				}
				continue
			}
			for _, m := range targs {
				if cmd.Delete {
					dcmd(cmd, &m.Root)
					if cmd.Name == "p" {
						// dp is a special case: it advances to the next message before the p.
						next := nextMsg(m)
						if next == nil {
							fmt.Fprintf(bout, "!address\n")
							dot = &m.Root
							break
						}
						m = next
					}
				}
				x := cmd.F(cmd, &m.Root)
				if x != nil {
					dot = x
				}
				// TODO: Break loop on interrupt.
			}
		}
	}
	qcmd(nil, nil)
}