User Tools

Site Tools


programming:irc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:irc [2021/06/02 06:05]
nanodano
programming:irc [2022/03/13 16:49] (current)
nanodano
Line 3: Line 3:
 IRC stands for Internet Relay Chat. IRC stands for Internet Relay Chat.
  
-===== DevDungeon IRC Server ===== 
  
-If you're looking for an existing server to join, connect to ''irc.devdungeon.com'' on port ''6667''.+===== 2-minute Intro to IRC ===== 
 + 
 +<html> 
 +<center> 
 +<iframe width="560" height="315" src="https://www.youtube.com/embed/-4ab5-F72nY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> 
 +</center> 
 +</html> 
 + 
 +===== Using IRC with netcat ===== 
 + 
 +<html> 
 +<center> 
 +<iframe width="560" height="315" src="https://www.youtube.com/embed/FDrR98ww6bE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> 
 +</center> 
 +</html> 
 + 
 +===== Basics ===== 
 + 
 +Once you are connected to an IRC server like ''irc.devdungeon.com'' you can start running commands. 
 +Commands start with a ''/''Here are the essential commands you need to know. 
 + 
 +  * ''/list'' - List chat rooms and participant count 
 +  * ''/who #general'' - See who is in the channel named #general 
 +  * ''/join #general'' - Join channel named #general 
 +  * ''/part #general'' - Leave channel named #general 
 +  * ''/msg nanodano hey'' - Send private message to NanoDano 
 +  * ''/quit'' - Disconnect from server 
 + 
 +Other commands: 
 + 
 +  * ''/away Be back in a few'' - Set an away message 
 +  * ''/back'' - Turn off away status 
 +  * ''/me is not surprised'' - Emote something; e.g. "NanoDano is not surprised" 
 +  * ''/motd'' - See the server's message of the day 
  
-<code> 
-irc.devdungeon.com 
-</code> 
  
 ===== Clients ===== ===== Clients =====
  
-Recommended clients:+There are many clients out there, but here are a few:
    
   * [[http://xchat.org/|XChat]] (GUI) - For Windows, download and install from the website. For Debian, install with ''apt install xchat''.   * [[http://xchat.org/|XChat]] (GUI) - For Windows, download and install from the website. For Debian, install with ''apt install xchat''.
-  * [[https://pidgin.im/|Pidgin]] (GUI) - For Windows, download and install from the website. For Debian, install with ''apt install pidgin''+  * [[https://pidgin.im/|Pidgin]] (GUI) - For Windows, download and install from the website. For Debian, install with ''apt install pidgin''. Pidgin is nice because it supports other protocols too so you can have your XMPP account and IRC setup together
-  * [[https://irssi.org/|irssi]] (Terminal) - For Windows, use Debian WSL. In Debian, install with ''apt install irssi''. In Mac, ''brew install irssi''. Use the normal IRC slash commands like ''/list'' and ''/join #general''. Use alt keys plus window number to switch between chat windows. E.g. ''ALT-1'' and ''ALT-2''.+  * [[other:irssi_terminal_chat|irssi]] (Terminal) - For Windows, use Debian WSL. In Debian, install with ''apt install irssi''. In Mac, ''brew install irssi''. Use the normal IRC slash commands like ''/list'' and ''/join #general''. Use alt keys plus window number to switch between chat windows. E.g. ''ALT-1'' and ''ALT-2''
 +  * netcat - You can use netcat as a client if you understand the IRC protocol. Check out my video tutorial [[https://youtu.be/FDrR98ww6bE|IRC with netcat]].
  
-==== Basics ==== 
  
-Commands start with a ''/'' and you can run some of the commands below to get started. They will allow you to find and join channels to chat.+===== Node.js Bot =====
  
- * ''/list'' - List chat rooms and participant count +<code javascript simple_irc_bot.js> 
- ''/who #general'' - See who is in the channel named #general +var net = require('net');  // https://nodejs.org/dist/latest/docs/api/net.html
- * ''/join #general'' - Join channel named #general +
- * ''/part #general'' - Leave channel named #general +
- * ''/msg nanodano hey'' - Send private message to NanoDano +
- * ''/quit'' - Disconnect from server+
  
-Tag someone by starting the message with their username and a colon. This will usually ping their client and is similar to tagging someone.+var BOT_NAME = 'mybot2'; 
 +var BOT_CHANNEL = '#bots';
  
-<code> +var client = new net.Socket();
-nanodano: hey! +
-</code>+
  
-==== Other commands ====+client.connect(6667, 'irc.devdungeon.com', function() { 
 +  client.write('USER ' + BOT_NAME +  ' 0 * :' + BOT_NAME + '\n'); 
 +  client.write('NICK ' + BOT_NAME + '\n'); // Must wait a second to register
  
-  ''/away Be back in a few'' - Set an away message +  setTimeout(function(){  
-  ''/back'' - Turn off away status +      client.write('JOIN + BOT_CHANNEL + '\n'); 
-  * ''/me is not surprised'' - Emote somethinge.g. "NanoDano is not surprised" +      client.write('PRIVMSG + BOT_CHANNEL + Hello my Node.js bot is online!\n'); 
-  * ''/motd'' - See the server's message of the day+   }, 3000)
 +    
 +});
  
 +client.on('data', function(data) {
 +  console.log('MESSAGE_FROM_SERVER: ' + data);
 + var text = data.toString();
 + var splitText = text.split(' ');
 +  // If data starts with PING, respond with PONG :whatever
 +  if (text.startsWith('PING')) {
 +    client.write('PONG ' + text.substring(5));  // Remove `PING ` and pong back what it sent me
 +    console.log('Recieved PING. PONGED.');
 +  } else if (splitText[1] == 'PRIVMSG') { // PM or chat room
 +    // If data comes from a channel (#general):
 +    //   :nanodano!johnd@107.181.165.217 PRIVMSG #general :Hello bot
 +    // If data comes from privmsg (dano):
 +    //   :nanodano!johnd@107.181.165.217 PRIVMSG danobot :hey
 +    console.log('Destination:' + splitText[2])
 +    // Was it a direct message or to the channel?
 +    if (splitText[2] == BOT_CHANNEL) {
 +       console.log('message was from ' + BOT_CHANNEL)
 +    } 
 +    if (splitText[2] == BOT_NAME) {
 +       console.log('message was directly to me (' + BOT_NAME + ')')
 +    }
 +    // Was the message a command?
 +    if (splitText[3].substring(1).startsWith('!')) {
 +      console.log('Command detected with ! prefix')
 +      // Cut off first character, and check command. Run appropriate function. !bitcoin. !ping !8-ball
 +      var command = splitText[3].substring(2).trim().toLowerCase();
 +      console.log('Command: `' + command + '`');
 +      if (command == 'ping') { 
 +        console.log('Ping command detected! Ponging.');
 +        client.write('PRIVMSG ' + BOT_CHANNEL + ' Pong\n');
 +      }
 +    }
 +    console.log('PrivMsg: ' + splitText[3].substring(1)); // Cut off : in front    
 +  } else {
 +    console.log('Discarded unexpected/unimportant message.');
 +  }
  
 +});
 +</code>
  
  
programming/irc.1622613955.txt.gz · Last modified: 2021/06/02 06:05 by nanodano