├── .github └── workflows │ └── swift.yml ├── .gitignore ├── CONTRIBUTORS.txt ├── LICENSE.txt ├── Makefile ├── Package.swift ├── README.md └── Sources └── IRC ├── IRCClient.swift ├── IRCClientDelegate.swift ├── IRCClientMessageTarget.swift ├── IRCClientOptions.swift ├── IRCRetryStrategyCB.swift └── ReExports.swift /.github/workflows/swift.yml: -------------------------------------------------------------------------------- 1 | name: Build and Test 2 | 3 | on: 4 | push: 5 | pull_request: 6 | schedule: 7 | - cron: "0 9 * * 1" 8 | 9 | jobs: 10 | linux: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | image: 16 | - swift:5.0.3-xenial 17 | - swift:5.1.5-xenial 18 | - swift:5.2.5-xenial 19 | - swift:5.3.2-xenial 20 | - swift:5.3.2-bionic 21 | container: ${{ matrix.image }} 22 | steps: 23 | - name: Checkout Repository 24 | uses: actions/checkout@v2 25 | - name: Build Swift Debug Package 26 | run: swift build -c debug 27 | - name: Build Swift Release Package 28 | run: swift build -c release 29 | nextstep: 30 | runs-on: macos-latest 31 | steps: 32 | - name: Select latest available Xcode 33 | uses: maxim-lobanov/setup-xcode@v1.2.1 34 | with: 35 | xcode-version: 12.2 36 | - name: Checkout Repository 37 | uses: actions/checkout@v2 38 | - name: Build Swift Debug Package 39 | run: swift build -c debug 40 | - name: Build Swift Release Package 41 | run: swift build -c release 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | 69 | *.xcodeproj 70 | .build-linux* 71 | Package.resolved 72 | dump*.json 73 | 74 | # hh 75 | .swiftpm 76 | .docker.build 77 | 78 | -------------------------------------------------------------------------------- /CONTRIBUTORS.txt: -------------------------------------------------------------------------------- 1 | Helge Hess 2 | 3 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2018 ZeeZide GmbH 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Makefile 2 | 3 | # local config 4 | SWIFT_BUILD=swift build 5 | SWIFT_CLEAN=swift package clean 6 | SWIFT_BUILD_DIR=.build 7 | 8 | # docker config 9 | SWIFT_BUILD_IMAGE="helje5/swift-dev:5.2.0" 10 | CONFIGURATION=release 11 | DOCKER_BUILD_DIR=".docker.build" 12 | SWIFT_DOCKER_BUILD_DIR="$(DOCKER_BUILD_DIR)/x86_64-unknown-linux/$(CONFIGURATION)" 13 | DOCKER_BUILD_PRODUCT="$(DOCKER_BUILD_DIR)/$(TOOL_NAME)" 14 | 15 | 16 | SWIFT_SOURCES=\ 17 | Sources/*/*/*.swift \ 18 | Sources/*/*/*/*.swift 19 | 20 | all: 21 | $(SWIFT_BUILD) 22 | 23 | clean : 24 | $(SWIFT_CLEAN) 25 | # We have a different definition of "clean", might be just German 26 | # pickyness. 27 | rm -rf $(SWIFT_BUILD_DIR) 28 | 29 | $(DOCKER_BUILD_PRODUCT): $(SWIFT_SOURCES) 30 | time docker run --rm \ 31 | -v "$(PWD):/src" \ 32 | -v "$(PWD)/$(DOCKER_BUILD_DIR):/src/.build" \ 33 | "$(SWIFT_BUILD_IMAGE)" \ 34 | bash -c 'cd /src && swift build -c $(CONFIGURATION)' 35 | ls -lah $(DOCKER_BUILD_PRODUCT) 36 | 37 | docker-all: $(DOCKER_BUILD_PRODUCT) 38 | 39 | docker-clean: 40 | rm -rf $(DOCKER_BUILD_PRODUCT) 41 | 42 | docker-distclean: 43 | rm -rf $(DOCKER_BUILD_DIR) 44 | 45 | distclean: clean docker-distclean 46 | 47 | docker-emacs: 48 | docker run --rm -it \ 49 | -v "$(PWD):/src" \ 50 | -v "$(PWD)/$(DOCKER_BUILD_DIR):/src/.build" \ 51 | "$(SWIFT_BUILD_IMAGE)" \ 52 | emacs /src 53 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.0 2 | 3 | import PackageDescription 4 | 5 | var packageDependencies : [ Package.Dependency ] = [ 6 | .package(url: "https://github.com/apple/swift-nio.git", 7 | from: "2.25.1"), 8 | .package(url: "https://github.com/SwiftNIOExtras/swift-nio-irc.git", 9 | from: "0.8.0") 10 | ] 11 | var targetDependencies : [ Target.Dependency ] = [ "NIO", "NIOIRC" ] 12 | if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) { 13 | packageDependencies += [ 14 | .package(url: "https://github.com/apple/swift-nio-transport-services", 15 | from: "1.9.1") 16 | ] 17 | targetDependencies += [ "NIOTransportServices" ] 18 | } 19 | 20 | let package = Package( 21 | name: "swift-nio-irc-client", 22 | products: [ 23 | .library(name: "IRC", targets: [ "IRC" ]) 24 | ], 25 | dependencies: packageDependencies, 26 | targets: [ 27 | .target(name: "IRC", dependencies: targetDependencies) 28 | ] 29 | ) 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftNIO IRC Client 2 | 3 | ![Swift4](https://img.shields.io/badge/swift-4-blue.svg) 4 | ![Swift5](https://img.shields.io/badge/swift-5-blue.svg) 5 | ![iOS](https://img.shields.io/badge/os-iOS-green.svg?style=flat) 6 | ![macOS](https://img.shields.io/badge/os-macOS-green.svg?style=flat) 7 | ![tuxOS](https://img.shields.io/badge/os-tuxOS-green.svg?style=flat) 8 | 9 | 10 | SwiftNIO-IRC is a Internet Relay Chat 11 | [protocol implementation](Sources/NIOIRC) for 12 | [SwiftNIO](https://github.com/apple/swift-nio) 13 | and a simple 14 | [IRC client](Sources/IRC). 15 | 16 | This module contains a simple IRC client library. We also 17 | provide: 18 | - [swift-nio-irc](https://github.com/SwiftNIOExtras/swift-nio-irc) - 19 | the raw Swift NIO IRC protocol implementation 20 | - [swift-nio-irc-webclient](https://github.com/NozeIO/swift-nio-irc-webclient) - 21 | a simple IRC webclient + WebSocket gateway based on this module, 22 | - [swift-nio-irc-eliza](https://github.com/NozeIO/swift-nio-irc-eliza) - 23 | a cheap yet scalable therapist, 24 | - [swift-nio-irc-server](https://github.com/NozeIO/swift-nio-irc-server) - 25 | a framework to build IRC servers, and MiniIRCd, a small sample server. 26 | 27 | To get started with this, pull 28 | [swift-nio-irc-server](https://github.com/NozeIO/swift-nio-irc-server) - 29 | a module to rule them all and in the darkness bind them. 30 | 31 | NIOIRC is a SwiftNIO port of the 32 | [Noze.io miniirc](https://github.com/NozeIO/Noze.io/tree/master/Samples/miniirc) 33 | example from 2016. 34 | 35 | 36 | ## Importing the module using Swift Package Manager 37 | 38 | An example `Package.swift `importing the necessary modules: 39 | 40 | ```swift 41 | // swift-tools-version:5.0 42 | 43 | import PackageDescription 44 | 45 | let package = Package( 46 | name: "IRCTests", 47 | dependencies: [ 48 | .package(url: "https://github.com/NozeIO/swift-nio-irc-client.git", 49 | from: "0.7.0") 50 | ], 51 | targets: [ 52 | .target(name: "MyIRCClient", 53 | dependencies: [ "IRC" ]) 54 | ] 55 | ) 56 | ``` 57 | 58 | 59 | ### Who 60 | 61 | Brought to you by 62 | [ZeeZide](http://zeezide.de). 63 | We like 64 | [feedback](https://twitter.com/ar_institute), 65 | GitHub stars, 66 | cool [contract work](http://zeezide.com/en/services/services.html), 67 | presumably any form of praise you can think of. 68 | -------------------------------------------------------------------------------- /Sources/IRC/IRCClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the swift-nio-irc open source project 4 | // 5 | // Copyright (c) 2018-2020 ZeeZide GmbH. and the swift-nio-irc project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | import NIO 16 | import NIOIRC 17 | 18 | #if canImport(NIOTransportServices) 19 | import NIOTransportServices 20 | #endif 21 | 22 | /** 23 | * A simple IRC client based on SwiftNIO. 24 | * 25 | * Checkout swift-nio-irc-eliza or swift-nio-irc-webclient for examples on this. 26 | * 27 | * The basic flow is: 28 | * - create a `IRCClient` object, quite likely w/ custom `IRCClientOptions` 29 | * - implement and assign an `IRCClientDelegate`, which is going to handle 30 | * incoming commands 31 | * - `connect` the client 32 | */ 33 | open class IRCClient : IRCClientMessageTarget { 34 | 35 | public let options : IRCClientOptions 36 | public let eventLoop : EventLoop 37 | public var delegate : IRCClientDelegate? 38 | 39 | public enum Error : Swift.Error { 40 | case writeError(Swift.Error) 41 | case stopped 42 | case notImplemented 43 | case internalInconsistency 44 | case unexpectedInput 45 | case channelError(Swift.Error) 46 | } 47 | 48 | enum State : CustomStringConvertible { 49 | case disconnected 50 | case connecting 51 | case registering(channel: Channel, nick: IRCNickName, userInfo: IRCUserInfo) 52 | case registered (channel: Channel, nick: IRCNickName, userInfo: IRCUserInfo) 53 | case error (Error) 54 | case requestedQuit 55 | case quit 56 | 57 | var isRegistered : Bool { 58 | switch self { 59 | case .registered: return true 60 | default: return false 61 | } 62 | } 63 | 64 | var nick : IRCNickName? { 65 | @inline(__always) get { 66 | switch self { 67 | case .registering(_, let v, _): return v 68 | case .registered (_, let v, _): return v 69 | default: return nil 70 | } 71 | } 72 | } 73 | 74 | var userInfo : IRCUserInfo? { 75 | @inline(__always) get { 76 | switch self { 77 | case .registering(_, _, let v): return v 78 | case .registered (_, _, let v): return v 79 | default: return nil 80 | } 81 | } 82 | } 83 | 84 | var channel : Channel? { 85 | @inline(__always) get { 86 | switch self { 87 | case .registering(let channel, _, _): return channel 88 | case .registered (let channel, _, _): return channel 89 | default: return nil 90 | } 91 | } 92 | } 93 | 94 | var canStartConnection : Bool { 95 | switch self { 96 | case .disconnected, .error: return true 97 | case .connecting: return false 98 | case .registering: return false 99 | case .registered: return false 100 | case .requestedQuit, .quit: return false 101 | } 102 | } 103 | 104 | var description : String { 105 | switch self { 106 | case .disconnected: return "disconnected" 107 | case .connecting: return "connecting..." 108 | case .registering(_, let nick, _): return "registering<\(nick)>..." 109 | case .registered (_, let nick, _): return "registered<\(nick)>" 110 | case .error (let error): return "error<\(error)>" 111 | case .requestedQuit: return "quitting..." 112 | case .quit: return "quit" 113 | } 114 | } 115 | } 116 | 117 | private var state : State = .disconnected 118 | private var userMode = IRCUserMode() 119 | 120 | var usermask : String? { 121 | guard case .registered(_, let nick, let info) = state else { return nil } 122 | let host = info.servername ?? options.hostname ?? "??" 123 | return "\(nick.stringValue)!~\(info.username)@\(host)" 124 | } 125 | 126 | private let bootstrap : NIOClientTCPBootstrapProtocol 127 | 128 | public init(options: IRCClientOptions) { 129 | self.options = options 130 | 131 | let eventLoop = options.eventLoopGroup.next() 132 | self.eventLoop = eventLoop 133 | 134 | // what a mess :-) 135 | #if canImport(NIOTransportServices) 136 | #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) 137 | var overrideBootstrap : NIOClientTCPBootstrapProtocol? 138 | if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) { 139 | if options.eventLoopGroup is NIOTSEventLoopGroup { 140 | overrideBootstrap = NIOTSConnectionBootstrap(group: eventLoop) 141 | } 142 | } 143 | #else 144 | let overrideBootstrap : NIOClientTCPBootstrapProtocol? = nil 145 | #endif 146 | #else 147 | let overrideBootstrap : NIOClientTCPBootstrapProtocol? = nil 148 | #endif 149 | 150 | self.bootstrap = overrideBootstrap ?? ClientBootstrap(group: eventLoop) 151 | 152 | _ = bootstrap.channelOption(ChannelOptions.reuseAddr, value: 1) 153 | 154 | _ = bootstrap.channelInitializer { [weak self] channel in 155 | return channel.pipeline 156 | .addHandler(IRCChannelHandler(), name: "de.zeezide.nio.irc.protocol") 157 | .flatMap { [weak self] _ in 158 | guard let me = self else { 159 | let error = channel.eventLoop.makePromise(of: Void.self) 160 | error.fail(Error.internalInconsistency) 161 | return error.futureResult 162 | } 163 | return channel.pipeline 164 | .addHandler(Handler(client: me), 165 | name: "de.zeezide.nio.irc.client") 166 | } 167 | } 168 | } 169 | deinit { 170 | _ = channel?.close(mode: .all) 171 | } 172 | 173 | 174 | // MARK: - Commands 175 | 176 | open func changeNick(_ nick: IRCNickName) { 177 | send(.NICK(nick)) 178 | } 179 | 180 | 181 | // MARK: - Connect 182 | 183 | var retryInfo = IRCRetryInfo() 184 | var channel : Channel? { @inline(__always) get { return state.channel } } 185 | 186 | open func connect() { 187 | guard eventLoop.inEventLoop else { return eventLoop.execute(self.connect) } 188 | 189 | guard state.canStartConnection else { return } 190 | _ = _connect(host: options.hostname ?? "localhost", port: options.port) 191 | } 192 | 193 | private func _connect(host: String, port: Int) -> EventLoopFuture { 194 | assert(eventLoop.inEventLoop, "threading issue") 195 | assert(state.canStartConnection, "cannot start connection!") 196 | 197 | clearListCollectors() 198 | userMode = IRCUserMode() 199 | state = .connecting 200 | 201 | retryInfo.attempt += 1 202 | 203 | return bootstrap.connect(host: host, port: port) 204 | .map { channel in 205 | self.retryInfo.registerSuccessfulConnect() 206 | 207 | guard case .connecting = self.state else { 208 | assertionFailure("called \(#function) but we are not connecting?") 209 | return channel 210 | } 211 | 212 | self.state = .registering(channel: channel, 213 | nick: self.options.nickname, 214 | userInfo: self.options.userInfo) 215 | self._register() 216 | return channel 217 | } 218 | } 219 | 220 | private func _register() { 221 | assert(eventLoop.inEventLoop, "threading issue") 222 | 223 | guard case .registering(_, let nick, let user) = state else { 224 | assertionFailure("called \(#function) but we are not connecting?") 225 | return 226 | } 227 | 228 | if let pwd = options.password { 229 | send(.otherCommand("PASS", [ pwd ])) 230 | } 231 | 232 | send(.NICK(nick)) 233 | send(.USER(user)) 234 | } 235 | 236 | func _closeOnUnexpectedError(_ error: Swift.Error? = nil) { 237 | assert(eventLoop.inEventLoop, "threading issue") 238 | 239 | if let error = error { 240 | self.retryInfo.lastSocketError = error 241 | } 242 | } 243 | 244 | open func close() { 245 | guard eventLoop.inEventLoop else { return eventLoop.execute(close) } 246 | _ = channel?.close(mode: .all) 247 | clearListCollectors() 248 | } 249 | 250 | 251 | // MARK: - Subscriptions 252 | 253 | var subscribedChannels = Set() 254 | 255 | private func _resubscribe() { 256 | if !subscribedChannels.isEmpty { 257 | // TODO: issues JOIN commands 258 | } 259 | 260 | // TODO: we have no queue, right? 261 | // _processQueue() 262 | } 263 | 264 | 265 | // MARK: - Retry 266 | 267 | #if false // TODO: finish Noze port 268 | private func retryConnectAfterFailure() { 269 | let retryHow : IRCRetryResult 270 | 271 | if let cb = options.retryStrategy { 272 | retryHow = cb(retryInfo) 273 | } 274 | else { 275 | if retryInfo.attempt < 10 { 276 | retryHow = .retryAfter(TimeInterval(retryInfo.attempt) * 0.200) 277 | } 278 | else { 279 | retryHow = .stop 280 | } 281 | } 282 | 283 | switch retryHow { 284 | case .retryAfter(let timeout): 285 | // TBD: special Retry status? 286 | if state != .connecting { 287 | state = .connecting 288 | eventLoop.scheduleTask(in: .milliseconds(timeout * 1000.0)) { 289 | self.state = .disconnected 290 | self.connect() 291 | } 292 | } 293 | 294 | case .error(let error): 295 | stop(error: error) 296 | 297 | case .stop: 298 | stop(error: IRCClientError.ConnectionQuit) 299 | } 300 | } 301 | #endif 302 | 303 | func handleRegistrationDone() { 304 | guard case .registering(let channel, let nick, let user) = state else { 305 | assertionFailure("called \(#function) but we are not registering?") 306 | return 307 | } 308 | 309 | state = .registered(channel: channel, nick: nick, userInfo: user) 310 | delegate?.client(self, registered: nick, with: user) 311 | 312 | self._resubscribe() 313 | } 314 | 315 | func handleRegistrationFailed(with message: IRCMessage) { 316 | guard case .registering(_, let nick, _) = state else { 317 | assertionFailure("called \(#function) but we are not registering?") 318 | return 319 | } 320 | // TODO: send to delegate 321 | print("ERROR: registration of \(nick) failed:", message) 322 | 323 | delegate?.clientFailedToRegister(self) 324 | _closeOnUnexpectedError() 325 | } 326 | 327 | 328 | // MARK: - List Collectors 329 | 330 | var messageOfTheDay = "" 331 | 332 | func clearListCollectors() { 333 | messageOfTheDay = "" 334 | } 335 | 336 | 337 | // MARK: - Handler Delegate 338 | 339 | func handlerDidDisconnect(_ context: ChannelHandlerContext) { // Q: own 340 | switch state { 341 | case .error, .quit: break // already handled 342 | case .registering, .connecting: 343 | delegate?.clientFailedToRegister(self) 344 | state = .disconnected 345 | default: 346 | state = .disconnected 347 | } 348 | } 349 | 350 | func handlerHandleResult(_ message: IRCMessage) { // Q: own 351 | if case .registering = state { 352 | if message.command.signalsSuccessfulRegistration { 353 | handleRegistrationDone() 354 | } 355 | 356 | if case .numeric(.errorNicknameInUse, _) = message.command { 357 | print("NEEDS NEW NICK!") 358 | // TODO: recover using a callback 359 | return handleRegistrationFailed(with: message) 360 | } 361 | else if message.command.isErrorReply { 362 | return handleRegistrationFailed(with: message) 363 | } 364 | } 365 | 366 | do { 367 | try irc_msgSend(message) 368 | } 369 | catch let error as IRCDispatcherError { 370 | // TBD: 371 | print("handle dispatcher error:", error) 372 | } 373 | catch { 374 | // TBD: 375 | print("handle generic error:", type(of: error), error) 376 | } 377 | 378 | } 379 | 380 | func handlerCaughtError(_ error: Swift.Error, 381 | in context: ChannelHandlerContext) // Q: own 382 | { 383 | retryInfo.lastSocketError = error 384 | state = .error(.channelError(error)) 385 | 386 | print("IRCClient error:", error) 387 | } 388 | 389 | 390 | // MARK: - Handler 391 | 392 | final class Handler : ChannelInboundHandler { 393 | 394 | typealias InboundIn = IRCMessage 395 | 396 | let client : IRCClient 397 | 398 | init(client: IRCClient) { 399 | self.client = client 400 | } 401 | 402 | func channelActive(context: ChannelHandlerContext) { 403 | } 404 | func channelInactive(context: ChannelHandlerContext) { 405 | client.handlerDidDisconnect(context) 406 | } 407 | 408 | func channelRead(context: ChannelHandlerContext, data: NIOAny) { 409 | let value = unwrapInboundIn(data) 410 | client.handlerHandleResult(value) 411 | } 412 | 413 | func errorCaught(context: ChannelHandlerContext, error: Error) { 414 | self.client.handlerCaughtError(error, in: context) 415 | context.close(promise: nil) 416 | } 417 | } 418 | 419 | 420 | // MARK: - Writing 421 | 422 | public var origin : String? { return nil } 423 | 424 | public func sendMessages(_ messages: T, 425 | promise: EventLoopPromise?) 426 | where T.Element == IRCMessage 427 | { 428 | // TBD: this looks a little more difficult than necessary. 429 | guard let channel = channel else { 430 | promise?.fail(Error.stopped) 431 | return 432 | } 433 | 434 | guard channel.eventLoop.inEventLoop else { 435 | return channel.eventLoop.execute { 436 | self.sendMessages(messages, promise: promise) 437 | } 438 | } 439 | 440 | let count = messages.count 441 | if count == 0 { 442 | promise?.succeed(()) 443 | return 444 | } 445 | if count == 1 { 446 | return channel.writeAndFlush(messages.first!, promise: promise) 447 | } 448 | 449 | guard let promise = promise else { 450 | for message in messages { 451 | channel.write(message, promise: nil) 452 | } 453 | return channel.flush() 454 | } 455 | 456 | EventLoopFuture 457 | .andAllSucceed(messages.map { channel.write($0) }, 458 | on: promise.futureResult.eventLoop) 459 | .cascade(to: promise) 460 | channel.flush() 461 | } 462 | } 463 | 464 | extension ChannelOptions { 465 | 466 | static let reuseAddr = 467 | ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), 468 | SO_REUSEADDR) 469 | 470 | } 471 | 472 | extension IRCCommand { 473 | 474 | var isErrorReply : Bool { 475 | guard case .numeric(let code, _) = self else { return false } 476 | return code.rawValue >= 400 // Hmmm 477 | } 478 | 479 | var signalsSuccessfulRegistration : Bool { 480 | switch self { 481 | case .MODE: return true // Freenode sends a MODE 482 | case .numeric(let code, _): 483 | switch code { 484 | case .replyWelcome, .replyYourHost, .replyMotD, .replyEndOfMotD: 485 | return true 486 | default: 487 | return false 488 | } 489 | 490 | default: return false 491 | } 492 | } 493 | 494 | } 495 | 496 | extension IRCClient : IRCDispatcher { 497 | 498 | public func irc_msgSend(_ message: NIOIRC.IRCMessage) throws { 499 | do { 500 | return try irc_defaultMsgSend(message) 501 | } 502 | catch let error as IRCDispatcherError { 503 | guard case .doesNotRespondTo = error else { throw error } 504 | } 505 | catch { throw error } 506 | 507 | switch message.command { 508 | /* Message of the Day coalescing */ 509 | case .numeric(.replyMotDStart, let args): 510 | messageOfTheDay = (args.last ?? "") + "\n" 511 | case .numeric(.replyMotD, let args): 512 | messageOfTheDay += (args.last ?? "") + "\n" 513 | case .numeric(.replyEndOfMotD, _): 514 | if !messageOfTheDay.isEmpty { 515 | delegate?.client(self, messageOfTheDay: messageOfTheDay) 516 | } 517 | messageOfTheDay = "" 518 | 519 | /* name reply */ 520 | // localhost - 521 | // localhost - 522 | case .numeric(.replyNameReply, _ /*let args*/): 523 | #if false 524 | // messageOfTheDay += (args.last ?? "") + "\n" 525 | #else 526 | break 527 | #endif 528 | case .numeric(.replyEndOfNames, _): 529 | #if false 530 | if !messageOfTheDay.isEmpty { 531 | delegate?.client(self, messageOfTheDay: messageOfTheDay) 532 | } 533 | messageOfTheDay = "" 534 | #else 535 | break 536 | #endif 537 | 538 | case .numeric(.replyTopic, let args): 539 | // :localhost 332 Guest31 #NIO :Welcome to #nio! 540 | guard args.count > 2, let channel = IRCChannelName(args[1]) else { 541 | return print("ERROR: topic args incomplete:", message) 542 | } 543 | delegate?.client(self, changeTopic: args[2], of: channel) 544 | 545 | /* join/part, we need the origin here ... (fix dispatcher) */ 546 | 547 | case .JOIN(let channels, _): 548 | guard let origin = message.origin, let user = IRCUserID(origin) else { 549 | return print("ERROR: JOIN is missing a proper origin:", message) 550 | } 551 | delegate?.client(self, user: user, joined: channels) 552 | 553 | case .PART(let channels, let leaveMessage): 554 | guard let origin = message.origin, let user = IRCUserID(origin) else { 555 | return print("ERROR: JOIN is missing a proper origin:", message) 556 | } 557 | delegate?.client(self, user: user, left: channels, with: leaveMessage) 558 | 559 | /* unexpected stuff */ 560 | 561 | case .otherNumeric(let code, let args): 562 | #if false 563 | print("OTHER NUM:", code, args) 564 | #endif 565 | delegate?.client(self, received: message) 566 | 567 | default: 568 | #if false 569 | print("OTHER COMMAND:", message.command, 570 | message.origin ?? "-", message.target ?? "-") 571 | #endif 572 | delegate?.client(self, received: message) 573 | } 574 | } 575 | 576 | open func doNotice(recipients: [ IRCMessageRecipient ], message: String) 577 | throws 578 | { 579 | delegate?.client(self, notice: message, for: recipients) 580 | } 581 | 582 | open func doMessage(sender : IRCUserID?, 583 | recipients : [ IRCMessageRecipient ], 584 | message : String) throws 585 | { 586 | guard let sender = sender else { // should never happen 587 | assertionFailure("got empty message sender!") 588 | return 589 | } 590 | delegate?.client(self, message: message, from: sender, for: recipients) 591 | } 592 | 593 | open func doNick(_ newNick: IRCNickName) throws { 594 | switch state { 595 | case .registering(let channel, let nick, let info): 596 | guard nick != newNick else { return } 597 | state = .registering(channel: channel, nick: newNick, userInfo: info) 598 | 599 | case .registered(let channel, let nick, let info): 600 | guard nick != newNick else { return } 601 | state = .registered(channel: channel, nick: newNick, userInfo: info) 602 | 603 | default: return // hmm 604 | } 605 | 606 | delegate?.client(self, changedNickTo: newNick) 607 | } 608 | 609 | open func doMode(nick: IRCNickName, add: IRCUserMode, remove: IRCUserMode) 610 | throws 611 | { 612 | guard let myNick = state.nick, myNick == nick else { 613 | return 614 | } 615 | 616 | var newMode = userMode 617 | newMode.subtract(remove) 618 | newMode.formUnion(add) 619 | if newMode != userMode { 620 | userMode = newMode 621 | delegate?.client(self, changedUserModeTo: newMode) 622 | } 623 | } 624 | 625 | open func doPing(_ server: String, server2: String? = nil) throws { 626 | let msg : IRCMessage 627 | 628 | msg = IRCMessage(origin: origin, // probably wrong 629 | command: .PONG(server: server, server2: server)) 630 | sendMessage(msg) 631 | } 632 | } 633 | -------------------------------------------------------------------------------- /Sources/IRC/IRCClientDelegate.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the swift-nio-irc open source project 4 | // 5 | // Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | /** 16 | * Delegate methods called by `IRCClient` upon receiving IRC commands. 17 | */ 18 | public protocol IRCClientDelegate { 19 | 20 | func client(_ client : IRCClient, 21 | registered nick : IRCNickName, 22 | with userInfo : IRCUserInfo) 23 | 24 | func clientFailedToRegister(_ client: IRCClient) 25 | 26 | func client(_ client: IRCClient, received message: IRCMessage) 27 | 28 | func client(_ client: IRCClient, messageOfTheDay: String) 29 | func client(_ client: IRCClient, notice message: String, 30 | for recipients: [ IRCMessageRecipient ]) 31 | func client(_ client: IRCClient, 32 | message: String, from user: IRCUserID, 33 | for recipients: [ IRCMessageRecipient ]) 34 | 35 | func client(_ client: IRCClient, changedUserModeTo mode: IRCUserMode) 36 | func client(_ client: IRCClient, changedNickTo nick: IRCNickName) 37 | 38 | func client(_ client: IRCClient, user: IRCUserID, joined: [ IRCChannelName ]) 39 | func client(_ client: IRCClient, user: IRCUserID, left: [ IRCChannelName ], 40 | with: String?) 41 | 42 | func client(_ client: IRCClient, 43 | changeTopic: String, of channel: IRCChannelName) 44 | } 45 | 46 | 47 | // MARK: - Default No-Op Implementations 48 | 49 | public extension IRCClientDelegate { 50 | 51 | func client(_ client: IRCClient, registered nick: IRCNickName, 52 | with userInfo: IRCUserInfo) {} 53 | func client(_ client: IRCClient, received message: IRCMessage) {} 54 | 55 | func clientFailedToRegister(_ client: IRCClient) {} 56 | 57 | func client(_ client: IRCClient, messageOfTheDay: String) {} 58 | func client(_ client: IRCClient, 59 | notice message: String, 60 | for recipients: [ IRCMessageRecipient ]) {} 61 | func client(_ client: IRCClient, 62 | message: String, from sender: IRCUserID, 63 | for recipients: [ IRCMessageRecipient ]) {} 64 | func client(_ client: IRCClient, changedUserModeTo mode: IRCUserMode) {} 65 | func client(_ client: IRCClient, changedNickTo nick: IRCNickName) {} 66 | 67 | func client(_ client: IRCClient, 68 | user: IRCUserID, joined channels: [ IRCChannelName ]) {} 69 | func client(_ client: IRCClient, 70 | user: IRCUserID, left channels: [ IRCChannelName ], 71 | with message: String?) {} 72 | func client(_ client: IRCClient, 73 | changeTopic: String, of channel: IRCChannelName) {} 74 | } 75 | 76 | -------------------------------------------------------------------------------- /Sources/IRC/IRCClientMessageTarget.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the swift-nio-irc open source project 4 | // 5 | // Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | import NIO 16 | import NIOIRC 17 | 18 | public protocol IRCClientMessageTarget : IRCMessageTarget { 19 | } 20 | 21 | public extension IRCClientMessageTarget { 22 | 23 | func send(_ command: IRCCommand) { 24 | let message = IRCMessage(command: command) 25 | sendMessages([ message ], promise: nil) 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Sources/IRC/IRCClientOptions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the swift-nio-irc open source project 4 | // 5 | // Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | import protocol NIO.EventLoopGroup 16 | import class NIO.MultiThreadedEventLoopGroup 17 | 18 | fileprivate let onDemandSharedEventLoopGroup = 19 | MultiThreadedEventLoopGroup(numberOfThreads: 1) 20 | 21 | /// Configuration options for the socket connects 22 | open class ConnectOptions : CustomStringConvertible { 23 | 24 | public var eventLoopGroup : EventLoopGroup 25 | public var hostname : String? 26 | public var port : Int 27 | 28 | public init(hostname: String? = "localhost", port: Int = 80, 29 | eventLoopGroup: EventLoopGroup? = nil) 30 | { 31 | self.hostname = hostname 32 | self.port = port 33 | self.eventLoopGroup = eventLoopGroup 34 | ?? MultiThreadedEventLoopGroup.currentEventLoop 35 | ?? onDemandSharedEventLoopGroup 36 | } 37 | 38 | public var description: String { 39 | var ms = "<\(type(of: self)):" 40 | appendToDescription(&ms) 41 | ms += ">" 42 | return ms 43 | } 44 | 45 | open func appendToDescription(_ ms: inout String) { 46 | if let hostname = hostname { ms += " \(hostname):\(port)" } 47 | else { ms += " \(port)" } 48 | } 49 | 50 | } 51 | 52 | 53 | import NIOIRC 54 | 55 | public let DefaultIRCPort = 6667 56 | 57 | /// Configuration options for the IRC client object 58 | open class IRCClientOptions : ConnectOptions { 59 | 60 | open var password : String? 61 | open var nickname : IRCNickName 62 | open var userInfo : IRCUserInfo 63 | open var retryStrategy : IRCRetryStrategyCB? 64 | 65 | public convenience init(nick: String) { 66 | self.init(nickname: IRCNickName(nick)!) 67 | } 68 | 69 | public init(port : Int = DefaultIRCPort, 70 | host : String = "localhost", 71 | password : String? = nil, 72 | nickname : IRCNickName, 73 | userInfo : IRCUserInfo? = nil, 74 | eventLoopGroup : EventLoopGroup? = nil) 75 | { 76 | self.password = password 77 | self.nickname = nickname 78 | self.retryStrategy = nil 79 | 80 | self.userInfo = userInfo ?? IRCUserInfo(username: nickname.stringValue, 81 | hostname: host, servername: host, 82 | realname: "NIO IRC User") 83 | 84 | super.init(hostname: host, port: port, eventLoopGroup: eventLoopGroup) 85 | } 86 | 87 | override open func appendToDescription(_ ms: inout String) { 88 | super.appendToDescription(&ms) 89 | ms += " \(nickname)" 90 | ms += " \(userInfo)" 91 | if password != nil { ms += " pwd" } 92 | if retryStrategy != nil { ms += " has-retryStrategy-cb" } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Sources/IRC/IRCRetryStrategyCB.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the swift-nio-irc open source project 4 | // 5 | // Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | import struct Foundation.Date 16 | import struct Foundation.TimeInterval 17 | 18 | /// A callback which defines the connect-retry strategy. 19 | public typealias IRCRetryStrategyCB = ( IRCRetryInfo ) -> IRCRetryResult 20 | 21 | 22 | /// Object passed to the RetryStrategy callback. Contains information on the 23 | /// number of tries etc. 24 | public struct IRCRetryInfo { 25 | 26 | var attempt : Int = 0 27 | var totalRetryTime : Date = Date() 28 | var timesConnected : Int = 0 29 | var lastSocketError : Error? = nil 30 | 31 | mutating func registerSuccessfulConnect() { 32 | self.timesConnected += 1 33 | self.totalRetryTime = Date() 34 | self.lastSocketError = nil 35 | self.attempt = 0 36 | } 37 | } 38 | 39 | public enum IRCRetryResult { 40 | case retryAfter(TimeInterval) 41 | case error(Swift.Error) 42 | case stop 43 | } 44 | 45 | /// This way the callback can do a simple: 46 | /// 47 | /// return 250 48 | /// 49 | /// instead of 50 | /// 51 | /// return .RetryAfter(0.250) 52 | /// 53 | /// To retry after 250ms. Makes it more similar 54 | /// to the original API. 55 | /// 56 | extension IRCRetryResult : ExpressibleByIntegerLiteral { 57 | 58 | public init(integerLiteral value: Int) { // milliseconds 59 | self = .retryAfter(TimeInterval(value) / 1000.0) 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Sources/IRC/ReExports.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the swift-nio-irc open source project 4 | // 5 | // Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | @_exported import protocol NIO.EventLoopGroup 16 | @_exported import enum NIOIRC.IRCCommand 17 | @_exported import struct NIOIRC.IRCMessage 18 | @_exported import struct NIOIRC.IRCNickName 19 | @_exported import struct NIOIRC.IRCChannelName 20 | @_exported import struct NIOIRC.IRCUserInfo 21 | @_exported import enum NIOIRC.IRCMessageRecipient 22 | @_exported import struct NIOIRC.IRCUserMode 23 | @_exported import struct NIOIRC.IRCUserID 24 | --------------------------------------------------------------------------------