Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Very Very Basic IRC bot
- # Note: more, uh, structure would be nice
- import socket
- cmds = {}
- # TCP/IP protocol!!
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # your command char
- cmdchar = "!"
- # takes a tuple
- s.connect(("irc.freenode.net", 6667))
- # initialization
- # fill in your values here because I cba
- s.sendall(b"""NICK probot
- USER probot 8 * :protcom's awesome as fuck irc bot
- JOIN ##powder-bots""")
- # infinite receive
- while True:
- # blocks until data received
- data = s.recv(1024)
- lines = data.split('\r\n')
- # TODO: handle IRC lines that get broken by the 1024-byte block limit
- # [ ... PRIVMSG ##po] [wder-bots :\important command\r\n ...]
- # ":boxmein!boxmein@boxmein PRIVMSG ##powder-bots :\command arg1"
- for line in lines:
- # "", "boxmein!boxmein@boxmein PRIVMSG ##powder-bots", "\command arg1"
- colons = line.split(':')
- # boxmein!boxmein@boxmein, PRIVMSG, ##powder-bots
- ircwords = line[1].split(' ')
- # Host masks are nickname!username@IP address/vhost
- hostmask = ircwords[0]
- # IRC protocol command, eg PRIVMSG/JOIN/...
- command = ircwords[1]
- # you should parse this part differently depending on each command
- # ... as different commands take different numbers of arguments
- if command == 'PRIVMSG':
- target = ircwords[2]
- text = colons[1]
- # basic command char detection
- if text[0:len(cmdchar)] == cmdchar:
- cmdword = text[len(cmdchar):]
- if cmdword in cmds:
- s.sendall(cmds[cmdword](text))
- # TODO: respond to PINGs!
- # see https://docs.python.org/3/library/socket.html
- # see https://en.wikipedia.org/wiki/List_of_Internet_Relay_Chat_commands
- # see https://tools.ietf.org/html/rfc1459
- # see https://tools.ietf.org/html/rfc2812 (some new stuff)
Advertisement
Add Comment
Please, Sign In to add comment