]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 596611: Add a hook to email_in.pl
authorFrédéric Buclin <LpSolit@gmail.com>
Thu, 4 Nov 2010 17:14:25 +0000 (18:14 +0100)
committerFrédéric Buclin <LpSolit@gmail.com>
Thu, 4 Nov 2010 17:14:25 +0000 (18:14 +0100)
r/a=mkanat

Bugzilla/Hook.pm
email_in.pl
extensions/Example/Extension.pm

index 4ec6b3e4a2c01e19105084d343ca48924f03ffc0..7a3c3f25d238628d449894e7f36e964a9be0fa67 100644 (file)
@@ -469,6 +469,34 @@ L</config_add_panels> if you want to add new panels.
 
 =back
 
+=head2 email_in_before_parse
+
+This happens right after an inbound email is converted into an Email::MIME
+object, but before we start parsing the email to extract field data. This
+means the email has already been decoded for you. It gives you a chance
+to interact with the email itself before L<email_in> starts parsing its content.
+
+=over
+
+=item C<mail> - An Email::MIME object. The decoded incoming email.
+
+=item C<fields> - A hashref. The hash which will contain extracted data.
+
+=back
+
+=head2 email_in_after_parse
+
+This happens after all the data has been extracted from the email, but before
+the reporter is validated, during L<email_in>. This lets you do things after
+the normal parsing of the email, such as sanitizing field data, changing the
+user account being used to file a bug, etc.
+
+=over
+
+=item C<fields> - A hashref. The hash containing the extracted field data.
+
+=back
+
 =head2 enter_bug_entrydefaultvars
 
 B<DEPRECATED> - Use L</template_before_process> instead.
index fbff450876e26921c16cb88d341fbe599e610691..e95589a9bb5b50be5f8972f8ef891b5e1c9bb944 100755 (executable)
@@ -54,6 +54,7 @@ use Bugzilla::Mailer;
 use Bugzilla::Token;
 use Bugzilla::User;
 use Bugzilla::Util;
+use Bugzilla::Hook;
 
 #############
 # Constants #
@@ -76,6 +77,8 @@ sub parse_mail {
     $input_email = Email::MIME->new($mail_text);
     
     my %fields;
+    Bugzilla::Hook::process('email_in_before_parse', { mail => $input_email,
+                                                       fields => \%fields });
 
     my $summary = $input_email->header('Subject');
     if ($summary =~ /\[\S+ (\d+)\](.*)/i) {
@@ -415,6 +418,9 @@ Bugzilla->usage_mode(USAGE_MODE_EMAIL);
 my @mail_lines = <STDIN>;
 my $mail_text = join("", @mail_lines);
 my $mail_fields = parse_mail($mail_text);
+
+Bugzilla::Hook::process('email_in_after_parse', { fields => $mail_fields });
+
 my $attachments = delete $mail_fields->{'attachments'};
 
 my $username = $mail_fields->{'reporter'};
index 60096692e730bd9385d0ce7146e21905b0e92fea..af123a08802734284e80638510f2c9ff503003e6 100644 (file)
@@ -238,6 +238,48 @@ sub config_modify_panels {
     push(@{ $verify_class->{choices} }, 'Example');
 }
 
+sub email_in_before_parse {
+    my ($self, $args) = @_;
+
+    my $subject = $args->{mail}->header('Subject');
+    # Correctly extract the bug ID from email subjects of the form [Bug comp/NNN].
+    if ($subject =~ /\[.*(\d+)\].*/) {
+        $args->{fields}->{bug_id} = $1;
+    }
+}
+
+sub email_in_after_parse {
+    my ($self, $args) = @_;
+    my $reporter = $args->{fields}->{reporter};
+    my $dbh = Bugzilla->dbh;
+
+    # No other check needed if this is a valid regular user.
+    return if login_to_id($reporter);
+
+    # The reporter is not a regular user. We create an account for him,
+    # but he can only comment on existing bugs.
+    # This is useful for people who reply by email to bugmails received
+    # in mailing-lists.
+    if ($args->{fields}->{bug_id}) {
+        # WARNING: we return now to skip the remaining code below.
+        # You must understand that removing this line would make the code
+        # below effective! Do it only if you are OK with the behavior
+        # described here.
+        return;
+
+        Bugzilla::User->create({ login_name => $reporter, cryptpassword => '*' });
+
+        # For security reasons, delete all fields unrelated to comments.
+        foreach my $field (keys %{$args->{fields}}) {
+            next if $field =~ /^(?:bug_id|comment|reporter)$/;
+            delete $args->{fields}->{$field};
+        }
+    }
+    else {
+        ThrowUserError('invalid_username', { name => $reporter });
+    }
+}
+
 sub flag_end_of_update {
     my ($self, $args) = @_;