1

I've created bot, using code from this page. Everything was good, when I was trying to reach irc.rizon.net. But problem arrives, when I've changed server to irc.alphachat.net.

#!/usr/bin/env python3
import socket

server = 'irc.alphachat.net'
channel = '#somechannel'
NICK = 'somenick'
IDENT = 'somenick'
REALNAME = 'somenick'
port = 6667

def joinchan(chan):
    ircsock.send(bytes('JOIN %s\r\n' % chan, 'UTF-8'))


def ping(): # This is our first function! It will respond to server Pings.
    ircsock.send(bytes("QUOTE PONG \r\n", 'UTF-8'))


def send_message(chan, msg):
    ircsock.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))

ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, port)) # Here we connect to the server using the port 6667
ircsock.send(bytes("USER "+ NICK +" "+ NICK +" "+ NICK +" :This bot\n", 'UTF-8')) # user authentication
ircsock.send(bytes("NICK "+ NICK +"\n", 'UTF-8')) # here we actually assign the nick to the bot

joinchan(channel) # Join the channel using the functions we previously defined

while 1: # Be careful with these! it might send you to an infinite loop
  ircmsg = ircsock.recv(2048).decode() # receive data from the server
  ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
  print(ircmsg) # Here we print what's coming from the server
  if ircmsg.find(' PRIVMSG ')!=-1:
     nick=ircmsg.split('!')[0][1:]
  if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
    ping()
  if ircmsg.find(":Hello "+ NICK) != -1: # If we can find "Hello Mybot" it will call the function hello()
    hello()

Problem is with ping command because I don't know how to answer to server:

:irc-us2.alphachat.net NOTICE * :*** Looking up your hostname...
:irc-us2.alphachat.net NOTICE * :*** Checking Ident
:irc-us2.alphachat.net NOTICE * :*** Found your hostname
:irc-us2.alphachat.net NOTICE * :*** No Ident response
PING :CE661578
:irc-us2.alphachat.net 451 * :You have not registered
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Why Me
  • 15
  • 3
  • Check http://stackoverflow.com/questions/4770598/python-irc-bot-wont-join The answer will show you how to properly PONG (don't send QUOTE for starters) – David Zech Aug 27 '15 at 19:50
  • @DavidZech I did it like in this link, which you gave, but I have still the same problem. Maybe its something with my ping function? Actually it look like: def ping(): ircsock.send(bytes("PONG: \r\n", 'UTF-8')) – Why Me Aug 27 '15 at 20:29
  • 1
    When receiving `PING :CE661578\r\n`, what you should send back is: `PONG CE661578\r\n` (https://tools.ietf.org/html/rfc1459#section-4.6.3) – Jessy Amyot Sep 29 '15 at 15:24

2 Answers2

0

With IRC, you should really split each line up by ' ' (space) into chunks to process it - something like this should work after your print (untested)

The reason it's not working is because you're not replying to PINGs properly

chunk = ircmsg.split(' ')
if chunk[0] == 'PING': # This is a ping
  ircsock.send(bytes('PONG :%s\r\n' % (chunk[1]), 'UTF-8')) # Send a pong!
if chunk[1] == 'PRIVMSG': # This is a message
  if chunk[3] == ':Hello': # Hey, someone said hello!
    send_message(chunk[2], "Hi there!") # chunk[2] is channel / private!
if chunk[1] == '001': # We've logged on
  joinchannel(channel) # Let's join!
  send_message(channel, "I've arrived! :-)") # Announce to the channel

Normally the command / numeric is found in the second parameter (chunk[1]) - The only exception I can think of is PING which is found in the first (chunk[0])

Also note that I moved joinchannel() - you should only be doing this after you're logged on.

Edit: Didn't realise the age of this post. Sorry!

JD Byrnes
  • 783
  • 6
  • 17
0

I believe you just need to make a small change to the string you send in response to the ping request.

try using:

ircsock.send(bytes("PONG pingis\n", "UTF-8"))

This ping response works for me on freenode.

zeduke
  • 1