Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 53 additions & 50 deletions dtella/client/dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class AbortTransfer_In(BaseDCProtocol):
def __init__(self, nick, dch):

self.nick = nick

# Steal connection from the DCHandler
self.factory = dch.factory
self.makeConnection(dch.transport)
Expand Down Expand Up @@ -249,7 +249,7 @@ def connectionMade(self):
self.addDispatch('$KillDtella', 0, self.d_KillDtella)

self.addDispatch('$MyNick', 1, self.d_MyNick)

# Chat messages waiting to be sent
self.chatq = []
self.chat_counter = 99999
Expand Down Expand Up @@ -302,9 +302,9 @@ def d_KillDtella(self):

def d_MyNick(self, nick):
# This is a fake RevConnect that we should terminate.

dcall_discard(self, 'init_dcall')

if self.state != 'login_1':
self.fatalError("$MyNick not expected.")
return
Expand Down Expand Up @@ -359,7 +359,7 @@ def d_GetInfo(self, nick, _):

if n.dcinfo:
self.pushInfo(n.nick, n.dcinfo)


def d_GetNickList(self):

Expand Down Expand Up @@ -502,7 +502,7 @@ def formatMyInfo(self):
suffix = self.main.state.suffix
if suffix:
loc = '%s|%s' % (loc, suffix)

info[2] = loc + info[2][-1:]

info = '$'.join(info)
Expand Down Expand Up @@ -532,7 +532,7 @@ def d_Search(self, addr_string, search_string):

packet.append(struct.pack('!B', len(search_string)))
packet.append(search_string)

osm.mrm.newMessage(''.join(packet), tries=4)

# If local searching is enabled, send the search to myself
Expand All @@ -543,7 +543,7 @@ def d_Search(self, addr_string, search_string):
def d_PrivateMsg(self, nick, _1, _2, _3, text):

text = remove_dc_escapes(text)

if nick == self.bot.nick:

# No ! is needed for commands in the private message context
Expand All @@ -553,7 +553,7 @@ def d_PrivateMsg(self, nick, _1, _2, _3, text):
def out(text):
if text is not None:
self.bot.say(text)

self.bot.commandInput(out, text)
return

Expand Down Expand Up @@ -715,7 +715,7 @@ def out(out_text, flag=[True]):

if out_text is not None:
self.pushStatus(out_text)

if self.bot.commandInput(out, text[1:], '!'):
return

Expand Down Expand Up @@ -800,7 +800,7 @@ def pushConnectToMe(self, ad, use_ssl):


def pushRevConnectToMe(self, nick):
self.sendLine("$RevConnectToMe %s %s" % (nick, self.nick))
self.sendLine("$RevConnectToMe %s %s" % (nick, self.nick))


def pushSearchRequest(self, ipp, search_string):
Expand All @@ -823,7 +823,7 @@ def scheduleChatRateControl(self):

def cb():
self.chatRate_dcall = reactor.callLater(1.0, cb)

if self.chatq:
args = self.chatq.pop(0)
self.broadcastChatMessage(*args)
Expand Down Expand Up @@ -970,7 +970,7 @@ def cb():
def event_AddNick(self, n):
if not self.isProtectedNick(n.nick):
self.pushHello(n.nick)


def event_RemoveNick(self, n, reason):
if not self.isProtectedNick(n.nick):
Expand All @@ -997,11 +997,11 @@ def event_ChatMessage(self, n, nick, text, flags):


class DCFactory(ServerFactory):

def __init__(self, main, listen_port):
self.main = main
self.listen_port = listen_port # spliced into search results

def buildProtocol(self, addr):
if addr.host != '127.0.0.1':
return None
Expand Down Expand Up @@ -1054,23 +1054,23 @@ def commandInput(self, out, line, prefix=''):
if not local.use_locations:
if cmd[0] in self.location_cmds:
return False

if cmd[0] in self.freeform_cmds:
try:
text = line.split(' ', 1)[1]
except IndexError:
text = None

f(out, text, prefix)

else:
def wrapped_out(line):
for l in word_wrap(line):
if l:
out(l)
else:
out(" ")

f(wrapped_out, cmd[1:], prefix)

return True
Expand All @@ -1091,7 +1091,7 @@ def syntaxHelp(self, out, key, prefix):

location_cmds = frozenset(['SUFFIX','USERS','SHARED','DENSE'])


minihelp = [
("--", "ACTIONS"),
("REJOIN", "Hop back online after a kick or collision"),
Expand Down Expand Up @@ -1181,20 +1181,20 @@ def syntaxHelp(self, out, key, prefix):
"This will list all the known locations, and show how many "
"bytes of data are being shared from each."
),

"DENSE":(
"",
"This will list all the known locations, and show the calculated "
"share density (bytes-per-user) for each."
),

"RANK":(
"<nick>",
"Compare your share size with everyone else in the network, and "
"show which place you're currently in. If <nick> is provided, "
"this will instead display the ranking of the user with that nick."
),

"UDP":(
"<port>",
"Specify a port number between 1-65536 to change the UDP port "
Expand All @@ -1217,7 +1217,7 @@ def syntaxHelp(self, out, key, prefix):
"found then a slow, but exhaustive, network discovery of possible peers "
"on the network will take place until one is found."
),

