How to use AddAddrContext method of email Package

Best Syzkaller code snippet using email.AddAddrContext

reporting_email.go

Source:reporting_email.go Github

copy

Full Screen

...165 to := email.MergeEmailLists([]string{cfg.Email}, notif.CC)166 if cfg.MailMaintainers && notif.Public {167 to = email.MergeEmailLists(to, notif.Maintainers, cfg.DefaultMaintainers)168 }169 from, err := email.AddAddrContext(fromAddr(c), notif.ID)170 if err != nil {171 return err172 }173 log.Infof(c, "sending notif %v for %q to %q: %v", notif.Type, notif.Title, to, body)174 if err := sendMailText(c, notif.Title, from, to, notif.ExtID, nil, body); err != nil {175 return err176 }177 cmd := &dashapi.BugUpdate{178 ID: notif.ID,179 Status: status,180 Notification: true,181 }182 ok, reason, err := incomingCommand(c, cmd)183 if !ok || err != nil {184 return fmt.Errorf("notif update failed: ok=%v reason=%v err=%v", ok, reason, err)185 }186 return nil187}188func emailPollJobs(c context.Context) error {189 jobs, err := pollCompletedJobs(c, emailType)190 if err != nil {191 return err192 }193 for _, job := range jobs {194 if err := emailReport(c, job); err != nil {195 log.Errorf(c, "failed to report job: %v", err)196 continue197 }198 if err := jobReported(c, job.JobID); err != nil {199 log.Errorf(c, "failed to mark job reported: %v", err)200 continue201 }202 }203 return nil204}205func emailReport(c context.Context, rep *dashapi.BugReport) error {206 templ, public := "", false207 switch rep.Type {208 case dashapi.ReportNew, dashapi.ReportRepro:209 templ = "mail_bug.txt"210 public = true211 case dashapi.ReportTestPatch:212 templ = "mail_test_result.txt"213 case dashapi.ReportBisectCause, dashapi.ReportBisectFix:214 templ = "mail_bisect_result.txt"215 public = true216 default:217 return fmt.Errorf("unknown report type %v", rep.Type)218 }219 cfg := new(EmailConfig)220 if err := json.Unmarshal(rep.Config, cfg); err != nil {221 return fmt.Errorf("failed to unmarshal email config: %v", err)222 }223 to := email.MergeEmailLists([]string{cfg.Email}, rep.CC)224 if cfg.MailMaintainers && public {225 to = email.MergeEmailLists(to, rep.Maintainers, cfg.DefaultMaintainers)226 }227 from, err := email.AddAddrContext(fromAddr(c), rep.ID)228 if err != nil {229 return err230 }231 log.Infof(c, "sending email %q to %q", rep.Title, to)232 return sendMailTemplate(c, rep.Title, from, to, rep.ExtID, nil, templ, rep)233}234// handleIncomingMail is the entry point for incoming emails.235func handleIncomingMail(w http.ResponseWriter, r *http.Request) {236 c := appengine.NewContext(r)237 if err := incomingMail(c, r); err != nil {238 log.Errorf(c, "handleIncomingMail: %v", err)239 }240}241func incomingMail(c context.Context, r *http.Request) error {242 msg, err := email.Parse(r.Body, ownEmails(c))243 if err != nil {244 // Malformed emails constantly appear from spammers.245 // But we have not seen errors parsing legit emails.246 // These errors are annoying. Warn and ignore them.247 log.Warningf(c, "failed to parse email: %v", err)248 return nil249 }250 // Ignore any incoming emails from syzbot itself.251 if ownEmail(c) == msg.From {252 return nil253 }254 log.Infof(c, "received email: subject %q, from %q, cc %q, msg %q, bug %q, cmd %q, link %q",255 msg.Subject, msg.From, msg.Cc, msg.MessageID, msg.BugID, msg.Command, msg.Link)256 if msg.Command == email.CmdFix && msg.CommandArgs == "exact-commit-title" {257 // Sometimes it happens that somebody sends us our own text back, ignore it.258 msg.Command, msg.CommandArgs = email.CmdNone, ""259 }260 bug, _, reporting := loadBugInfo(c, msg)261 if bug == nil {262 return nil // error was already logged263 }264 emailConfig := reporting.Config.(*EmailConfig)265 // A mailing list can send us a duplicate email, to not process/reply266 // to such duplicate emails, we ignore emails coming from our mailing lists.267 mailingList := email.CanonicalEmail(emailConfig.Email)268 fromMailingList := email.CanonicalEmail(msg.From) == mailingList269 mailingListInCC := checkMailingListInCC(c, msg, mailingList)270 log.Infof(c, "from/cc mailing list: %v/%v", fromMailingList, mailingListInCC)271 if msg.Command == email.CmdTest {272 return handleTestCommand(c, msg)273 }274 if fromMailingList && msg.Command != email.CmdNone {275 log.Infof(c, "duplicate email from mailing list, ignoring")276 return nil277 }278 cmd := &dashapi.BugUpdate{279 Status: emailCmdToStatus[msg.Command],280 ID: msg.BugID,281 ExtID: msg.MessageID,282 Link: msg.Link,283 CC: msg.Cc,284 }285 switch msg.Command {286 case email.CmdNone, email.CmdUpstream, email.CmdInvalid, email.CmdUnDup:287 case email.CmdFix:288 if msg.CommandArgs == "" {289 return replyTo(c, msg, "no commit title", nil)290 }291 cmd.FixCommits = []string{msg.CommandArgs}292 case email.CmdDup:293 if msg.CommandArgs == "" {294 return replyTo(c, msg, "no dup title", nil)295 }296 cmd.DupOf = msg.CommandArgs297 case email.CmdUnCC:298 cmd.CC = []string{email.CanonicalEmail(msg.From)}299 default:300 if msg.Command != email.CmdUnknown {301 log.Errorf(c, "unknown email command %v %q", msg.Command, msg.CommandStr)302 }303 return replyTo(c, msg, fmt.Sprintf("unknown command %q", msg.CommandStr), nil)304 }305 ok, reply, err := incomingCommand(c, cmd)306 if err != nil {307 return nil // the error was already logged308 }309 if !ok && reply != "" {310 return replyTo(c, msg, reply, nil)311 }312 if !mailingListInCC && msg.Command != email.CmdNone && msg.Command != email.CmdUnCC {313 warnMailingListInCC(c, msg, mailingList)314 }315 return nil316}317var emailCmdToStatus = map[email.Command]dashapi.BugStatus{318 email.CmdNone: dashapi.BugStatusUpdate,319 email.CmdUpstream: dashapi.BugStatusUpstream,320 email.CmdInvalid: dashapi.BugStatusInvalid,321 email.CmdUnDup: dashapi.BugStatusOpen,322 email.CmdFix: dashapi.BugStatusOpen,323 email.CmdDup: dashapi.BugStatusDup,324 email.CmdUnCC: dashapi.BugStatusUnCC,325}326func handleTestCommand(c context.Context, msg *email.Email) error {327 args := strings.Split(msg.CommandArgs, " ")328 if len(args) != 2 {329 return replyTo(c, msg, fmt.Sprintf("want 2 args (repo, branch), got %v", len(args)), nil)330 }331 reply := handleTestRequest(c, msg.BugID, email.CanonicalEmail(msg.From),332 msg.MessageID, msg.Link, msg.Patch, args[0], args[1], msg.Cc)333 if reply != "" {334 return replyTo(c, msg, reply, nil)335 }336 return nil337}338func handleEmailBounce(w http.ResponseWriter, r *http.Request) {339 c := appengine.NewContext(r)340 body, err := ioutil.ReadAll(r.Body)341 if err != nil {342 log.Errorf(c, "email bounced: failed to read body: %v", err)343 return344 }345 if nonCriticalBounceRe.Match(body) {346 log.Infof(c, "email bounced: address not found")347 } else {348 log.Errorf(c, "email bounced")349 }350 log.Infof(c, "%s", body)351}352// These are just stale emails in MAINTAINERS.353var nonCriticalBounceRe = regexp.MustCompile(`\*\* Address not found \*\*|550 #5\.1\.0 Address rejected`)354func loadBugInfo(c context.Context, msg *email.Email) (bug *Bug, bugReporting *BugReporting, reporting *Reporting) {355 if msg.BugID == "" {356 if msg.Command == email.CmdNone {357 // This happens when people CC syzbot on unrelated emails.358 log.Infof(c, "no bug ID (%q)", msg.Subject)359 } else {360 log.Errorf(c, "no bug ID (%q)", msg.Subject)361 from, err := email.AddAddrContext(ownEmail(c), "HASH")362 if err != nil {363 log.Errorf(c, "failed to format sender email address: %v", err)364 from = "ERROR"365 }366 if err := replyTo(c, msg, fmt.Sprintf(replyNoBugID, from), nil); err != nil {367 log.Errorf(c, "failed to send reply: %v", err)368 }369 }370 return nil, nil, nil371 }372 bug, _, err := findBugByReportingID(c, msg.BugID)373 if err != nil {374 log.Errorf(c, "can't find bug: %v", err)375 from, err := email.AddAddrContext(ownEmail(c), "HASH")376 if err != nil {377 log.Errorf(c, "failed to format sender email address: %v", err)378 from = "ERROR"379 }380 if err := replyTo(c, msg, fmt.Sprintf(replyBadBugID, from), nil); err != nil {381 log.Errorf(c, "failed to send reply: %v", err)382 }383 return nil, nil, nil384 }385 bugReporting, _ = bugReportingByID(bug, msg.BugID)386 if bugReporting == nil {387 log.Errorf(c, "can't find bug reporting: %v", err)388 if err := replyTo(c, msg, "Can't find the corresponding bug.", nil); err != nil {389 log.Errorf(c, "failed to send reply: %v", err)390 }391 return nil, nil, nil392 }393 reporting = config.Namespaces[bug.Namespace].ReportingByName(bugReporting.Name)394 if reporting == nil {395 log.Errorf(c, "can't find reporting for this bug: namespace=%q reporting=%q",396 bug.Namespace, bugReporting.Name)397 return nil, nil, nil398 }399 if reporting.Config.Type() != emailType {400 log.Errorf(c, "reporting is not email: namespace=%q reporting=%q config=%q",401 bug.Namespace, bugReporting.Name, reporting.Config.Type())402 return nil, nil, nil403 }404 return bug, bugReporting, reporting405}406func checkMailingListInCC(c context.Context, msg *email.Email, mailingList string) bool {407 if email.CanonicalEmail(msg.From) == mailingList {408 return true409 }410 for _, cc := range msg.Cc {411 if email.CanonicalEmail(cc) == mailingList {412 return true413 }414 }415 msg.Cc = append(msg.Cc, mailingList)416 return false417}418func warnMailingListInCC(c context.Context, msg *email.Email, mailingList string) {419 reply := fmt.Sprintf("Your '%v' command is accepted, but please keep %v mailing list"+420 " in CC next time. It serves as a history of what happened with each bug report."+421 " Thank you.",422 msg.CommandStr, mailingList)423 if err := replyTo(c, msg, reply, nil); err != nil {424 log.Errorf(c, "failed to send email reply: %v", err)425 }426}427func sendMailTemplate(c context.Context, subject, from string, to []string, replyTo string,428 attachments []aemail.Attachment, template string, data interface{}) error {429 body := new(bytes.Buffer)430 if err := mailTemplates.ExecuteTemplate(body, template, data); err != nil {431 return fmt.Errorf("failed to execute %v template: %v", template, err)432 }433 return sendMailText(c, subject, from, to, replyTo, attachments, body.String())434}435func sendMailText(c context.Context, subject, from string, to []string, replyTo string,436 attachments []aemail.Attachment, body string) error {437 msg := &aemail.Message{438 Sender: from,439 To: to,440 Subject: subject,441 Body: body,442 Attachments: attachments,443 }444 if replyTo != "" {445 msg.Headers = mail.Header{"In-Reply-To": []string{replyTo}}446 msg.Subject = replySubjectPrefix + msg.Subject447 }448 return sendEmail(c, msg)449}450func replyTo(c context.Context, msg *email.Email, reply string, attachment *aemail.Attachment) error {451 var attachments []aemail.Attachment452 if attachment != nil {453 attachments = append(attachments, *attachment)454 }455 from, err := email.AddAddrContext(fromAddr(c), msg.BugID)456 if err != nil {457 return err458 }459 log.Infof(c, "sending reply: to=%q cc=%q subject=%q reply=%q",460 msg.From, msg.Cc, msg.Subject, reply)461 replyMsg := &aemail.Message{462 Sender: from,463 To: []string{msg.From},464 Cc: msg.Cc,465 Subject: replySubjectPrefix + msg.Subject,466 Body: email.FormReply(msg.Body, reply),467 Attachments: attachments,468 Headers: mail.Header{"In-Reply-To": []string{msg.MessageID}},469 }...

Full Screen

Full Screen

AddAddrContext

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "net/smtp"3import "net/mail"4import "net/textproto"5import "bytes"6func main() {7 auth := smtp.PlainAuth(

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