You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.adoc 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. = KtIrc {version}
  2. Chris Smith
  3. :version: 0.11.0
  4. :toc: left
  5. :toc-position: left
  6. :toclevels: 5
  7. == About KtIrc
  8. KtIrc is a Kotlin JVM library for connecting to and interacting with IRC servers.
  9. It is still in an early stage of development. Its main features:
  10. .Built for Kotlin
  11. KtIrc is written in and designed for use in Kotlin; it uses extension methods,
  12. DSLs, sealed classes, and so on, to make it much easier to use than an
  13. equivalent Java library.
  14. .Coroutine-powered
  15. KtIrc uses co-routines for all of its input/output which lets it deal with
  16. IRC messages in the background while your app does other things, without
  17. the overhead of creating a new thread per IRC client.
  18. .Modern IRC standards
  19. KtIrc supports many IRCv3 features such as SASL authentication, message IDs,
  20. server timestamps, replies, reactions, account tags, and more. These features
  21. (where server support is available) make it easier to develop bots and
  22. clients, and enhance IRC with new user-facing functionality.
  23. == Getting started
  24. === Installing
  25. All you need to do to start using KtIrc is add a single dependency.
  26. KtIrc is published to JCenter, making it quick and easy to pull in
  27. to almost any project. The examples below show how to add the JCenter
  28. repository and then the KtIrc dependency; you may already be using
  29. JCenter for other dependencies -- in that case just skip the
  30. repository configuration!
  31. [TIP]
  32. ====
  33. KtIrc adheres to semantic versioning: you can expect to upgrade between
  34. minor versions without problems (e.g. from `0.1.2` to `0.13.7`); major
  35. version changes may include breaking changes such as the removal of
  36. deprecated methods. You should check the changelog before updating to
  37. a new major version.
  38. ====
  39. .Gradle (using Kotlin DSL)
  40. [source,kotlin,subs="attributes"]
  41. ----
  42. repositories {
  43. jcenter()
  44. }
  45. dependencies {
  46. implementation("com.dmdirc:ktirc:{version}")
  47. }
  48. ----
  49. .Gradle (using Groovy DSL)
  50. [source,groovy,subs="attributes"]
  51. ----
  52. buildscript {
  53. repositories {
  54. jcenter()
  55. }
  56. }
  57. implementation 'com.dmdirc:ktirc:{version}'
  58. ----
  59. .Maven
  60. [source,xml,subs="attributes"]
  61. ----
  62. <repositories>
  63. <repository>
  64. <id>jcenter</id>
  65. <url>https://jcenter.bintray.com</url>
  66. </repository>
  67. </repositories>
  68. <dependencies>
  69. <dependency>
  70. <groupId>com.dmdirc</groupId>
  71. <artifactId>ktirc</artifactId>
  72. <version>{version}</version>
  73. </dependency>
  74. </dependencies>
  75. ----
  76. === Creating your first client
  77. KtIrc provides a DSL ("domain specific language") for configuring a
  78. client that allows you to set the connection details, the user's
  79. details, and configure the behaviour of KtIrc itself. The DSL is
  80. accessed through the `IrcClient` function. For full details of all
  81. supported options, see the <<IrcClient DSL>> reference.
  82. A basic client will look like this:
  83. [source,kotlin]
  84. ----
  85. val client = IrcClient {
  86. server {
  87. host = "my.server.com"
  88. }
  89. profile {
  90. nickname = "nick"
  91. username = "username"
  92. realName = "Hi there"
  93. }
  94. }
  95. ----
  96. === Connecting and handling events
  97. Getting KtIrc to start connecting is as simple as calling the `connect()`
  98. method, but before that we probably want to add an event listener to deal
  99. with incoming messages:
  100. [source,kotlin]
  101. ----
  102. client.onEvent { event -> <1>
  103. when (event) { <2>
  104. is ServerReady ->
  105. client.sendJoin("#ktirc") <3>
  106. is ServerDisconnected ->
  107. client.connect()
  108. is MessageReceived ->
  109. if (event.message == "!test") <4>
  110. client.reply(event, "Test successful!") <5>
  111. }
  112. }
  113. client.connect() <6>
  114. ----
  115. <1> An event listener is registered using the `onEvent` method. It receives
  116. a single IrcEvent.
  117. <2> A Kotlin `when` statement provides a convenient way to switch on the
  118. type of event received.
  119. <3> Most common IRC commands have `send` methods defined to quickly and
  120. safely send the message with the right formatting.
  121. <4> Kotlin smart-casts the event, so you can access the properties specific
  122. to the matched event class, such as `message`.
  123. <5> The IrcClient class provides useful methods to react and respond to
  124. events.
  125. <6> The connect() method starts connecting and returns immediately. You'll
  126. receive events updating you on the progress.
  127. In this example, we're waiting for three events: `ServerReady`, which occurs
  128. after we have connected and the server has sent us all of the pre-amble
  129. such as its configuration and capabilities; `ServerDisconnected` which
  130. is raised whenever KtIrc gets disconnected from (or fails to connect to) the
  131. IRC server; and `MessageReceived` which occurs, unsuprisingly, whenever a
  132. message is received. KtIrc has many events: for more information, see the
  133. <<Events>> reference.
  134. [CAUTION]
  135. ====
  136. With this code, KtIrc will immediately try to reconnect as soon as it is
  137. disconnected. If the server closes the connection early (due to, for
  138. example, a bad password or the user being banned) this will result in a
  139. huge number of connection attempts in a short time. In real code you should
  140. always delay reconnections -- preferably with a backoff -- to avoid
  141. excessive connection attempts.
  142. ====
  143. You can see that KtIrc provides a number of useful methods for sending
  144. requests to the server, and reacting and responding to events. IRC
  145. commands that KtIrc supports can be invoked using the `send*` methods,
  146. which are documented in the <<Messages>> reference. Other useful methods
  147. such as `reply` can be found in the <<Utility methods>> reference.
  148. == IrcClient DSL
  149. The DSL for creating a new `IrcClient` allows you to set a number of
  150. options relating to how KtIrc connects, what user details it provides,
  151. and how it behaves. The full range of options available in the DSL is
  152. shown below:
  153. [source,kotlin]
  154. ----
  155. server {
  156. host = "irc.example.com"
  157. port = 6667
  158. useTls = true
  159. password = "H4ckTh3Pl4n3t"
  160. }
  161. profile {
  162. nickname = "MyBot"
  163. username = "bot"
  164. realName = "Botomatic v1.2"
  165. }
  166. behaviour {
  167. requestModesOnJoin = true
  168. alwaysEchoMessages = true
  169. }
  170. sasl {
  171. mechanisms += "PLAIN"
  172. username = "botaccount"
  173. password = "s3cur3"
  174. }
  175. ----
  176. === Server settings
  177. The server block allows you to specify the details of the IRC server you
  178. wish to connect to:
  179. * `host` - the hostname or IP address of the server *(required)*
  180. * `port` - the port to connect on _(default: 6667)_
  181. * `useTls` - whether to use a secure connection or not _(default: false)_
  182. * `password` - the password to provide to the server _(default: null)_
  183. An alternative more compact syntax is available for configuring server details:
  184. [source,kotlin]
  185. ----
  186. server("irc.example.com", 6667, true, "H4ckTh3Pl4n3t")
  187. ----
  188. You can, if you wish, combine the two or use named parameters:
  189. [source,kotlin]
  190. ----
  191. server(useTls = true, port = 6697) {
  192. host = "irc.example.com"
  193. password = "H4ckTh3Pl4n3t"
  194. }
  195. ----
  196. === User profile
  197. The user profile controls how KtIrc will present itself to the IRC server, and
  198. how other users on that server will see the KtIrc user:
  199. * `nickname` - the initial nickname you wish to use *(required)*
  200. * `username` - the "username" to provide to the server _(default: KtIrc)_
  201. * `realName` - the "real name" that will be seen by other clients
  202. _(default: KtIrc User)_
  203. [TIP]
  204. ====
  205. The "username" is sometimes called the "ident" or "gecos". Some IRC servers
  206. will check for an ident reply from your host and use that in place of the
  207. username provided if it gets a response. The username (or ident reply)
  208. becomes part of your client's hostmask, and is visible to other users. It
  209. is unrelated to nickserv or other account usernames.
  210. ====
  211. As with the <<Server settings>> you can use a more compact syntax:
  212. [source,kotlin]
  213. ----
  214. profile("nickname", "username", "real name")
  215. ----
  216. === Behaviour
  217. The behaviour block allows you to tweak how KtIrc itself operates. These
  218. options allow you perform common operations automatically, or enjoy more
  219. advanced IRC features even if the server doesn't support them:
  220. * `requestModesOnJoin` - if enabled, automatically requests channel modes
  221. when the client joins a new channel _(default: false)_
  222. * `alwaysEchoMessages` - if enabled, every message you send will result
  223. in a `MessageReceived` event being returned. Servers that support the
  224. IRCv3 `echo-message` capability will do this automatically; enabling the
  225. behaviour will make all servers act the same way _(default: false)_
  226. The behaviour block is optional in its entirety.
  227. === SASL configuration
  228. SASL ("Simple Authentication and Security Layer") is a standard mechanism
  229. for securely authenticating to a service that has recently been adopted
  230. for use in IRC. SASL supports a number of 'mechanisms' that describe how
  231. the data will be exchanged between the client and server. KtIrc supports
  232. the following mechanisms:
  233. * `EXTERNAL` - the server uses some external means to authenticate the
  234. client, instead of a username and password. On most servers this
  235. means checking the client certificate against one registered with
  236. the user's account. _(disabled by default)_
  237. * `PLAIN` - the client sends the username and password in plain text
  238. during the connection phase. This offers slightly more security
  239. than calling `nickserv identify` (for example) after connecting.
  240. * `SCRAM-SHA-1` - this mechanism involves a "salted challenge" being
  241. completed which results in both the server and the client proving that
  242. they know the user's password, but without it every being transmitted.
  243. This is based on the `SHA-1` algorithm which has known issues, but is
  244. more than sufficient when used in this manner.
  245. * `SCRAM-SHA-256` - the same as `SCRAM-SHA-1` but using the `SHA-256`
  246. algorithm instead, which is more modern and secure.
  247. To use `PLAIN`, `SCRAM-SHA-1` or `SCRAM-SHA-256`, you must supply a username
  248. and password in the configuration:
  249. [source,kotlin]
  250. ----
  251. sasl {
  252. username = "botaccount"
  253. password = "s3cur3"
  254. }
  255. ----
  256. KtIrc enables `SCRAM-SHA-256`, `SCRAM-SHA-1` and `PLAIN` by default, and will
  257. use them in that order of preference if the server supports more than one.
  258. You can modify the `mechanisms` parameter if you wish to disable one:
  259. [source,kotlin]
  260. ----
  261. sasl {
  262. mechanisms -= "PLAIN"
  263. username = "botaccount"
  264. password = "s3cur3"
  265. }
  266. ----
  267. You can also clear all the default mechanisms and provide your own list:
  268. [source,kotlin]
  269. ----
  270. sasl {
  271. mechanisms("SCRAM-SHA-256", "PLAIN")
  272. username = "botaccount"
  273. password = "s3cur3"
  274. }
  275. ----
  276. If you wish to enable the `EXTERNAL` mechanism, you do not need to provide
  277. a username or password:
  278. [source,kotlin]
  279. ----
  280. sasl {
  281. mechanisms("EXTERNAL")
  282. }
  283. ----
  284. Alternatively, if you wish to enable `EXTERNAL` but fall back to other
  285. mechanisms if it doesn't work:
  286. [source,kotlin]
  287. ----
  288. sasl {
  289. mechanisms += "EXTERNAL"
  290. username = "botaccount"
  291. password = "s3cur3"
  292. }
  293. ----
  294. The SASL block is optional in its entirety.
  295. == State
  296. KtIrc attempts to track all reasonable state of the IRC network. This includes
  297. details about the server, channels the client is joined to, and users that are
  298. also in those channels. The state is exposed in a several fields accessible
  299. from the `IrcClient`:
  300. === ServerState
  301. The server state provides information about the server, and our connection to
  302. it.
  303. [IMPORTANT]
  304. ====
  305. The server state will be updated frequently while KtIrc is connecting to a
  306. server. The values within it should not be relied upon until a `ServerReady`
  307. event is received, as they may be incomplete or estimates before then.
  308. ====
  309. .serverState.status (ServerStatus)
  310. Provides an enum containing the current server state. One of:
  311. * `Disconnected` - the server is not connected
  312. * `Connecting` - we are attempting to establish a connection
  313. * `Negotiating` - we are logging in, negotiating capabilities, etc
  314. * `Ready` - we are connected and commands may be sent
  315. .serverState.localNickname (String)
  316. The current nickname we are using on the IRC server. While connecting this
  317. will default to the nickname from the <<User profile>>, but it may be updated
  318. if e.g. the nick is in use or not allowed.
  319. .serverState.serverName (String)
  320. The name the server uses for itself. While connecting this defaults to the
  321. hostname given in the <<Server settings>>, but it will be updated to the
  322. value provided by the server. For example, you may connect to
  323. `irc.example.com` and during the negotiation phase KtIrc will see that it
  324. is actually talking to `server3.uk.irc.example.com` and update the
  325. serverName to reflect that.
  326. [TIP]
  327. ====
  328. For a user-friendly identifier most servers provide a `NETWORK` token in
  329. the ISUPPORT reply, which is available via the <<Features>> property.
  330. ====
  331. .serverState.channelModePrefix (ModePrefixMapping)
  332. Provides a mapping from channel user modes (such as "o" for op, "v" for
  333. voice) to the prefixes used before nicknames (such as "@" and "+").
  334. To map prefixes to modes, you can use the `getMode()` or `getModes()`
  335. functions:
  336. [source,kotlin]
  337. ----
  338. getMode('@') == 'o'
  339. getModes("@+") == "ov"
  340. ----
  341. .serverState.channelTypes (String)
  342. Contains the types of channels that are allowed by the server, such as
  343. `\#&amp;` for normal channels ("#") and local channels ("&").
  344. ==== Capabilities
  345. The IRCv3 specifications introduce the concept of 'capability negotiation'.
  346. This allows the client and server to negotiate and enable new capabilities
  347. that are mutually supported.
  348. The capabilities state contains the following properties:
  349. .serverState.capabilities.negotiationState (CapabilitiesNegotiationState)
  350. The current state of negotiating with the server. One of:
  351. * `AWAITING_LIST` - we have requested a list of capabitilies and are awaiting
  352. a reply
  353. * `AWAITING_ACK` - we have sent the capabilities we want to enable, and are
  354. waitin for the server to acknowledge them
  355. * `AUTHENTICATING` - we are attempting to authenticate with SASL
  356. * `FINISHED` - we have completed negotiation
  357. Where a server does not support IRCv3 capability negotiation, the state will
  358. remain at `AWAITING_LIST`.
  359. .serverState.capabilities.advertisedCapabilities (Map<String, String>)
  360. Contains a map of capability names to values that the server offered. This
  361. should only be required for advance use cases, such as looking up the
  362. languages offered by a server when providing the user with a choice of
  363. translations.
  364. .serverState.capabilities.enabledCapabilities (Map<Capability, String>)
  365. Contains a map of capabilities that KtIrc has successfully negotiated with
  366. the server.
  367. ===== Supported capabilities
  368. * `sasl` - used to perform SASL authentication during connection
  369. * `message-tags` - allows arbitrary tags on messages
  370. * `server-time` - the server adds a timestamp tag to each incoming message
  371. * `account-tag` - the server adds an account tag to incoming user messages
  372. * `userhost-in-names` - the NAMES reply includes users hosts not just nicknames
  373. * `multi-prefix` - all modes are included in nick prefixes (e.g. `@+nick`)
  374. * `extended-join` - more information is sent when a user joins a channel
  375. * `batch` - allows multi-line responses to be batched together
  376. * `echo-message` - echos the client's own messages back to it
  377. * `draft/labeled-responses` - responses are labeled so the client knows which
  378. incoming message corresponds to which command it sent
  379. * `account-notify` - the server sends a message when a user's account changes
  380. * `away-notify` - the server sends a message when a user's away state changes
  381. * `chghost` - the server sends a message when a user's host changes
  382. ==== Features
  383. Features are KtIrc's way of exposing the information the server declares in
  384. its ISUPPORT messages. These describe how the server is configured, and what
  385. limits are placed on clients. You access features using the `features` map
  386. in the server state:
  387. [source,kotlin]
  388. ----
  389. ircClient.serverState.features[ServerFeature.Network]
  390. ----
  391. The following features are available:
  392. * `Network` - the name of the network the server belongs to __(String?)__
  393. * `ServerCaseMapping` - the current case mapping of the server __(CaseMapping!)__
  394. * `Modeprefixes` - the user mode prefix mapping (e.g. ov to @+) __(ModePrefixMapping!)__
  395. * `MaximumChannels` - the maximum number of channels a user can join __(Int?)__
  396. * `ChannelModes` - the modes supported in channels __(Array<String>?)__
  397. * `ChannelTypes` - the types of channel supported (e.g. "#&") __(String!)__
  398. * `MaximumChannelNameLength` - how long channel names may be __(Int!)__
  399. * `WhoxSupport` - whether the server supports extended whos ("WHOX") __(Boolean!)__
  400. [NOTE]
  401. ====
  402. If the server does not define a feature, KtIrc will either fall back to a
  403. default value based on the IRC RFCs or common practice (for those features
  404. identified with a non-null type such as `Int!` or `String!`); otherwise
  405. the value of the feature will be `null` (such as for those identified as
  406. `Int?` or `String?` types).
  407. ====
  408. === UserState
  409. The client's UserState object tracks the details of all users in common
  410. channels. It can be used to find the most up-to-date and comprehensive
  411. information for those users, as well as the set of channels that we share
  412. with them.
  413. The UserState is accessed via the `userState` property of IrcClient and
  414. acts as a map, accessible using either a nickname or a `User` object:
  415. [source,kotlin]
  416. ----
  417. ircClient.userState["acidBurn"]
  418. val user: User = myIrcEvent.user
  419. ircClient.userState[user]
  420. ----
  421. The UserState returns a `KnownUser` object which exposes a `details`
  422. property containing the user details, and a `channels` property
  423. containing the common channel names. You can also use the `in`
  424. operator to check if the user is in a channel:
  425. [source,kotlin]
  426. ----
  427. ircClient.userState["acidBurn"]?.let { knownUser -> <1>
  428. val accountName = knownUser.account
  429. val inChannel = "#channel" in knownUser <2>
  430. val allChannels = knownUser.channels <3>
  431. }
  432. ----
  433. <1> If the user isn't known, the call to `get` (using the `[]` operator)
  434. returns null, so we use a `let` statement to deal only with the case
  435. that the user is found.
  436. <2> Check if the user is present on the common channel `#channel`. If
  437. the KtIrc client is not joined to that channel, it will always return
  438. false. You can also use the `contains("#channel")` method instead of
  439. the `in` operator.
  440. <3> Returns all common channels we share with the user; will never
  441. include channels that the KtIrc client is not joined to.
  442. === ChannelState
  443. The ChannelState keeps track of the state for all channels that the client
  444. is joined to. It is indexed by channel name:
  445. [source,kotlin]
  446. ----
  447. ircClient.channelState["#ktirc"]
  448. ----
  449. Each channel's state contains the following properties:
  450. * `receivingUserList` - boolean value indicating whether we are in the process
  451. of receiving the list of users for the channel. If we are, the `users`
  452. property will be incomplete.
  453. * `modesDiscovered` - boolean value indicating whether we have received the
  454. full set of modes set on the channel. The `requestModesOnJoin` <<Behaviour>>
  455. allows you to make KtIrc request these automatically.
  456. * `topic` - a ChannelTopic object representing the current channel topic.
  457. If no topic is set, then a ChannelTopic with `null` properties will be
  458. provided.
  459. * `users` - a map of all known users in the channel, see <<Channel users>>
  460. for more information
  461. * `modes` - A map of the current channel modes and their values. Only
  462. complete if `modesDiscovered` is true.
  463. ==== Channel users
  464. Channel users are accessed using the `users` property, which provides an
  465. iterable map of nickname to `ChannelUser`. Each `ChannelUser` contains
  466. the nickname and current modes for that user. To get further details about
  467. a user, such as their hostmask or real name, you should query the <<UserState>>
  468. with the given nickname.
  469. [source,kotlin]
  470. ----
  471. ircClient.channelState["#ktirc"]?.users?.forEach { user ->
  472. println("${user.nickname} has modes ${user.modes}")
  473. }
  474. ----
  475. == Events
  476. Incoming lines from the IRC server are converted by KtIrc to subclasses of
  477. `IrcEvent`. These, along with other more advance events, are then published
  478. to users of the client using the `onEvent` method in `IrcClient`.
  479. All events extend `IrcEvent`, which offers a single `metadata` property.
  480. This contains details related to the event:
  481. * `time` - the time at which the message occurred (if the server supports
  482. the `server-time` capability), or the time at which we received it.
  483. Always present.
  484. * `batchId` - an opaque string identifier for the batch the message is
  485. part of (if the server supports the `batch` capability). Null for
  486. messages not in a batch.
  487. * `messageId` - a unique, opaque string identifier for the message if
  488. the server supports the `msgid` tag. Null otherwise.
  489. * `label` - a unique, opaque string identifier that ties a message to
  490. a labelled command that was sent by KtIrc, if the server supports
  491. the `labelled-replies` capability. Null otherwise.
  492. Several specialised versions of `IrcEvent` are used which allow for easier
  493. processing:
  494. .TargetedEvent
  495. A `TargetedEvent` is one that is targeted at either a user or a channel.
  496. `TargetedEvent` exposes a string `target` property that identifies the
  497. target of the message. This allows you to direct messages to the right
  498. handler or UI component more easily:
  499. [source,kotlin]
  500. ----
  501. ircClient.onEvent { event ->
  502. when (event) {
  503. is TargetedEvent -> dispatchEvent(event.target, event)
  504. }
  505. }
  506. ----
  507. .SourcedEvent
  508. A large number of events come from a remote IRC user, and it can be
  509. useful to handle these in the same way. KtIrc offers a `SourcedEvent`
  510. interface for all events that originate from a user, and it exposes
  511. a single `user` property:
  512. [source,kotlin]
  513. ----
  514. ircClient.onEvent { event ->
  515. when (event) {
  516. is SourcedEvent -> notifyAboutUserActivity(event.user)
  517. }
  518. }
  519. ----
  520. .ChannelMembershipAdjustment
  521. A number of events describe how the membership of a channel changes --
  522. namely, joins, parts, quits, kicks, names replies, and nick changes.
  523. All of these events implement the `ChannelMembershipAdjustment` interface
  524. which reduces the amount of logic you need to do if you wish to maintain
  525. a membership list (for example in a UI). The interface exposes three
  526. properties:
  527. * `addedUser` - a single nickname to be added _(String)_
  528. * `removedUser` - a single nickname to be removed _(String)_
  529. * `replacedUsers` - a list of nicknames to replace any existing ones with
  530. _(Array<String>)_
  531. All the properties are nullable, and most events will only populate
  532. one of the three.
  533. === Server events
  534. ==== ServerConnecting
  535. * Type: IrcEvent
  536. * Properties: _(none)_
  537. This event is raised by KtIrc as soon as it starts attempting to connect to
  538. a server. It will be followed by either a <<ServerConnected>> or a
  539. <<ServerConnectionError>> event at some point.
  540. ==== ServerConnected
  541. * Type: IrcEvent
  542. * Properties: _(none)_
  543. This event is raised by KtIrc when it has connected to the server, and is
  544. starting the process of registering, negotiating capabilities, etc.
  545. The server will *not* yet be ready for use - a <<ServerReady>> event will
  546. follow once all of the initial setup has completed.
  547. ==== ServerConnectionError
  548. * Type: IrcEvent
  549. * Properties:
  550. ** `error`: `ConnectionError` - the type of error that occurred
  551. ** `details`: `String?` - information about the error, if available
  552. This event is raised by KtIrc when a problem occurred while connecting
  553. to the server. The `ConnectionError` enum will provide the cause of
  554. the error, if known:
  555. * `UnresolvableAddress` - the hostname provided could not be resolved
  556. to an IP address
  557. * `ConnectionRefused` - the server did not answer a connection request
  558. on the given port
  559. * `BadTlsCertificate` - there was an issue with the TLS certificate the
  560. server presented (e.g. it was out of date, for the wrong domain, etc)
  561. * `Unknown` - the exact cause of the error isn't known
  562. This event will be followed by a <<ServerDisconnected>> event.
  563. ==== ServerWelcome
  564. * Type: IrcEvent
  565. * Properties:
  566. ** `server`: `String` - the name the server supplied for itself
  567. ** `localNick`: `String` - the nickname the server says we are using
  568. This event is raised in response to the server sending a 001 WELCOME
  569. message. It contains the name that the server supplied for itself
  570. (for example, KtIrc may connect to a round-robin address like
  571. `irc.example.com` and the server it actually connects to then
  572. identifies itself as `node3.uk.irc.example.com`), and the nickname
  573. that the server says we are using.
  574. ==== ServerReady
  575. * Type: IrcEvent
  576. * Properties: _(none)_
  577. This event is raised by KtIrc when it has connected to a server,
  578. registered with the IRC network, and received all of the server's
  579. initial data describing its configurations and its features.
  580. At this point it is safe to start issuing commands, checking
  581. state, joining channels, etc.
  582. ==== ServerDisconnected
  583. * Type: IrcEvent
  584. * Properties: _(none)_
  585. Raised in all cases where KtIrc has attempted to connect to an IRC server and
  586. has now been disconnected. KtIrc will not automatically attempt to reconnect;
  587. the `connect()` method should be called again after an appropriate delay.
  588. NOTE: All of KtIrc's internal state, such as details about users and
  589. channels, will be reset when disconnected from the server. State should not
  590. be queried until a new <<ServerReady>> event has been received, at which
  591. point it will have been recreated.
  592. ==== MotdLineReceived
  593. * Type: IrcEvent
  594. * Properties:
  595. ** `line`: `String` - the line of the message of the day that was received
  596. ** `first`: `Boolean` - true if the line is the first one received
  597. The MotdLineReceived event is raised whenever the server sends a single
  598. line of its Message of the Day. The `first` parameter is set on the
  599. first line of the MOTD so that special formatting or UI handling can
  600. be applied. When the MOTD is finished, a <<MotdFinished>> event is raised.
  601. ==== MotdFinished
  602. * Type: IrcEvent
  603. * Properties:
  604. ** `missing`: `Boolean` - indicates the MOTD was missing
  605. This event occurs in two circumstances: when the server has sent a
  606. series of <<MotdLineReceived>> events and has reached the end of the
  607. Message of the Day; or when the server has no MOTD to send and
  608. informs the client that the MOTD is missing.
  609. === Channel events
  610. NOTE: Many events such as <<MessageReceived>> apply to both channels and
  611. users. These are documented in the <<Channel/User events>> category.
  612. ==== ChannelJoined
  613. * Type: IrcEvent, TargetedEvent, SourcedEvent, ChannelMembershipAdjustment
  614. * Properties:
  615. ** `user`: `User` - the user that joined the channel
  616. ** `target`: `String` - the channel that was joined
  617. Raised whenever a user joins a channel, including the KtIrc client. You
  618. can determine whether the join applies to another user or the local client
  619. using the <<isLocalClient>> utility method.
  620. When the local client joins a new channel, this event will typically be
  621. followed by one or more <<ChannelNamesReceived>> events, then
  622. <<ChannelNamesFinished>>, <<ChannelTopicDiscovered>> and if the
  623. `requestModesOnJoin` <<Behaviour>> is enabled a <<ModeChanged>> event.
  624. ==== ChannelJoinFailed
  625. * Type: IrcEvent, TargetedEvent
  626. * Properties:
  627. ** `target`: `String` - the channel that we tried to join
  628. ** `reason`: `JoinError` - the error that prevented us from joining
  629. The ChannelJoinFailed event is raised when we attempt to join a channel
  630. but the server doesn't allow us to do so. The reason parameter enumerates
  631. the possible problems:
  632. * `TooManyChannels` - we are already in the maximum number of channels allowed
  633. by the server.
  634. * `NoHiding` - the channel is no-hiding (+H), but we have invisible join/parts
  635. enabled.
  636. * `NeedKey` - the channel is keyed (+k) and a valid key was not provided
  637. * `NeedInvite` - the channel is invite only (+i) and no invite was received.
  638. * `NeedRegisteredNick` - the channel is limited to registered users only, and we
  639. are not registered.
  640. * `NeedTls` - the channel is secure-only, and we're not using TLS.
  641. * `NeedAdmin` - the channel is limited to server admins and we are not one.
  642. * `NeedOper` - the channel is limited to ircops and we are not one.
  643. * `Banned` - we are banned from the channel.
  644. * `ChannelFull` - the channel is limited (+l) and currently full.
  645. * `BadChannelName` - the channel name is disallowed by the server.
  646. * `Throttled` - we're trying to joiin too many channels and have been throttled.
  647. * `Unknown` - we don't know why.
  648. [WARNING]
  649. ====
  650. ChannelJoinFailed events are generated on a _best-effort_ basis by KtIrc. Error
  651. handling on IRC is very poorly standardised, and varies wildly between server
  652. implementations. For example, trying to join a secure-only channel on an
  653. ircd-seven server will send a NOTICE to the user instead of an error response,
  654. so no `ChannelJoinFailed` event will be raised.
  655. When tracking whether a join suceeded or failed you should combine monitoring
  656. for the response with a reasonable timeout, and assume failure if the timeout
  657. lapses without a <<ChannelJoined>> or <<ChannelJoinFailed>> event occurring.
  658. ====
  659. ==== ChannelParted
  660. * Type: IrcEvent, TargetedEvent, SourcedEvent, ChannelMembershipAdjustment
  661. * Properties:
  662. ** `user`: `User` - the user that parted the channel
  663. ** `target`: `String` - the channel that was parted
  664. ** `reason`: `String` - the user-supplied reason for parting
  665. Raised when any user parts a channel that we are on. Users can supply a reason
  666. when parting a channel; if they have done so the `reason` property will be
  667. non-empty.
  668. ==== ChannelUserKicked
  669. * Type: IrcEvent, TargetedEvent, SourcedEvent, ChannelMembershipAdjustment
  670. * Properties:
  671. ** `user`: `User` - the user that performed the kick
  672. ** `victim`: `String` - the nickname of the user that was kicked
  673. ** `target`: `String` - the channel that the victim was kicked from
  674. ** `reason`: `String` - the user-supplied reason for kicking
  675. This event occurs when a user is kicked (forcibly removed) from a channel.
  676. NOTE: The `user` is the one performing the kick, and will remain in the
  677. channel. The `victim` is the one being forcibly ejected.
  678. ==== ChannelQuit
  679. * Type: IrcEvent, TargetedEvent, SourcedEvent, ChannelMembershipAdjustment
  680. * Properties:
  681. ** `user`: `User` - the user that quit
  682. ** `target`: `String` - the channel that the user was in
  683. ** `reason`: `String` - the user-supplied reason for quitting
  684. After a <<UserQuit>> event, KtIrc will "fan out" the event to all of the
  685. channels that we share with the user and raise a `ChannelQuit` event for
  686. each channel. This is designed to make implementing certain features easier;
  687. if you fully handle a UserQuit event there is no need to also handle the
  688. ChannelQuit events, and vice-versa.
  689. Users and servers can supply a reason when a user quits; if supplied then
  690. the `reason` parameter will be non-empty.
  691. ==== ChannelNickChanged
  692. * Type: IrcEvent, TargetedEvent, SourcedEvent, ChannelMembershipAdjustment
  693. * Properties:
  694. ** `user`: `User` - the user who has changed their nickname
  695. ** `target`: `String` - the channel that the user is in
  696. ** `newNick`: `String` - the user's new nickname
  697. After a <<UserNickChanged>> event, KtIrc will "fan out" the event to
  698. all of the channels that we share with the user and raise a `ChannelNickChanged`
  699. event for each channel. This is designed to make implementing certain features
  700. easier; if you fully handle a UserNickChanged event there is no need to also
  701. handle the ChannelNickChanged events, and vice-versa.
  702. TIP: The user property will contain the user's old details, but you will
  703. not be able to access additional information from the <<UserState>> using
  704. these details as KtIrc will have internally renamed the user to use the
  705. new nickname.
  706. ==== ChannelNamesReceived
  707. * Type: IrcEvent, TargetedEvent
  708. * Properties:
  709. ** `target`: `String` - the channel that the user is in
  710. ** `names`: `List<String>` - the partial list of names that are in the channel
  711. When we join a channel (or manually request it) the IRC server sends the
  712. list of channel members in a sequence of NAMES messages. KtIrc raises a
  713. `ChannelNamesReceived` event for each of these messages.
  714. WARNING: The given names may not be a complete list of members of the channel,
  715. as more names could follow. The format of the names varies between IRC servers
  716. and depending on the IRCv3 <<Capabilities>> that KtIrc negotiated. Most
  717. implementations should simply wait for <<ChannelNamesFinished>> and then request
  718. the complete list of names from KtIrc's <<ChannelState>>.
  719. ==== ChannelNamesFinished
  720. * Type: IrcEvent, TargetedEvent, ChannelMembershipAdjustment
  721. * Properties:
  722. ** `target`: `String` - the channel whose names response has finished
  723. Raised when the IRC server has finished receiving all of the names of users
  724. that are currently in a channel. At this point you can query the channel's
  725. <<ChannelState>> to get a detailed list of members.
  726. ==== ChannelTopicDiscovered
  727. * Type: IrcEvent, TargetedEvent
  728. * Properties:
  729. ** `target`: `String` - the channel whose topic was discovered
  730. ** `topic`: `String?` - the topic in the channel, if any
  731. `ChannelTopicDiscovered` occurs when we join a channel (or manually request
  732. that the server repeats the current topic) and contains the current channel
  733. topic. If there is no topic set, the `topic` parameter will be `null`.
  734. Metadata about the topic, such as who set it and when, is contained in the
  735. <<ChannelTopicMetadataDiscovered>> event which should follow this one, if
  736. the topic was set.
  737. ==== ChannelTopicMetadataDiscovered
  738. * Type: IrcEvent, TargetedEvent
  739. * Properties:
  740. ** `target`: `String` - the channel whose topic metadata was discovered
  741. ** `user`: `User` - the user who set the topic
  742. ** `setTime`: `LocalDateTime` - the time at which the topic was set
  743. Provides meta-data relating to a topic that was previously set on the
  744. channel.
  745. NOTE: The given user may not exist on the network any more, or may have
  746. changed details since the topic was set. You should not expect to be able
  747. to look up the user's details in the <<UserState>>, or interact with them
  748. directly on IRC.
  749. ==== ChannelTopicChanged
  750. * Type: IrcEvent, TargetedEvent, SourcedEvent
  751. * Properties:
  752. ** `user`: `User` - the user who has changed the topic
  753. ** `target`: `String` - the channel that the topic was changed in
  754. ** `topic`: `String?` - the channel's new topic
  755. Raised when a user changes the topic of a channel we are joined to. If
  756. the topic was cleared/removed, the `topic` parameter will be `null`.
  757. === Channel/User events
  758. TODO
  759. ==== MessageReceived
  760. TODO
  761. ==== NoticeReceived
  762. TODO
  763. ==== ActionReceived
  764. TODO
  765. ==== CtcpReceived
  766. TODO
  767. ==== CtcpReplyReceived
  768. TODO
  769. ==== UserQuit
  770. TODO
  771. ==== UserNickChanged
  772. TODO
  773. ==== UserHostChanged
  774. TODO
  775. ==== UserAccountChanged
  776. TODO
  777. ==== ModeChanged
  778. TODO
  779. === Other events
  780. ==== PingReceived
  781. * Type: IrcEvent
  782. * Properties:
  783. ** `nonce`: `ByteArray` - the unique data that must be included in the reply
  784. Raised when the IRC server sends a PING message to the client. KtIrc will
  785. automatically reply with an appropriate PONG.
  786. ==== ServerFeaturesUpdated
  787. * Type: IrcEvent
  788. * Properties:
  789. ** `serverFeatures`: `ServerFeatureMap` - the features supplied by the server
  790. Corresponds to the server sending a single 005 ISUPPORT line. Multiple
  791. events of this type may be raised in quick succession when features are
  792. split over multiple lines.
  793. In general, you should wait for a <<ServerReady>> event and then query the
  794. <<Features>> instead of relying on this event.
  795. ==== ServerCapabilitiesReceived
  796. TODO
  797. ==== ServerCapabilitiesAcknowledged
  798. TODO
  799. ==== ServerCapabilitiesFinished
  800. TODO
  801. ==== AuthenticationMessage
  802. TODO
  803. ==== SaslFinished
  804. TODO
  805. ==== SaslMechanismNotAvailableError
  806. TODO
  807. ==== BatchStarted
  808. TODO
  809. ==== BatchFinished
  810. TODO
  811. ==== BatchReceived
  812. TODO
  813. ==== NicknameChangeFailed
  814. TODO
  815. == Messages
  816. TODO
  817. == Utility methods
  818. TODO
  819. === isLocalClient
  820. TODO
  821. === react
  822. TODO
  823. === reply
  824. TODO
  825. == IRCv3 support
  826. The following table shows KtIrc's IRCv3 support as of this release:
  827. [cols=3,options="header"]
  828. |===
  829. | Feature
  830. | Status
  831. | Notes
  832. 3+h| Capability negotiation
  833. | https://ircv3.net/specs/core/capability-negotiation.html[CAP]
  834. | Supported
  835. | See <<Supported capabilities>> for the caps KtIrc will negotiate
  836. | https://ircv3.net/specs/core/capability-negotiation.html#cap-ls-version[CAP 302]
  837. | Supported
  838. | See <<Supported capabilities>> for the caps KtIrc will negotiate
  839. | https://ircv3.net/specs/core/capability-negotiation.html#cap-notify[cap-notify]
  840. 2+| Not yet supported
  841. 3+h| Published specifications
  842. | https://ircv3.net/specs/extensions/account-notify-3.1.html[account-notify] v3.1
  843. | Supported
  844. | See <<UserAccountChanged>>
  845. | https://ircv3.net/specs/extensions/account-tag-3.2.html[account-tag] v3.2
  846. | Supported
  847. | Accounts are automatically added to `User` properties in events
  848. | https://ircv3.net/specs/extensions/away-notify-3.1.html[away-notify] v3.1
  849. | Partial support
  850. | Negotiated but not yet exposed as an event
  851. | https://ircv3.net/specs/extensions/batch-3.2.html[batch] v3.2
  852. | Supported
  853. | See <<BatchReceived>>
  854. | https://ircv3.net/specs/extensions/chghost-3.2.html[chghost] v3.2
  855. | Supported
  856. | See <<UserHostChanged>>
  857. | https://ircv3.net/specs/extensions/echo-message-3.2.html[echo-message] v3.2
  858. | Supported
  859. | See also the `alwaysEchoMessages` <<Behaviour>>
  860. | https://ircv3.net/specs/extensions/extended-join-3.1.html[extended-join] v3.1
  861. | Supported
  862. | Additional details are automatically added to `User` properties in events
  863. | https://ircv3.net/specs/extensions/invite-notify-3.2.html[invite-notify] v3.2
  864. 2+| Not yet supported
  865. | https://ircv3.net/specs/extensions/message-tags.html[message-tags]
  866. | Supported
  867. | Exposed in the metadata property of <<Events>>
  868. | https://ircv3.net/specs/core/monitor-3.2.html[monitor]
  869. 2+| Not yet supported
  870. | https://ircv3.net/specs/extensions/multi-prefix-3.1.html[multi-prefix] v3.1
  871. | Supported
  872. | Automatically included in <<ChannelState>>
  873. | https://ircv3.net/specs/extensions/sasl-3.1.html[SASL] v3.1
  874. | Supported
  875. | See <<SASL configuration>>
  876. | https://ircv3.net/specs/extensions/sasl-3.2.html[SASL] v3.2
  877. | Partial support
  878. | Notifications via `cap-notify` not yet supported. See <<SASL configuration>>
  879. | https://ircv3.net/specs/extensions/server-time-3.2.html[server-time] v3.2
  880. | Supported
  881. | Exposed in the metadata property of <<Events>>
  882. | https://ircv3.net/specs/extensions/sts.html[sts]
  883. 2+| Not yet supported
  884. | https://ircv3.net/specs/extensions/userhost-in-names-3.2.html[userhost-in-names] v3.2
  885. | Supported
  886. | Automatically included in <<UserState>>
  887. | https://ircv3.net/specs/extensions/webirc.html[webirc]
  888. 2+| Not yet supported
  889. 3+h| Draft specifications
  890. | https://github.com/ircv3/ircv3-specifications/pull/363[brb]
  891. 2+| Not yet supported
  892. | https://github.com/ircv3/ircv3-specifications/pull/308[channel renaming]
  893. 2+| Not yet supported
  894. | https://github.com/ircv3/ircv3-specifications/pull/349[chathistory]
  895. 2+| Not yet supported
  896. | https://github.com/ircv3/ircv3-specifications/pull/346[delivered]
  897. 2+| Not yet supported
  898. | https://github.com/ircv3/ircv3-specifications/pull/304[editmsg]
  899. 2+| Not yet supported
  900. | https://ircv3.net/specs/extensions/labeled-response.html[labeled-response]
  901. | Supported
  902. | Exposed in the metadata property of <<Events>>
  903. | https://ircv3.net/specs/extensions/message-ids.html[message-ids]
  904. 2+| Not yet supported
  905. | https://github.com/ircv3/ircv3-specifications/pull/330[migrate]
  906. 2+| Not yet supported
  907. | https://ircv3.net/specs/client-tags/react.html[react]
  908. | Partial support
  909. | Sending via <<react>> method, no events generated
  910. | https://github.com/ircv3/ircv3-specifications/pull/347[read]
  911. 2+| Not yet supported
  912. | https://github.com/ircv3/ircv3-specifications/pull/276[register]
  913. 2+| Not yet supported
  914. | https://ircv3.net/specs/client-tags/reply.html[reply]
  915. | Partial support
  916. | Sending via <<reply>> method, not processed on incoming messages
  917. | https://github.com/ircv3/ircv3-specifications/pull/306[resume]
  918. 2+| Not yet supported
  919. | https://github.com/ircv3/ircv3-specifications/pull/361[setname]
  920. 2+| Not yet supported
  921. | https://github.com/ircv3/ircv3-specifications/pull/357[standard replies]
  922. 2+| Not yet supported
  923. | https://github.com/ircv3/ircv3-specifications/pull/348[typing]
  924. 2+| Not yet supported
  925. 3+^h|Vendor specifications
  926. |===