-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodechat.py
More file actions
394 lines (350 loc) · 13.8 KB
/
nodechat.py
File metadata and controls
394 lines (350 loc) · 13.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
""" Multiuser Node Chat script for x/84, https://github.com/jquast/x84
This script was coded by Jeff Quast to be a part of x/84 v1.x and it
was taken out in favour of the splitscreen chatter. It's not supported
anymore and is to be used on your own RISK.
"""
# unfortunately the hardlinemode stuff that irssi and such uses
# is not used, so each line causes a full screen fresh ..
import time
POLL_KEY = 0.15 # blocking ;; how often to poll keyboard
POLL_OUT = 0.25 # seconds elapsed before screen update, prevents flood
CHANNEL = None
NICKS = dict()
EXIT = False
def show_help():
""" return string suitable for response to /help. """
return u'\n'.join((
u' /join #channel',
u' /act mesg',
u' /part [reason]',
u' /quit [reason]',
u' /users',
u' /whois handle',))
def process(mesg):
"""
Process a command recieved by event system. and return string
suitable for displaying in chat window.
"""
from x84.bbs import getsession
session = getsession()
sid, tgt_channel, (handle, cmd, args) = mesg
ucs = u''
# pylint: disable=W0602
# Using global for 'NICKS' but no assignment is done
global NICKS
if (CHANNEL != tgt_channel and 'sysop' not in session.user.groups):
ucs = u''
elif cmd == 'join':
if handle not in NICKS:
NICKS[handle] = sid
ucs = show_join(handle, sid, tgt_channel)
elif handle not in NICKS:
NICKS[handle] = sid
elif cmd == 'part':
if handle in NICKS:
del NICKS[handle]
ucs = show_part(handle, sid, tgt_channel, args)
elif cmd == 'say':
ucs = show_say(handle, tgt_channel, args)
elif cmd == 'act':
ucs = show_act(handle, tgt_channel, args)
else:
ucs = u'unhandled: %r' % (mesg,)
return ucs
def show_act(handle, tgt_channel, mesg):
""" return terminal sequence for /act performed by handle. """
from x84.bbs import getsession, getterminal
session, term = getsession(), getterminal()
return u''.join((
time.strftime('%H:%M'), u' * ',
(term.bold_green(handle) if handle != session.user.handle
else term.green(handle)),
(u':%s' % (tgt_channel,)
if 'sysop' in session.user.groups
else u''), u' ',
mesg,))
def show_join(handle, sid, chan):
""" return terminal sequence for /join performed by handle. """
from x84.bbs import getsession, getterminal
session, term = getsession(), getterminal()
return u''.join((
time.strftime('%H:%M'), u' ',
term.blue('-'), u'!', term.blue('-'),
u' ', term.bold_cyan(handle), u' ',
(u''.join((term.bold_black('['),
term.cyan(sid), term.bold_black(']'), u' ',))
if 'sysop' in session.user.groups else u''),
'has joined ',
term.bold(chan),))
def show_part(handle, sid, chan, reason):
""" return terminal sequence for /part performed by handle. """
from x84.bbs import getsession, getterminal
session, term = getsession(), getterminal()
return u''.join((
time.strftime('%H:%M'), u' ',
term.blue('-'), u'!', term.blue('-'),
u' ', term.bold_cyan(handle), u' ',
(u''.join((term.bold_black('['),
term.cyan(sid), term.bold_black(']'), u' ',))
if 'sysop' in session.user.groups else u''),
'has left ',
term.bold(chan),
u' (%s)' % (reason,) if reason and 0 != len(reason) else u'',))
def show_whois(attrs):
""" return terminal sequence for /whois result. """
from x84.bbs import getsession, getterminal, timeago
session, term = getsession(), getterminal()
return u''.join((
time.strftime('%H:%M'), u' ',
term.blue('-'), u'!', term.blue('-'),
u' ', term.bold(attrs['handle']), u' ',
(u''.join((term.bold_black('['),
term.cyan(attrs['sid']), term.bold_black(']'), u' ',))
if 'sysop' in session.user.groups else u''), u'\n',
time.strftime('%H:%M'), u' ',
term.blue('-'), u'!', term.blue('-'),
u' ', u'CONNECtED ',
term.bold_cyan(timeago(time.time() - attrs['connect_time'])),
' AGO.', u'\n',
time.strftime('%H:%M'), u' ',
term.blue('-'), u'!', term.blue('-'),
u' ', term.bold(u'idlE: '),
term.bold_cyan(timeago(time.time() - attrs['idle'])), u'\n',
))
def show_nicks(handles):
""" return terminal sequence for /users result. """
from x84.bbs import getterminal
term = getterminal()
return u''.join((
time.strftime('%H:%M'), u' ',
term.blue('-'), u'!', term.blue('-'),
u' ', term.bold_cyan('%d' % (len(handles))), u' ',
u'user%s: ' % (u's' if len(handles) > 1 else u''),
u', '.join(handles) + u'\n',))
def show_say(handle, tgt_channel, mesg):
""" return terminal sequence for /say performed by handle. """
from x84.bbs import getsession, getterminal, get_user
session, term = getsession(), getterminal()
return u''.join((
time.strftime('%H:%M'), u' ',
term.bold_black(u'<'),
(term.bold_red(u'@') if handle != 'anonymous'
and 'sysop' in get_user(handle).groups
else u''),
(handle if handle != session.user.handle
else term.bold(handle)),
(u':%s' % (tgt_channel,)
if 'sysop' in session.user.groups
else u''),
term.bold_black(u'>'), u' ',
mesg,))
def get_inputbar(pager):
""" Return ScrollingEditor for use as inputbar. """
from x84.bbs import getterminal, ScrollingEditor
term = getterminal()
width = pager.visible_width - 2
yloc = (pager.yloc + pager.height) - 2
xloc = pager.xloc + 2
ibar = ScrollingEditor(width=width, yloc=yloc, xloc=xloc)
ibar.enable_scrolling = True
ibar.max_length = 512
ibar.colors['highlight'] = term.cyan_reverse
return ibar
def get_pager(pager=None):
""" Return Pager for use as chat window. """
from x84.bbs import getterminal, Pager
term = getterminal()
height = (term.height - 4)
width = int(term.width * .9)
yloc = term.height - height - 1
xloc = int(term.width / 2) - (width / 2)
new_pager = Pager(height, width, yloc, xloc)
if pager is not None:
content = pager.content
# little hack to keep empty lines from re-importing
for row in range(len(content)):
ucs = content[row]
if ucs.startswith(u'\x1b(B'):
ucs = ucs[len(u'\x1b(B'):]
if ucs.endswith(u'\x1b[m'):
ucs = ucs[len(u'\x1b[m'):]
content[row] = ucs
new_pager.update('\r\n'.join(content))
new_pager.enable_scrolling = True
new_pager.colors['border'] = term.cyan
new_pager.glyphs['right-vert'] = u'|'
new_pager.glyphs['left-vert'] = u'|'
new_pager.glyphs['bot-horiz'] = u''
return new_pager
def main(channel=None, caller=None):
""" Main procedure. """
# pylint: disable=R0914,R0912,W0603
# Too many local variables
# Too many branches
# Using the global statement
from x84.bbs import getsession, getterminal, getch, echo
session, term = getsession(), getterminal()
global CHANNEL, NICKS
CHANNEL = '#partyline' if channel is None else channel
NICKS = dict()
# sysop repy_to is -1 to force user, otherwise prompt
if channel == session.sid and caller not in (-1, None):
echo(u''.join((
term.normal, u'\a',
u'\r\n', term.clear_eol,
u'\r\n', term.clear_eol,
term.bold_green(u' ** '),
caller,
u' would like to chat, accept? ',
term.bold(u'['),
term.bold_green_underline(u'yn'),
term.bold(u']'),
)))
while True:
inp = getch()
if inp in (u'y', u'Y'):
break
elif inp in (u'n', u'N'):
return False
def refresh(pager, ipb, init=False):
""" Returns terminal sequence suitable for refreshing screen. """
session.activity = 'Chatting in %s' % (
CHANNEL if not CHANNEL.startswith('#')
and not 'sysop' in session.user.groups
else u'PRiVAtE ChANNEl',) if CHANNEL is not None else (
u'WAitiNG fOR ChAt')
pager.move_end()
return u''.join((
u''.join((u'\r\n', term.clear_eol,
u'\r\n', term.clear_eol,
term.bold_cyan(u'//'),
u' CitZENS bANd'.center(term.width).rstrip(),
term.clear_eol,
(u'\r\n' + term.clear_eol) * (pager.height + 2),
pager.border())) if init else u'',
pager.title(u''.join((
term.bold_cyan(u']- '),
CHANNEL if CHANNEL is not None else u'',
term.bold_cyan(u' -['),))),
pager.refresh(),
ipb.refresh(),))
def process_cmd(pager, msg):
""" Process command recieved and display result in chat window. """
cmd, args = msg.split()[0], msg.split()[1:]
# pylint: disable=W0603
# Using the global statement
global CHANNEL, NICKS, EXIT
if cmd.lower() == '/help':
pager.append(show_help())
return True
elif cmd.lower() == '/join' and len(args) == 1:
part_chan('lEAViNG fOR ANOthER ChANNEl')
CHANNEL = args[0]
NICKS = dict()
join_chan()
return True
elif cmd.lower() in ('/act', '/me',):
act(u' '.join(args))
elif cmd.lower() == '/say':
say(u' '.join(args))
elif cmd.lower() == '/part':
part_chan(u' '.join(args))
CHANNEL = None
NICKS = dict()
return True
elif cmd.lower() == '/quit':
part_chan('quit')
EXIT = True
elif cmd.lower() == '/users':
pager.append(show_nicks(NICKS.keys()))
return True
elif cmd.lower() == '/whois' and len(args) == 1:
whois(args[0])
return False
def broadcast_cc(payload):
""" Broadcast chat even, carbon copy ourselves. """
session.send_event('global', ('chat', payload))
session.buffer_event('global', ('chat', payload))
def join_chan():
""" Bradcast chat even for /join. """
payload = (session.sid, CHANNEL, (session.user.handle, 'join', None))
broadcast_cc(payload)
def say(mesg):
""" Signal chat event for /say. """
payload = (session.sid, CHANNEL, (session.user.handle, 'say', mesg))
broadcast_cc(payload)
def act(mesg):
""" Signal chat event for /act. """
payload = (session.sid, CHANNEL, (session.user.handle, 'act', mesg))
broadcast_cc(payload)
def part_chan(reason):
""" Signal chat event for /part. """
payload = (session.sid, CHANNEL, (session.user.handle, 'part', reason))
broadcast_cc(payload)
def whois(handle):
""" Perform /whois request for ``handle``. """
if not handle in NICKS:
return
session.send_event('route', (NICKS[handle], 'info-req', session.sid,))
def whois_response(attrs):
""" Display /whois response for given ``attrs``. """
return show_whois(attrs)
pager = get_pager(None) # output window
readline = get_inputbar(pager) # input bar
echo(refresh(pager, readline, init=True))
echo(pager.append("tYPE '/quit' tO EXit."))
dirty = time.time()
join_chan()
while not EXIT:
inp = getch(POLL_KEY)
# poll for and process screen resize
if session.poll_event('refresh') or (
inp in (term.KEY_REFRESH, unichr(12))):
pager = get_pager(pager)
saved_inp = readline.content
readline = get_inputbar(pager)
readline.content = saved_inp
echo(refresh(pager, readline, init=True))
dirty = None
# poll for and process chat events,
mesg = session.poll_event('global')
if mesg is not None:
otxt = process(mesg[1])
if 0 != len(otxt):
echo(pager.append(otxt))
dirty = None if dirty is None else time.time()
# poll for whois response
data = session.poll_event('info-ack')
if data is not None:
# session id, attributes = data
echo(pager.append(whois_response(data[1])))
dirty = None if dirty is None else time.time()
# process keystroke as input, or, failing that,
# as a command key to the pager. refresh portions of
# input bar or act on cariage return, accordingly.
elif inp is not None:
otxt = readline.process_keystroke(inp)
if readline.carriage_returned:
if readline.content.startswith('/'):
if process_cmd(pager, readline.content):
pager = get_pager(pager)
echo(refresh(pager, readline, init=True))
elif (0 != len(readline.content.strip())
and CHANNEL is not None):
say(readline.content)
readline = get_inputbar(pager)
echo(readline.refresh())
elif 0 == len(otxt):
if type(inp) is int:
echo(pager.process_keystroke(inp))
else:
echo(u''.join((
readline.fixate(-1),
readline.colors.get('highlight', u''),
otxt, term.normal)))
# update pager contents. Its a lot for 9600bps ..
if dirty is not None and time.time() - dirty > POLL_OUT:
echo(refresh(pager, readline))
dirty = None
echo(u''.join((term.move(term.height, 0), term.normal)))
return True