"INVITE":(
"",
"If you wish to invite another user to join the network using the "
Expand Down Expand Up @@ -1261,7 +1261,7 @@ def handleCmd_HELP(self, out, args, prefix):
if not local.use_locations:
if command in self.location_cmds:
continue

if command == "--":
out("")
out(" --%s--" % description)
Expand All @@ -1284,9 +1284,9 @@ def handleCmd_HELP(self, out, args, prefix):
if not local.use_locations:
if key in self.location_cmds:
raise KeyError

(head, body) = self.bighelp[key]

except KeyError:
out("Sorry, no help available for '%s'." % key)

Expand Down Expand Up @@ -1317,15 +1317,16 @@ def handleCmd_ADD(self, out, args, prefix):
response = urllib2.urlopen(local.read_board_view_url)
text = self.main.pk_enc.decrypt(response.read().decode("base64")) + '\n' + ' '.join(args).lower()
text = '\n'.join(text.split('\n')[-500 : ])
text = self.main.pk_enc.encrypt(text).encode("base64")
text = self.main.pk_enc.encrypt(text).encode("base64")

URL = local.read_board_edit_url
URL = local.read_board_edit_url + local.read_board_edit_id

params = [
('id', local.read_board_edit_id),
('text', text),
('submit', 'Save'),
('mode', 'edit')]
params = {
'text' : text,
'mode' : 'edit',
'submit': 'Save',
'id' : local.read_board_edit_id
}

data = urllib.urlencode(params)
print("Uploading ...")
Expand All @@ -1334,9 +1335,11 @@ def handleCmd_ADD(self, out, args, prefix):
response = urllib2.urlopen(req)
resp_code = response.getcode()
if resp_code == 200:
print(response.geturl())
out(response.url)
print(response.read())
else:
out('Error: Could not upload file. HTTP Response Code %s\n' % resp_code)

except urllib2.URLError, e:
print e
out('Error: Could not connect to %s\n' % URL)
Expand All @@ -1362,7 +1365,7 @@ def handleCmd_UDP(self, out, args, prefix):
out("Changing UDP port to: %d" % port)
self.main.changeUDPPort(port)
return

self.syntaxHelp(out, 'UDP', prefix)


Expand All @@ -1375,7 +1378,7 @@ def handleCmd_ADDPEER(self, out, args, prefix):
else:
if not ad.port:
out("Port number must be nonzero.")

elif ad.auth('sx', self.main):
self.main.state.refreshPeer(ad, 0)
out("Added to peer cache: %s" % ad.getTextIPPort())
Expand All @@ -1394,11 +1397,11 @@ def handleCmd_FINDPEERS(self, out, args, prefix):
if len(args) == 0:
self.main.discoverPeers()
return

self.syntaxHelp(out, 'FINDPEERS', prefix)


def handleCmd_INVITE(self, out, args, prefix):
def handleCmd_INVITE(self, out, args, prefix):
if len(args) == 0:
osm = self.main.osm
if osm:
Expand All @@ -1415,9 +1418,9 @@ def handleCmd_INVITE(self, out, args, prefix):
out(" !addpeer %s" % ad.getTextIPPort())
out("")
return

self.syntaxHelp(out, 'INVITE', prefix)


def handleCmd_PERSISTENT(self, out, args, prefix):
if len(args) == 0:
Expand Down Expand Up @@ -1486,7 +1489,7 @@ def handleCmd_REJOIN(self, out, args, prefix):
out("Rejoining...")
self.dch.doRejoin()
return

self.syntaxHelp(out, 'REJOIN', prefix)


Expand All @@ -1495,7 +1498,7 @@ def handleCmd_USERS(self, out, args, prefix):
if not self.dch.isOnline():
out("You must be online to use %sUSERS." % prefix)
return

self.showStats(
out,
"User Counts",
Expand All @@ -1510,7 +1513,7 @@ def handleCmd_SHARED(self, out, args, prefix):
if not self.dch.isOnline():
out("You must be online to use %sSHARED." % prefix)
return

self.showStats(
out,
"Bytes Shared",
Expand All @@ -1531,7 +1534,7 @@ def compute(u,b):
return (b/u, u)
except ZeroDivisionError:
return (0, u)

self.showStats(
out,
"Share Density",
Expand Down Expand Up @@ -1565,7 +1568,7 @@ def handleCmd_RANK(self, out, args, prefix):
else:
self.syntaxHelp(out, 'RANK', prefix)
return

if target is osm.me:
who = "You are"
else:
Expand Down Expand Up @@ -1595,9 +1598,9 @@ def handleCmd_RANK(self, out, args, prefix):
out("%s %s %d%s place, with a share size of %s." %
(who, tie, rank, suffix, format_bytes(target.shared))
)

def handleCmd_TOPIC(self, out, topic, prefix):

if not self.dch.isOnline():
out("You must be online to use %sTOPIC." % prefix)
return
Expand All @@ -1621,7 +1624,7 @@ def handleCmd_SUFFIX(self, out, text, prefix):

self.main.state.suffix = text
self.main.state.saveState()

out("Set location suffix to \"%s\"" % text)

osm = self.main.osm
Expand All @@ -1642,7 +1645,7 @@ def showStats(self, out, title, compute, format, peers_only):

if peers_only and not n.is_peer:
continue

try:
ucount[n.location] += 1
bcount[n.location] += n.shared
Expand Down Expand Up @@ -1704,7 +1707,7 @@ def handleCmd_VERSION_OVERRIDE(self, out, text, prefix):
def handleCmd_DEBUG(self, out, text, prefix):

out(None)

if not text:
return

Expand Down
Loading