Advertisement
Aha2Y

Python, PyBNC - Rev 4

Apr 2nd, 2012
2,735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PyCon 7.65 KB | None | 0 0
  1. #!/usr/bin/python
  2. from ConfigParser import RawConfigParser as ConfParser
  3. from optparse import OptionParser
  4. from collections import deque
  5. from time import sleep, time, gmtime, strftime
  6. import ConfigParser
  7. import datetime
  8. import os
  9. import sys
  10. import socket
  11. import codecs
  12. from irc import *
  13. from select import select
  14.  
  15. os.system('clear')
  16. timestamp = '[' +'\x1b[32m' + datetime.datetime.now().strftime("%a %d, %H:%M:%S") + '\x1b[0m' + ']'
  17. my = IRCbot()
  18. start = time()
  19.  
  20. print('%s Starting Pybnc by Aha2Y. (Pid number: %s)' % (timestamp, os.getpid()))
  21. try:
  22.    config = ConfigParser.RawConfigParser()
  23.    config.readfp(codecs.open("config.ini", "r", "utf8"))
  24.    chanQueue = deque() ; channels = []
  25.    print('%s Successfully loaded config.ini' % (timestamp))
  26. except IOError as config:
  27.    print('%s [\x1b[31mError\x1b[0m] PyZNC Can\'t read config.ini...' % (timestamp))
  28.    print('%s [\x1b[31mError\x1b[0m] Be sure you have the config file in the same dir as PyZNC is!' % (timestamp))
  29.    sys.exit()
  30.    
  31. irc = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  32. bnc = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  33. print('%s Connecting to the IRC server (%s:%s)' % (timestamp, config.get('irc-connection', 'server'), config.getint('irc-connection', 'port')))
  34. print('%s Connecting to the BNC server (%s:%s)' % (timestamp, config.get('bnc-connection', 'server'), config.getint('bnc-connection', 'port')))
  35. irc.connect ((config.get('irc-connection', 'server'),config.getint('irc-connection', 'port')))
  36. bnc.connect ((config.get('bnc-connection', 'server'),config.getint('bnc-connection', 'port')))
  37. irc.send ('NICK %s\r\n' % config.get('irc-bot', 'nickname'))
  38. bnc.send ('NICK %s\r\n' % config.get('irc-bot', 'nickname'))
  39. irc.send ('USER %s %s pybnc :%s\r\n' % (config.get('irc-bot', 'nickname'), config.get('irc-bot', 'ident'), config.get('irc-bot', 'realname')))
  40. bnc.send ('USER %s %s pybnc :%s\r\n' % (config.get('irc-bot', 'nickname'), config.get('irc-bot', 'ident'), config.get('irc-bot', 'realname')))
  41.  
  42. while 1:
  43.     (rl, wl, xl) = select([irc, bnc], [], [])
  44.     for sock in rl:
  45.         data = readline(sock)
  46.         raw = parse(data)
  47.         if sock == bnc:
  48.             if raw[0] == "PING":
  49.                sock.send("PONG %s\r\n" % raw[1])
  50.                print('%s Received ping from the bnc server, Sending pong.' % timestamp)
  51.             print "[\x1b[32mbnc\x1b[0m]", raw
  52.             if len(raw) > 3 and raw[3] == "*** You need to send your password. Try /quote PASS <username>:<password>":
  53.                bnc.send('PASS %s \r\n' % config.get('bnc-connection', 'login'))
  54.                print('%s Bouncer Connection established.' % timestamp)
  55.             if raw[0] == "*status!znc@znc.in":
  56.                if raw[1] == "PRIVMSG":
  57.                   if "Running for" in raw[3]:
  58.                      channel = chanQueue.popleft()
  59.                      uptime = " ".join(raw[3:])
  60.                      irc.send('PRIVMSG %s :The bouncer is %s\r\n' % (channel, uptime))
  61.                   if "- http://znc.in" in raw[3]:
  62.                      version = raw[3].strip('- http://znc.in')
  63.                   if "IPv6:" in raw[3]:
  64.                      channel = chanQueue.popleft()
  65.                      version2 = " ".join(raw[3:])
  66.                      irc.send('PRIVMSG %s :The bouncer is running: %s\r\n' % (channel, version))
  67.                      irc.send('PRIVMSG %s :And the settings: %s\r\n' % (channel, version2))
  68.         elif sock == irc:
  69.             if raw[0] == "PING":
  70.                sock.send("PONG %s\r\n" % raw[1])
  71.                print('%s Received ping from the irc server, Sending pong.' % timestamp)
  72.             print "[\x1b[31mirc\x1b[0m]", raw
  73.             if raw[1] == "439" and raw[3] == "Please wait while we process your connection.":
  74.                my.server = raw[0]
  75.             if raw[1] == "376" and raw[3] == "End of /MOTD command.":
  76.                irc.send('JOIN %s \r\n' % config.get('irc-bot', 'mainchan'))
  77.                print('%s IRC Connection established.' % timestamp)
  78.             if raw[0].startswith("NickServ!"):
  79.                if raw[1] == "NOTICE" and "This nickname is registered" in raw[3]:
  80.                   irc.send('PRIVMSG NickServ :identify %s \r\n' % config.get('irc-bot', 'nickpass'))
  81.             if raw[1] == "PRIVMSG":
  82.                my.sender = raw[0]
  83.                my.nick = raw[0].split("!")[0]
  84.                my.auth = data.split('@')[0][1:]
  85.                if len(raw) > 3:
  86.                   if raw[3] == "!uptime":
  87.                      chanQueue.append(raw[2])
  88.                      end = time()
  89.                      uptime = end - start
  90.                      bnc.send('PRIVMSG *status :uptime \r\n')
  91.                      irc.send('PRIVMSG %s :The bot is running for %s \r\n' % (raw[2], duration_human(uptime)))
  92.                if raw[3] == "!rehash":
  93.                   try:
  94.                      if config.get('staff', my.auth) == 'admin':
  95.                         chanQueue.append(raw[2])
  96.                         bnc.send('PRIVMSG *status :rehash\r\n')
  97.                         irc.send('PRIVMSG %s :Bouncer successfuly rehashed!\r\n' % raw[2])
  98.                   except ConfigParser.NoOptionError:
  99.                      irc.send('PRIVMSG %s :I don\'t recognize you. \r\n' % raw[2])
  100.                if raw[3] == "!shutdown":
  101.                   try:
  102.                      if config.get('staff', my.auth) == 'admin':
  103.                         chanQueue.append(raw[2])
  104.                         bnc.send('PRIVMSG *status :shutdown\r\n')
  105.                   except ConfigParser.NoOptionError:
  106.                      irc.send('PRIVMSG %s :I don\'t recognize you. \r\n' % raw[2])
  107.                if raw[3] == "!restart":
  108.                   try:
  109.                      if config.get('staff', my.auth) == 'admin':
  110.                         chanQueue.append(raw[2])
  111.                         bnc.send('PRIVMSG *status :reboot\r\n')
  112.                         irc.send('PRIVMSG %s :Restarting server...\r\n' % raw[2])
  113.                   except ConfigParser.NoOptionError:
  114.                      irc.send('PRIVMSG %s :I don\'t recognize you. \r\n' % raw[2])
  115.                if raw[3].startswith("!global "):
  116.                   try:
  117.                      if config.get('staff', my.auth) == 'admin':
  118.                         globalmsg = raw[3].split(" ", 1)[1]
  119.                         globalmessage = " ".join(raw[3:])
  120.                         bnc.send('PRIVMSG *status :broadcast %s\r\n' % globalmsg)
  121.                         irc.send('PRIVMSG %s :[Global] %s\r\n' % (raw[2], globalmsg))
  122.                   except ConfigParser.NoOptionError:
  123.                      irc.send('PRIVMSG %s :I don\'t recognize you. \r\n' % raw[2])
  124.                   if raw[3] == "!version":
  125.                      chanQueue.append(raw[2])
  126.                      bnc.send('PRIVMSG *status :version \r\n')
  127.                   if raw[3] == "!help":
  128.                      irc.send('PRIVMSG %s :Available commands: !version, !uptime, !whoami, !rehash, !restart, !shutdown, !global\r\n' % raw[2])
  129.                   if raw[3] == "!get nick":
  130.                      irc.send('PRIVMSG %s :%s \r\n' % (raw[2], my.nick))
  131.                   if raw[3] == "!get sender":
  132.                      irc.send('PRIVMSG %s :%s \r\n' % (raw[2], my.sender))
  133.                   if raw[3] == "!get server":
  134.                      irc.send('PRIVMSG %s :%s \r\n' % (raw[2], my.server))
  135.                   if raw[3] == "!whoami":
  136.                      try:
  137.                         if config.get('staff', my.auth) == 'admin':
  138.                            irc.send('PRIVMSG %s :You are authenticated as admin. \r\n' % raw[2])
  139.                      except ConfigParser.NoOptionError:
  140.                            irc.send('PRIVMSG %s :I don\'t recognize you. \r\n' % raw[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement