How to use MergeEmailLists method of email Package

Best Syzkaller code snippet using email.MergeEmailLists

parser.go

Source:parser.go Github

copy

Full Screen

...70 } else {71 ccList = append(ccList, cleaned)72 }73 }74 ccList = MergeEmailLists(ccList)75 body, attachments, err := parseBody(msg.Body, msg.Header)76 if err != nil {77 return nil, err78 }79 bodyStr := string(body)80 patch, cmd, cmdArgs := "", "", ""81 if !fromMe {82 for _, a := range attachments {83 _, patch, _ = ParsePatch(string(a))84 if patch != "" {85 break86 }87 }88 if patch == "" {89 _, patch, _ = ParsePatch(bodyStr)90 }91 cmd, cmdArgs = extractCommand(body)92 }93 link := ""94 if match := groupsLinkRe.FindStringSubmatchIndex(bodyStr); match != nil {95 link = bodyStr[match[2]:match[3]]96 }97 email := &Email{98 BugID: bugID,99 MessageID: msg.Header.Get("Message-ID"),100 Link: link,101 Subject: msg.Header.Get("Subject"),102 From: from[0].String(),103 Cc: ccList,104 Body: string(body),105 Patch: patch,106 Command: cmd,107 CommandArgs: cmdArgs,108 }109 return email, nil110}111// AddAddrContext embeds context into local part of the provided email address using '+'.112// Returns the resulting email address.113func AddAddrContext(email, context string) (string, error) {114 addr, err := mail.ParseAddress(email)115 if err != nil {116 return "", fmt.Errorf("failed to parse %q as email: %v", email, err)117 }118 at := strings.IndexByte(addr.Address, '@')119 if at == -1 {120 return "", fmt.Errorf("failed to parse %q as email: no @", email)121 }122 addr.Address = addr.Address[:at] + "+" + context + addr.Address[at:]123 return addr.String(), nil124}125// RemoveAddrContext extracts context after '+' from the local part of the provided email address.126// Returns address without the context and the context.127func RemoveAddrContext(email string) (string, string, error) {128 addr, err := mail.ParseAddress(email)129 if err != nil {130 return "", "", fmt.Errorf("failed to parse %q as email: %v", email, err)131 }132 at := strings.IndexByte(addr.Address, '@')133 if at == -1 {134 return "", "", fmt.Errorf("failed to parse %q as email: no @", email)135 }136 plus := strings.LastIndexByte(addr.Address[:at], '+')137 if plus == -1 {138 return email, "", nil139 }140 context := addr.Address[plus+1 : at]141 addr.Address = addr.Address[:plus] + addr.Address[at:]142 return addr.String(), context, nil143}144// extractCommand extracts command to syzbot from email body.145// Commands are of the following form:146// ^#syz cmd args...147func extractCommand(body []byte) (cmd, args string) {148 cmdPos := bytes.Index(append([]byte{'\n'}, body...), []byte("\n"+commandPrefix))149 if cmdPos == -1 {150 return151 }152 cmdPos += len(commandPrefix)153 cmdEnd := bytes.IndexByte(body[cmdPos:], '\n')154 if cmdEnd == -1 {155 cmdEnd = len(body) - cmdPos156 }157 cmdLine := strings.TrimSpace(string(body[cmdPos : cmdPos+cmdEnd]))158 if cmdLine == "" {159 return160 }161 split := strings.Split(cmdLine, " ")162 cmd = split[0]163 if len(split) > 1 {164 args = strings.TrimSpace(strings.Join(split[1:], " "))165 }166 return167}168func parseBody(r io.Reader, headers mail.Header) (body []byte, attachments [][]byte, err error) {169 mediaType, params, err := mime.ParseMediaType(headers.Get("Content-Type"))170 if err != nil {171 return nil, nil, fmt.Errorf("failed to parse email header 'Content-Type': %v", err)172 }173 disp, _, _ := mime.ParseMediaType(headers.Get("Content-Disposition"))174 if disp == "attachment" {175 // Note: mime package handles quoted-printable internally.176 if strings.ToLower(headers.Get("Content-Transfer-Encoding")) == "base64" {177 r = base64.NewDecoder(base64.StdEncoding, r)178 }179 attachment, err := ioutil.ReadAll(r)180 if err != nil {181 return nil, nil, fmt.Errorf("failed to read email body: %v", err)182 }183 return nil, [][]byte{attachment}, nil184 }185 if mediaType == "text/plain" {186 body, err := ioutil.ReadAll(r)187 if err != nil {188 return nil, nil, fmt.Errorf("failed to read email body: %v", err)189 }190 return body, nil, nil191 }192 if !strings.HasPrefix(mediaType, "multipart/") {193 return nil, nil, nil194 }195 mr := multipart.NewReader(r, params["boundary"])196 for {197 p, err := mr.NextPart()198 if err == io.EOF {199 return body, attachments, nil200 }201 if err != nil {202 return nil, nil, fmt.Errorf("failed to parse MIME parts: %v", err)203 }204 body1, attachments1, err1 := parseBody(p, mail.Header(p.Header))205 if err1 != nil {206 return nil, nil, err1207 }208 if body == nil {209 body = body1210 }211 attachments = append(attachments, attachments1...)212 }213}214// MergeEmailLists merges several email lists removing duplicates and invalid entries.215func MergeEmailLists(lists ...[]string) []string {216 const (217 maxEmailLen = 1000218 maxEmails = 50219 )220 merged := make(map[string]bool)221 for _, list := range lists {222 for _, email := range list {223 addr, err := mail.ParseAddress(email)224 if err != nil || len(addr.Address) > maxEmailLen {225 continue226 }227 merged[addr.Address] = true228 }229 }...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful