Advertisement
Aha2Y

Untitled

Apr 3rd, 2012
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. #!/usr/bin/python
  2. from ConfigParser import RawConfigParser as ConfParser
  3. from BeautifulSoup import BeautifulSoup
  4. from optparse import OptionParser
  5. from collections import deque
  6. from time import sleep, time, gmtime, strftime
  7. import ConfigParser
  8. import os
  9. import sys
  10. import socket
  11. import codecs
  12. from irc import *
  13. from select import select
  14. import urllib
  15. import imp
  16. import modules.fml
  17. import modules.meme
  18. import modules.bitly
  19.  
  20. my = IRCbot()
  21. start = time()
  22.  
  23. try:
  24.    config = ConfigParser.RawConfigParser()
  25.    config.readfp(codecs.open("config.ini", "r", "utf8"))
  26. except IOError as config:
  27.    sys.exit()
  28.    
  29. def main():
  30.     """ A demo daemon main routine, write a datestamp to
  31.        /tmp/daemon-log every 10 seconds.
  32.    """
  33.     import time
  34.  
  35.     f = open("/tmp/daemon-log", "w")
  36.     while 1:
  37.         f.write('%s\n' % time.ctime(time.time()))
  38.         f.flush()
  39.         time.sleep(10)
  40.  
  41.  
  42. if __name__ == "__main__":
  43.     # do the UNIX double-fork magic, see Stevens' "Advanced
  44.     # Programming in the UNIX Environment" for details (ISBN 0201563177)
  45.     try:
  46.         pid = os.fork()
  47.         if pid > 0:
  48.             # exit first parent
  49.             sys.exit(0)
  50.     except OSError, e:
  51.         print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
  52.         sys.exit(1)
  53.  
  54.     # decouple from parent environment
  55.     os.chdir("/")
  56.     os.setsid()
  57.     os.umask(0)
  58.  
  59.     # do second fork
  60.     try:
  61.         pid = os.fork()
  62.         if pid > 0:
  63.             # exit from second parent, print eventual PID before
  64.             print "Daemon PID %d" % pid
  65.             sys.exit(0)
  66.     except OSError, e:
  67.         print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
  68.         sys.exit(1)
  69.    
  70. irc = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  71. irc.connect ((config.get('connection', 'server'),config.getint('connection', 'port')))
  72. irc.send ('NICK %s\r\n' % config.get('irc-bot', 'nickname'))
  73. irc.send ('USER %s %s Internets :%s\r\n' % (config.get('irc-bot', 'ident'), config.get('irc-bot', 'nickname'), config.get('irc-bot', 'realname')))
  74. while 1:
  75.     (rl, wl, xl) = select([irc], [], [])
  76.     for sock in rl:
  77.         data = readline(sock)
  78.         raw = parse(data)
  79.         if sock == irc:
  80.             print raw
  81.             if raw[0] == "PING":
  82.                sock.send("PONG %s\r\n" % raw[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement