COMP112 2020 Tri 1

Assignment 6+7: Networks. Building an IRC Client

  • Due 4 Jun 23:59

Note: you have a choice of either doing the separate assignment 6 and assignment 7 (1 week each) or the combined assignment 6+7 (2 weeks). Only do 6+7 if you have been following the 112 lectures and are doing well in the assignments. It is perfectly acceptable to do the separate 6 and 7.

Overview

In this assignment you will be implementing an Internet Relay Chat client that will enable a user to chat with other users via an IRC server.

Goals

After completing this assignment you should know the basics of implementing a networked application that can interact with a remote server via sockets, and follows a simple network protocol.

Preparation

Download the zip file and extract it to your home folder. It should contain template for the Java program you are to complete. Read through the whole assignment to see what you need to do.

Summary

To Submit

  • Your ChatClient.java program (along with any additional files needed to run the program).
  • The Reflection.txt file with answers to the questions.

Internet Relay Chat

Internet Relay Chat is a simple network system and protocol that allows users to communicate ("chat") with other users over a network. It requires a server program running somewhere on the internet that manages the communication, and a collection of client programs that enable users to interact with each other via the server.

For this assignment, you will be implementing an IRC client program.

Using an IRC client program, a user can connect and login to a server, providing a nick name, user name, and a real name. The server will respond with messages saying that the login was successful, or unsuccessful (eg, the nickname is already in use).

Once logged in, that user can send text messages to another user, using the PRIVMSG command, if they know the other user's username or nickname.

A user can also join "channels", which are groups of users, with the JOIN command. (JOINing a channel that doesn't exist yet will create the channel). They can then send text messages to the channel (using the PRIVMSG command), and all the users currently in that channel will receive the messages. The user will also receive any text messages that anyone else sends to the channel. The user can leave a channel with the PART command.

A user can find out a variety of information from the server. For example, the LIST command will ask for all the channels on the server.

The IRC protocol is essentially asynchronous. That is, the client can send messages to the server, and the server can independently send messages to the client. The protocol does not require the client to acknowledge or respond to anything that the server sends. This makes the design of your client program simpler. The program will
  • Set up a socket that connects to the server, and a Scanner and PrintWriter connected to the inputStream and outputStream of the socket, respectively.
  • Log in to the server,
  • Start up a thread that is listening to the server, and deals with all the messages that the server sends. There are a few such messages that should be acted on (SQUIT) or responded to (PING), but mostly it should just display the messages to the user.
  • Respond to buttons or other user events by sending messages to the server.

Apart from during the login process (and dealing with PINGs), sending to the server and listening to the server will be almost completely separate sides to the program.

Your program should provide an interface that allows the user to do at least the following:
  • Connect and login to a server
  • Disconnect from the server
  • Join a channel
  • Leave a channel
  • Send text messages to a person or a channel
  • List all the currently available channels
  • List all the users currently on the server.

The connection and login process is the trickiest. It involves setting up a socket connection to the server, asking the user for their nickname (and real name), sending two messages to log in, then watching the messages returned by the server until there is a message confirming successful login or a message reporting some kind of failure. If the login is successful, the program will start up a thread to listen to the server.

One part of your IRC client program will listen to messages from the server (after the user has connected and logged in). It will need display to the user all the text messages from other users. It is actually a good idea (and easiest) to display all the messages from server, though a nice design will separate out the different kinds of messages. This part of the program must also deal with PING messages and SQUIT messages. If the program receives a PING message (described below), it must immediately send back a matching PONG message, otherwise the server will disconnect the user. If the program receives a SQUIT message, the server has quit, and the program should close the connection.

The other elements of your user interface (buttons, textfields, etc) should let the user send messages to the server.

The last part of this handout gives examples of the format of most of the messages that you will need to deal with in the core and completions. But you may want to refer to web pages with more detailed descriptions of the messages and their format, eg http://en.wikipedia.org/wiki/List_of_Internet_Relay_Chat_commands, http://irchelp.org/irchelp/rfc/chapter4.html

There is a template, ChatClient.java, for your program that provides the outline of a design. Although you are not required to use it, we strongly recommend that you do. The lecture notes provide examples of how to write java code to connect to sockets, set up a Scanner to process the messages from the socket and a PrintStream to send messages to the socket.

Core:

Your IRC client should
  • Let the user connect to irc.ecs.vuw.ac.nz, and log in with a nickname and real name.
  • Display to the user all the text messages sent to them. (It is OK if it shows all messages.)
  • Let the user join and leave a channel.
  • Let the user send a message to a channel.
  • Let the user send a message to an individual user.
  • Let the user ask for a list of channels and see the server's answers

Completion:

Your IRC client should
  • Respond appropriately to the 433 error message ("nickname in use") when attempting to log in. You can test this by trying to log in as "ChatBot"
  • Have a window that shows only the conversations (text messages sent to user and by the user). It should show messages from the server in a separate window. Note, there is an ExampleTextWindow.java file that shows how to create a new text window and put text into it.
  • Collect up all the messages from the server in response to a LIST message, and display them nicely (eliminating the sender and the message command).

Challenge:

There are lots more things that a good IRC client should provide. Implement some of them. Here are a few suggestions:
  • Display the text messages nicely, showing just the nicknames of the senders of the messages.
  • Have separate windows for each channel the user has joined, and display messages in the right windows.
  • Respond properly to some error messages. (The file Replies.java has a list of all the numeric commands and error messages from a server.)
  • Add more facilities to access information from the server. (Some of the commands include HELP, INFO, ISON, MOTD, NAMES, USERS).

Testing and Running your program

We have set up an IRC server on one of the school machines. Its host name is irc.ecs.vuw.ac.nz. While you are testing and debugging your client program, use this server!. (Once you are confident that your program works, you can use it on "real" servers if you wish, but do not do this until you have tested it well on our local server.) While you are testing and debugging, *use your usercode for your username and your nickname*. If the nickname is already used (because you had logged in already and hadn't disconnected properly), add a digit on the end.

The irc.ecs.vuw.ac.nz server has at least one channel called #100Test. You can join that channel to test out your join and part commands. There is a chatbot (nickname ChatBot) running on the system that sends a message to the #100Test channel every 20 seconds; if you join the channel, you should receive these messages. The chatbot will also respond to messages - if you send a message to ChatBot, or to the #100Test channel, the ChatBot will send a message back to you. This will enable you to test your system even if there are no other users logged in.

Message format

The IRC protocol specifies the format of all messages sent to or from the server. Each message is a single line, that must be terminated with "\r\n". (This means that you should not use println: you should use print and include the "\r\n" in the String being printed.)

There are four parts to an IRC message:
  • The source (ie, where the message came from). It starts with a ":" and is a string of characters with no spaces. The client messages should always leave the source out, so your program doesn't need to include a source when sending a message to the server.
  • The command, which is a single word, all in capitals, or a 3-digit number. All the messages the client sends will have a word command; many of the message from the server have a number, which are codes for error or information messages.
  • The arguments to the command: strings with no spaces in them. The number of arguments depends on the command
  • An extended argument, which starts with a ":" (like the source) and consists of all the remaining characters up to the end of the message, including spaces. Some commands have an extended argument; some don't.

For example, here are some commands

Messages from a client

NICK Noobie \r\n
command NICK, set nickname.
The first message when logging in
arguments one: the nickname to use
extended argument none
USER Noobie 0 unused :Peter Andreae\r\n
command USER, give user name and real name to go with the nickname.
The second message when logging in.
arguments three: username, 0 (mode), anything
extended argument :Peter Andreae the real name (starting with : of course)
JOIN #100Test \r\n
command JOIN
arguments one: the name of the channel to join
extended argument none
PRIVMSG #100Test :Hi, I'm a new user trying out this channel\r\n
command PRIVMSG (send a text message)
arguments one: #100Test, (the channel)
extended argument :Hi to the end, this is the text message that will be sent to all the users on the #100Test channel
PART #100Test \r\n
command PART
arguments one: the name of the channel to leave
extended argument none
LIST \r\n
command LIST, a request for a list of all the channels
arguments one: the name of the channel to leave
extended argument none
PONG xyzuvw \r\n
command PONG, the correct response to a PING message
arguments xyzuvw, which must match the argument in the PING message
extended argument none

Messages from the server
:irc.ecs.vuw.ac.nz NOTICE AUTH :*** Found your hostname
source irc.ecs.vuw.ac.nz
command NOTICE (a special kind of text message from the server)
arguments one: AUTH
extended argument *** Found your hostname
:irc.ecs.vuw.ac.nz.net 004 noobie irc.ecs.vuw.ac.nz hybrid-7.2.3 CDGabiklnrsxyz blnstvI
source irc.ecs.vuw.ac.nz
command 004, a code indicating that logging in was successful
arguments five: user server, version, user modes, channel modes
extended argument none
:irc.ecs.vuw.ac.nz 433 * ChatBot :Nickname is already in use.
source irc.ecs.vuw.ac.nz
command 433, a code indicating that the nickname is already used, logging in was unsuccessful
arguments two: * and ChatBot
extended argument Nickname is already in use.
PING :irc.ecs.vuw.ac.nz.net xyzuvw
source irc.ecs.vuw.ac.nz
command PING, asking whether the client is present
arguments an argument xyzuvw may be included. If it is included, then the client should send the code back xyzuvw in the PONG=
Note, The documentation previously had an error (it said =PING
came after the server address)
extended argument none
:irc.ecs.vuw.ac.nz 322 noobie #100Test 3 : <\tt>
source irc.ecs.vuw.ac.nz
command 322, a code for one channel in a list of channels, in response to a LIST message
arguments noobie #100Test 3, recipient, channel name, number of users
extended argument emtpy
irc.ecs.vuw.ac.nz 323 noobie :End of /LIST <\tt>
source irc.ecs.vuw.ac.nz
command 322, a code for indicating the end of a list of channels
arguments noobie recipient,
extended argument End of /LIST
:ChatBot!~ChatBot@belfast.ecs.vuw.ac.nz PRIVMSG #100Test :He turned away, and Tom was beginning to breathe more easily
source ChatBot!~ChatBot@belfast.ecs.vuw.ac.nz
command PRIVMSG, indicating a text message from ChatBot
arguments #100Test, the channel the text message was sent from
extended argument He turned etc, the actual message

Reflection

Answer the following questions in the Reflection.txt file, and make sure you submit it.

  1. Briefly list the features that you implemented in your Chat Client.
  2. Describe how you tested your program, and discuss how much confidence you have that your testing shows that the program works correctly.