Skip to content

Instantly share code, notes, and snippets.

@Krayons
Created September 26, 2012 21:19
Show Gist options
  • Save Krayons/3790651 to your computer and use it in GitHub Desktop.
Save Krayons/3790651 to your computer and use it in GitHub Desktop.
import directory
import os, sys, inspect
# realpath() with make your script run, even if you symlink it :)
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
# use this if you want to include modules from a subforder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
# Info:
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
# __file__ fails if script is called in different ways on Windows
# __file__ fails if someone does os.chdir() before
# sys.argv[0] also fails because it doesn't not always contains the path
@Krayons
Copy link
Author

Krayons commented Sep 26, 2012

import socket, re

import commands.speak
from ircHelpers import IRCHelper

import os, sys, inspect

realpath() with make your script run, even if you symlink it :)

cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)

use this if you want to include modules from a subforder

cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"commands")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)

class IRCBot:
def init(self, network, port, channel, nickname, tempCacheSize=4096):
self.network = network
self.port = port
self.channel = channel
self.nickname = nickname
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    self.tempCacheSize = tempCacheSize

    self.ircHelper = IRCHelper(self)

    self.commandModule = CommandModule(self.ircHelper)
    self.regexIsCommand = re.compile(r".*(?P<command>!!.*)")



def run(self):
    self.socket.connect((self.network, self.port))
    self.log(self.socket.recv(self.tempCacheSize))
    self.socket.send('NICK %s \r\n' % self.nickname)
    self.socket.send('USER %s some stuff :Python IRC\r\n' % self.nickname)#change this
    self.socket.send('JOIN %s \r\n' % self.channel)
    self.mainLoop()

def mainLoop(self):
    while True:
        recievedData = self.socket.recv(self.tempCacheSize)
        self.log(recievedData)

        isCommand = self.regexIsCommand.match(recievedData)
        if isCommand:
            self.commandModule.runCommand(isCommand.group('command'))

        #temporary quit method, should be changed so only admins can use
        if recievedData.find('!!quit') != -1:
            self.log("Quitting")
            self.socket.send('QUIT\r\n')
            break

        #make sure we don't time out of server
        if recievedData.find('PING') != -1:
            self.socket.send('PONG %s \r\n' % recievedData.split()[1])

def log(self, stringToLog):
    #change eventually to log in a file but for now print is fine
    print stringToLog

ircbot = IRCBot('irc.freenode.net', 6667, '#progether', 'WorkingIRCBot')
ircbot.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment