Posted by kahrn on Wed 13 Aug 14:16
report abuse | download | new post
- """
- ctcp.py
- author: Ricky Hewitt [kahrn] -- <kahrny@gmail.com>
- Public Domain
- description:
- An example/script of how to use IRC CTCP replies using standard python.
- Special thanks to Gian Mario Tagliaretti (http://code.activestate.com/recipes/299411/)
- for original irc connecting/socket code.
- """
- # external modules
- import socket
- import string
- import time
- import platform
- import re
- # Configuration values
- NICK = "" #eg: randombot
- NETWORK = "" # eg: irc.network.com
- PORT = 6667
- IDENT=NICK
- REALNAME=NICK
- CHANNEL = "" # channel to join.. e.g. #test
- #ctcp values
- VERSION_REPLY = "randombot under " + platform.system() + " " + platform.release() + " "
- # set buffer
- buffer_read = ""
- def get_username(username):
- return username[string.find(username, ":")+1:string.find(username, "!")]
- def ctcp_check(socket_inst, line, username):
- """Check a specified line that has been formatted using string.split for CTCP requests, and reply using
- the specified socket instance (socket_inst) to the username."""
- if len(line) == 4:
- if (line[3] == ":\x01VERSION\x01"):
- print "[" + username + " VERSION]"
- socket_inst.send("NOTICE " + username+" :" + chr(001) + "VERSION " + VERSION_REPLY + chr(001) + "\r\n")
- if (line[3] == ":\x01TIME\x01"):
- print "[" + username + " TIME]"
- # Format: Sat Aug 02 01:07:59 2008
- socket_inst.send("NOTICE " + username+" :" + chr(001) + "TIME " + time.strftime("%a %b %d %H:%M:%S %Y", time.gmtime()) + chr(001) + "\r\n")
- if (line[3] == ":\x01FINGER\x01"):
- print "[" + username + " FINGER]"
- socket_inst.send("NOTICE " + username+" :" + chr(001) + "FINGER Quit it!" + chr(001) + "\r\n")
- if len(line) == 5:
- # Check for a properly crafted ping reply
- if (line[3] == ":\x01PING") and re.compile(r'[0123456789]').sub('', line[4]) == "\x01":
- # picks out timestamp
- recv_timestamp = line[4][0:len(line[4])-1]
- socket_inst.send("NOTICE " + username+" :" + chr(001) + "PING " + str(recv_timestamp) + chr(001) + "\r\n")
- # Join the network
- s=socket.socket( )
- s.connect((NETWORK, PORT))
- s.send("NICK %s\r\n" % NICK)
- s.send("USER %s %s bla :%s\r\n" % (IDENT, NETWORK, REALNAME))
- while 1:
- buffer_read=buffer_read+s.recv(1024)
- buffer = string.split(buffer_read, "\n")
- buffer_read=buffer.pop( )
- for line in buffer:
- line=string.rstrip(line)
- # print line with timestamp
- print time.strftime("[%H:%M] ", time.gmtime()) + line
- # Split lines for further analysis
- line=string.split(line)
- # check for MODE (means we're probably connected.) and join channel
- # NOTE: No idea if this is how it should be done or if it'll work on other networks (only for this example!!)
- if (line[0] == ":"+NICK) and (line[1] == "MODE") and (line[2] == NICK):
- s.send("JOIN " + CHANNEL + "\r\n")
- # Respond to server PING/PONG event.
- if(line[0]=="PING"):
- s.send("PONG " + line[1] + "\r\n")
- # PRIVMSG could be a CTCP request, \x01 would confirm this.
- if line[1] == "PRIVMSG" and string.find(line[3], ":\x01") == 0:
- ctcp_check(s, line, get_username(line[0]))
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.