irc wrapper class not registering

irc wrapper class not registering

Postby DTSCode » Wed Mar 26, 2014 7:54 pm

so i wrote a class that makes it easier or cleaner rather to connect to an irc channel, but the problem is its not registering now. i looked at the rfc and other bots and asked on irc, but still nothing. also, before you look at my code, I am fully aware of how non conforming my style is, but right now i just want the code to work before i make it look pretty

Code: Select all
import socket
import string

class irc:
    __Server   = "irc.quakenet.org"
    __Channels = ["#botdevgroundzero"]
    __Nick     = "DTSRecorder-Dev"
    __Port     = 6667
    __Socket   = socket.socket()
    __Buffer   = ""
    __Response = None

    def __init__(self): return

    def setServer(self, Server):
        self.__Server = Server
        return self

    def setChannels(self, Channel):
        self.__Channels = Channel
        return self

    def setNick(self, Nick):
        self.__Nick = Nick
        return self

    def setPort(self, Port):
        self.__Port = Port
        return self

    def getServer  (self): return self.__Server
    def getChannels(self): return self.__Channels
    def getNick    (self): return self.__Nick
    def getPort    (self): return self.__Port
    def getBuffer  (self): return self.__Response

    def addChannel(self, Channel):
        if not Channel in self.__Channels:
            self.__Channels.append(Channel)

        return self

    def connect(self):
        self.__Socket.connect((self.__Server, self.__Port))
        self.__Socket.send("NICK %s\r\n" % (self.__Nick))
        self.__Socket.send("USER %s %s bla: %s\r\n" % (self.__Nick, self.__Server, self.__Nick))

    def joinChannels(self):
        for Channel in self.__Channels:
            self.__Socket.send("JOIN %s\r\n" % (Channel))

        return self

    def listen(self):
        self.__Buffer = self.__Buffer + self.__Socket.recv(1024)
        self.__Response = string.split(self.__Buffer, "\n")
        self.__Buffer = self.__Response.pop()
        return self

    def respond(self):
        for Line in self.__Response:
            Line = string.rstrip(Line)
            Line = string.split (Line)

            if Line[0] == "PING":
                self.__Socket.send("PONG %s\r\n" % Line[1])

        return self

    def send(self, Message):
        self.__Socket.send("%s\r\n" % (Message))
        return self

    def sendMessage(self, Recepient, Message):
        self.__Socket.send("PRIVMSG %s :%s\r\n" % (Recepient, Message))
        return self

    def changeNick(self, NewNick):
        self.__Socket.send(":%s NICK %s\r\n" % (self.__Nick, NewNick))
        self.__Nick = NewNick
        return self
DTSCode
 
Posts: 5
Joined: Thu Jan 30, 2014 2:41 pm

Re: irc wrapper class not registering

Postby stranac » Wed Mar 26, 2014 9:39 pm

What does "its not registering" mean exactly?
Also, I hope you know there are better ways to do network programming than using raw sockets.

P.S. That code makes me want to hit you.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: irc wrapper class not registering

Postby DTSCode » Thu Mar 27, 2014 7:00 pm

the response im getting back from the server (quakenet.org) in this case, is saying EnemyBot Register first or something like that. and what is the better way to do network programming? also, im sorry, but why does my code make you want to hit me? (im very new to python and havent had time to read the pep yet on proper styles)
DTSCode
 
Posts: 5
Joined: Thu Jan 30, 2014 2:41 pm

Re: irc wrapper class not registering

Postby stranac » Thu Mar 27, 2014 8:10 pm

DTSCode wrote:the response im getting back from the server (quakenet.org) in this case, is saying EnemyBot Register first or something like that.

Not sure, but that might mean the nickname has to be registered with nickserv.
I know very little about the actual IRC protocol.

The better way to do network programming would be Twisted.
For IRC, specifically, take a look at: http://twistedmatrix.com/documents/13.2 ... lient.html
For an example bot, you can take a look at this: https://gist.github.com/habnabit/5823693

Things that make me hate your code:
  • names starting with __ - you should never use those
  • using setter/getter methods where simple attribute access would be fine
Also, your __init__ is unnecessary, and your class attributes should be instance attributes instead.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: irc wrapper class not registering

Postby DTSCode » Thu Mar 27, 2014 8:33 pm

ok thanks ill check out twisted (and quakenet doesnt have a nickserv). also, why shouldnt i use __? and whats attribute access?
DTSCode
 
Posts: 5
Joined: Thu Jan 30, 2014 2:41 pm

Re: irc wrapper class not registering

Postby stranac » Thu Mar 27, 2014 9:14 pm

DTSCode wrote:why shouldnt i use __?

It's ugly and does nothing useful.
DTSCode wrote:and whats attribute access?

I simply meant you can do:
Code: Select all
something.x = 3

instead of having to define a set_x() method and using:
Code: Select all
something.set_x(3)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: irc wrapper class not registering

Postby DTSCode » Thu Mar 27, 2014 9:21 pm

ok. i actually started rewriting my class from scratch and changed it to that
DTSCode
 
Posts: 5
Joined: Thu Jan 30, 2014 2:41 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 0 guests