r/dailyprogrammer • u/G33kDude 1 1 • Mar 16 '16
[2016-03-16] Challenge #258 [Intermediate] Challenge #258 [Intermediate] IRC: Responding to commands
Description
In the last challenge we initiated a connection to an IRC server. This time we are going to utilise that connection by responding to user input. On an IRC server you can communicate with other users either directly, or in a group chatroom known as a channel. Channel names are distinguished from users by a prefixed character (# on freenode) in the name.
After connecting to an IRC server you will receive some informational text from the server known as the Message Of The Day, or MOTD. The server will buffer any messages (particularly attempts to join channels) sent before it has finished. The end of the MOTD is marked by the message RPL_ENDOFMOTD which is defined as the number 376. You don't necessarily have to wait for the end of the MOTD before joining, but I've found it usually works better if you do.
:wolfe.freenode.net 376 GeekBot :End of /MOTD command.To join a channel you must use the JOIN message. It takes a single parameter, which is a comma separated list of one or more channels.
JOIN #reddit-dailyprogrammer,#botters-testOnce you have sent this message, you will receive one or more JOIN message(s) back from the server for every channel you were successfully able to join. The message you receive back will be prefixed with yourself as the origin.
:GeekBot!G33kDude@192-168-1-42.isp.com JOIN #reddit-dailyprogrammer
:GeekBot!G33kDude@192-168-1-42.isp.com JOIN #botters-testAfter you've been joined to the channel, you can send text to the channel using the PRIVMSG message. It takes two parameters, the first being the the comma separated list of users or channels to send the text to, and the second being the colon prefixed message text.
PRIVMSG #reddit-dailyprogrammer :Hello World!In addition to being able to send messages, you can receive messages that have been sent to the channel by other users. You should listen for a phrase prefixed with your name, then respond to that chat message. For example, you might see the following chat message.
:GeekDude!G33kDude@192-168-1-42.isp.com PRIVMSG #ahkscript :GeekBot: random 20Your code would parse this message, and see the chatted contents were GeekBot: random 20. In response, your program might do something like generate a random number, and chat it back.
PRIVMSG #ahkscript :GeekDude: 4 // chosen by fair 20 sided dice roll // guaranteed to be randomInput Description
In addition to the input from last time's challenge, there will also be two line specifying a channel to join, and a message to chat upon joining.
chat.freenode.net:6667
Nickname
Username
Real Name
#reddit-dailyprogrammer,#rdp,#botters-test
Hello World!Output Description
In addition to the last challenge's output, you must also pick and respond to one or more chat commands. These commands must take at least one parameter, and the return value should be chatted back to the same channel prefixed with the nick of the person who invoked the command.
The following code block has the prefix > for outgoing messages, and < for incoming messages.
>NICK Nickname
>USER Username 0 * :Real Name
<: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 Nickname
--- A bit later ---
<:wolfe.freenode.net 376 MyRC_Bot :End of /MOTD command.
>JOIN #reddit-dailyprogrammer,#rdp,#botters-test
<:GeekBot!G33kDude@192-168-1-42.isp.com JOIN #reddit-dailyprogrammer
>PRIVMSG #reddit-dailyprogrammer :Hello World!
<:GeekBot!G33kDude@192-168-1-42.isp.com JOIN #rdp
>PRIVMSG #rdp :Hello World!
<:GeekBot!G33kDude@192-168-1-42.isp.com JOIN #botters-test
>PRIVMSG #botters-test :Hello World!
--- Wait for chat ---
<:GeekDude!G33kDude@192-168-1-42.isp.com PRIVMSG #reddit-dailyprogrammer :GeekBot: sum 12 8 7 3 5
>PRIVMSG #reddit-dailyprogrammer :GeekDude: The sum is 35Also, don't forget to return any incoming PING messages!
Challenge Input
Your bot should handle commands sent to it directly as well as through normal channels. When you receive such a message, the channel parameter of PRIVMSG is set to your own nickname.
:GeekDude!G33kDude@192-168-1-42.isp.com PRIVMSG GeekBot :GeekBot: mult 6 9Challenge Output
You will have to recognize that the message has been sent directly to you, so you can send your own reply directly back. If you tried to send to the same destination as the original message (as you would with a regular channel message), you would end up sending the chat to yourself.
PRIVMSG GeekDude :GeekDude: 42Bonus
When communicating with the bot directly via private message, nickname prefixes for calling commands and for return values should be optional. For example, the following should work:
<:GeekDude!G33kDude@192-168-1-42.isp.com PRIVMSG GeekBot :GeekBot: div 1 9801
>PRIVMSG GeekDude :GeekDude: 0.00010203...
<:GeekDude!G33kDude@192-168-1-42.isp.com PRIVMSG GeekBot :div 1 9801
>PRIVMSG GeekDude :0.00010203...Notes
Be careful not to allow your bot to generate any newlines in response to a command. For example, if your bot did hex to ascii conversion (GeekBot: hex2ascii 0D0A) someone could potentially cause the bot to send a new protocol message, which could do all sorts of nasty things. This includes sending the QUIT message which would disconnect the bot, or making it spam people potentially getting it banned. If your bot is registered to an account, someone could use this technique to delete the account, or reset the password.
To verify your code is joining channels and chatting correctly, I suggest joining the channel(s) in advance using an IRC client, such as the web based http://webchat.freenode.net/.
You can see the full original IRC specification at https://tools.ietf.org/html/rfc1459. See also, http://ircdocs.horse/specs/.
A Regular Expression For IRC Messages
I get the distinct feeling I've missed something, so if you see anything off let me know.
7
u/fibonacci__ 1 0 Mar 16 '16 edited Mar 16 '16
Python
6
u/IceDane 0 0 Mar 17 '16
I have been working on an IRC bot in Haskell on and off for a long time now. The result is a pretty modular bot that makes it easy to write plugins, has database access for saving state, and sandboxes plugins in their own thread. Plugins can also affect very little of the internal state of the core bot, which is why plugins can be stateful.
I wrote a silly plugin to demo it, and it looks like this:
Another example is the plugin that handles the PING message.
The bot in its entirety can be found here.
The code is messy since I haven't spent much time cleaning it up, and the bot has undergone several major rewrites and refactorings. The plugin development API is not quite how I want it to be. Retriving the parameters to a user command and the name of the sender is pretty ugly. The bot is essentially a big, hierarchical monad transformer stack, so I may just adjust their types to be over
MaybeTor something like that.4
u/fvandepitte 0 0 Mar 16 '16
Haskell
Feedback is welcome
https://gist.github.com/fvandepitte/a40456aef586178a43e8
4
u/notsokratis Mar 16 '16
Android java
3
u/13467 1 1 Mar 16 '16 edited Mar 16 '16
More tiny Ruby! Fits neatly in my terminal at only 21 lines. The final bit of regex sorcery is a bit dense, but all in all, it has a certain elegance to it :)
(It even solves the private message bonus challenge!)
3
u/commandlineluser Mar 17 '16 edited Mar 17 '16
Bash with bonus
2
u/Regimardyl Mar 16 '16 edited Mar 18 '16
Extension of my asynchronous Tcl solution for the easy challenge. Uses the regex hammer for (almost) everything.
EDIT:
!tclcommand, allowing (safe) evaluation of any Tcl expressionEDIT2: Limited message length and escape newlines
2
u/spiegeleixxl Mar 16 '16
Javascript / NodeJS
Not pretty, but doing it's job, so feel free to shun me. :-)
https://gist.github.com/spiegeleixxl/245c872a1c08f0b3e370
2
u/sj1K Mar 16 '16
Python 3 with bonus
2
u/Scroph 0 0 Mar 17 '16 edited Mar 17 '16
D (dlang) solution. The input parameters are supposed to be piped in. This time I used the IRC parsing regular expression, this has to be the first time I use regex in my life. The regex is evaluated at compile time so there wouldn't be too much overhead. All the bot does is reversing whatever message you send to it, you can test it yourself if you want, it's called Scroph_bot :
Edit : fixed bug where it replies "Hello world !" to incoming JOIN messages.
2
u/draegtun Mar 17 '16 edited Mar 17 '16
Rebol (with bonus)
Bot currently responds to following commands...
... where N is an integer. Invalid commands return "eh?" :)
NB. This script is adapted from my earlier EASY version - https://www.reddit.com/r/dailyprogrammer/comments/4ad23z/20160314_challenge_258_easy_irc_making_a/d0ztz3d
2
u/themonsterpus Mar 18 '16 edited Mar 18 '16
C#
Still pretty new to language so kind of hacky, but it works:
EDIT: fixed issue where it was responding to every message.
2
u/Tetsumi- 1 0 Mar 19 '16
Racket with bonus
2
u/phoshzzle Mar 21 '16
Done in C
2
u/JulianDeclercq Mar 22 '16
C++ with Qt 5.5 Build with my code from the [Easy] challenge. Feedback is greatly appreciated. I do realise some things are proably a bit iffy, please do comment improvements :) It does have all functionality though.
1
u/PopeOh Apr 20 '16
Nim version here. Thanks for giving the link to the regular expression it really helped and prevented me from trying to implement something like that myself ;-)
The bot supports the commands
sumandmultand responds to channel and private messages.