Conversation
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
…chments Adds `cmd/gmail-reader/main.go` that: * Authenticates via OAuth 2.0 Desktop flow to the Gmail API. * Polls for specific emails matching a configurable query using `time.Ticker`. * Extracts email metadata and attachments. * Forwards attachments as multiple files in a multipart/form-data POST request under the `payload` key to a configurable local Webhook URL. * Marks successfully forwarded emails as read to prevent duplicate processing. * Retrieves configurations for polling interval, webhook URL, and search query via standard environment variables. Co-authored-by: Gerifield <195914+Gerifield@users.noreply.github.com>
dbaf74c to
abe3776
Compare
There was a problem hiding this comment.
Pull request overview
Adds a new standalone Go executable to poll Gmail for messages matching a query, extract body/attachments, and forward them as multipart/form-data to a configurable local HTTP endpoint.
Changes:
- Added
cmd/gmail-reader/main.goimplementing OAuth flow, polling, message extraction, and multipart forwarding. - Updated
go.mod/go.sumto include Gmail API + OAuth2 dependencies (and related indirect upgrades).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| go.mod | Adds OAuth2 + Gmail API dependencies and updates indirect deps to support the new executable. |
| go.sum | Updates checksums for newly added/updated dependencies. |
| cmd/gmail-reader/main.go | Implements the Gmail polling + forwarding script, including token caching and env-based config. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| h["Content-Disposition"] = []string{fmt.Sprintf(`form-data; name="payload"; filename="%s"`, escapeQuotes(att.Filename))} | ||
| if att.MimeType != "" { |
There was a problem hiding this comment.
Attachment filenames come from email content and can contain CR/LF or other special characters. Building Content-Disposition manually with fmt.Sprintf(...) risks header injection / malformed multipart bodies. Prefer multipart.FileContentDisposition(...) (already used in pkg/httpBotter/logic.go) or explicitly reject/strip \r and \n in filenames.
| h := make(map[string][]string) | ||
| h["Content-Disposition"] = []string{fmt.Sprintf(`form-data; name="payload"; filename="%s"`, escapeQuotes(att.Filename))} | ||
| if att.MimeType != "" { | ||
| h["Content-Type"] = []string{att.MimeType} | ||
| } else { | ||
| h["Content-Type"] = []string{"application/octet-stream"} | ||
| } | ||
|
|
||
| part, err := writer.CreatePart(h) | ||
| if err != nil { |
There was a problem hiding this comment.
multipart.Writer.CreatePart expects a textproto.MIMEHeader, but this code builds a plain map[string][]string. This won’t compile (cannot use h (type map[string][]string) as textproto.MIMEHeader). Use make(textproto.MIMEHeader) (and import net/textproto), or follow the existing pattern used in pkg/httpBotter/logic.go.
| decoded, err := base64.URLEncoding.DecodeString(attachObj.Data) | ||
| if err != nil { | ||
| log.Printf("Unable to decode attachment %s: %v", part.Filename, err) | ||
| return | ||
| } | ||
|
|
||
| data.Attachments = append(data.Attachments, attachment{ | ||
| Filename: part.Filename, | ||
| Data: decoded, | ||
| MimeType: part.MimeType, | ||
| }) | ||
| } else if part.MimeType == "text/plain" && part.Body != nil && part.Body.Data != "" { | ||
| // It's the body | ||
| decoded, err := base64.URLEncoding.DecodeString(part.Body.Data) | ||
| if err == nil { | ||
| data.Body += string(decoded) | ||
| } | ||
| } else if part.MimeType == "text/html" && data.Body == "" && part.Body != nil && part.Body.Data != "" { | ||
| // Fallback to HTML body if plain text is not found yet | ||
| decoded, err := base64.URLEncoding.DecodeString(part.Body.Data) | ||
| if err == nil { |
There was a problem hiding this comment.
Gmail message/attachment bodies are base64url-encoded and commonly omit padding. Using base64.URLEncoding.DecodeString will fail on unpadded data. Prefer base64.RawURLEncoding.DecodeString(...) (or URLEncoding.WithPadding(base64.NoPadding)) for attachObj.Data and part.Body.Data.
| if data.Body != "" { | ||
| _ = writer.WriteField("Body", data.Body) | ||
| } | ||
|
|
There was a problem hiding this comment.
The repo’s /message endpoint reads the user message from form field message (r.PostFormValue("message")). This client sends Sender/Subject/Date/Body fields but never sends message, so the server will see an empty prompt. Consider populating a message field (e.g., subject + body + metadata) and keeping payload for attachments.
| // Populate unified message field expected by the /message endpoint. | |
| // This combines metadata and body so that r.PostFormValue("message") | |
| // on the server side receives a meaningful prompt. | |
| var messageContentBuilder strings.Builder | |
| if data.Sender != "" { | |
| messageContentBuilder.WriteString("From: ") | |
| messageContentBuilder.WriteString(data.Sender) | |
| messageContentBuilder.WriteString("\n") | |
| } | |
| if data.Subject != "" { | |
| messageContentBuilder.WriteString("Subject: ") | |
| messageContentBuilder.WriteString(data.Subject) | |
| messageContentBuilder.WriteString("\n") | |
| } | |
| if data.Date != "" { | |
| messageContentBuilder.WriteString("Date: ") | |
| messageContentBuilder.WriteString(data.Date) | |
| messageContentBuilder.WriteString("\n") | |
| } | |
| if data.Body != "" { | |
| messageContentBuilder.WriteString("\n") | |
| messageContentBuilder.WriteString(data.Body) | |
| } | |
| messageContent := messageContentBuilder.String() | |
| if messageContent != "" { | |
| _ = writer.WriteField("message", messageContent) | |
| } |
| _ = writer.WriteField("Sender", data.Sender) | ||
| _ = writer.WriteField("Subject", data.Subject) | ||
| _ = writer.WriteField("Date", data.Date) | ||
|
|
||
| // Add body if no attachments or to provide context | ||
| if data.Body != "" { | ||
| _ = writer.WriteField("Body", data.Body) |
There was a problem hiding this comment.
writer.WriteField(...) return values are ignored. If a write fails, the script will still send the request (possibly missing required fields) with no surfaced error. Capture these errors and return early so the message remains unread and can be retried with a clear log.
| _ = writer.WriteField("Sender", data.Sender) | |
| _ = writer.WriteField("Subject", data.Subject) | |
| _ = writer.WriteField("Date", data.Date) | |
| // Add body if no attachments or to provide context | |
| if data.Body != "" { | |
| _ = writer.WriteField("Body", data.Body) | |
| if err := writer.WriteField("Sender", data.Sender); err != nil { | |
| return fmt.Errorf("failed to write Sender field: %w", err) | |
| } | |
| if err := writer.WriteField("Subject", data.Subject); err != nil { | |
| return fmt.Errorf("failed to write Subject field: %w", err) | |
| } | |
| if err := writer.WriteField("Date", data.Date); err != nil { | |
| return fmt.Errorf("failed to write Date field: %w", err) | |
| } | |
| // Add body if no attachments or to provide context | |
| if data.Body != "" { | |
| if err := writer.WriteField("Body", data.Body); err != nil { | |
| return fmt.Errorf("failed to write Body field: %w", err) | |
| } |
| log.Fatalf("Unable to cache oauth token: %v", err) | ||
| } | ||
| defer f.Close() | ||
| json.NewEncoder(f).Encode(token) |
There was a problem hiding this comment.
json.NewEncoder(f).Encode(token) error is ignored. If the token can’t be written (disk full, permission issues after open, etc.), the script may proceed but re-auth every run. Handle/return the encode error (and consider logging without printing sensitive paths).
| json.NewEncoder(f).Encode(token) | |
| if err := json.NewEncoder(f).Encode(token); err != nil { | |
| log.Fatalf("Unable to encode oauth token to file: %v", err) | |
| } |
|
|
||
| func loadConfig() Configuration { | ||
| cfg := Configuration{ | ||
| WebhookURL: "http://localhost:8080/webhook", |
There was a problem hiding this comment.
The default webhook URL (http://localhost:8080/webhook) doesn’t match the repo’s server route (POST /message in internal/server/server.go). With defaults, this script will 404 against the local server-bot. Consider defaulting to /message (or documenting that this targets a different endpoint).
| WebhookURL: "http://localhost:8080/webhook", | |
| WebhookURL: "http://localhost:8080/message", |
Implemented a standalone Go script that authenticates to the Gmail API, polls for unread emails with attachments (or matching a query), extracts them, and forwards them as multipart/form-data to a configurable local endpoint.
Key Changes:
cmd/gmail-reader/main.goimplementing the requested logic.POLLING_INTERVAL,WEBHOOK_URL, andSEARCH_QUERYenvironment variables with sensible defaults as requested.(Note: Tests failing in other packages were present before my changes, and my standalone file does not impact them.)
PR created automatically by Jules for task 110509471110472474 started by @Gerifield