Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

[SOLVED] Python IRC issues...

Holiverh's izHawt Python....

[SOLVED] Python IRC issues...

Postby Holiverh » Mon Sep 14, 2009 1:12 pm

HIYA!

After my first attempt at writing a IRC bot purely in Python sort of worked, i decided to rewrite it and tried to add some better error handling etc. Well it won't work at all now! And i don't now why! So i'm wondering if ya'll could give it a quick glance over and see if i made any silly mistakes?

Don't worry if you've never done anything with IRC before neither have i and it's easy to understand!

Code: Select all
HOST = "ice.coldfront.net"
PORT = 6667
CHANNEL = "#CN-Informatist"
USER = "HBot HBot HBot HBot"
NICK = "PoloBot"


import socket
e = "\r\n" # Endline string
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

_buffer = [] # Stream buffer

attempts = 0
connected = 0
while connected < 4 and attempts < 5: #Start attempt connection loop

    try:
        irc.connect((HOST,PORT))
        connect = connect+1
    except:
        irc.close()
        print "Connection failed @ INIT"

    try:
        irc.send("NICK "+NICK+e)
        connect = connect+1
    except:
        irc.close()
        print "Connection failed @ send:NICK"

    try:
        irc.send("USER "+USER+e)
        connect = connect+1
    except:
        irc.close()
        print "Connection failed @ send:USER"

    try:
        irc.send("JOIN "+CHANNEL+e)
        connected = connected+1
    except:
        irc.close()
        print "Connection failed @ send:JOIN"
    attempts = attempts+1

while True:
    getStream()
    checkPing(_buffer[0])

    readBuffer(_buffer)

    clearBuffer(_buffer)
   
def getStream():
    global irc
    global _buffer
    _buffer.append(irc.recv(4096))
    return None
def checkPing(s=str):
    global irc
    if s.find("PING") != -1:
        irc.send("PONG" + s.split()[1]+e)
        return True
    else:
        return False
def readBuffer(buf=list):
    for item in buf:
        return item
def clearBuffer(buf=list):
    while len(buf) >= 0:
        del buf[0]
    return None

It starts by just declaring some global varibles like 'HOST', 'NICK' etc. And also created the buffer to store each 'line'.

After that it enters the 'connection loop', where it attempts to connect to the server. This is where my problems are, i think... The out put it's currently returning is:
Code: Select all
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Which kinda speaks for itself... It cannot connect, and i don't know why!? It makes no sense as it uses the same protocol as the first attempt does, and that made a near perfect connection. The only thing that is different is there are no exception traps in the first one...possible issue?
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: Python IRC issues...

Postby Holiverh » Tue Sep 15, 2009 3:35 am

Oh i feel so embarassed... :| I realised a potential problem with careless exception catching. It was a simple name error that was causing it to fail, a small typo in the varible that incrments the 'counted' varible. :(

So aprently it works! Yay lol... I feel so stupid lol... So from now on you may see 'HollyBot' or her brother 'PoloBot' sitting in the channel #CN-Informatist on the coldfront network.

I won't lock this thread because i may have some more questions.

The source code is not freely avaliable as of yet! So no using it! :P
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Python IRC issues...

Postby Dink » Tue Sep 15, 2009 7:24 am

Awesome work Holly. I'm sure I'll see it sometime lol.
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Python IRC issues...

Postby Holiverh » Tue Sep 15, 2009 12:43 pm

Dink wrote:Awesome work Holly. I'm sure I'll see it sometime lol.
Thank you Dink. I've been beffing it up over the 24 hours and there is a new function i'd like someone else to look at before i run it. Because it's dealing with files and that's a dangerous game...

FYI, this is the transcripter function...

Code: Select all
def recordTranscript(status=int,stream=str):
    global irc
    timestamp = datetime.datetime.utcnow().strftime("%Y-%M-%D %H:%M:%S")
    _file = open("test001.txt","a")
    if status == 1:
        try:
            _file.write(timestamp+" >>> "+stream)
            _file.close()
            return True,1
        except:
            print "Transcript error @ START"
            return False,1
    else:
        try:
            _file.write("Recording stopped @ "+timestamp)
            _file.close()
            return True,0
        except:
            print "Transcript error @ STOP"
            return False,0
   
    size = os.path.getsize("test001.txt")/1024
    if round(size,2) > 50.00:
        try:
            _file.write("Recording stopped @ "+timestamp+" f/ Max file size exceeded")
            _file.close()
            return True,2
        except:
            print "Transcript error @ FILE"
            return False,2
    return None
It should work... But one minor thing. Would the calculation in the varible 'size' return the size in kilobytes?

So what do you think? Anything that looks slightly out of place let me know, i don't want a repete of the first code lol...

Total lines in file: 132
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Python IRC issues...

Postby Dink » Tue Sep 15, 2009 2:16 pm

With the file-size I am not entirely sure. It depends upon what Python records its file-size in if you know what I mean. I am assuming it does in bits? In that case to convert to kilobytes it would be multiplied by 1000. Although if it was in Mb the original size then dividing by 1024 as you have would convert it to kilobytes (I am assuming it is 50 kilobytes you wish to restrict the size to).

Why do you have "return True,1" and "return False,1" as part of the if statements when checking if it works, wouldn't one be "return True,1" and the other "return False,0" or vice versa etc?

Sorry, I haven't gotten around to learning all about IRC yet so anything I have said may be totally wrong. :ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Python IRC issues...

Postby Holiverh » Tue Sep 15, 2009 4:11 pm

Dink wrote:With the file-size I am not entirely sure. It depends upon what Python records its file-size in if you know what I mean. I am assuming it does in bits? In that case to convert to kilobytes it would be multiplied by 1000. Although if it was in Mb the original size then dividing by 1024 as you have would convert it to kilobytes (I am assuming it is 50 kilobytes you wish to restrict the size to).

Why do you have "return True,1" and "return False,1" as part of the if statements when checking if it works, wouldn't one be "return True,1" and the other "return False,0" or vice versa etc?

Sorry, I haven't gotten around to learning all about IRC yet so anything I have said may be totally wrong. :ugeek:
os.path.getsize() returns the size in bytes thus divide by 1024 would be correct? And yes the file was meant to be limited to 50kb.

And about the return statements. The 'True' or 'False' tells me whether it worked or not. the numbers tell me which part succecced or failed.

Well i ran it... It worked nicely as you can see by my little conversation in #CNOlympus:
15-15-15 15:15:15 >>> :Gogeta!Mibbit@coldfront-F57D3E4B.dyn.optonline.net PRIVMSG #cnolympus :lol, point-giving

15-15-15 15:15:15 >>> :Holly!Holly@coldfront-F8542967.lutn.cable.ntl.com PRIVMSG #CNOlympus :Points are bad...

15-15-15 15:15:15 >>> :queenhailee[Olympus]!qh@118303CA.8263B7A0.1D109FF2.IP PRIVMSG #cnolympus :I hope we all don't live to regret that Rudekker :P

15-15-15 15:15:15 >>> :Gogeta!Mibbit@coldfront-F57D3E4B.dyn.optonline.net PRIVMSG #cnolympus :do i get aops now?

15-15-15 15:15:15 >>> :Rudekker!ngates@coldfront-AB6B952.dsl.milwwi.sbcglobal.net PRIVMSG #cnolympus :What would life be without regrets? Honestly.

15-15-15 15:15:15 >>> :queenhailee[Olympus]!qh@118303CA.8263B7A0.1D109FF2.IP PRIVMSG #cnolympus :i could be talked into ahops

15-15-15 15:15:15 >>> :queenhailee[Olympus]!qh@118303CA.8263B7A0.1D109FF2.IP PRIVMSG #cnolympus :being as how you are a treaty partner and all.

15-15-15 15:15:15 >>> :Gogeta!Mibbit@coldfront-F57D3E4B.dyn.optonline.net PRIVMSG #cnolympus :but

15-15-15 15:15:15 >>> :queenhailee[Olympus]!qh@118303CA.8263B7A0.1D109FF2.IP PRIVMSG #cnolympus :you'll have to arm wrestle DK for the ops

15-15-15 15:15:15 >>> :Gogeta!Mibbit@coldfront-F57D3E4B.dyn.optonline.net PRIVMSG #cnolympus :im your loyal subject
I left it to record for quite a while, but as you can see the timestamps didn't work...which shouldn't be too hard to fix, hopefully. :lol:

After the minor stuff is sorted out i'll work on getting rid of all that poitnless data like:
15-15-15 15:15:15 >>> :Holly!Holly@coldfront-F8542967.lutn.cable.ntl.com PRIVMSG #CNOlympus :Hehe.
And turn it into:
<TIMESTAMP> >>> <Holly> Hehe.

:) Well chuffed.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Python IRC issues...

Postby Dink » Tue Sep 15, 2009 6:43 pm

Awesome work! :ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia


Return to Python

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred