15
15
use OCP \Security \ICrypto ;
16
16
17
17
class SieveClientFactory {
18
- /** @var ICrypto */
19
- private $ crypto ;
18
+ private ICrypto $ crypto ;
19
+ private IConfig $ config ;
20
+ private array $ cache = [];
20
21
21
- /** @var IConfig */
22
- private $ config ;
23
-
24
- private $ cache = [];
25
-
26
- /**
27
- * @param ICrypto $crypto
28
- * @param IConfig $config
29
- */
30
22
public function __construct (ICrypto $ crypto , IConfig $ config ) {
31
23
$ this ->crypto = $ crypto ;
32
24
$ this ->config = $ config ;
@@ -41,43 +33,62 @@ public function getClient(Account $account): ManageSieve {
41
33
if (empty ($ user )) {
42
34
$ user = $ account ->getMailAccount ()->getInboundUser ();
43
35
}
44
-
45
36
$ password = $ account ->getMailAccount ()->getSievePassword ();
46
37
if (empty ($ password )) {
47
38
$ password = $ account ->getMailAccount ()->getInboundPassword ();
48
39
}
49
40
50
- $ sslMode = $ account ->getMailAccount ()->getSieveSslMode ();
51
- if (empty ($ sslMode )) {
52
- $ sslMode = true ;
53
- } elseif ($ sslMode === 'none ' ) {
54
- $ sslMode = false ;
55
- }
56
-
57
- $ params = [
58
- 'host ' => $ account ->getMailAccount ()->getSieveHost (),
59
- 'port ' => $ account ->getMailAccount ()->getSievePort (),
60
- 'user ' => $ user ,
61
- 'password ' => $ this ->crypto ->decrypt ($ password ),
62
- 'secure ' => $ sslMode ,
63
- 'timeout ' => $ this ->config ->getSystemValueInt ('app.mail.sieve.timeout ' , 5 ),
64
- 'context ' => [
65
- 'ssl ' => [
66
- 'verify_peer ' => $ this ->config ->getSystemValueBool ('app.mail.verify-tls-peer ' , true ),
67
- 'verify_peer_name ' => $ this ->config ->getSystemValueBool ('app.mail.verify-tls-peer ' , true ),
68
- ]
69
- ],
70
- ];
71
-
72
41
if ($ account ->getDebug () || $ this ->config ->getSystemValueBool ('app.mail.debug ' )) {
73
- $ fn = 'mail- ' . $ account ->getUserId () . '- ' . $ account ->getId () . '-sieve.log ' ;
74
- $ params ['logger ' ] = new SieveLogger ($ this ->config ->getSystemValue ('datadirectory ' ) . '/ ' . $ fn );
42
+ $ logFile = $ this ->config ->getSystemValue ('datadirectory ' ) . '/mail- ' . $ account ->getUserId () . '- ' . $ account ->getId () . '-sieve.log ' ;
43
+ } else {
44
+ $ logFile = null ;
75
45
}
76
46
77
- $ this ->cache [$ account ->getId ()] = new ManageSieve ($ params );
47
+ $ this ->cache [$ account ->getId ()] = $ this ->createClient (
48
+ $ account ->getMailAccount ()->getSieveHost (),
49
+ $ account ->getMailAccount ()->getSievePort (),
50
+ $ user ,
51
+ $ this ->crypto ->decrypt ($ password ),
52
+ $ account ->getMailAccount ()->getSieveSslMode (),
53
+ $ logFile ,
54
+ );
78
55
}
79
56
80
57
return $ this ->cache [$ account ->getId ()];
81
58
}
82
59
60
+ /**
61
+ * @param string $sslMode possible values: '', 'none', 'ssl' or 'tls'
62
+ * @param ?string $logFile absolute path for logFile or null to disable logging
63
+ * @throws ManageSieve\Exception
64
+ */
65
+ public function createClient (string $ host , int $ port , string $ user , string $ password , string $ sslMode , ?string $ logFile ): ManageSieve {
66
+ if (empty ($ sslMode )) {
67
+ $ sslMode = true ;
68
+ } elseif ($ sslMode === 'none ' ) {
69
+ $ sslMode = false ;
70
+ }
71
+
72
+ $ params = [
73
+ 'host ' => $ host ,
74
+ 'port ' => $ port ,
75
+ 'user ' => $ user ,
76
+ 'password ' => $ password ,
77
+ 'secure ' => $ sslMode ,
78
+ 'timeout ' => $ this ->config ->getSystemValueInt ('app.mail.sieve.timeout ' , 5 ),
79
+ 'context ' => [
80
+ 'ssl ' => [
81
+ 'verify_peer ' => $ this ->config ->getSystemValueBool ('app.mail.verify-tls-peer ' , true ),
82
+ 'verify_peer_name ' => $ this ->config ->getSystemValueBool ('app.mail.verify-tls-peer ' , true ),
83
+
84
+ ]
85
+ ],
86
+ ];
87
+
88
+ if ($ logFile !== null ) {
89
+ $ params ['logger ' ] = new SieveLogger ($ logFile );
90
+ }
91
+
92
+ return new ManageSieve ($ params );
93
+ }
83
94
}
0 commit comments