-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyTOX.py
executable file
·188 lines (155 loc) · 5.52 KB
/
pyTOX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
###Startup and commandline file
import service
import hash_util
import random
import simple_network
import node
import time
import Queue
import ChatService
from threading import *
import sys
import json
from urllib2 import urlopen
local_mode=False
def myIP():
if not local_mode:
myip = json.load(urlopen('http://httpbin.org/ip'))['origin']
print "just got my ip:", myip
return myip
else:
return "127.0.0.1"
services = {}
commands = {}
myhashkey = None
def add_service(service_object):
s_name = service_object.service_id
services[s_name] = service_object
def attach_services():
for s_name in services.keys():
node.add_service(services[s_name])
commands_list = services[s_name].attach_to_console()
if not commands_list is None:
for c in commands_list:
commands[c] = services[s_name]
chat = None
def setup_Node(addr="localhost", port=None):
global myhashkey, chat
node.IPAddr = addr
node.ctrlPort = port
chat = ChatService.ChatService()
myhashkey = chat.myinfo.hashid
node.thisNode = node.Node_Info(node.IPAddr, node.ctrlPort, myhashkey)
#node.net_server = dummy_network.start(node.thisNode, node.handle_message)
node.net_server = simple_network.NETWORK_SERVICE("", node.ctrlPort)
#### setup services here
#database_name = str(node.thisNode.key)+".db"
#add_service(db.Shelver(database_name))
add_service(service.Internal_Service())
add_service(chat)
#add_service(service.ECHO_service())
#add_service(Topology_Service.Topology())
#add_service(filesystem_service.FileSystem())
#add_service(map_reduce.Map_Reduce_Service())
####
attach_services()
def join_ring(node_name, node_port):
othernode = node.Node_Info(node_name, node_port)
node.join(othernode)
def no_join():
node.create()
class Context(object): #a context describes a place a user can type, and the message has to go somewhere
def __init__(self, name, type, dest):
self.name = name
self.type = type
self.dest = dest
self.chatserv = chat
def focus(self):
if self.type == "channel":
print "chatting on channel",self.dest
else:
print "chatting with", self.dest
def say(self, mstr):
command = ""
args = ""
if self.type == "chat":
command = "/send"
args = self.dest+" "+mstr
elif self.type == "channel":
command = "/post"
args = args = self.dest+" "+mstr
#print "say", command, args
mytarget = chat.handle_command(command, args)
t = Thread(target=mytarget)
t.daemon = True
t.start()
def console():
if not chat.channelsurfer.running:
chat.channelsurfer.start()
curr_context = Context("dev","channel","dev")
curr_context.focus()
userinput = raw_input()
while not ( userinput == "/q" or userinput == "/Q"):
if len(userinput) >0 and userinput[0]!= "/":#handle raw_input
curr_context.say(userinput)
elif len(userinput) >0 and userinput[0]== "/": #this is a command
precommand = userinput.split(" ",1)
command = precommand[0]
if command == "/chan":
args = precommand[1]
curr_context = Context(args, "channel", args)
curr_context.focus()
chat.handle_command("/listen",args)
elif command == "/chat":
args = precommand[1]
if not chat.get_friend(args) is None:
curr_context.focus()
curr_context = Context(args,"chat",args)
elif command == "/help" or command == "/h" or command == "/?":
print """
This is the Help Document:
It is mostly a placeholder
commands:
/chat $friendname --- Directs all input as chat to given friend
/chan $channame --- Directs all input to the given channel
listens to the channel if you are not already
/add $userinfopack --- given the massive pile of encoded RSA key info
for a user, it adds them as a friend
/listen $channame --- adds the channel to the list you listen to
/rename $oldnic $newnic --- renames a friend (only shows change in chats
not channels)
/ping $friend --- sends a ping to check if a friend is online
/who --- pings all your friends
/whoami --- prints the string you need to give other
users in order to add you as a friend
"""
else:
args = ""
if len(precommand) > 1:
args = precommand[1]
mytarget = chat.handle_command(command, args)
t = Thread(target=mytarget)
t.daemon = True
t.start()
userinput = raw_input()
node.net_server.stop()
def main():
myip = myIP()
node.IPAddr = myip
args = sys.argv
if len(args) > 1 and args[1] != "?":
local_port = int(args[1])
else:
local_port = random.randint(9000, 9999)
other_IP = args[2] if len(args) > 2 else "131.96.49.89"
other_port = int(args[3]) if len(args) > 3 else 9000
setup_Node(addr=myip,port=local_port)
if not other_IP is None and not other_port is None:
join_ring(other_IP, other_port)
else:
no_join()
node.startup()
console()
if __name__ == "__main__":
main()