Operation Manual

practice to use all-capital letters for their namesthat way its easy to see at glance whether a particular section of the code is
using a constant or a variable. Type the following two lines into the program:
RPL_NAMREPLY = 353
RPL_ENDOFNAMES = 366
These are IRC status codes, provided by the server to indicate when particular operations have completed. These are used by
the program to know when it has received the required list of names from the IRC server. Next, set up the variables for the
server connection by entering the following lines:
irc = {
host : chat.freenode.net,
port : 6667,
channel : #raspiuserguide,
namesinterval : 5
}
The first line tells Python to create a dict data type. Short for dictionary, this allows multiple variables to be stored in a single
master variablein this case, irc. These individual variables can then be recalled later in the program. Although you could write
this program without using dicts to store variables, it would make the program significantly more difficult to read. The dict begins
with the opening curly brace, and ends with the closing curly brace on the final line.
The host variable should be set to the fully-qualified domain name (FQDN) of the IRC server to which the program will
connect. In this example, chat.freenode.net is used, but if you want to customise the program to use a different server,
change the domain name here. The port variable tells the program which network port IRC is running on, which will usually be
6667. The channel variable tells Python which channel to join in order to monitor the users, while namesinterval controls
how long the program waits to refresh the list of users, measured in seconds.
Set up a second dict to store the user-specific variables by typing in the following lines:
user = {
nick : botnick,
username : botuser,
hostname : localhost,
servername : localhost,
realname : Raspberry Pi Names Bot
}
As with irc, all these variables are stored within a dict called user to make it clear which variables pertain to which section. The
nick variable should be set to the IRC nickname the program will use. Dont use your usual nickname if youre planning to
connect to the IRC server at the same time; instead, try appending -bot to the end of your name to make it clear that the user is
a program rather than a real person. Do the same with username, and fill in the realname variable with a descriptive message
about whom the bot belongs to. The hostname and servername variables can be left set to localhost, or altered to match
your Internet address.
The socket module requires the user to create a socket object. This object provides network connectivity to the rest of the
program. Create the socket object by typing in the following line:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Next, you need to tell the program to try connecting to the IRC server specified in the variables at the start of the program. Type
the following lines:
print Connecting to %(host)s:%(port)s... % irc
try:
s.connect((irc[host], irc[port]))
except socket.error:
print Error connecting to IRC server
%(host)s:%(port)s % irc
sys.exit(1)
The try and except commands are included in this code for error handling. If the system fails to connect to the server
because the Pi isnt connected to the Internet, for example, or because the server is down for maintenancethe program will
print an error message and gracefully exit. The s.connect line tells the socket module to try connecting to the IRC server, using
the host and port variables held in the irc dict.
If the program doesnt quit from the exception, it has successfully connected to the IRC server. Before you can get a list of