-
Notifications
You must be signed in to change notification settings - Fork 720
/
ExtendedEmailPublisherDescriptor.java
880 lines (747 loc) · 28.5 KB
/
ExtendedEmailPublisherDescriptor.java
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
package hudson.plugins.emailext;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.HostnamePortRequirement;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Util;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.plugins.emailext.plugins.EmailTriggerDescriptor;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
import hudson.security.Permission;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.BiFunction;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.scriptsecurity.scripts.ApprovalContext;
import org.jenkinsci.plugins.scriptsecurity.scripts.ClasspathEntry;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.scriptsecurity.scripts.languages.GroovyLanguage;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest2;
/**
* These settings are global configurations
*/
@Extension
@Symbol({"email-ext", "extendedEmailPublisher"})
public final class ExtendedEmailPublisherDescriptor extends BuildStepDescriptor<Publisher> {
public static final Logger LOGGER = Logger.getLogger(ExtendedEmailPublisherDescriptor.class.getName());
/**
* The default e-mail address suffix appended to the user name found from
* changelog, to send e-mails. Null if not configured.
*/
private String defaultSuffix;
private MailAccount mailAccount = new MailAccount();
private List<MailAccount> addAccounts = new ArrayList<>();
private String charset;
/**
* This is a global default content type (mime type) for emails.
*/
private String defaultContentType;
/**
* This is a global default subject line for sending emails.
*/
private String defaultSubject;
/**
* This is a global default body for sending emails.
*/
private String defaultBody;
/**
* This is the global default pre-send script.
*/
private String defaultPresendScript;
/**
* This is the global default post-send script.
*/
private String defaultPostsendScript;
private List<GroovyScriptPath> defaultClasspath = new ArrayList<>();
private transient List<EmailTriggerDescriptor> defaultTriggers = new ArrayList<>();
private List<String> defaultTriggerIds = new ArrayList<>();
/**
* This is the global emergency email address
*/
private String emergencyReroute;
/**
* The maximum size of all the attachments (in bytes)
*/
private long maxAttachmentSize = -1;
/*
* This is a global default recipient list for sending emails.
*/
private String recipientList = "";
/*
* The default Reply-To header value
*/
private String defaultReplyTo = "";
/*
* This is a global list of domains where we can send emails to
*/
private String allowedDomains = null;
/*
* This is a global excluded committers list for not sending commit emails.
*/
private String excludedCommitters = "";
private transient boolean overrideGlobalSettings;
/**
* If non-null, set a List-ID email header.
*/
private String listId;
private boolean precedenceBulk;
private boolean debugMode = false;
/**
* If true, then the 'Email Template Testing' link will only be displayed
* for users with ADMINISTER privileges.
*/
private boolean requireAdminForTemplateTesting = false;
/**
* Enables the "Watch This Job" feature
*/
private boolean enableWatching;
/**
* Enables the "Allow Unregistered Emails" feature
*/
private boolean enableAllowUnregistered;
/**
* Enables the email throttling feature
*/
private boolean throttlingEnabled = false;
private transient String smtpHost;
private transient String smtpPort;
private transient String smtpAuthUsername;
private transient Secret smtpAuthPassword;
private transient boolean useSsl = false;
private transient BiFunction<MailAccount, Run<?, ?>, Authenticator> authenticatorProvider =
(acc, run) -> new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
DomainRequirement domainRequirement = null;
if (StringUtils.isNotBlank(acc.getSmtpHost()) && StringUtils.isNotBlank(acc.getSmtpPort())) {
domainRequirement =
new HostnamePortRequirement(acc.getSmtpHost(), Integer.parseInt(acc.getSmtpPort()));
}
StandardUsernamePasswordCredentials c = CredentialsProvider.findCredentialById(
acc.getCredentialsId(), StandardUsernamePasswordCredentials.class, run, domainRequirement);
if (c == null) {
return null;
}
return new PasswordAuthentication(c.getUsername(), Secret.toString(c.getPassword()));
}
};
private Object readResolve() {
if (smtpHost != null) {
mailAccount.setSmtpHost(smtpHost);
}
if (smtpPort != null) {
mailAccount.setSmtpPort(smtpPort);
}
if (smtpAuthUsername != null) {
mailAccount.setSmtpUsername(smtpAuthUsername);
}
if (smtpAuthPassword != null) {
mailAccount.setSmtpPassword(smtpAuthPassword);
}
if (useSsl) {
mailAccount.setUseSsl(useSsl);
}
/*
* Versions 2.71 and earlier correctly left the address unset for the default account,
* relying solely on the system admin email address from the Jenkins Location settings for
* the default account and using the address specified on the account only for additional
* accounts. Versions 2.72 through 2.77 incorrectly set the address for the default account
* to the system admin email address from the Jenkins Location settings at the time the
* descriptor was first saved without propagating further changes from the Jenkins Location
* settings to the default account. To clear up this bad state, we unconditionally clear the
* address and rely once again solely on the system admin email address from the Jenkins
* Location settings for the default account.
*/
if (mailAccount.getAddress() != null) {
mailAccount.setAddress(null);
}
return this;
}
public ExtendedEmailPublisherDescriptor() {
super(ExtendedEmailPublisher.class);
load();
if (defaultBody == null && defaultSubject == null && emergencyReroute == null) {
defaultBody = ExtendedEmailPublisher.DEFAULT_BODY_TEXT;
defaultSubject = ExtendedEmailPublisher.DEFAULT_SUBJECT_TEXT;
emergencyReroute = ExtendedEmailPublisher.DEFAULT_EMERGENCY_REROUTE_TEXT;
}
if (mailAccount == null) {
mailAccount = new MailAccount();
}
mailAccount.setDefaultAccount(true);
}
@Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED, before = InitMilestone.JOB_LOADED)
public static void autoConfigure() {
ExtendedEmailPublisherDescriptor descriptor = ExtendedEmailPublisher.descriptor();
if (Jenkins.get().isUseSecurity() && !StringUtils.isBlank(descriptor.getDefaultPostsendScript())
|| !StringUtils.isBlank(descriptor.getDefaultPresendScript())) {
descriptor.setDefaultPostsendScript(descriptor.getDefaultPostsendScript());
descriptor.setDefaultPresendScript(descriptor.getDefaultPresendScript());
try {
descriptor.setDefaultClasspath(descriptor.getDefaultClasspath());
} catch (FormException e) {
// Some of the old configured classpaths probably used some environment variable, let's clean those out
List<GroovyScriptPath> newList = new ArrayList<>();
for (GroovyScriptPath path : descriptor.getDefaultClasspath()) {
URL u = path.asURL();
if (u != null) {
try {
new ClasspathEntry(u.toString());
newList.add(path);
} catch (MalformedURLException mfue) {
LOGGER.log(
Level.WARNING,
"The default classpath contained a malformed url, will be ignored.",
mfue);
}
}
}
try {
descriptor.setDefaultClasspath(newList);
} catch (FormException e1) {
assert false : e1;
}
}
}
}
@NonNull
@Override
public String getDisplayName() {
return Messages.ExtendedEmailPublisherDescriptor_DisplayName();
}
public String getAdminAddress() {
JenkinsLocationConfiguration config = JenkinsLocationConfiguration.get();
if (config != null) {
if (StringUtils.isBlank(mailAccount.getAddress())) {
return config.getAdminAddress();
}
}
return mailAccount.getAddress();
}
public String getDefaultSuffix() {
return defaultSuffix;
}
@DataBoundSetter
public void setDefaultSuffix(String defaultSuffix) {
this.defaultSuffix = Util.fixEmptyAndTrim(defaultSuffix);
}
public boolean isThrottlingEnabled() {
return throttlingEnabled;
}
@DataBoundSetter
public void setThrottlingEnabled(boolean throttlingEnabled) {
this.throttlingEnabled = throttlingEnabled;
}
@Restricted(NoExternalUse.class)
Session createSession(MailAccount acc, ExtendedEmailPublisherContext context) {
final String SMTP_PORT_PROPERTY = "mail.smtp.port";
final String SMTP_SOCKETFACTORY_PORT_PROPERTY = "mail.smtp.socketFactory.port";
Properties props = new Properties(System.getProperties());
if (acc.getSmtpHost() != null) {
props.put("mail.smtp.host", acc.getSmtpHost());
}
if (acc.getSmtpPort() != null) {
props.put(SMTP_PORT_PROPERTY, acc.getSmtpPort());
}
if (acc.isUseSsl()) {
/* This allows the user to override settings by setting system properties but
* also allows us to use the default SMTPs port of 465 if no port is already set.
* It would be cleaner to use smtps, but that's done by calling session.getTransport()...
* and thats done in mail sender, and it would be a bit of a hack to get it all to
* coordinate, and we can make it work through setting mail.smtp properties.
*/
if (props.getProperty(SMTP_SOCKETFACTORY_PORT_PROPERTY) == null) {
String port = acc.getSmtpPort() == null ? "465" : mailAccount.getSmtpPort();
props.put(SMTP_PORT_PROPERTY, port);
props.put(SMTP_SOCKETFACTORY_PORT_PROPERTY, port);
}
if (props.getProperty("mail.smtp.socketFactory.class") == null) {
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
props.put("mail.smtp.socketFactory.fallback", "false");
// RFC 2595 specifies additional checks that must be performed on the server's
// certificate to ensure that the server you connected to is the server you intended
// to connect to. This reduces the risk of "man in the middle" attacks.
if (props.getProperty("mail.smtp.ssl.checkserveridentity") == null) {
props.put("mail.smtp.ssl.checkserveridentity", "true");
}
}
if (acc.isUseTls()) {
/* This allows the user to override settings by setting system properties and
* also allows us to use the default STARTTLS port, 587, if no port is already set.
* Only the properties included below are required to use STARTTLS and they are
* not expected to be enabled simultaneously with SSL (it will actually throw a
* "javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"
* if SMTP server expects only TLS).
*/
if (props.getProperty(SMTP_SOCKETFACTORY_PORT_PROPERTY) == null) {
String port = acc.getSmtpPort() == null ? "587" : mailAccount.getSmtpPort();
props.put(SMTP_PORT_PROPERTY, port);
props.put(SMTP_SOCKETFACTORY_PORT_PROPERTY, port);
}
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
}
if (!StringUtils.isBlank(acc.getCredentialsId())) {
props.put("mail.smtp.auth", "true");
}
if (acc.isUseOAuth2()) {
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
}
// avoid hang by setting some timeout.
props.put("mail.smtp.timeout", "60000");
props.put("mail.smtp.connectiontimeout", "60000");
try {
String ap = acc.getAdvProperties();
if (ap != null && !StringUtils.isBlank(ap.trim())) {
props.load(new StringReader(ap));
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Parameters parse fail.", e);
}
return Session.getInstance(props, getAuthenticator(acc, context));
}
private Authenticator getAuthenticator(final MailAccount acc, final ExtendedEmailPublisherContext context) {
if (acc == null || StringUtils.isBlank(acc.getCredentialsId())) {
return null;
}
return authenticatorProvider.apply(acc, context.getRun());
}
public String getHudsonUrl() {
return Jenkins.get().getRootUrl();
}
public List<MailAccount> getAddAccounts() {
return addAccounts;
}
@DataBoundSetter
public void setAddAccounts(List<MailAccount> addAccounts) {
this.addAccounts = addAccounts;
}
@Deprecated
public String getSmtpServer() {
return mailAccount.getSmtpHost();
}
@Deprecated
public void setSmtpServer(String smtpServer) {
mailAccount.setSmtpHost(smtpServer);
}
@Deprecated
public String getSmtpUsername() {
return mailAccount.getSmtpUsername();
}
@SuppressWarnings("unused")
@Deprecated
public void setSmtpUsername(String username) {
mailAccount.setSmtpUsername(username);
}
@Deprecated
public Secret getSmtpPassword() {
return mailAccount.getSmtpPassword();
}
@SuppressWarnings("unused")
@DataBoundSetter
@Deprecated
public void setSmtpPassword(String password) {
mailAccount.setSmtpPassword(password);
}
// Make API match Mailer plugin
@SuppressWarnings("unused")
public void setSmtpAuth(String userName, String password) {
mailAccount.setSmtpUsername(userName);
mailAccount.setSmtpPassword(password);
}
@Deprecated
public boolean getUseSsl() {
return mailAccount.isUseSsl();
}
@SuppressWarnings("unused")
@Deprecated
public void setUseSsl(boolean useSsl) {
mailAccount.setUseSsl(useSsl);
}
@Deprecated
public String getSmtpPort() {
return mailAccount.getSmtpPort();
}
@SuppressWarnings("unused")
@Deprecated
public void setSmtpPort(String port) {
mailAccount.setSmtpPort(nullify(port));
}
@Deprecated
public String getAdvProperties() {
return mailAccount.getAdvProperties();
}
@Deprecated
public void setAdvProperties(String advProperties) {
mailAccount.setAdvProperties(advProperties);
}
public String getCharset() {
String c = charset;
if (StringUtils.isBlank(c)) {
c = "UTF-8";
}
return c;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setCharset(String charset) {
this.charset = Util.fixEmptyAndTrim(charset);
}
public String getDefaultContentType() {
return defaultContentType;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setDefaultContentType(String contentType) {
if (StringUtils.isBlank(contentType)) {
this.defaultContentType = "text/plain";
} else {
this.defaultContentType = contentType;
}
}
@SuppressWarnings({"lgtm[jenkins/csrf]", "lgtm[jenkins/no-permission-check]"})
public FormValidation doCheckDefaultSuffix(@QueryParameter String value) {
if (value.matches("@[A-Za-z0-9.\\-]+") || Util.fixEmptyAndTrim(value) == null) {
return FormValidation.ok();
} else {
return FormValidation.error(Messages.Mailer_Suffix_Error());
}
}
public String getDefaultSubject() {
return defaultSubject;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setDefaultSubject(String subject) {
if (subject == null) {
this.defaultSubject = ExtendedEmailPublisher.DEFAULT_SUBJECT_TEXT;
} else {
this.defaultSubject = subject;
}
}
public String getDefaultBody() {
return defaultBody;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setDefaultBody(String body) {
if (StringUtils.isBlank(body)) {
this.defaultBody = ExtendedEmailPublisher.DEFAULT_BODY_TEXT;
} else {
this.defaultBody = body;
}
}
public String getEmergencyReroute() {
return emergencyReroute;
}
@DataBoundSetter
public void setEmergencyReroute(String emergencyReroute) {
if (StringUtils.isBlank(emergencyReroute)) {
this.emergencyReroute = ExtendedEmailPublisher.DEFAULT_EMERGENCY_REROUTE_TEXT;
} else {
this.emergencyReroute = Util.fixEmptyAndTrim(emergencyReroute);
}
}
public long getMaxAttachmentSize() {
return maxAttachmentSize;
}
@DataBoundSetter
public void setMaxAttachmentSize(long bytes) {
if (bytes < 0) {
bytes = -1; // set to default "empty" value
}
this.maxAttachmentSize = bytes;
}
public MailAccount getMailAccount() {
return mailAccount;
}
@DataBoundSetter
public void setMailAccount(MailAccount mailAccount) {
this.mailAccount = mailAccount;
this.mailAccount.setDefaultAccount(true);
}
public long getMaxAttachmentSizeMb() {
if (maxAttachmentSize < 0) {
return -1;
}
return maxAttachmentSize / (1024 * 1024);
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setMaxAttachmentSizeMb(long mb) {
if (mb < 0) {
setMaxAttachmentSize(mb);
} else {
setMaxAttachmentSize(mb * (1024 * 1024));
}
}
public String getDefaultRecipients() {
return recipientList;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setDefaultRecipients(String recipients) {
this.recipientList = recipients == null ? "" : recipients;
}
public String getAllowedDomains() {
return allowedDomains;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setAllowedDomains(String allowed) {
this.allowedDomains = allowed == null ? "" : allowed;
}
public String getExcludedCommitters() {
return excludedCommitters;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setExcludedCommitters(String excluded) {
this.excludedCommitters = excluded == null ? "" : excluded;
}
@Deprecated
public boolean getOverrideGlobalSettings() {
return overrideGlobalSettings;
}
public String getListId() {
return listId;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setListId(String id) {
this.listId = id;
}
public boolean getPrecedenceBulk() {
return precedenceBulk;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setPrecedenceBulk(boolean bulk) {
this.precedenceBulk = bulk;
}
public String getDefaultReplyTo() {
return defaultReplyTo;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setDefaultReplyTo(String to) {
this.defaultReplyTo = to == null ? "" : to;
}
public boolean isSecurityEnabled() {
return false;
}
public boolean isAdminRequiredForTemplateTesting() {
return requireAdminForTemplateTesting;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setAdminRequiredForTemplateTesting(boolean requireAdmin) {
this.requireAdminForTemplateTesting = requireAdmin;
}
public boolean isWatchingEnabled() {
return enableWatching;
}
public boolean isAllowUnregisteredEnabled() {
return enableAllowUnregistered;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setWatchingEnabled(boolean enabled) {
this.enableWatching = enabled;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setAllowUnregisteredEnabled(boolean enabled) {
this.enableAllowUnregistered = enabled;
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
public @CheckForNull String getDefaultPresendScript() {
return defaultPresendScript;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setDefaultPresendScript(@CheckForNull String script) {
script = StringUtils.trimToNull(script);
this.defaultPresendScript = script == null
? null
: ScriptApproval.get()
.configuring(
script,
GroovyLanguage.get(),
ApprovalContext.create().withCurrentUser());
}
public @CheckForNull String getDefaultPostsendScript() {
return defaultPostsendScript;
}
@SuppressWarnings("unused")
@DataBoundSetter
public void setDefaultPostsendScript(@CheckForNull String script) {
script = StringUtils.trimToNull(script);
this.defaultPostsendScript = script == null
? null
: ScriptApproval.get()
.configuring(
script,
GroovyLanguage.get(),
ApprovalContext.create().withCurrentUser());
}
public List<GroovyScriptPath> getDefaultClasspath() {
return defaultClasspath;
}
@DataBoundSetter
public void setDefaultClasspath(List<GroovyScriptPath> defaultClasspath) throws FormException {
if (Jenkins.get().isUseSecurity()) {
ScriptApproval approval = ScriptApproval.get();
ApprovalContext context = ApprovalContext.create().withCurrentUser();
for (GroovyScriptPath path : defaultClasspath) {
URL u = path.asURL();
if (u != null) {
try {
approval.configuring(new ClasspathEntry(u.toString()), context);
} catch (MalformedURLException e) {
throw new FormException(e, "defaultClasspath");
}
}
}
}
this.defaultClasspath = defaultClasspath;
}
public List<String> getDefaultTriggerIds() {
if (defaultTriggerIds.isEmpty()) {
if (!defaultTriggers.isEmpty()) {
for (EmailTriggerDescriptor t : this.defaultTriggers) {
// we have to do the below because a bunch of stuff is not serialized for the Descriptor
EmailTriggerDescriptor d = Jenkins.get().getDescriptorByType(t.getClass());
if (d != null && !defaultTriggerIds.contains(d.getId())) {
defaultTriggerIds.add(d.getId());
}
}
} else {
FailureTrigger.DescriptorImpl f =
Jenkins.get().getDescriptorByType(FailureTrigger.DescriptorImpl.class);
if (f != null) {
defaultTriggerIds.add(f.getId());
}
}
save();
}
return defaultTriggerIds;
}
@DataBoundSetter
public void setDefaultTriggerIds(List<String> triggerIds) {
defaultTriggerIds = triggerIds;
}
@SuppressWarnings({"lgtm[jenkins/csrf]", "lgtm[jenkins/no-permission-check]", "unused"})
public ListBoxModel doFillDefaultContentTypeItems() {
ListBoxModel items = new ListBoxModel();
items.add(Messages.contentType_plainText(), "text/plain");
items.add(Messages.contentType_html(), "text/html");
return items;
}
@Override
public boolean configure(StaplerRequest2 req, JSONObject formData) throws FormException {
req.bindJSON(this, formData);
save();
return super.configure(req, formData);
}
private String nullify(String v) {
if (v != null && v.length() == 0) {
v = null;
}
return v;
}
@Override
public String getHelpFile() {
return "/plugin/email-ext/help/main.html";
}
@SuppressWarnings({"lgtm[jenkins/csrf]", "lgtm[jenkins/no-permission-check]"})
public FormValidation doAddressCheck(@QueryParameter final String value) {
try {
new InternetAddress(value);
return FormValidation.ok();
} catch (AddressException e) {
return FormValidation.error(e.getMessage());
}
}
@SuppressWarnings({"lgtm[jenkins/csrf]", "lgtm[jenkins/no-permission-check]"})
public FormValidation doRecipientListRecipientsCheck(@QueryParameter final String value) {
return new EmailRecipientUtils().validateFormRecipientList(value);
}
@SuppressWarnings({"lgtm[jenkins/csrf]", "lgtm[jenkins/no-permission-check]"})
public FormValidation doMaxAttachmentSizeCheck(@QueryParameter final String value) {
try {
String testValue = value.trim();
// we support an empty value (which means default)
// or a number
if (testValue.length() > 0) {
Long.parseLong(testValue);
}
return FormValidation.ok();
} catch (Exception e) {
return FormValidation.error(e.getMessage());
}
}
public boolean isMatrixProject(Object project) {
return project instanceof MatrixProject;
}
public boolean isDebugMode() {
return debugMode;
}
public void setDebugMode(boolean debugMode) {
this.debugMode = debugMode;
}
public void debug(PrintStream logger, String format, Object... args) {
if (debugMode) {
logger.format(format, args);
logger.println();
}
}
@NonNull
@Override
public Permission getRequiredGlobalConfigPagePermission() {
return Jenkins.MANAGE;
}
BiFunction<MailAccount, Run<?, ?>, Authenticator> getAuthenticatorProvider() {
return authenticatorProvider;
}
void setAuthenticatorProvider(BiFunction<MailAccount, Run<?, ?>, Authenticator> authenticatorProvider) {
this.authenticatorProvider = authenticatorProvider;
}
}