# ------------------------------------------------------------------------------------------------------------------------ # This file is part of the puar irc bot. # Last modified: Wed March 19, 2008. # ------------------------------------------------------------------------------------------------------------------------ # Copyright (c) 2008, BSD License. # A copy can be found here: http:#www.opensource.org/licenses/bsd-license.php # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * Neither the name of the Garabatos de licor nor the names of its contributors may # be used to endorse or promote products derived from this software # without specific prior written permission. # # This software is provided by the copyright holders and contributors # "as is" and any express or implied warranties, including, but not # limited to, the implied warranties of merchantability and fitness for # a particular purpose are disclaimed. in no event shall the copyright owner or # contributors be liable for any direct, indirect, incidental, special, # exemplary, or consequential damages (including, but not limited to, # procurement of substitute goods or services; loss of use, data, or # profits; or business interruption) however caused and on any theory of # liability, whether in contract, strict liability, or tort (including # negligence or otherwise) arising in any way out of the use of this # software, even if advised of the possibility of such damage. # Import all the needed python libraries. import bisect, datetime, re, select, socket, string, sys, time, types # ------------------------------------------------------------------------------------------------------------------------ # Irc configuration. # ------------------------------------------------------------------------------------------------------------------------ # Irc settings. # These set the default values for the bot # connection, host, realname and so on. ircHost = 'dune.irc-hispano.org' ircPort = 6667 ircPass = None ircSock = socket.socket() # ------------------------------------------------------------------------------------------------------------------------ # Bot configuration. # ------------------------------------------------------------------------------------------------------------------------ # Bot settings. # Default values for everything that can # be configured on the irc bot. botNname = 'puarNick' botIdent = 'puarIdent' botRname = 'puarName' botOwner = 'puarOwner' # Configuration as lists. ircConf = [ircHost, ircPort, ircPass] botConf = [botNname, botIdent, botRname, botOwner] # Channels to join on connect. # When puar connects the irc server, it will try # to join all those channels, in order. botChans = ['#archlinux','#console','#washa'] # ------------------------------------------------------------------------------------------------------------------------ # Errors. # ------------------------------------------------------------------------------------------------------------------------ # Puar error types. # Different error messages as a dict. puarError = { 'puarConfig' : 'The bot is not properly configured', 'puarConnect' : 'Cannot connect to the irc server', 'puarDisconnect' : 'Cannot disconnect from the irc server', 'puarJoinChannel' : 'Cannot join channel specified', 'puarStart' : 'Cannot start puar, revise settings', } # Exit raising a message. # Used for errors. def puarHalt(message): print ':: Error :: ' + puarError[message] exit() # ------------------------------------------------------------------------------------------------------------------------ # Irc related functions. # ------------------------------------------------------------------------------------------------------------------------ # puarConnect() - Irc connection handler. # Try to connect to the irc server configured. def puarConnect(): try: ircSock.connect((ircHost,ircPort)) ircSock.send("NICK %s\r\n" % botNname) ircSock.send("USER %s %s bla :%s\r\n" % (botIdent,ircHost,botRname)) except: puarHalt('puarConnect') # puarDisconnect() - Irc disconnect. # Disconnect from a given irc server gracefully. def puarDisconnect(msg = None): try: ircSock.send("QUIT %s \r\n" % msg) ircSock.close() except: puarHalt('puarDisconnect') # puarJoin() - Join a channel. # Try to join a given irc channel. def puarJoin(chan): try: ircSock.send("JOIN %s \r\n" % chan) except: puarHalt('puarJoinChannel') # puarPart() - Part a channel. # Try to part from a given irc channel. def puarPart(chan, msg = ''): try: ircSock.send("PART %s %s \r\n" % msg) except: puarHalt('puarJoinChannel')