]> jfr.im git - z_archive/pyp10.git/blob - pyp10.py
Move join code into pyp10.py
[z_archive/pyp10.git] / pyp10.py
1 #!/usr/bin/python
2
3 import socket, time
4
5
6 uplink = None
7 modules = {}
8 modcount = 0
9
10 class config(object):
11 name = 'services.p10'
12 numeric = ']S'
13 uplink = {
14 'address': '127.0.0.1',
15 'port': 4400,
16 'name': 'test.p10',
17 'password': 'password',
18 'vhost': '', #bind to this ip - empty string '' for auto-select
19 }
20 autoload = ['q', 'do']
21
22 b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]"
23
24
25 class Uplink(object):
26 def __init__(self):
27 global uplink
28 uplink = self
29
30 self.lastnum = None # last numeric used, as [int,int,int]
31 self.nicks = {} # 'nick': Pseudo-object
32 self.nums = {} # 'num': Pseudo-object
33 self.data = "" # receive buffer
34
35 self.bursting = True
36 self.bursted = 0
37 self.burstchans = {}
38
39 self.sock = socket.socket()
40 self.sock.bind((config.uplink['vhost'], 0))
41 self.sock.connect((config.uplink['address'], config.uplink['port']))
42
43 self._transmit("PASS %s" % (config.uplink['password']))
44 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})
45 def send(self, line, source=None, **kwargs):
46 if source is None:
47 source = config.numeric
48 self._transmit(source+" "+(line % kwargs))
49 def _transmit(self, line):
50 print ">", line
51 self.sock.sendall(line+"\r\n")
52 def _receive(self):
53 self.data += self.sock.recv(4096)
54 while "\n" in self.data:
55 pieces = self.data.split("\n", 1)
56 line = pieces[0].strip()
57 print "<", line
58 self._process(line)
59 self.data = pieces[1]
60 return True
61 def loop(self):
62 keepgoing = True
63 while keepgoing:
64 if self.bursting and self.bursted >= modcount:
65 self._burstisdone()
66 keepgoing = self._receive()
67
68 def _process(self, line):
69 words = line.split()
70 if words[1] == "G" or words[1] == "PING":
71 self.send("Z %(numeric)s :%(id)s" % {'numeric': config.numeric, 'id': config.uplink['name']})
72 elif words[1] == "EB":
73 self.send("EA")
74 elif words[1] == "P" or words[1] == "PRIVMSG":
75 source = words[0]
76 target = words[2]
77 extra = ' '.join(words[3:])
78 if extra[0] == ':':
79 extra = extra[1:]
80 if '@' in target:
81 tonick = target.split('@', 1)
82 self.nicks[tonick].gotmsg(extra, source, target)
83 elif '#' in target:
84 pass # no processing
85 elif '$' in target:
86 pass # no processing
87 else:
88 self.nums[target].gotmsg(extra, source, target)
89 def _newnum(self):
90 if self.lastnum is None:
91 self.lastnum = [0,0,0]
92 else:
93 self.lastnum = [i+1 for i in self.lastnum]
94 num = config.numeric
95 num += b64[self.lastnum[2]]
96 num += b64[self.lastnum[1]]
97 num += b64[self.lastnum[0]]
98 return num
99
100 def makenick(self, obj, nick, ident, realname):
101 newnum = self._newnum()
102 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)
103 self.nums[newnum] = obj
104 self.nicks[nick] = obj
105 return newnum
106 def join(self, chan, source, op=False):
107 # if self.bursting:
108 # if chan not in self.burstchans:
109 # self.burstchans[chan] = {'ops':[], 'regs':[]}
110 #
111 # if op:
112 # self.burstchans[chan]['ops'].append(source)
113 # else:
114 # self.burtschans[chan]['regs'].append(source)
115 # else: # not bursting
116 self.send("J %(chan)s %(time)s", source, chan=chan, time=time.time()+3600)
117 if op:
118 self.send("OM %(chan)s +nto %(num)s", source, chan=chan, num=source)
119 def endburst(self, module):
120 self.bursted += 1
121 print module.num, self.bursted, modcount
122 def _burstisdone(self):
123 self.bursting = False
124 for chname, chan in self.burstchans.iteritems():
125 users = chan['regs']
126
127 if len(chan['ops']) != 0:
128 chan['ops'][0] += ':o'
129 users.extend(chan['ops'])
130
131 mems = ','.join(users)
132 self.send("B %(chan)s 780000001 +nt %(members)s", chan=chname, members=mems)
133 self.send("EB")
134 self.burtchans = {}
135
136 class Account(object):
137 pass
138
139 class Client(object):
140 pass
141
142 uplink = Uplink()
143
144 for modu in config.autoload:
145 modules[modu] = (__import__('modules.'+str(modu), globals(), locals(), ['Pseudo'], 0)).Pseudo(uplink)
146 modcount += 1
147
148 uplink.loop()