package cmd import ( "fmt" "os" "github.com/frenocorp/pop/internal/webhook" "github.com/spf13/cobra" ) func webhookCmd() *cobra.Command { cmd := &cobra.Command{ Use: "webhook", Short: "Manage webhooks for ProtonMail", Long: `Add, list, verify, and remove webhooks for real-time ProtonMail notifications.`, } cmd.AddCommand(webhookListCmd()) cmd.AddCommand(webhookAddCmd()) cmd.AddCommand(webhookVerifyCmd()) cmd.AddCommand(webhookRemoveCmd()) return cmd } func webhookListCmd() *cobra.Command { return &cobra.Command{ Use: "list", Short: "List all configured webhooks", RunE: func(cmd *cobra.Command, args []string) error { store, err := webhook.NewWebhookStore() if err != nil { return fmt.Errorf("failed to create webhook store: %w", err) } webhooks, err := store.ListWebhooks() if err != nil { return fmt.Errorf("failed to list webhooks: %w", err) } if len(webhooks) == 0 { fmt.Println("No webhooks configured.") return nil } for _, wh := range webhooks { fmt.Printf("Name: %s\n URL: %s\n ID: %s\n Events: %v\n Active: %t\n Created: %s\n\n", wh.Name, wh.URL, wh.ID, wh.Events, wh.Active, wh.CreatedAt) } return nil }, } } func webhookAddCmd() *cobra.Command { var name, url string var events []string cmd := &cobra.Command{ Use: "add ", Short: "Add a webhook endpoint", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { name = args[0] if url == "" { return fmt.Errorf("webhook URL is required (--url)") } if len(events) == 0 { return fmt.Errorf("at least one event type is required (--events)") } eventTypes := make([]webhook.EventType, len(events)) for i, e := range events { eventTypes[i] = webhook.EventType(e) } store, err := webhook.NewWebhookStore() if err != nil { return fmt.Errorf("failed to create webhook store: %w", err) } wh, err := store.AddWebhook(name, url, eventTypes, "") if err != nil { return fmt.Errorf("failed to add webhook: %w", err) } fmt.Printf("Webhook added:\n Name: %s\n URL: %s\n ID: %s\n Events: %v\n", wh.Name, wh.URL, wh.ID, wh.Events) return nil }, } cmd.Flags().StringVar(&url, "url", "", "Webhook URL") cmd.Flags().StringArrayVar(&events, "events", []string{"mail.received"}, "Event types (comma-separated, e.g. mail.received,mail.sent)") cmd.MarkFlagRequired("url") cmd.MarkFlagRequired("events") return cmd } func webhookVerifyCmd() *cobra.Command { return &cobra.Command{ Use: "verify", Short: "Verify webhook signatures", RunE: func(cmd *cobra.Command, args []string) error { store, err := webhook.NewWebhookStore() if err != nil { return fmt.Errorf("failed to create webhook store: %w", err) } webhooks, err := store.ListWebhooks() if err != nil { return fmt.Errorf("failed to list webhooks: %w", err) } if len(webhooks) == 0 { fmt.Println("No webhooks to verify.") return nil } testPayload := []byte(`{"test": true}`) for _, wh := range webhooks { valid := webhook.VerifySignature(wh.Secret, testPayload, webhook.ComputeSignature(wh.Secret, testPayload)) if valid { fmt.Printf("Webhook %s (%s): signature OK\n", wh.ID, wh.URL) } else { fmt.Printf("Webhook %s (%s): signature FAILED\n", wh.ID, wh.URL) } } return nil }, } } func webhookRemoveCmd() *cobra.Command { return &cobra.Command{ Use: "remove ", Short: "Remove a webhook", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { id := args[0] store, err := webhook.NewWebhookStore() if err != nil { return fmt.Errorf("failed to create webhook store: %w", err) } if err := store.RemoveWebhook(id); err != nil { return fmt.Errorf("failed to remove webhook: %w", err) } fmt.Printf("Removed webhook: %s\n", id) return nil }, } } func init() { _ = os.Getenv }