-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlpmcbot.py
54 lines (45 loc) · 1.79 KB
/
lpmcbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
import socket
import string
from botlib import *
HOST = 'irc.freenode.net' # The server we want to connect to
PORT = 6667 # The connection port (commonly 6667 for IRC)
NICK = 'LPMCBot' # The nickname of the bot
USER = 'LPMCbot' # The username of the bot
REALNAME = 'LPMCBot' # The real name of the bot
CHANNEL = '#LPMCBot' # The default channel for the bot
readbuffer = '' # Used to store incoming messages from the server
s = socket.socket() # Create the socket
s.connect((HOST, PORT)) # Connect to the server
# Identify to the server
# Command: USER
# Parameters: <username> <hostname> <servername> <realname>
s.send('USER ' + USER + ' ' + HOST + ' server :' + REALNAME + '\n')
# Give the nickname to the server
# Command: NICK
# Parameters: <nickname> [ <hopcount> ]
s.send('NICK ' + NICK + '\n')
# Infinite listening loop while the bot is in the channel
while True:
line = s.recv(500) # Receive a server message (max 500 characters)
print line # Output the server message
# Look for the freenode welcome message
if 'Welcome to the freenode Internet Relay Chat Network' in line:
# Join the channel
s.send('JOIN ' + CHANNEL + '\n')
# Handle a private message
if 'PRIVMSG' in line:
# Use the message parsing function
send_msg = parsemsg(line)
s.send(send_msg)
if send_msg == 'QUIT\n':
print "QUITTING"
break
# Handle a PING from the server
if 'PING' in line:
# Remove the trailing characters
line = line.rstrip()
# Split the line into an array (using whitespace as a delimiter)
line = line.split()
# Send back PONG with the correct parameter
s.send('PONG ' + line[1] + '\n')