]> jfr.im git - z_archive/pyp10.git/blob - pyp10.py
Updates, add modules/do.py (eval/exec for debugging)
[z_archive/pyp10.git] / pyp10.py
1 #!/usr/bin/python
2
3 import socket, time
4
5
6 uplink = None
7 modules = {}
8
9 class config(object):
10 name = 'services.p10'
11 numeric = ']S'
12 uplink = {
13 'address': '127.0.0.1',
14 'port': 4400,
15 'name': 'test.p10',
16 'password': 'password',
17 'vhost': '', #bind to this ip - empty string '' for auto-select
18 }
19 autoload = ['q', 'do']
20
21 b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]"
22
23
24 class Uplink(object):
25 def __init__(self):
26 global uplink
27 uplink = self
28
29 self.lastnum = None # last numeric used, as [int,int,int]
30 self.nicks = {} # 'nick': Pseudo-object
31 self.nums = {} # 'num': Pseudo-object
32 self.data = "" # receive buffer
33
34 self.sock = socket.socket()
35 self.sock.bind((config.uplink['vhost'], 0))
36 self.sock.connect((config.uplink['address'], config.uplink['port']))
37
38 self._transmit("PASS %s" % (config.uplink['password']))
39 self._transmit("SERVER %(name)s 1 %(time)s %(time)s J10 %(numeric)s]]] +s :PyP10 Services" % {'name': config.name, 'time': time.time(), 'numeric': config.numeric})
40 self.send("EB")
41 def send(self, line, source=None, **kwargs):
42 if source is None:
43 source = config.numeric
44 self._transmit(source+" "+(line % kwargs))
45 def _transmit(self, line):
46 print ">", line
47 self.sock.sendall(line+"\r\n")
48 def _receive(self):
49 self.data += self.sock.recv(4096)
50 while "\n" in self.data:
51 pieces = self.data.split("\n", 1)
52 line = pieces[0].strip()
53 print "<", line
54 self._process(line)
55 self.data = pieces[1]
56 return True
57 def loop(self):
58 keepgoing = True
59 while keepgoing:
60 keepgoing = self._receive()
61
62 def _process(self, line):
63 words = line.split()
64 if words[1] == "G" or words[1] == "PING":
65 self.send("Z %(numeric)s :%(id)s" % {'numeric': config.numeric, 'id': config.uplink['name']})
66 elif words[1] == "P" or words[1] == "PRIVMSG":
67 source = words[0]
68 target = words[2]
69 extra = ' '.join(words[3:])
70 if extra[0] == ':':
71 extra = extra[1:]
72 if '@' in target:
73 tonick = target.split('@', 1)
74 self.nicks[tonick].gotmsg(extra, source, target)
75 elif '#' in target:
76 pass # no processing
77 elif '$' in target:
78 pass # no processing
79 else:
80 self.nums[target].gotmsg(extra, source, target)
81 def _newnum(self):
82 if self.lastnum is None:
83 self.lastnum = [0,0,0]
84 else:
85 self.lastnum = [i+1 for i in self.lastnum]
86 num = config.numeric
87 num += b64[self.lastnum[2]]
88 num += b64[self.lastnum[1]]
89 num += b64[self.lastnum[0]]
90 return num
91
92 def makenick(self, obj, nick, ident, realname):
93 newnum = self._newnum()
94 self.send("N %(nick)s 1 %(time)s %(ident)s %(host)s +doknXr pyp10 DAqAAB %(num)s :%(name)s", nick=nick, ident=ident, name=realname, time=time.time(), host=config.name, num=newnum)
95 self.nums[newnum] = obj
96 self.nicks[nick] = obj
97 return newnum
98
99
100 class Account(object):
101 pass
102
103 class Client(object):
104 pass
105
106 uplink = Uplink()
107
108 for modu in config.autoload:
109 modules[modu] = (__import__('modules.'+str(modu), globals(), locals(), ['Pseudo'], 0)).Pseudo(uplink)
110
111 uplink.loop()