boxmein

basic as hell IRC bot

Jul 3rd, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Very Very Basic IRC bot
  3. # Note: more, uh, structure would be nice
  4.  
  5.  
  6. import socket
  7. cmds = {}
  8.  
  9. # TCP/IP protocol!!
  10. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11.  
  12. # your command char
  13. cmdchar = "!"
  14.  
  15. # takes a tuple
  16. s.connect(("irc.freenode.net", 6667))
  17.  
  18. # initialization
  19. # fill in your values here because I cba
  20. s.sendall(b"""NICK probot
  21. USER probot 8 * :protcom's awesome as fuck irc bot
  22. JOIN ##powder-bots""")
  23.  
  24. # infinite receive
  25. while True:
  26.   # blocks until data received
  27.   data = s.recv(1024)
  28.   lines = data.split('\r\n')
  29.   # TODO: handle IRC lines that get broken by the 1024-byte block limit
  30.   # [ ... PRIVMSG ##po] [wder-bots :\important command\r\n ...]
  31.  
  32.  
  33.   # ":boxmein!boxmein@boxmein PRIVMSG ##powder-bots :\command arg1"
  34.   for line in lines:
  35.  
  36.     # "", "boxmein!boxmein@boxmein PRIVMSG ##powder-bots", "\command arg1"
  37.     colons = line.split(':')
  38.  
  39.     # boxmein!boxmein@boxmein, PRIVMSG, ##powder-bots
  40.     ircwords = line[1].split(' ')
  41.  
  42.     # Host masks are nickname!username@IP address/vhost
  43.     hostmask = ircwords[0]
  44.  
  45.     # IRC protocol command, eg PRIVMSG/JOIN/...
  46.     command = ircwords[1]
  47.  
  48.  
  49.     # you should parse this part differently depending on each command
  50.     # ... as different commands take different numbers of arguments
  51.     if command == 'PRIVMSG':
  52.       target = ircwords[2]
  53.       text = colons[1]
  54.       # basic command char detection
  55.       if text[0:len(cmdchar)] == cmdchar:
  56.         cmdword = text[len(cmdchar):]
  57.         if cmdword in cmds:
  58.           s.sendall(cmds[cmdword](text))
  59.     # TODO: respond to PINGs!
  60.     # see https://docs.python.org/3/library/socket.html
  61.     # see https://en.wikipedia.org/wiki/List_of_Internet_Relay_Chat_commands
  62.     # see https://tools.ietf.org/html/rfc1459
  63.     # see https://tools.ietf.org/html/rfc2812 (some new stuff)
Advertisement
Add Comment
Please, Sign In to add comment