Advertisement
BeamNG_IRC

Untitled

Mar 2nd, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.87 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import socket, urllib2;
  3. from time import gmtime, strftime;
  4. '''
  5. BeamBot remade.
  6. The old BeamBot was a compilation of horrible scripts written when I really had no idea what I was doing.
  7. By Dan Jones for #BeamNG
  8. '''
  9.  
  10. class BeamBot:
  11.     HostName = "irc.3phasegaming.net"; # the hostname/IP of the IRC server
  12.     Port = 6667; # the port used
  13.     NickName = "BeamBot"; # the bots nickname
  14.     Channel = "#BeamNG" # our default channel to join
  15.     Connected = False; # we use this later when connecting
  16.  
  17. class Logging:
  18.     LogName = "BeamNG.txt" # the file we will log messages to
  19.     CleanLine = "null"; # this will hold the lines we use for logging
  20.     CurrentUser = "null"; # last user who sent a message
  21.     FileLine = "null"; # this is our file line
  22. Bot = BeamBot; # initialize our bot information class
  23. Log = Logging; # initialize our logging class
  24.  
  25. def CheckForCommands(DataLine): # check for commands
  26.     CommandsFile = open("commands.txt", "a+"); # open our file that contains the commands
  27.     for Log.FileLine in CommandsFile.readlines():
  28.         if(Log.FileLine.split(":")[0].upper() == Log.CleanLine.split(" ", 1)[0].upper()): # if the command is found we will send the response to the channel
  29.             CommandsFile.close();
  30.             return "PRIVMSG " + Bot.Channel + " :" + Log.FileLine.split(":", 1)[1]; # return the message we will send to the channel
  31.         Log.FileLine = CommandsFile.readline(); # read a new line into the buffer
  32.     CommandsFile.close();
  33.     return "";
  34.  
  35. def CheckReg(UserName): # this function will check if a user is registered and activated at BeamNG.com
  36.     test = "test";
  37.  
  38. def CheckForFunkyCommands(DataLine): # here we look for some funky commands
  39.     try: # make sure we have the required info
  40.         PossiblyFunky = DataLine.split("!")[1].split(" ")[0];
  41.         FunkyParameter = DataLine.split("!")[1].split(" ", 1)[1];
  42.     except: # funky paramter missing
  43.         return "PRIVMSG " + Bot.Channel + " :Command parameter missing. All '!' commands require atleast one parameter!";
  44.  
  45.     if(PossiblyFunky.upper() == "USER"): # user wants to find someones BeamNG account...
  46.         ToReturn = "PRIVMSG " + Bot.Channel + " :" + "http://www.beamng.com/member.php?username=" + FunkyParameter.replace(" ", "%20"); # set our return variable
  47.         Log.FileLine = ToReturn;
  48.         return ToReturn;
  49.  
  50.  
  51. def ParseRawDataAndLog(DataLine): # this function will return a clean string to log with etc
  52.     try:
  53.         Log.CurrentUser = DataLine.split("!")[0][1:]; # last user to send a message
  54.         Log.CleanLine = DataLine.split("@")[1].split(":", 1)[1]; # the message sent
  55.         if(DataLine.split("@")[1].split(":")[0][-6:].find("JOIN") != -1): # user joined the channel
  56.             print(Log.CurrentUser + " Joined the channel\n");
  57.             LoggingFile = open(Log.LogName, 'a+'); # open our log for writing, ONLY ONCE!
  58.             LoggingFile.write("(" + Log.CurrentUser + " joined the channel)\n"); # write to our logging file
  59.             LoggingFile.close(); # close our file after writing the line
  60.  
  61.             #:root!daniel@daniels.irc.network-2F8176DB.take.awful.pictures KICK #fun TechnicolorDalek :i dont hate you
  62.  
  63.         if(DataLine.split("@")[1].split(":", 1)[0].find("KICK") != -1): # user was kicked
  64.             print(DataLine.split("#")[1].split(" ")[1].split(" ")[0] + " was kicked from the channel. Reason: " + Log.CleanLine + "\n");
  65.             LoggingFile = open(Log.LogName, 'a+'); # open our log for writing, ONLY ONCE!
  66.             LoggingFile.write("(" + DataLine.split("#")[1].split(" ")[1].split(" ")[0] + " was kicked from the channel. Reason: " + Log.CleanLine + ")\n"); # write to our logging file
  67.             LoggingFile.close(); # close our file after writing the line
  68.  
  69.         if((DataLine.split("@")[1].split(":", 1)[0].find("QUIT") != -1) or (DataLine.split("@")[1].split(":", 1)[0].find("PART") != -1) or (DataLine.split("@")[1].split(":", 1)[0].find("TIMEOUT") != -1)): # user quit
  70.             print(Log.CurrentUser + " Left the channel.\n");
  71.             LoggingFile = open(Log.LogName, 'a+'); # open our log for writing, ONLY ONCE!
  72.             LoggingFile.write("(" + Log.CurrentUser + " Left the channel.)\n"); # write to our logging file
  73.             LoggingFile.close(); # close our file after writing the line
  74.  
  75.         # Don't juge this line, he's a very special retard
  76.         if(Log.CurrentUser != Bot.NickName and DataLine.split("@")[1].split(":", 1)[0][-6:].find("JOIN") == -1 and DataLine.split("@")[1].split(":")[0].find("KICK") == -1): # we really want to ignore the bot...
  77.             print("<" + Log.CurrentUser + "> " + " " + Log.CleanLine);
  78.             LoggingFile = open(Log.LogName, 'a+'); # open our log for writing, ONLY ONCE!
  79.             LoggingFile.write(strftime("%H:%M:%S", gmtime()) + " <" + Log.CurrentUser + "> " + " " + Log.CleanLine + "\n"); # write to our logging file
  80.             LoggingFile.close(); # close our file after writing the line
  81.  
  82.  
  83.     except: # line confused as a channel message, simply ignore
  84.         print("Line confused as channel message, ignoring...\n");
  85.  
  86. print("Connecting as: " + Bot.NickName + " to: " + Bot.HostName + " : " + str(Bot.Port));
  87.  
  88. '''
  89. at this point we are ready to create and connect to our socket
  90. we will then run a loop that will recieve information, handle PING/PONG, log messages, parse commands from file etc
  91. '''
  92. try:
  93.     Socket = socket.socket(); # setup our socket for connections
  94.     Socket.connect((Bot.HostName, Bot.Port)); # connect to our IRC server
  95.     Data = Socket.makefile('r', 4096); # our data stream
  96.     MessageToSend = "";
  97. except:
  98.     print("Connection error.\nAre you sure " + Bot.HostName + " has an IRC daemon running on port " + str(Bot.Port) + "?");
  99.     raise SystemExit(0); # stop the bot at this point
  100. # We are connected to the server, we must now send IMPORTANT user information to be accepted
  101. Socket.send("NICK " + Bot.NickName + "\r\n"); # auth with our nickname
  102. Socket.send("USER " + Bot.NickName + " " + Bot.NickName + " " + Bot.NickName + " :" + Bot.NickName + "\r\n"); # auth our real name etc
  103. while True: # this loop will run forever
  104.     DataLine = Data.readline().strip(); # this will hold our raw line of data
  105.     if(DataLine.find("PING")) != -1: # detected ping
  106.         Socket.send("PONG :" + DataLine.split(":")[1]); # send pong
  107.         if(not Bot.Connected): # if we are not connected, we need to auth
  108.             print("Authenticating and joining " + Bot.Channel + "..");
  109.             Socket.send("PRIVMSG R : Login <>\r\n");
  110.             Socket.send("MODE " + Bot.NickName + " +x\r\n"); # set mode +x
  111.             Socket.send("JOIN " + Bot.Channel + "\r\n"); # join our channel
  112.             Bot.Connected = True; #set the bot as connected
  113.     if(len(DataLine) > 1 and Bot.Connected and DataLine.find("!") != -1):
  114.         ParseRawDataAndLog(DataLine); # this will return a clean string that we can use for commans and logging
  115.         if(Log.CleanLine[:1].find("?") != -1): # if the line is a POSSIBLE command, look for it
  116.             MessageToSend = CheckForCommands(DataLine); # this will call our function that will check a list of commands for one matching what a user has typed
  117.            
  118.         if(Log.CleanLine[:1].find("!") != -1): # this line could POSSIBLY be a funky command, lets get FUNKAH
  119.             MessageToSend = CheckForFunkyCommands(Log.CleanLine); # lets look for some funky commands
  120.            
  121.         if(MessageToSend != ""): # make sure we have a message to send
  122.             Socket.sendall("\n" + MessageToSend + "\n"); # send the message to the channel
  123.             LoggingFile = open(Log.LogName, 'a+'); # open our log for writing, ONLY ONCE!
  124.             LoggingFile.write(strftime("%H:%M:%S", gmtime()) + "<" + Bot.NickName + "> " + " " + Log.FileLine.split(":", 1)[1]); # write to our logging file
  125.             LoggingFile.close(); # close our file after writing the line
  126.             MessageToSend = "";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement