Хранилища Subversion pytwidcpp

Сравнить редакции

Не учитывать пробелы Редакция 1 → Редакция 2

/branches/current/tools/__init__.py
Новый файл
0,0 → 1,2
# -*- coding: utf-8 -*-
""" Direct Connect tools package"""
/branches/current/tools/dctools.py
Новый файл
0,0 → 1,64
# -*- coding: utf-8 -*-
import sys
 
class PacketProcessor:
""" Class for processing tcp packets,
and make DC++ packets. (Merge packets from
one command). """
def __init__(self):
""" Clear buffer and initialize class. """
buffer = ""
 
def _isFull(self, command):
""" Check buffer for full command. """
if len(command) == 0:
return False
if command[len(command) - 1] == '|':
return True
else:
return False
 
def Process(self, socket, packet, commandProcessor):
""" Process new tcp packet. """
# Save previous packets
prev = self.buffer
# Clear buffer
self.buffer = ""
# Merge previous and packet
buf = prev + packet
# Split
commands = buf.split('|')
# If Merge result not is full command.
if not self._isFull(buf):
# If count of commands in Merge result is 1
if len(commands) == 1:
# Save Merge result to buffer
self.buffer = commands[0]
commands = []
else:
# Save first from all commands to buffer
self.buffer = commands[len(commands) - 1]
commands = commands[0:len(commands)-1]
# Process every command
for i in commands:
commandProcessor(i, socket)
 
def ClearBuffer(self):
self.buffer = ""
 
 
def createKey(lock):
""" Function create key from lock for DC connections. """
key = {}
for i in xrange(1, len(lock)):
key[i] = ord(lock[i]) ^ ord(lock[i-1])
key[0] = ord(lock[0]) ^ ord(lock[len(lock)-1]) ^ ord(lock[len(lock)-2]) ^ 5
for i in xrange(0, len(lock)):
key[i] = ((key[i]<<4) & 240) | ((key[i]>>4) & 15)
out = ''
for i in xrange(0, len(lock)):
out += unichr(key[i])
out = out.replace(u'\0', u'/%DCN000%/').replace(u'\5', u'/%DCN005%/').replace(u'\44', u'/%DCN036%/')
out = out.replace(u'\140', u'/%DCN096%/').replace(u'\174', u'/%DCN124%/').replace(u'\176', u'/%DCN126%/')
return out
 
/branches/current/client.py
Новый файл
0,0 → 1,31
# -*- coding: utf-8 -*-
""" DC++ Networks Client"""
 
from twisted.internet import reactor, protocol
from nmdc import NMDCClientToHub
 
class NMDCFactory(protocol.ClientFactory):
protocol=NMDCClientToHub
 
def startedConnecting(self, connector):
print "Started Connecting"
pass
 
def clientConnectionFailed(self, connector, reason):
print "Connection Failed"
reactor.stop()
 
def clientConnectionLost(self, connector, reason):
print "Connection Lost"
reactor.stop()
 
 
# this connects the protocol to a server runing on port 8000
def main():
f = NMDCFactory()
reactor.connectTCP("127.0.0.1", 31337 , f)
reactor.run()
 
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
/branches/current/__init__.py
Новый файл
0,0 → 1,3
# -*- coding: utf-8 -*-
""" Python-twisted NMDC/ADC Client/Library """
 
/branches/current/nmdc.py
Новый файл
0,0 → 1,105
# -*- coding: utf-8 -*-
""" Neo-Modus Direct Connect Protocol Package """
from protocol import NMDC
 
class NMDCClientToHub(NMDC):
def nmdc_BadPass(self):
pass
 
def nmdc_BotList(self):
pass
 
def nmdc_Close(self):
pass
 
def nmdc_ConnectToMe(self):
pass
 
def nmdc_ForceMove(self):
pass
 
def nmdc_GetNickList(self):
pass
 
