Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import socket
  4. import MySQLdb
  5. import signal
  6. from select import poll, POLLIN
  7. from time import time
  8. from os import getpid
  9. from sys import exit
  10.  
  11. #### DEFINITIONS ####
  12. class state:
  13. ### CONFIG ###
  14. trigger = "!"
  15. db = {
  16. 'host': 'localhost',
  17. 'user': 'devcsw',
  18. 'pass': 'j00m',
  19. 'db' : 'devcsw',
  20. }
  21. pidfile = "snake.pid"
  22. identfile = "/home/bot/.oidentd.conf"
  23.  
  24. ### MISC. ###
  25. uplink = {}
  26.  
  27. def processIn(fh, sock, buf):
  28. bid = socks[fh][0]
  29. if buf.find("PING :") == 0:
  30. raw(sock, "PONG :"+buf[6:])
  31. elif buf.find("PRIVMSG") != -1:
  32. msgBegin = buf.find(':', 1)+1
  33. trigger = buf[msgBegin:msgBegin+1]
  34. if trigger == state.trigger:
  35. nuhMask = buf[1:buf.find(' ')]
  36. nick = nuhMask[0:nuhMask.find('!')]
  37. toChan = buf[buf.find(' ')+9:msgBegin-2]
  38. processMsg(fh, sock, buf, nick, nuhMask, toChan, buf[msgBegin+1:])
  39. def processMsg(fh, sock, buf, nick, nuhMask, toChan, msg):
  40. print "nick="+nick+";nuhMask="+nuhMask+";toChan="+toChan+";msg="+msg
  41.  
  42. def writePid(fn):
  43. fh = open(fn, 'w')
  44. fh.write(str(getpid()))
  45. fh.close()
  46. def writeIdent(fn, ident):
  47. fh = open(fn, 'w')
  48. fh.write("global { reply \""+ident+"\" }")
  49. fh.close()
  50.  
  51. def sigHdl(signum, frm):
  52. if signum == signal.SIGINT:
  53. print str(time())+"*GOT SIGINT - dying"
  54. for v in bots.values():
  55. myS = v[1]
  56. raw(myS, "QUIT :Going away")
  57. myS.close()
  58. exit()
  59.  
  60. def raw(sock, buf):
  61. print str(time())+">"+buf
  62. sock.send(buf+"\r")
  63. def get(sock):
  64. data = sock.recv(1)
  65. if not data: return None
  66. while (data[-1] != "\n"):
  67. data += sock.recv(1)
  68. data = data.strip(" \r\t\n")
  69. print str(time())+"<"+data
  70. return data
  71.  
  72. #### MAIN CODE ####
  73. writePid("snake.pid")
  74. signal.signal(signal.SIGHUP, signal.SIG_IGN)
  75. signal.signal(signal.SIGINT, sigHdl)
  76. dbh = MySQLdb.connect(host=state.db['host'],user=state.db['user'],passwd=state.db['pass'],db=state.db['db'])
  77. poller = poll()
  78. bots = {}
  79. socks = {}
  80.  
  81. cursor = dbh.cursor()
  82. cursor.execute("SELECT id, nick, ident, gecos, irchost, ircport, ircpass FROM snk_bots")
  83. row = cursor.fetchone()
  84. while row is not None:
  85. writeIdent(state.identfile, row[2])
  86.  
  87. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  88. s.connect((row[4], row[5]))
  89. if row[6] is not None: raw(s, "PASS :"+row[6])
  90. raw(s, "NICK "+row[1])
  91. raw(s, "USER "+row[2]+" * * :"+row[3])
  92.  
  93. error = False
  94. buf = get(s)
  95. while buf:
  96. if buf is None:
  97. print str(time())+"*Skipping rest of registration for bot "+str(row[0])
  98. error = True
  99. break
  100. elif buf.find("ERROR :") == 0:
  101. print str(time())+"*Skipping rest of registration for bot "+str(row[0])
  102. error = True
  103. break
  104. elif buf.find("PING :") == 0:
  105. raw(s, "PONG :"+buf[6:])
  106. elif buf.find("433 * "+row[1]) != -1:
  107. raw(s, "NICK "+row[1]+"_")
  108. elif buf.find("001") != -1:
  109. state.uplink[row[0]] = buf[1:buf.find(" ")]
  110. print str(time())+"*Found 001 from "+state.uplink[row[0]]
  111. break;
  112. buf = get(s)
  113.  
  114. if error:
  115. s.close()
  116. dbh.cursor().execute("UPDATE snk_bots SET online = 0 WHERE id = "+str(row[0]))
  117. row = cursor.fetchone()
  118. continue
  119. id = row[0]
  120. bots[id] = (s.fileno(), s)
  121. socks[s.fileno()] = (id, s)
  122. poller.register(s, POLLIN)
  123.  
  124. dbh.cursor().execute("UPDATE snk_bots SET online = 1 WHERE id = "+str(row[0]))
  125.  
  126. row = cursor.fetchone()
  127.  
  128. cursor = dbh.cursor()
  129. cursor.execute("SELECT c.botid, c.chname FROM snk_chans AS c, snk_bots AS b WHERE b.id = c.botid AND b.online = 1")
  130. row = cursor.fetchone()
  131. while row is not None:
  132. id = row[0]
  133. chname = row[1]
  134. raw(bots[id][1], "JOIN "+chname)
  135. row = cursor.fetchone()
  136.  
  137. while True:
  138. readSocks = poller.poll()
  139. for fheve in readSocks:
  140. fh = fheve[0]
  141. s = socks[fh][1]
  142. buf = get(s)
  143. if buf is None:
  144. print str(time())+"*LOST SOCKET "+str(socks[fh][0])
  145. poller.unregister(fh)
  146. else:
  147. processIn(fh, s, buf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement