|
| 1 | +# Challenge 16 - Write Your Own IRC Client |
| 2 | + |
| 3 | +This challenge corresponds to the sixteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-irc. |
| 4 | + |
| 5 | +## Table of contents |
| 6 | + |
| 7 | +- [Description](#description) |
| 8 | +- [Usage](#usage) |
| 9 | +- [Run tests](#run-tests) |
| 10 | +- [TODOs](#todos) |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +The IRC client is written using the `net` module of Node.js. |
| 15 | +You can find more about the IRC protocol from https://datatracker.ietf.org/doc/html/rfc2812. |
| 16 | + |
| 17 | +- `command-types.ts`: Command types returned by server and supported by client |
| 18 | +- `parser.ts`: A parser class that parses the data sent by server into a more usable message interface |
| 19 | +- `types.ts`: All the interface definitions are present in this file |
| 20 | +- `utils.ts`: Utility functions used by IRC client |
| 21 | +- `irc-client.ts`: The main IRC Client login is written here. |
| 22 | +- `irc-client.index.ts`: A command line interface for IRC Client to interact with the server |
| 23 | + |
| 24 | +The IRC Client supports file based logging via [winston](https://www.npmjs.com/package/winston) |
| 25 | + |
| 26 | +The following commands are supported by the IRC Client: |
| 27 | + |
| 28 | +- JOIN |
| 29 | +- PART |
| 30 | +- NICK |
| 31 | +- PRIVMSG |
| 32 | +- QUIT |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +To use the IRC Client command line interface, you can update the following variables present in `irc-client.index.ts` and use the `ts-node` command to start the client. |
| 37 | + |
| 38 | +```typescript |
| 39 | +const host = 'irc.freenode.net'; |
| 40 | +const port = 6667; |
| 41 | +const nickName = 'MJ'; // Use you nickname |
| 42 | +const fullName = 'Mohit Jain'; // Use your full name |
| 43 | +const debug = true; // Enable winston logging |
| 44 | +``` |
| 45 | + |
| 46 | +```bash |
| 47 | +npx ts-node irc-client.index.ts |
| 48 | +``` |
| 49 | + |
| 50 | +The following commands are supported by the IRC Client: |
| 51 | + |
| 52 | +```bash |
| 53 | +# Connect to server |
| 54 | +client>connect |
| 55 | + |
| 56 | +# Join a channel |
| 57 | +client>/join <channel-name> |
| 58 | + |
| 59 | +# Leave a channel |
| 60 | +client>/part <channel-name> |
| 61 | + |
| 62 | +# Change your nickname |
| 63 | +client>/nick <new-nickname> |
| 64 | + |
| 65 | +# Send a message to a channel |
| 66 | +client>/privmsg <channel-name> <message> |
| 67 | + |
| 68 | +# Quit the IRC Client |
| 69 | +client>/quit |
| 70 | +``` |
| 71 | + |
| 72 | +You can also use the IRC Client Class in your own code by importing it from `irc-client.ts`. |
| 73 | + |
| 74 | +```typescript |
| 75 | +import IRCClient from './irc-client'; |
| 76 | + |
| 77 | +// logger is an instance of winston.Logger otherwise undefined |
| 78 | +const client = new IRCClient(host, port, nickName, fullName, debug, logger); |
| 79 | + |
| 80 | +// Connect the client to the server |
| 81 | +await client.connect(); |
| 82 | + |
| 83 | +// Join a channel |
| 84 | +await client.join([{ channel: '#cc' }]); |
| 85 | + |
| 86 | +// Send message to a channel |
| 87 | +client.privateMessage('#cc', 'Hello World!'); |
| 88 | + |
| 89 | +// Part a channel |
| 90 | +await client.part({ channels: ['#cc'], partMessage: 'Bye Bye' }); |
| 91 | + |
| 92 | +// Update your nickname |
| 93 | +await client.nick('MJ'); |
| 94 | + |
| 95 | +// Quit the server |
| 96 | +await client.quit('Bye Bye'); |
| 97 | + |
| 98 | +// Get details about a channel |
| 99 | +const channelDetails = client.getChannelDetails('#cc'); |
| 100 | +``` |
| 101 | + |
| 102 | +You can add listeners to different command types supported by the IRC Client. The client exposes the `on()` method as mentioned in [types.ts](https://github.com/jainmohit2001/coding-challenges/blob/416a47f715fff82964bd8def81c26ab72cfe8978/src/16/types.ts#L189). |
| 103 | + |
| 104 | +## Run tests |
| 105 | + |
| 106 | +To run the tests for the IRC Client, go to the root directory of this repository and run the following command: |
| 107 | + |
| 108 | +```bash |
| 109 | +npm run test tests/16/ |
| 110 | +``` |
| 111 | + |
| 112 | +The tests are located in the `tests/16/` directory. |
| 113 | + |
| 114 | +## TODOs |
| 115 | + |
| 116 | +- [ ] Add support for multiple channels for JOIN and PART commands. |
| 117 | +- [ ] Add command line option support for `irc-client.index.ts`. |
| 118 | +- [ ] Separate the command handling from the main IRC client code into more a structured and scalable format. |
| 119 | +- [ ] Improve tests. |
0 commit comments