def nmdc_GetPass(self):
pass
def nmdc_Hello(self, params):
self.sendLine("$Version 1,0091|$GetNickList|$MyINFO $ALL twistedbot TEST<++ V:0.698,M:A,H:1/0/0,S:5>$ $0.005$$0$|")
#self.makeAnswer("Version","1,0099")
#self.makeAnswer("GetNickList","")
#self.makeAnswer("MyINFO","$ALL twistedbot<++ V:0.698,M:A,H:1/0/0,S:5>$ $0.005$$0$")
 
def nmdc_HubIsFull(self):
pass
 
def nmdc_HubName(self, params):
pass
 
def nmdc_HubTopic(self):
pass
 
def nmdc_Key(self, params):
print "nmdc_Key"
pass
 
def nmdc_Kick(self):
pass
 
def nmdc_Lock(self, params):
from tools.dctools import createKey
keylock = createKey(params)
print "nmdc_Lock: %s" %keylock
self.makeAnswer("Supports",
"UserCommand NoGetINFO NoHello UserIP2 TTHSearch ZPipe0")
self.makeAnswer("Key", keylock)
self.makeAnswer("ValidateNick", "twistedbot")
 
def nmdc_LogedIn(self):
pass
 
def nmdc_MyINFO(self):
pass
 
def nmdc_MyPass(self):
pass
 
def nmdc_NickList(self):
pass
 
def nmdc_OpForceMove(self):
pass
 
def nmdc_OpList(self):
pass
 
def nmdc_Quit(self):
pass
 
def nmdc_RevConnectToMe(self):
pass
 
def nmdc_Search(self):
pass
 
def nmdc_SR(self):
pass
 
def nmdc_Supports(self, params):
pass
 
def nmdc_UserCommand(self):
pass
 
def nmdc_UserIP(self):
pass
 
def nmdc_Version(self):
pass
 
def nmdc_ValidateNick(self):
pass
 
def nmdc_ValidateDenide(self):
pass
 
/branches/current/protocol.py
Новый файл
0,0 → 1,84
# -*- coding: utf-8 -*-
""" PyCODC NMDC/ADC Client """
from twisted.internet import protocol
from twisted.protocols import basic
from twisted.python import log
 
import socket
import re
 
class NMDC(protocol.Protocol):
"""
NMDC Protocol implementation
"""
 
buffer = ""
encoding = None
hostname = None
 
def dataReceived(self, data):
print "Data: %s" % data
lines = (self.buffer + data).split("|")
print self.buffer
for line in lines:
if len(line) <= 2:
continue
print "Line: %s" % line
command, params = self.parseLine(line)
self.handleCommand(command, params)
 
def connectionLost(self, reason):
print "Conlost: %s" %self
pass
 
def connectionMade(self):
print "Made connect"
if self.hostname is None:
self.hostname = socket.getfqdn()
 
def sendLine(self, line):
_to_send = line.encode('utf-8')
self.transport.write(_to_send)
 
def parseLine(self, line):
"""
Returns NMDC command and params
"""
print "parse: %s" % line
if line.startswith("$"):
try:
m = re.match("^\$(?P<command>[a-zA-Z]+)[ ](?P<params>.+$)", line)
 
command = m.group("command")
params = m.group("params")
print "NMDC Command: %s; Params: %s" %(command, params)
except:
print "Exception: %s"% line
return command, params
else:
return "", ""
 
def handleCommand(self, command, params):
print "handleCommand: %s %s" % (command, params)
method = getattr(self, "nmdc_%s" % command, None)
try:
if method is not None:
method(params)
else: self.nmdc_unknown(command, params)
except:
print "handleCommand except"
log.deferr()
pass
 
def makeAnswer(self, command, params):
if params is not "":
answer = "$%s %s|" % (command, params)
else:
answer = "$%s|"% command
 
self.sendLine(answer)
print "makeAnswer: %s" % answer
 
def nmdc_unknown(self, command, params):
print "nmdc_%s doesn't exist. Called with params: %s" % (command,params)