pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

kahrn private pastebin - collaborative debugging tool What's a private pastebin?


Posted by kahrn on Wed 13 Aug 14:16
report abuse | download | new post

  1. """
  2. ctcp.py
  3. author: Ricky Hewitt [kahrn] -- <kahrny@gmail.com>
  4. Public Domain
  5.  
  6. description:
  7. An example/script of how to use IRC CTCP replies using standard python.
  8. Special thanks to Gian Mario Tagliaretti (http://code.activestate.com/recipes/299411/)
  9. for original irc connecting/socket code.
  10. """
  11.  
  12. # external modules
  13. import socket
  14. import string
  15. import time
  16. import platform
  17. import re
  18.  
  19. # Configuration values
  20. NICK = "" #eg: randombot
  21. NETWORK = "" # eg: irc.network.com
  22. PORT = 6667
  23. IDENT=NICK
  24. REALNAME=NICK
  25. CHANNEL = "" # channel to join.. e.g. #test
  26.  
  27. #ctcp values
  28. VERSION_REPLY = "randombot under " + platform.system() + " " + platform.release() + " "
  29.  
  30. # set buffer
  31. buffer_read = ""
  32.  
  33. def get_username(username):
  34.     return username[string.find(username, ":")+1:string.find(username, "!")]
  35.  
  36. def ctcp_check(socket_inst, line, username):
  37.     """Check a specified line that has been formatted using string.split for CTCP requests, and reply using
  38.    the specified socket instance (socket_inst) to the username."""
  39.     if len(line) == 4:
  40.         if (line[3] == ":\x01VERSION\x01"):
  41.             print "[" + username + " VERSION]"
  42.             socket_inst.send("NOTICE " + username+" :" + chr(001) + "VERSION " + VERSION_REPLY + chr(001) + "\r\n")
  43.        
  44.         if (line[3] == ":\x01TIME\x01"):
  45.             print "[" + username + " TIME]"
  46.             # Format: Sat Aug 02 01:07:59 2008
  47.             socket_inst.send("NOTICE " + username+" :" + chr(001) + "TIME " + time.strftime("%a %b %d %H:%M:%S %Y", time.gmtime()) + chr(001) + "\r\n")
  48.            
  49.         if (line[3] == ":\x01FINGER\x01"):
  50.             print "[" + username + " FINGER]"
  51.             socket_inst.send("NOTICE " + username+" :" + chr(001) + "FINGER Quit it!" + chr(001) + "\r\n")
  52.        
  53.     if len(line) == 5:
  54.         # Check for a properly crafted ping reply
  55.         if (line[3] == ":\x01PING") and re.compile(r'[0123456789]').sub('', line[4]) == "\x01":
  56.             # picks out timestamp
  57.             recv_timestamp = line[4][0:len(line[4])-1]
  58.             socket_inst.send("NOTICE " + username+" :" + chr(001) + "PING " + str(recv_timestamp) + chr(001) + "\r\n")
  59.  
  60. # Join the network
  61. s=socket.socket( )
  62. s.connect((NETWORK, PORT))
  63. s.send("NICK %s\r\n" % NICK)
  64. s.send("USER %s %s bla :%s\r\n" % (IDENT, NETWORK, REALNAME))
  65.  
  66.  
  67. while 1:
  68.     buffer_read=buffer_read+s.recv(1024)
  69.     buffer = string.split(buffer_read, "\n")
  70.     buffer_read=buffer.pop( )
  71.  
  72.     for line in buffer:
  73.         line=string.rstrip(line)
  74.         # print line with timestamp
  75.         print time.strftime("[%H:%M] ", time.gmtime()) + line
  76.        
  77.         # Split lines for further analysis
  78.         line=string.split(line)
  79.        
  80.         # check for MODE (means we're probably connected.) and join channel
  81.         # NOTE: No idea if this is how it should be done or if it'll work on other networks (only for this example!!)
  82.         if (line[0] == ":"+NICK) and (line[1] == "MODE") and (line[2] == NICK):
  83.             s.send("JOIN " + CHANNEL + "\r\n")
  84.            
  85.         # Respond to server PING/PONG event.
  86.         if(line[0]=="PING"):
  87.             s.send("PONG " + line[1] + "\r\n")
  88.  
  89.         # PRIVMSG could be a CTCP request, \x01 would confirm this.
  90.         if line[1] == "PRIVMSG" and string.find(line[3], ":\x01") == 0:
  91.             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.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post