~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

__metaclass__ = type
__all__ = [
    'BugEmailCommands',
    ]

import os

from lazr.lifecycle.event import (
    ObjectCreatedEvent,
    ObjectModifiedEvent,
    )
from lazr.lifecycle.interfaces import (
    IObjectCreatedEvent,
    IObjectModifiedEvent,
    )
from lazr.lifecycle.snapshot import Snapshot
from zope.component import getUtility
from zope.event import notify
from zope.interface import (
    implements,
    providedBy,
    )
from zope.schema.interfaces import (
    TooLong,
    ValidationError,
    )

from canonical.launchpad.webapp.authorization import check_permission
from canonical.launchpad.webapp.interfaces import ILaunchBag
from lp.app.errors import (
    NotFoundError,
    UserCannotUnsubscribePerson,
    )
from lp.app.validators.name import valid_name
from lp.bugs.interfaces.bug import (
    CreateBugParams,
    IBug,
    IBugAddForm,
    IBugSet,
    )
from lp.bugs.interfaces.bugtask import (
    BugTaskImportance,
    BugTaskStatus,
    IBugTask,
    IllegalTarget,
    )
from lp.bugs.interfaces.cve import ICveSet
from lp.registry.interfaces.distribution import IDistribution
from lp.registry.interfaces.distributionsourcepackage import (
    IDistributionSourcePackage,
    )
from lp.registry.interfaces.distroseries import IDistroSeries
from lp.registry.interfaces.pillar import IPillarNameSet
from lp.registry.interfaces.product import IProduct
from lp.registry.interfaces.productseries import IProductSeries
from lp.registry.interfaces.projectgroup import IProjectGroup
from lp.registry.interfaces.sourcepackage import ISourcePackage
from lp.registry.interfaces.sourcepackagename import ISourcePackageName
from lp.services.mail.commands import (
    EditEmailCommand,
    EmailCommand,
    EmailCommandCollection,
    )
from lp.services.mail.helpers import (
    get_error_message,
    get_person_or_team,
    )
from lp.services.mail.interfaces import (
    BugTargetNotFound,
    EmailProcessingError,
    IBugEditEmailCommand,
    IBugEmailCommand,
    IBugTaskEditEmailCommand,
    IBugTaskEmailCommand,
    )
from lp.services.messages.interfaces.message import IMessageSet


error_templates = os.path.join(os.path.dirname(__file__), 'errortemplates')


class BugEmailCommand(EmailCommand):
    """Creates new bug, or returns an existing one."""
    implements(IBugEmailCommand)

    _numberOfArguments = 1
    RANK = 0

    def execute(self, parsed_msg, filealias):
        """See IBugEmailCommand."""
        self._ensureNumberOfArguments()
        bugid = self.string_args[0]

        if bugid == 'new':
            message = getUtility(IMessageSet).fromEmail(
                parsed_msg.as_string(),
                owner=getUtility(ILaunchBag).user,
                filealias=filealias,
                parsed_message=parsed_msg)
            description = message.text_contents
            if description.strip() == '':
                # The report for a new bug must contain an affects command,
                # since the bug must have at least one task
                raise EmailProcessingError(
                    get_error_message(
                        'no-affects-target-on-submit.txt',
                        error_templates=error_templates),
                    stop_processing=True)

            # Check the message validator.
            validator = IBugAddForm['comment'].validate
            try:
                validator(description)
            except TooLong:
                raise EmailProcessingError(
                    'The description is too long. If you have lots of '
                    'text to add, use an attachment instead.',
                    stop_processing=True)
            except ValidationError as e:
                # More a just in case than any real expectation of getting
                # something.
                raise EmailProcessingError(
                    str(e),
                    stop_processing=True)

            params = CreateBugParams(
                msg=message, title=message.title,
                owner=getUtility(ILaunchBag).user)
            return params, None
        else:
            try:
                bugid = int(bugid)
            except ValueError:
                raise EmailProcessingError(
                    get_error_message(
                        'bug-argument-mismatch.txt',
                        error_templates=error_templates))

            try:
                bug = getUtility(IBugSet).get(bugid)
            except NotFoundError:
                bug = None
            if bug is None or not check_permission('launchpad.View', bug):
                raise EmailProcessingError(
                    get_error_message(
                        'no-such-bug.txt',
                        error_templates=error_templates,
                        bug_id=bugid))
            return bug, None


class PrivateEmailCommand(EmailCommand):
    """Marks a bug public or private.

    We do not subclass `EditEmailCommand` because we must call
    `IBug.setPrivate` to update privacy settings, rather than just
    updating an attribute.
    """

    implements(IBugEditEmailCommand)

    _numberOfArguments = 1
    RANK = 3

    def execute(self, context, current_event):
        """See `IEmailCommand`. Much of this method has been lifted from
        `EditEmailCommand.execute`.
        """
        # Parse args.
        self._ensureNumberOfArguments()
        private_arg = self.string_args[0]
        if private_arg == 'yes':
            private = True
        elif private_arg == 'no':
            private = False
        else:
            raise EmailProcessingError(
                get_error_message(
                    'private-parameter-mismatch.txt',
                    error_templates=error_templates),
                stop_processing=True)

        if isinstance(context, CreateBugParams):
            if context.security_related:
                # BugSet.createBug() requires new security bugs to be private.
                private = True
            context.private = private
            return context, current_event

        # Snapshot.
        edited_fields = set()
        if IObjectModifiedEvent.providedBy(current_event):
            context_snapshot = current_event.object_before_modification
            edited_fields.update(current_event.edited_fields)
        else:
            context_snapshot = Snapshot(
                context, providing=providedBy(context))

        # Apply requested changes.
        edited = context.setPrivate(private, getUtility(ILaunchBag).user)

        # Update the current event.
        if edited and not IObjectCreatedEvent.providedBy(current_event):
            edited_fields.add('private')
            current_event = ObjectModifiedEvent(
                context, context_snapshot, list(edited_fields))

        return context, current_event


class SecurityEmailCommand(EmailCommand):
    """Marks a bug as security related."""

    implements(IBugEditEmailCommand)

    _numberOfArguments = 1
    RANK = 2

    def execute(self, context, current_event):
        """See `IEmailCommand`.

        Much of this method was lifted from
        `EditEmailCommand.execute`.
        """
        # Parse args.
        self._ensureNumberOfArguments()
        [security_flag] = self.string_args
        if security_flag == 'yes':
            security_related = True
        elif security_flag == 'no':
            security_related = False
        else:
            raise EmailProcessingError(
                get_error_message(
                    'security-parameter-mismatch.txt',
                    error_templates=error_templates),
                stop_processing=True)

        if isinstance(context, CreateBugParams):
            context.security_related = security_related
            if security_related:
                # BugSet.createBug() requires new security bugs to be private.
                context.private = True
            return context, current_event

        # Take a snapshot.
        edited = False
        edited_fields = set()
        if IObjectModifiedEvent.providedBy(current_event):
            context_snapshot = current_event.object_before_modification
            edited_fields.update(current_event.edited_fields)
        else:
            context_snapshot = Snapshot(
                context, providing=providedBy(context))

        # Apply requested changes.
        user = getUtility(ILaunchBag).user
        if security_related:
            if context.setPrivate(True, user):
                edited = True
                edited_fields.add('private')
        if context.security_related != security_related:
            context.setSecurityRelated(security_related, user)
            edited = True
            edited_fields.add('security_related')

        # Update the current event.
        if edited and not IObjectCreatedEvent.providedBy(current_event):
            current_event = ObjectModifiedEvent(
                context, context_snapshot, list(edited_fields))

        return context, current_event


class SubscribeEmailCommand(EmailCommand):
    """Subscribes someone to the bug."""

    implements(IBugEditEmailCommand)
    RANK = 7

    def execute(self, bug, current_event):
        """See IEmailCommand."""
        string_args = list(self.string_args)
        # preserve compatibility with the original command that let you
        # specify a subscription type
        if len(string_args) == 2:
            # Remove the subscription_name
            string_args.pop()

        user = getUtility(ILaunchBag).user

        if len(string_args) == 1:
            person = get_person_or_team(string_args.pop())
        elif len(string_args) == 0:
            # Subscribe the sender of the email.
            person = user
        else:
            raise EmailProcessingError(
                get_error_message(
                    'subscribe-too-many-arguments.txt',
                    error_templates=error_templates))

        if isinstance(bug, CreateBugParams):
            if len(bug.subscribers) == 0:
                bug.subscribers = [person]
            else:
                bug.subscribers.append(person)
            return bug, current_event

        if bug.isSubscribed(person):
            # but we still need to find the subscription
            for bugsubscription in bug.subscriptions:
                if bugsubscription.person == person:
                    break

        else:
            bugsubscription = bug.subscribe(person, user)
            notify(ObjectCreatedEvent(bugsubscription))

        return bug, current_event


class UnsubscribeEmailCommand(EmailCommand):
    """Unsubscribes someone from the bug."""

    implements(IBugEditEmailCommand)
    RANK = 8

    def execute(self, bug, current_event):
        """See IEmailCommand."""
        if isinstance(bug, CreateBugParams):
            # Return the input because there is not yet a bug to
            # unsubscribe too.
            return bug, current_event

        string_args = list(self.string_args)
        if len(string_args) == 1:
            person = get_person_or_team(string_args.pop())
        elif len(string_args) == 0:
            # Subscribe the sender of the email.
            person = getUtility(ILaunchBag).user
        else:
            raise EmailProcessingError(
                get_error_message(
                    'unsubscribe-too-many-arguments.txt',
                    error_templates=error_templates))

        if bug.isSubscribed(person):
            try:
                bug.unsubscribe(person, getUtility(ILaunchBag).user)
            except UserCannotUnsubscribePerson:
                raise EmailProcessingError(
                    get_error_message(
                        'user-cannot-unsubscribe.txt',
                        error_templates=error_templates,
                        person=person.displayname))
        if bug.isSubscribedToDupes(person):
            bug.unsubscribeFromDupes(person, person)

        return bug, current_event


class SummaryEmailCommand(EditEmailCommand):
    """Changes the title of the bug."""

    implements(IBugEditEmailCommand)
    _numberOfArguments = 1
    RANK = 1

    def execute(self, bug, current_event):
        """See IEmailCommand."""
        if bug is None:
            raise EmailProcessingError(
                get_error_message(
                    'command-with-no-bug.txt',
                    error_templates=error_templates),
                stop_processing=True)

        # Do a manual control of the number of arguments, in order to
        # provide a better error message than the default one.
        if len(self.string_args) > 1:
            raise EmailProcessingError(
                get_error_message(
                    'summary-too-many-arguments.txt',
                    error_templates=error_templates))

        if isinstance(bug, CreateBugParams):
            bug.title = self.string_args[0]
            return bug, current_event

        return EditEmailCommand.execute(self, bug, current_event)

    def convertArguments(self, context):
        """See EmailCommand."""
        return {'title': self.string_args[0]}


class DuplicateEmailCommand(EmailCommand):
    """Marks a bug as a duplicate of another bug."""

    implements(IBugEditEmailCommand)
    _numberOfArguments = 1
    RANK = 6

    def execute(self, context, current_event):
        """See IEmailCommand."""
        if isinstance(context, CreateBugParams):
            # No one intentially reports a duplicate bug. Bug email commands
            # support CreateBugParams, so in this case, just return.
            return context, current_event
        self._ensureNumberOfArguments()
        [bug_id] = self.string_args

        if bug_id != 'no':
            try:
                bug = getUtility(IBugSet).getByNameOrID(bug_id)
            except NotFoundError:
                raise EmailProcessingError(
                    get_error_message(
                        'no-such-bug.txt',
                        error_templates=error_templates,
                        bug_id=bug_id))
        else:
            # 'no' is a special value for unmarking a bug as a duplicate.
            bug = None

        duplicate_field = IBug['duplicateof'].bind(context)
        try:
            duplicate_field.validate(bug)
        except ValidationError, error:
            raise EmailProcessingError(error.doc())

        context_snapshot = Snapshot(
            context, providing=providedBy(context))
        context.markAsDuplicate(bug)
        current_event = ObjectModifiedEvent(
            context, context_snapshot, 'duplicateof')
        notify(current_event)
        return bug, current_event


class CVEEmailCommand(EmailCommand):
    """Links a CVE to a bug."""

    implements(IBugEditEmailCommand)

    _numberOfArguments = 1
    RANK = 5

    def execute(self, bug, current_event):
        """See IEmailCommand."""
        [cve_sequence] = self.string_args
        cve = getUtility(ICveSet)[cve_sequence]
        if cve is None:
            raise EmailProcessingError(
                'Launchpad can\'t find the CVE "%s".' % cve_sequence)
        if isinstance(bug, CreateBugParams):
            bug.cve = cve
            return bug, current_event

        bug.linkCVE(cve, getUtility(ILaunchBag).user)
        return bug, current_event


class AffectsEmailCommand(EmailCommand):
    """Either creates a new task, or edits an existing task."""

    implements(IBugTaskEmailCommand)
    _numberOfArguments = 1
    RANK = 0

    @classmethod
    def _splitPath(cls, path):
        """Split the path part into two.

        The first part is the part before any slash, and the other is
        the part behind the slash.
        """
        if '/' not in path:
            return path, ''
        else:
            return tuple(path.split('/', 1))

    @classmethod
    def _normalizePath(cls, path):
        """Normalize the path.

        Previously the path had to start with either /distros/ or
        /products/. Simply remove any such prefixes to stay backward
        compatible.
        """
        for prefix in ['/distros/', '/products/', '/']:
            if path.startswith(prefix):
                path = path[len(prefix):]
                break
        return path

    @classmethod
    def getBugTarget(cls, path):
        """Return the IBugTarget with the given path.

        Path should be in any of the following forms:

            $product
            $product/$product_series
            $distribution
            $distribution/$source_package
            $distribution/$distro_series
            $distribution/$distro_series/$source_package
        """
        path = cls._normalizePath(path)
        name, rest = cls._splitPath(path)
        pillar = getUtility(IPillarNameSet).getByName(
            name, ignore_inactive=True)
        if pillar is None:
            raise BugTargetNotFound(
                "There is no project named '%s' registered in Launchpad." %
                    name)

        # We can't check for IBugTarget, since ProjectGroup is an IBugTarget
        # we don't allow bugs to be filed against.
        if IProjectGroup.providedBy(pillar):
            products = ", ".join(product.name for product in pillar.products)
            raise BugTargetNotFound(
                "%s is a group of projects. To report a bug, you need to"
                " specify which of these projects the bug applies to: %s" % (
                    pillar.name, products))
        assert IDistribution.providedBy(pillar) or IProduct.providedBy(pillar)

        if not rest:
            return pillar
        # Resolve the path that is after the pillar name.
        if IProduct.providedBy(pillar):
            series_name, rest = cls._splitPath(rest)
            product_series = pillar.getSeries(series_name)
            if product_series is None:
                raise BugTargetNotFound(
                    "%s doesn't have a series named '%s'." % (
                        pillar.displayname, series_name))
            elif not rest:
                return product_series
        else:
            assert IDistribution.providedBy(pillar)
            # The next step can be either a distro series or a source
            # package.
            series_name, rest = cls._splitPath(rest)
            try:
                series = pillar.getSeries(series_name)
            except NotFoundError:
                package_name = series_name
            else:
                if not rest:
                    return series
                else:
                    pillar = series
                    package_name, rest = cls._splitPath(rest)
            package = pillar.getSourcePackage(package_name)
            if package is None:
                raise BugTargetNotFound(
                    "%s doesn't have a series or source package named '%s'."
                    % (pillar.displayname, package_name))
            elif not rest:
                return package

        assert rest, "This is the fallback for unexpected path components."
        raise BugTargetNotFound("Unexpected path components: %s" % rest)

    def execute(self, bug, bug_event):
        """See IEmailCommand."""
        if bug is None:
            raise EmailProcessingError(
                get_error_message(
                    'command-with-no-bug.txt',
                    error_templates=error_templates),
                stop_processing=True)

        string_args = list(self.string_args)
        try:
            path = string_args.pop(0)
        except IndexError:
            raise EmailProcessingError(
                get_error_message(
                    'affects-no-arguments.txt',
                    error_templates=error_templates),
                stop_processing=True)
        try:
            bug_target = self.getBugTarget(path)
        except BugTargetNotFound, error:
            raise EmailProcessingError(unicode(error), stop_processing=True)
        event = None

        if isinstance(bug, CreateBugParams):
            # Enough information has been gathered to create a new bug.
            kwargs = {
                'product': IProduct(bug_target, None),
                'distribution': IDistribution(bug_target, None),
                'sourcepackagename': ISourcePackageName(bug_target, None),
                }
            bug.setBugTarget(**kwargs)
            bug, bug_event = getUtility(IBugSet).createBug(
                bug, notify_event=False)
            event = ObjectCreatedEvent(bug.bugtasks[0])
            # Continue because the bug_target may be a subordinate bugtask.

        bugtask = bug.getBugTask(bug_target)
        if (bugtask is None and
            IDistributionSourcePackage.providedBy(bug_target)):
            # If there's a distribution task with no source package, use
            # that one.
            bugtask = bug.getBugTask(bug_target.distribution)
            if bugtask is not None:
                bugtask_before_edit = Snapshot(
                    bugtask, providing=IBugTask)
                bugtask.transitionToTarget(bug_target)
                event = ObjectModifiedEvent(
                    bugtask, bugtask_before_edit, ['sourcepackagename'])

        if bugtask is None:
            try:
                bugtask = self._create_bug_task(bug, bug_target)
            except IllegalTarget as e:
                raise EmailProcessingError(
                    get_error_message(
                        'cannot-add-task.txt',
                        error_templates=error_templates,
                        bug_id=bug.id,
                        target_name=bug_target.name, reason=e[0]),
                    stop_processing=True)
            event = ObjectCreatedEvent(bugtask)

        return bugtask, event, bug_event

    def _targetBug(self, user, bug, series, sourcepackagename=None):
        """Try to target the bug the given distroseries.

        If the user doesn't have permission to target the bug directly,
        only a nomination will be created.
        """
        product = None
        distribution = None
        if IDistroSeries.providedBy(series):
            distribution = series.distribution
            if sourcepackagename:
                general_target = distribution.getSourcePackage(
                    sourcepackagename)
            else:
                general_target = distribution
        else:
            assert IProductSeries.providedBy(series), (
                "Unknown series target: %r" % series)
            assert sourcepackagename is None, (
                "A product series can't have a source package.")
            product = series.product
            general_target = product
        general_task = bug.getBugTask(general_target)
        if general_task is None:
            # A series task has to have a corresponding
            # distribution/product task.
            general_task = bug.addTask(user, general_target)

        # We know the target is of the right type, and we just created
        # a pillar task, so if canBeNominatedFor == False then a task or
        # nomination must already exist.
        if not bug.canBeNominatedFor(series):
            # A nomination has already been created.
            nomination = bug.getNominationFor(series)
        else:
            nomination = bug.addNomination(target=series, owner=user)

        # Automatically approve an existing or new nomination if possible.
        if not nomination.isApproved() and nomination.canApprove(user):
            nomination.approve(user)

        if nomination.isApproved():
            if sourcepackagename:
                return bug.getBugTask(
                    series.getSourcePackage(sourcepackagename))
            else:
                return bug.getBugTask(series)
        else:
            # We can't return a nomination, so return the
            # distribution/product bugtask instead.
            return general_task

    def _create_bug_task(self, bug, bug_target):
        """Creates a new bug task with bug_target as the target."""
        user = getUtility(ILaunchBag).user
        if (IProductSeries.providedBy(bug_target) or
            IDistroSeries.providedBy(bug_target)):
            return self._targetBug(user, bug, bug_target)
        elif ISourcePackage.providedBy(bug_target):
            return self._targetBug(
                user, bug, bug_target.distroseries,
                bug_target.sourcepackagename)
        else:
            return bug.addTask(user, bug_target)


class AssigneeEmailCommand(EditEmailCommand):
    """Assigns someone to the bug."""

    implements(IBugTaskEditEmailCommand)

    _numberOfArguments = 1
    RANK = 2

    def convertArguments(self, context):
        """See EmailCommand."""
        person_name_or_email = self.string_args[0]

        # "nobody" is a special case that means assignee == None.
        if person_name_or_email == "nobody":
            return {self.name: None}

        return {self.name: get_person_or_team(person_name_or_email)}

    def setAttributeValue(self, context, attr_name, attr_value):
        """See EmailCommand."""
        context.transitionToAssignee(attr_value)


class MilestoneEmailCommand(EditEmailCommand):
    """Sets the milestone for the bugtask."""

    implements(IBugTaskEditEmailCommand)

    _numberOfArguments = 1
    RANK = 3

    def convertArguments(self, context):
        """See EmailCommand."""
        user = getUtility(ILaunchBag).user
        milestone_name = self.string_args[0]

        if milestone_name == '-':
            # Remove milestone
            return {self.name: None}
        elif self._userCanEditMilestone(user, context):
            milestone = context.pillar.getMilestone(milestone_name)
            if milestone is None:
                raise EmailProcessingError(
                    "The milestone %s does not exist for %s. Note that "
                    "milestones are not automatically created from emails; "
                    "they must be created on the website." % (
                        milestone_name, context.pillar.title))
            else:
                return {self.name: milestone}
        else:
            raise EmailProcessingError(
                "You do not have permission to set the milestone for %s. "
                "Only owners, drivers and bug supervisors may assign "
                "milestones." % (context.pillar.title,))

    def _userCanEditMilestone(self, user, bugtask):
        """Can the user edit the Milestone field?"""
        # Adapted from BugTaskEditView.userCanEditMilestone.

        # XXX: GavinPanella 2007-10-18 bug=154088: Consider
        # refactoring this method and the userCanEditMilestone method
        # on BugTaskEditView into a new method on IBugTask. This is
        # non-trivial because check_permission cannot be used in a
        # database class.

        pillar = bugtask.pillar
        bug_supervisor = pillar.bug_supervisor
        if user is not None and bug_supervisor is not None:
            if user.inTeam(bug_supervisor):
                return True
        return check_permission("launchpad.Edit", pillar)


class DBSchemaEditEmailCommand(EditEmailCommand):
    """Helper class for edit DBSchema attributes.

    Subclasses should set 'dbschema' to the correct schema.

    For example, if context.foo can be assigned to values in
    FooDBSchema, the follwing command class could be created:

        class FooEmailCommand(DBSchemaEditEmailCommand):
            dbschema = FooDBSchema
    """

    implements(IBugTaskEditEmailCommand)

    _numberOfArguments = 1

    def convertArguments(self, context):
        """See EmailCommand."""
        item_name = self.string_args[0]
        dbschema = self.dbschema
        try:
            dbitem = dbschema.items[item_name.upper()]
        except KeyError:
            dbitem = None

        if dbitem is None or dbitem.name == 'UNKNOWN':
            possible_items = [
                item.name.lower() for item in dbschema.items
                if item.name != 'UNKNOWN']
            possible_values = ', '.join(possible_items)
            raise EmailProcessingError(
                    get_error_message(
                        'dbschema-command-wrong-argument.txt',
                         command_name=self.name,
                         arguments=possible_values,
                         example_argument=possible_items[0]))

        return {self.name: dbitem}


class StatusEmailCommand(DBSchemaEditEmailCommand):
    """Changes a bug task's status."""
    dbschema = BugTaskStatus
    RANK = 4

    def setAttributeValue(self, context, attr_name, attr_value):
        """See EmailCommand."""
        user = getUtility(ILaunchBag).user

        if not context.canTransitionToStatus(attr_value, user):
            raise EmailProcessingError(
                'The status cannot be changed to %s because you are not '
                'the registrant or a bug supervisor for %s.' % (
                    attr_value.name.lower(), context.pillar.displayname))

        context.transitionToStatus(attr_value, user)


class ImportanceEmailCommand(DBSchemaEditEmailCommand):
    """Changes a bug task's importance."""
    dbschema = BugTaskImportance
    RANK = 5


class ReplacedByImportanceCommand(EmailCommand):
    """This command has been replaced by the 'importance' command."""
    implements(IBugTaskEditEmailCommand)
    RANK = 1

    def execute(self, context, current_event):
        raise EmailProcessingError(
                get_error_message(
                    'bug-importance.txt',
                    error_templates=error_templates,
                    argument=self.name))


class TagEmailCommand(EmailCommand):
    """Assigns a tag to or removes a tag from bug."""

    implements(IBugEditEmailCommand)
    RANK = 4

    def execute(self, bug, current_event):
        """See `IEmailCommand`."""
        # Tags are always lowercase.
        string_args = [arg.lower() for arg in self.string_args]
        if bug.tags is None:
            tags = []
        else:
            tags = list(bug.tags)
        for arg in string_args:
            # Are we adding or removing a tag?
            if arg.startswith('-'):
                remove = True
                tag = arg[1:]
            else:
                remove = False
                tag = arg
            # Tag must be a valid name.
            if not valid_name(tag):
                raise EmailProcessingError(
                    get_error_message(
                        'invalid-tag.txt',
                        error_templates=error_templates,
                        tag=tag))
            if remove:
                try:
                    tags.remove(tag)
                except ValueError:
                    raise EmailProcessingError(
                        get_error_message(
                            'unassigned-tag.txt',
                            error_templates=error_templates,
                            tag=tag))
            else:
                tags.append(arg)
        bug.tags = tags

        return bug, current_event


class BugEmailCommands(EmailCommandCollection):
    """A collection of email commands."""

    _commands = {
        'bug': BugEmailCommand,
        'private': PrivateEmailCommand,
        'security': SecurityEmailCommand,
        'summary': SummaryEmailCommand,
        'subscribe': SubscribeEmailCommand,
        'unsubscribe': UnsubscribeEmailCommand,
        'duplicate': DuplicateEmailCommand,
        'cve': CVEEmailCommand,
        'affects': AffectsEmailCommand,
        'assignee': AssigneeEmailCommand,
        'milestone': MilestoneEmailCommand,
        'status': StatusEmailCommand,
        'importance': ImportanceEmailCommand,
        'severity': ReplacedByImportanceCommand,
        'priority': ReplacedByImportanceCommand,
        'tag': TagEmailCommand,
    }