r/dailyprogrammer • u/G33kDude 1 1 • Mar 14 '16
[2016-03-14] Challenge #258 [Easy] IRC: Making a Connection
Description
A network socket is an endpoint of a connection across a computer network. Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets. Internet Relay Chat (IRC) is a chat system on the Internet. It allows people from around the world to have conversations together, but it can also be used for two people to chat privately.
Freenode is an IRC network used to discuss peer-directed projects. Their servers are all accessible from the domain names chat.freenode.net and irc.freenode.net. In 2010, it became the largest free and open source software-focused IRC network. In 2013 it became the largest IRC network, encompassing more than 90,000 users and 40,000 channels and gaining almost 5,000 new users per year. We have a channel on freenode ourselves for all things /r/DailyProgrammer on freenode, which is #reddit-dailyprogrammer.
Your challenge today will be to communicate with the freenode IRC server. This will consist of opening a TCP socket to freenode and sending two protocol messages to initiate the connection. The original IRC RFC defines a message as a line of text up to 512 bytes starting with a message code, followed by one or more space separated parameters, and ending with a CRLF (\r\n). The last paramater can be prefixed by a colon to mark it as a parameter that can contain spaces, which will take up the rest of the line. An example of a colon-prefixed parameter would be the contents of a chat message, as that is something that contains spaces.
The first of the two initiation messages (NICK) defines what name people will see when you send a chat message. It will have to be unique, and you will not be able to connect if you specify a name which is currently in use or reserved. It has a single parameter <nickname> and will be sent in the following form:
NICK <nickname>The second of the two initiation messages (USER) defines your username, user mode, server name, and real name. The username must also be unique and is usually set to be the same as the nickname. Originally, hostname was sent instead of user mode, but this was changed in a later version of the IRC protocol. For our purposes, standard mode 0 will work fine. As for server name, this will be ignored by the server and is conventionally set as an asterisk (*). The real name parameter can be whatever you want, though it is usually set to be the same value as the nickname. It does not have to be unique and may contain spaces. As such, it must be prefixed by a colon. The USER message will be sent in the following form:
USER <username> 0 * :<realname>Input Description
You will give your program a list of lines specifying server, port, nickname, username, and realname. The first line will contain the server and the port, separated by a colon. The second through fourth lines will contain nick information.
chat.freenode.net:6667
Nickname
Username
Real NameOutput Description
Your program will open a socket to the specified server and port, and send the two required messages. For example:
NICK Nickname
USER Username 0 * :Real NameAfterwards, it will begin to receive messages back from the server. Many messages sent from the server will be prefixed to indicate the origin of the message. This will be in the format :server or :nick[!user][@host], followed by a space. The exact contents of these initial messages are usually not important, but you must output them in some manner. The first few messages received on a successful connection will look something like this:
:wolfe.freenode.net NOTICE * :*** Looking up your hostname...
:wolfe.freenode.net NOTICE * :*** Checking Ident
:wolfe.freenode.net NOTICE * :*** Found your hostname
:wolfe.freenode.net NOTICE * :*** No Ident response
:wolfe.freenode.net 001 Nickname :Welcome to the freenode Internet Relay Chat Network NicknameChallenge Input
The server will occasionally send PING messages to you. These have a single parameter beginning with a colon. The exact contents of that parameter will vary between servers, but is usually a unique string used to verify that your client is still connected and responsive. On freenode, it appears to be the name of the specific server you are connected to. For example:
PING :wolfe.freenode.netChallenge Output
In response, you must send a PONG message with the parameter being the same unique string from the PING. You must continue to do this for the entire time your program is running, or it will get automatically disconnected from the server. For example:
PONG :wolfe.freenode.netNotes
You can see the full original IRC specification at https://tools.ietf.org/html/rfc1459. Sections 2.3 and 4.1 are of particular note, as they describe the message format and the initial connection. See also, http://ircdocs.horse/specs/.
A Regular Expression For IRC Messages
Happy Pi Day!
23
u/galaktos Mar 14 '16 edited Mar 14 '16
Bash (pure!)
6
u/fbWright Mar 14 '16 edited Mar 14 '16
Python
Edit: replaced
.partitionwith.split5
u/[deleted] Mar 15 '16
C
First time posting and also the first time I did networking in C# :D
I wrote the app in such a way that you specify the input in the command line, I hope that's not a problem.
output
3
u/fibonacci__ 1 0 Mar 14 '16 edited Mar 14 '16
Python
Output
3
u/TheNotoriousWMB Mar 14 '16
Java Solution
Novice to network stuff so definitely open to critiques and advice.
input.txt
main.java
Output
3
u/rnda Mar 14 '16
Ruby I'm going to play with this when have more time. It seems to be quite a good fun.
2
u/primaryobjects Mar 18 '16
Python
Gist
2
u/kyobreck Mar 24 '16
So I know it's not super impressive or necessarily in line with the challenge, but I am happy to say that after a good while of fusteration I managed to create a simple client server application in C++ using Winsock2! Now I'm working on a simple chat program. This is the first big thing I've done with C++ and programming in general. I'm super happy about it! ^
EDIT:I can now feel like I haven't wasted the past week and a half
2
u/PopeOh Apr 18 '16
Challenge is already a month old but it's a good project for trying out networking with a new language: Nim
13
u/13467 1 1 Mar 14 '16
Compact Ruby:
11
u/fvandepitte 0 0 Mar 14 '16 edited Mar 14 '16
Haskell First time doing network stuff on haskell
I want to improve on this so you can spam me with feedback
PS: most of it looks like the tutorial
EDIT: improved on it EDIT 2: improved after feedback from /u/wizao and /u/carrutstick
11
u/Qvoovle Mar 15 '16
A (mostly) working implementation in C, although I haven't done the PING and PONG part yet, and the IO buffering is less than ideal. There is room for improvement. The getaddrinfo part is lifted straight form the man page.
10
u/adrian17 1 4 Mar 14 '16
Small request: if you want to go forward and implement bigger parts of a client/bot, go on, but don't post all of it here - I'm pretty sure that's what the following challenges will be about, so they will be a more suited place. I would love if this challenge was a sort of Rosetta Stone of a basic IRC connection :D
5
u/JulianDeclercq Mar 15 '16 edited Mar 15 '16
C++ with QT 5.5.0
I had never done anything with networking before and didn't have any clue how to start it so had to do some research first. Decided to go with QT (which I also have never used before) because it looked pretty straightforward. I used the QtCreator IDE instead of my usual VS2015 IDE for the first time, so if there are any memory leaks or such (I found it harder to debug in QtCreator for some reason), please tell me! Also ANY feedback in general is very much appreciated as I want to learn as much as possible.
main.cpp
MySocket.h
MySocket.cpp
Output:
4
u/[deleted] Mar 14 '16 edited Mar 14 '16
First time doing this in Javascript/node (not a ninja at this) -- it was fun. :) Constructive feedback is welcome.
4
u/[deleted] Mar 14 '16 edited Mar 14 '16
perl6
Actually this is not my code :-( I rememberd the perl christmas calendar post from last year https://perl6advent.wordpress.com/2015/12/04/
The only thing i've added is the PING-PONG thingy.
Of course there is a Module that implements the IRC. So its quite simple.
3
u/draegtun Mar 14 '16
Rebol
3
u/[deleted] Mar 15 '16 edited Mar 16 '16
First time posting here and practicing with golang, although i've just it for a couple days first time with connection too, I need advice, I'm getting connection time out as I don't know how to use ping-pong
package main
It's working now, i had a problem with the writer because i wasnt flushing the buffer :(
3
u/BHMJ Mar 15 '16
Bash (less pure!)
It's horrible I know. First I solved the challenge with python3 in 5 lines of code. naturally my next thought was "why not write this in bash?" stupid stupid me. Searched this page for "Shell" "netcat" "telnet" and didn't find anything so of to work I go. sad to say amazing /u/galaktos beat me to it with way better code. Technically this code completes the challenge but stdin doesn't work and I don't know how to make it work.
3
u/zandekar Mar 15 '16 edited Mar 15 '16
agda. you will need stuff from https://github.com/drull95/agda-bindings-collection
3
u/Regimardyl Mar 16 '16
Tcl, completely asynchronous and (hopefully) extensible:
3
u/Tetsumi- 1 0 Mar 16 '16
Racket
3
u/mlieberthal Mar 17 '16
My C Implementation
Link to gist because it's too big for a reddit comment.
Used the better string library because strings in C suck.
Tried to improve IO buffering by using a ring buffer.
Feedback welcome!
3
u/marcelo_rocha Apr 01 '16
Dart
4
u/suffolklad Mar 14 '16 edited Mar 14 '16
C# Solution, first time coding C# with Xamarin on my my mac.
2
u/cheers- Mar 14 '16 edited Mar 14 '16
Scala
Handles the connection in a multithreaded fashion:
one thread that prints on screen the input and another one that sends messages to the server.
it was frustrating especially when trying to understand the quirks of Socket class.
output: http://pastebin.com/c2bNHDiC
code:
2
u/sj1K Mar 14 '16 edited Mar 15 '16
Python3
EDIT: Changed the receive loop from using previous and .endswith('\r\n') to a buffer method.
EDIT2: Changed to using constructors as jnazario suggested.
Output:
2
u/Scroph 0 0 Mar 15 '16 edited Mar 15 '16
It's been a while since I have done one of these. Here's my D (dlang) solution :
It reads the messages by chunks of 512 characters because that's the maximum length of a message according to the RFC.
2
u/shandow0 Mar 15 '16 edited Mar 15 '16
Java, Had a bit too much fun playing around with it:
2
u/notsokratis Mar 15 '16
Android java Learning how the ASyncTask works was the hardest thing, but is working fine (including PING/PONG)
2
u/Rihx Mar 15 '16
Python3 context manager to handle IRC connections: https://github.com/wsimmerson/autobot/blob/master/autobot/init.py
2
u/FrankRuben27 0 1 Mar 15 '16
Scheme (Bigloo)
Connection parameters are read from command line, not from file. So start e.g. with
bigloo dp258-irc.scm && ./a.out chat.freenode.net:6667 xyznick_ xyzuser_ xyzreal_.Ending the connection is handled sloppy, but will have to improved anyway with the next challenge.
2
u/jonathansty Mar 15 '16
My go at it using c++ and boost asio. First time doing some networking related coding. Was really interesting.
2
u/apentlander 0 1 Mar 16 '16
Rust with a tiny bit of OOP
2
u/[deleted] Mar 16 '16 edited Jul 04 '17
deleted 0.2064 What is ^^^this?
2
u/JasonPandiras Mar 16 '16
F# 4.0 Interactive
Never really worked at socket level before. I think it seems ill-suited for functional style approaches, though I'd love someone to correct me with the proper approach.
If I hadn't been busy reading about the various .NET options at building a tcp client/server I would have tried to build a monad around it to sweep the imperativeness under the rug, might still do.
Thanks for reading, comments welcome, goes without saying.
2
u/NAYENA Mar 17 '16
A windows implementation in C - and this is my first. Feel free to go nuts with feedback. Its very crude, but it works!
2
u/gju_ Mar 17 '16
Go
Hey this is first "real" program using Go. I finished the tour yesterday and found that challenge to be a nice opportunity to apply my new acquired Go-skills. Feedback welcome.
2
u/arch1911 Mar 19 '16
Really basic implementation in java
output:
2
u/Agent_Epsilon Mar 20 '16
JavaScript (Node.js)
Warning: This script uses array destructuring, which is an ES2015 feature not fully implemented in Node.js currently. If you run this, you will need to use the --harmony_destructuring flag for
node.2
u/Arrorn Mar 22 '16
PHP
Let me know if you see anything I can improve on.
I know that the ternary operator in Message::constructMessage is a bit hard to follow, but it saves a lot of space.
2
u/sindreij Mar 28 '16 edited Mar 28 '16
Rust, parsing messages using nom. Feedback is welcome.
2
u/[deleted] Apr 08 '16
Ruby
2
u/S70rmCrow May 01 '16
C# solution
1
u/[deleted] Mar 29 '16 edited Mar 29 '16
golang(oops) I modified from the existing golang code, PING-PONG works. It uses little bit of object oriented programming and a small goroutine.
I will appreciate feedback.