func New(endpoint string, authtype authTypes, username string, token string) (client *Client, err error) { if endpoint == "" { endpoint = GITHUB_API } endpoint_uri, err := url.Parse(endpoint) if err != nil { return nil, InvalidEndpoint } switch authtype { case AUTH_NONE: if username != "" || token != "" { return nil, errors.New("username and token must be empty when using AUTH_NONE") } case AUTH_OAUTH2: fallthrough case AUTH_PASSWORD: if username == "" || token == "" { return nil, InvalidAuthData } case AUTH_NETRC: if machine, err := netrc.FindMachine(os.ExpandEnv("${HOME}/.netrc"), endpoint_uri.Host); err != nil || machine.Name == "default" { return nil, InvalidAuthData } else { username = machine.Login token = machine.Password } default: return nil, UnknownAuthMethod } client = &Client{endpoint: endpoint_uri, username: username, token: token, authtype: authtype, Client: &http.Client{}} return client, nil }
func getCreds(u *url.URL) (user, pass string) { m, err := netrc.FindMachine(netrcPath, u.Host) if err != nil { fmt.Printf("netrc error (%s): %v", u.Host, err) } return m.Login, m.Password }
func netrcApiKey() string { if u, err := user.Current(); err == nil { netrcPath := u.HomeDir + "/.netrc" if _, err := os.Stat(netrcPath); err == nil { key, _ := netrc.FindMachine(netrcPath, "api.heroku.com") if key.Password != "" { return key.Password } } } return "" }
func (n NetrcAuth) String() string { netrcPath := n.NetrcPath if netrcPath == "" { netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc") } apiURL, _ := url.Parse(gitHubAPIURL) credentials, err := netrc.FindMachine(netrcPath, apiURL.Host) if err != nil { panic(fmt.Errorf("netrc error (%s): %v", apiURL.Host, err)) } return fmt.Sprintf("Basic %s", hashAuth(credentials.Login, credentials.Password)) }
func getCreds(u *url.URL) (user, pass string) { if u.User != nil { pw, _ := u.User.Password() return u.User.Username(), pw } m, err := netrc.FindMachine(os.Getenv("HOME")+"/.netrc", u.Host) if err != nil { log.Fatalf("netrc error (%s): %v", u.Host, err) } return m.Login, m.Password }
func main() { machine := *flag.String("machine", "", "Remote machine name") login := *flag.String("login", "", "User of the remote machine") password := *flag.String("password", "", "Password of the remote machine") directory := *flag.String("directory", "", "Password of the remote machine") flag.Parse() println("") println("upChanges", version) println("") if machine == "" { if len(os.Args) > 1 { machine = os.Args[1] } else { exit("No machine given.") } } if directory == "" { if len(os.Args) > 2 { directory = os.Args[len(os.Args)-1] } else { exit("No directory given.") } } fmt.Println(machine) fmt.Println(directory) usr, err := user.Current() exitIf(err) if login == "" && password == "" { // Open .netrc mach, err := netrc.FindMachine(usr.HomeDir+"/.netrc", machine) exitIf(err) fmt.Printf("%v", mach) } }
func main() { version := flag.Bool("version", false, "prints current version") flag.Parse() if *version { fmt.Println(VERSION) fmt.Fprintf(os.Stderr, "Upgrade: go get -u github.com/kaihendry/imap2json\n") os.Exit(0) } verbose := flag.Bool("v", false, "verbose") flag.Usage = usage flag.Parse() if flag.NArg() != 1 { usage() } iurl, err := url.ParseRequestURI(flag.Arg(0)) if err != nil { usage() } if iurl.Scheme != "imaps" && iurl.Scheme != "imap" { usage() } var ( c *imap.Client cmd *imap.Command rsp *imap.Response ) // Lets check if we can reach the host tc, err := net.Dial("tcp", iurl.Host+":"+iurl.Scheme) if err == nil { tc.Close() if *verbose { fmt.Printf("Dial to %s succeeded\n", iurl.Host) } } else { panic(err) } if *verbose { imap.DefaultLogger = log.New(os.Stdout, "", 0) imap.DefaultLogMask = imap.LogConn | imap.LogRaw } if iurl.Scheme == "imaps" { fmt.Println("Making a secure connection to", iurl.Host) c, err = imap.DialTLS(iurl.Host, nil) if err != nil { fmt.Println(err.Error()) } } else { // It's just imap c, _ = imap.Dial(iurl.Host) } // Logout once done defer func() { c.Logout(30 * time.Second) }() //fmt.Println("Server says hello:", c.Data[0].Info) //c.Data = nil if iurl.User == nil { fmt.Println("Logging in Anonymously...") c.Anonymous() } else { // Authenticate if c.State() == imap.Login { user := iurl.User.Username() pass, _ := iurl.User.Password() n := os.Getenv("HOME") + "/.netrc" m, err := netrc.FindMachine(n, iurl.Host) if err == nil { user = m.Login pass = m.Password fmt.Println("Using", user, "from", n) } c.Login(user, pass) } else { fmt.Printf("Login not presented") return } if err != nil { fmt.Printf("login failed, exiting...\n") return } } if iurl.Path != "" { // Has user asked us to grab a particular folder/mailbox? // Remove / prefix mailbox := iurl.Path[1:] fmt.Println("Selecting mailbox:", mailbox) c.Select(mailbox, true) } else { c.Select("INBOX", true) } rcmd, err := imap.Wait(c.Send("UID THREAD", "references UTF-8 all")) if err != nil { fmt.Println("Your IMAPD server", iurl.Host, "sadly does not support UID THREAD (rfc5256)") fmt.Println("Please consider exporting your email and serving it via http://dovecot.org/ IMAPD") panic(err) } // Flatten thread tree stucture flat := dumpl(rcmd.Data[0].Fields[1:]) // fmt.Println("Flat:", flat) err = os.MkdirAll("raw", 0777) err = os.MkdirAll("c", 0777) if err != nil { panic(err) } err = os.MkdirAll("c", 0777) if err != nil { panic(err) } // Fetch everything TODO: Only fetch what's in THREAD but not in raw/ set, _ := imap.NewSeqSet("1:*") cmd, _ = c.Fetch(set, "UID", "BODY[]") // Process responses while the command is running for cmd.InProgress() { // Wait for the next response (no timeout) c.Recv(-1) // Process message response into temporary data structure for _, rsp = range cmd.Data { m := rsp.MessageInfo() entiremsg := imap.AsBytes(m.Attrs["BODY[]"]) if msg, _ := mail.ReadMessage(bytes.NewReader(entiremsg)); msg != nil { id := int(m.UID) s := fmt.Sprintf("raw/%d.txt", id) // Writing out message ids to raw // fmt.Printf("WROTE: %d\n", id) err := ioutil.WriteFile(s, entiremsg, 0644) if err != nil { panic(err) } } } cmd.Data = nil } // Refer to Array based structure in JSON-design.mdwn var archive []Conversation for _, j := range flat { var c Conversation for i, k := range j { if i == 0 { // First message gets hashed s := fmt.Sprintf("raw/%d.txt", k) entiremsg, err := ioutil.ReadFile(s) if err != nil { panic(err) // continue ? } h := sha1.New() h.Write(entiremsg) c.Id = fmt.Sprintf("%x", h.Sum(nil)) m, err := getMsg(k) // fmt.Println(m.Header) if err != nil { m = Msg{Header: nil, Body: "Missing " + string(k)} } c.Msgs = append(c.Msgs, m) } else { // Subsequent messages in the conversation m, err := getMsg(k) if err != nil { m = Msg{Header: nil, Body: "Missing " + string(k)} } c.Msgs = append(c.Msgs, m) } } c.Count = len(c.Msgs) json, _ := json.MarshalIndent(c, "", " ") s := fmt.Sprintf("c/%s.json", c.Id) err = ioutil.WriteFile(s, json, 0644) if err != nil { panic(err) } else { fmt.Printf("Wrote %s.json\n", c.Id) } // For mail.json, we only need the first message for the index prunebody := c.Msgs[0] prunebody.Body = "" archive = append(archive, Conversation{c.Id, c.Count, []Msg{prunebody}}) } // Marshall to mail.json json, _ := json.MarshalIndent(archive, "", " ") err = ioutil.WriteFile("mail.json", json, 0644) if err != nil { panic(err) } else { fmt.Println("Built mail.json!\t\t\tNoticed a bug? https://github.com/kaihendry/imap2json/issues\n") } index := "index.html" if _, err := os.Stat(index); os.IsNotExist(err) { fmt.Printf("No %s found, therefore creating %s\n", index, index) ioutil.WriteFile(index, []byte(strings.Replace(html, "VERSION", VERSION, 1)), 0644) } }