asyncio.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env python3
  2. import asyncore
  3. import threading
  4. import time
  5. import re
  6. import socket
  7. import sys
  8. class IRCClient(asyncore.dispatcher):
  9. HOST="irc.irchighway.net"
  10. PORT=6667
  11. NICK="MauBot3321321"
  12. IDENT="maubot"
  13. REALNAME="MauritsBot"
  14. IGNORE=["NOTICE","PART","QUIT", "332","333", "372", "353","366", "251", "252", "254", "255","265","266","396"]
  15. LOOKING_FOR="cryptonomicon"
  16. CHANNEL="#ebooks"
  17. buffer = []
  18. readbuffer=b''
  19. joined=False
  20. connected=False
  21. def __init__(self):
  22. asyncore.dispatcher.__init__(self)
  23. self.create_socket()
  24. self.connect( (self.HOST, self.PORT) )
  25. nickstr="NICK %s\r\n" % self.NICK
  26. userstr="USER %s %s bla :%s\r\n" % (self.IDENT, self.HOST, self.REALNAME)
  27. self.send_queue(nickstr)
  28. self.send_queue(userstr)
  29. def handle_connect(self):
  30. print("connected")
  31. self.connected=True
  32. pass
  33. def handle_close(self):
  34. print("closed")
  35. self.connected=False
  36. self.close()
  37. def handle_read(self):
  38. data=self.recv(8192)
  39. if not (data[-1]==10 and data[-2]==13):
  40. self.readbuffer+=data
  41. return
  42. data=self.readbuffer+data
  43. data=data.decode('utf-8')
  44. self.readbuffer=b''
  45. lines=data.splitlines()
  46. for line in lines:
  47. words=line.split(' ')
  48. if len(words)<2:
  49. continue
  50. msg_from=words[0]
  51. comm=words[1]
  52. # if comm in self.IGNORE:
  53. # continue
  54. if comm=="JOIN" and self.NICK in msg_from:
  55. print("JOINED")
  56. print(msg_from)
  57. #self.who()
  58. #self.search(self.LOOKING_FOR)
  59. #self.book("!Mysfyt Neal Stephenson - Cryptonomicon (v5.0) (mobi).rar ::INFO:: 1.7MB")
  60. #self.book("!Pondering Neal Stephenson - Cryptonomicon (v5.0) (mobi).rar ::INFO:: 2.1MB")
  61. self.joined=True
  62. if comm=="PRIVMSG":
  63. if words[2] != self.NICK:
  64. continue
  65. self.parse_msg(line);
  66. if comm=="PING":
  67. print("PINGGGGG")
  68. self.pong(line)
  69. continue
  70. if comm=="376": #END MOTD
  71. self.join(self.CHANNEL)
  72. continue
  73. # if not self.joined:
  74. # continue
  75. print(line)
  76. def parse_msg(self,msg):
  77. msg=msg.split(':')[2]
  78. msg=msg.replace("\x01","")
  79. print(msg)
  80. args=msg.split(" ")
  81. size=int(args.pop())
  82. port=int(args.pop())
  83. ip=self.ip_from_decimal(int(args.pop()))
  84. args.pop(0) #"DCC"
  85. args.pop(0) #"SEND"
  86. print(args)
  87. filename="_".join(args)
  88. print("File %s ip %s port: %d size: %d " % (filename,ip,port,size))
  89. n=self.netcat(ip,port,size)
  90. fname='/tmp/1234-%s' % filename
  91. f=open(fname, 'wb')
  92. f.write(n)
  93. f.close()
  94. def search(self,book):
  95. print("searching for %s" % book)
  96. self.send_queue("PRIVMSG %s :@search %s \r\n" % (self.CHANNEL,book))
  97. def pong(self,data):
  98. #PING :excalibur.pa.us.irchighway.net
  99. print("PONG??")
  100. print(data)
  101. msg=data.split(':')[1]
  102. print(msg)
  103. self.send_queue("PONG %s\r\n" % msg)
  104. def join(self,channel):
  105. self.send_queue("JOIN %s\r\n" % channel)
  106. def writable(self):
  107. return (len(self.buffer) > 0 and self.connected)
  108. def handle_write(self):
  109. out=self.buffer.pop()
  110. self.send(out)
  111. def send_queue(self,msg):
  112. add=bytes(str(msg),"utf-8")
  113. print("sending: %s" % msg)
  114. self.buffer.append(add)
  115. def book(self,book):
  116. print("asking for %s" % book)
  117. self.send_queue("PRIVMSG %s :%s \r\n" % (self.CHANNEL,book))
  118. def who(self):
  119. self.send_queue("WHO %s" % self.CHANNEL)
  120. def ip_from_decimal(self,dec):
  121. return ".".join([ str(int(b,2)) for b in self.b_octets(bin(dec)[2:]) ])
  122. def b_octets(self,l):
  123. return [l[i:i + 8] for i in range(0, len(l), 8)]
  124. def netcat(self,ip,port,size):
  125. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  126. s.connect((ip,port))
  127. s.shutdown(socket.SHUT_WR)
  128. out=b''
  129. char=["-","\\", "|","/"]
  130. i=0
  131. while True:
  132. data = s.recv(1024)
  133. if data=="":
  134. break
  135. out=out+data
  136. #sys.stdout.write("\r%s" % char[i])
  137. #i=i+1
  138. #if i>=len(char):
  139. # i=0
  140. print("got some data")
  141. if len(out) >= size:
  142. break
  143. s.close()
  144. return out
  145. client = IRCClient()
  146. loop_thread = threading.Thread(target=asyncore.loop, name="Asyncore Loop")
  147. loop_thread.start()