Skip to content

Commit

Permalink
Formatting and regen Jazzy
Browse files Browse the repository at this point in the history
  • Loading branch information
quanvo87 committed Jun 1, 2017
1 parent dd2c978 commit b715cdb
Show file tree
Hide file tree
Showing 37 changed files with 250 additions and 87 deletions.
44 changes: 37 additions & 7 deletions Sources/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ public struct Attachment {
/// - additionalHeaders: Additional headers for the attachment.
/// (optional)
/// - related: Related `Attachment`s of this attachment. (optional)
public init(data: Data, mime: String, name: String, inline: Bool = false, additionalHeaders: [String: String]? = nil, relatedAttachments: [Attachment]? = nil) {
self.init(type: .data(data: data, mime: mime, name: name, inline: inline), additionalHeaders: additionalHeaders, relatedAttachments: relatedAttachments)
public init(data: Data,
mime: String,
name: String,
inline: Bool = false,
additionalHeaders: [String: String]? = nil,
relatedAttachments: [Attachment]? = nil) {
self.init(type: .data(data: data,
mime: mime,
name: name,
inline: inline),
additionalHeaders: additionalHeaders,
relatedAttachments: relatedAttachments)
}

/// Initialize an `Attachment` from a local file.
Expand All @@ -52,9 +62,19 @@ public struct Attachment {
/// - additionalHeaders: Additional headers for the attachment.
/// (optional)
/// - related: Related `Attachment`s of this attachment. (optional)
public init(filePath: String, mime: String = "application/octet-stream", name: String? = nil, inline: Bool = false, additionalHeaders: [String: String]? = nil, relatedAttachments: [Attachment]? = nil) {
public init(filePath: String,
mime: String = "application/octet-stream",
name: String? = nil,
inline: Bool = false,
additionalHeaders: [String: String]? = nil,
relatedAttachments: [Attachment]? = nil) {
let name = name ?? NSString(string: filePath).lastPathComponent
self.init(type: .file(path: filePath, mime: mime, name: name, inline: inline), additionalHeaders: additionalHeaders, relatedAttachments: relatedAttachments)
self.init(type: .file(path: filePath,
mime: mime,
name: name,
inline: inline),
additionalHeaders: additionalHeaders,
relatedAttachments: relatedAttachments)
}

/// Initialize an HTML `Attachment`.
Expand All @@ -68,11 +88,21 @@ public struct Attachment {
/// - additionalHeaders: Additional headers for the attachment.
/// (optional)
/// - related: Related `Attachment`s of this attachment. (optional)
public init(htmlContent: String, characterSet: String = "utf-8", alternative: Bool = true, additionalHeaders: [String: String]? = nil, relatedAttachments: [Attachment]? = nil) {
self.init(type: .html(content: htmlContent, characterSet: characterSet, alternative: alternative), additionalHeaders: additionalHeaders, relatedAttachments: relatedAttachments)
public init(htmlContent: String,
characterSet: String = "utf-8",
alternative: Bool = true,
additionalHeaders: [String: String]? = nil,
relatedAttachments: [Attachment]? = nil) {
self.init(type: .html(content: htmlContent,
characterSet: characterSet,
alternative: alternative),
additionalHeaders: additionalHeaders,
relatedAttachments: relatedAttachments)
}

private init(type: AttachmentType, additionalHeaders: [String: String]?, relatedAttachments: [Attachment]?) {
private init(type: AttachmentType,
additionalHeaders: [String: String]?,
relatedAttachments: [Attachment]?) {
self.type = type
self.additionalHeaders = additionalHeaders
self.relatedAttachments = relatedAttachments
Expand Down
14 changes: 10 additions & 4 deletions Sources/AuthEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import Cryptor

struct AuthEncoder {
static func cramMD5(challenge: String, user: String, password: String) throws -> String {
guard let hmac = HMAC(using: HMAC.Algorithm.md5, key: password).update(string: try challenge.base64Decoded())?.final() else {
throw SMTPError(.md5HashChallengeFail)
guard
let hmac = HMAC(using: HMAC.Algorithm.md5,
key: password).update(string: try challenge.base64Decoded())?.final()
else {
throw SMTPError(.md5HashChallengeFail)
}
let digest = CryptoUtils.hexString(from: hmac)
return ("\(user) \(digest)").base64Encoded
Expand All @@ -43,8 +46,11 @@ struct AuthEncoder {

extension String {
func base64Decoded() throws -> String {
guard let data = Data(base64Encoded: self), let base64Decoded = String(data: data, encoding: .utf8) else {
throw SMTPError(.base64DecodeFail(self))
guard
let data = Data(base64Encoded: self),
let base64Decoded = String(data: data, encoding: .utf8)
else {
throw SMTPError(.base64DecodeFail(self))
}
return base64Decoded
}
Expand Down
30 changes: 21 additions & 9 deletions Sources/Login.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class Login {
}

if group.wait(timeout: DispatchTime.now() + .seconds(self.timeout)) == .timedOut {
self.done(socket: nil, error: SMTPError(.couldNotConnectToServer(self.hostname, self.timeout)))
self.done(socket: nil,
error: SMTPError(.couldNotConnectToServer(self.hostname,
self.timeout)))
}
}
}
Expand All @@ -93,7 +95,8 @@ class Login {
private extension Login {
func connect(_ port: Port) throws {
try socket.connect(to: hostname, port: port)
_ = try SMTPSocket.parseResponses(try socket.readFromSocket(), command: .connect)
_ = try SMTPSocket.parseResponses(try socket.readFromSocket(),
command: .connect)
}

func loginToServer() throws {
Expand Down Expand Up @@ -138,7 +141,9 @@ private extension Login {
if resArr.first == "AUTH" {
let args = resArr.dropFirst()
for arg in args {
if let authMethod = AuthMethod(rawValue: arg), authMethods.contains(authMethod) {
if let authMethod = AuthMethod(rawValue: arg),
authMethods.contains(authMethod)
{
return authMethod
}
}
Expand All @@ -150,8 +155,11 @@ private extension Login {

private extension Login {
func loginCramMD5() throws {
let challenge = try auth(authMethod: .cramMD5, credentials: nil).message
try authPassword(try AuthEncoder.cramMD5(challenge: challenge, user: email, password: password))
let challenge = try auth(authMethod: .cramMD5,
credentials: nil).message
try authPassword(try AuthEncoder.cramMD5(challenge: challenge,
user: email,
password: password))
}

func loginLogin() throws {
Expand All @@ -162,14 +170,18 @@ private extension Login {
}

func loginPlain() throws {
_ = try auth(authMethod: .plain, credentials: AuthEncoder.plain(user: email, password: password))
_ = try auth(authMethod: .plain,
credentials: AuthEncoder.plain(user: email,
password: password))
}

func loginXOAuth2() throws {
guard let accessToken = accessToken else {
throw SMTPError(.noAccessToken)
}
_ = try auth(authMethod: .xoauth2, credentials: AuthEncoder.xoauth2(user: email, accessToken: accessToken))
_ = try auth(authMethod: .xoauth2,
credentials: AuthEncoder.xoauth2(user: email,
accessToken: accessToken))
}
}

Expand All @@ -189,11 +201,11 @@ private extension Login {
func auth(authMethod: AuthMethod, credentials: String?) throws -> Response {
return try socket.send(.auth(authMethod, credentials))
}

func authUser(_ user: String) throws {
return try socket.send(.authUser(user))
}

func authPassword(_ password: String) throws {
return try socket.send(.authPassword(password))
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/SMTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ public struct SMTP {
/// - password: Password to log in to server.
/// - port: `Port` to connect to the server on. Defaults to `587`.
/// - ssl: `SSL` containing configuration info for connecting securely
/// through SSL/TLS.
/// through SSL/TLS. (optional)
/// - authMethods: `AuthMethod`s to use to log in to the
/// server. Defaults to `CRAM-MD5`, `LOGIN`, and `PLAIN`.
/// - domainName: Client domain name used when communicating with the
/// server. Defaults to `localhost`.
/// - accessToken: Access token used if logging in through `XOAUTH2`.
/// (optional)
/// - timeout: How long to try connecting to the server to before
/// returning an error. Defaults to `10` seconds.
///
Expand Down Expand Up @@ -106,7 +107,7 @@ public struct SMTP {
/// - Parameters:
/// - mail: `Mail` object to send.
/// - completion: Callback when sending finishes. `Error` is nil on
/// success.
/// success. (optional)
public func send(_ mail: Mail, completion: ((Error?) -> Void)? = nil) {
send([mail]) { (_, failed) in
if let error = failed.first?.1 {
Expand Down
4 changes: 3 additions & 1 deletion Sources/SMTPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ extension SMTPSocket {
let resArr = responses.components(separatedBy: CRLF)
for res in resArr {
if res == "" { break }
validResponses.append(Response(code: try getResponseCode(res, command: command), message: getResponseMessage(res), response: res))
validResponses.append(Response(code: try getResponseCode(res, command: command),
message: getResponseMessage(res),
response: res))
}
return validResponses
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/SSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public struct SSL {
/// - password: Password for the chain file (optional).
/// - selfSigned: True if certs are `self-signed`, false otherwise. Defaults to true.
/// - cipherSuite: Optional String containing the cipher suite to use.
public init(withChainFilePath chainFilePath: String? = nil, withPassword password: String? = nil, usingSelfSignedCerts selfSigned: Bool = true, cipherSuite: String? = nil) {
public init(withChainFilePath chainFilePath: String? = nil,
withPassword password: String? = nil,
usingSelfSignedCerts selfSigned: Bool = true,
cipherSuite: String? = nil) {
config = SSLService.Configuration(withChainFilePath: chainFilePath,
withPassword: password,
usingSelfSignedCerts: selfSigned,
Expand Down
5 changes: 4 additions & 1 deletion Sources/Sender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class Sender {
fileprivate var failed = [(Mail, Error)]()
var dataSender: DataSender

init(socket: SMTPSocket, pending: [Mail], progress: Progress, completion: Completion) {
init(socket: SMTPSocket,
pending: [Mail],
progress: Progress,
completion: Completion) {
self.socket = socket
self.pending = pending
self.progress = progress
Expand Down
2 changes: 1 addition & 1 deletion Sources/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct User {
/// Initializes a `User`.
///
/// - Parameters:
/// - name: Display name for the user. Defaults to nil.
/// - name: Display name for the user. (optional)
/// - email: Email address for the user.
public init(name: String? = nil, email: String) {
self.name = name
Expand Down
50 changes: 45 additions & 5 deletions Tests/SwiftSMTPTests/TestLogin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ class TestLogin: XCTestCase {

func testBadCredentials() throws {
let x = expectation(description: "Fail login with bad credentials.")
try Login(hostname: hostname, email: email, password: "", port: port, ssl: ssl, authMethods: authMethods, domainName: domainName, accessToken: nil, timeout: timeout) { (_, err) in
try Login(hostname: hostname,
email: email,
password: "",
port: port,
ssl: ssl,
authMethods: authMethods,
domainName: domainName,
accessToken: nil,
timeout: timeout) { (_, err) in
if let err = err as? SMTPError, case .badResponse = err {
x.fulfill()
} else {
Expand All @@ -40,7 +48,15 @@ class TestLogin: XCTestCase {

func testBadPort() throws {
let x = expectation(description: "Login timeout because bad port.")
try Login(hostname: hostname, email: email, password: password, port: 1, ssl: ssl, authMethods: authMethods, domainName: domainName, accessToken: nil, timeout: timeout) { (_, err) in
try Login(hostname: hostname,
email: email,
password: password,
port: 1,
ssl: ssl,
authMethods: authMethods,
domainName: domainName,
accessToken: nil,
timeout: timeout) { (_, err) in
if let err = err as? SMTPError, case .couldNotConnectToServer(_, _) = err {
x.fulfill()
} else {
Expand All @@ -52,7 +68,15 @@ class TestLogin: XCTestCase {

func testLogin() throws {
let x = expectation(description: "Login with Login auth.")
try Login(hostname: hostname, email: email, password: password, port: port, ssl: ssl, authMethods: [.login], domainName: domainName, accessToken: nil, timeout: timeout) { (_, err) in
try Login(hostname: hostname,
email: email,
password: password,
port: port,
ssl: ssl,
authMethods: [.login],
domainName: domainName,
accessToken: nil,
timeout: timeout) { (_, err) in
XCTAssertNil(err, String(describing: err))
x.fulfill()
}.login()
Expand All @@ -61,7 +85,15 @@ class TestLogin: XCTestCase {

func testPlain() throws {
let x = expectation(description: "Login with Plain auth.")
try Login(hostname: hostname, email: email, password: password, port: port, ssl: ssl, authMethods: [.plain], domainName: domainName, accessToken: nil, timeout: timeout) { (_, err) in
try Login(hostname: hostname,
email: email,
password: password,
port: port,
ssl: ssl,
authMethods: [.plain],
domainName: domainName,
accessToken: nil,
timeout: timeout) { (_, err) in
XCTAssertNil(err, String(describing: err))
x.fulfill()
}.login()
Expand All @@ -70,7 +102,15 @@ class TestLogin: XCTestCase {

func testPort0() throws {
let x = expectation(description: "Fail login because port can't be 0.")
try Login(hostname: hostname, email: email, password: password, port: 0, ssl: ssl, authMethods: authMethods, domainName: domainName, accessToken: nil, timeout: timeout) { (_, err) in
try Login(hostname: hostname,
email: email,
password: password,
port: 0,
ssl: ssl,
authMethods: authMethods,
domainName: domainName,
accessToken: nil,
timeout: timeout) { (_, err) in
XCTAssertNotNil(err, "Should get error here, but error was nil.")
x.fulfill()
}.login()
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-05-25)</p>
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-06-01)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.7.5</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/AuthMethod.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-05-25)</p>
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-06-01)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.7.5</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/Ports.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-05-25)</p>
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-06-01)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.7.5</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/SMTPError.html
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-05-25)</p>
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-06-01)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.7.5</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/Structs.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-05-25)</p>
<p>&copy; 2017 <a class="link" href="" target="_blank" rel="external">IBM</a>. All rights reserved. (Last updated: 2017-06-01)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.7.5</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
Expand Down
Loading

0 comments on commit b715cdb

Please sign in to comment.