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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
|
# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org>
#
# This file is part of Poezio.
#
# Poezio is free software: you can redistribute it and/or modify
# it under the terms of the zlib license. See the COPYING file.
"""
a Tab object is a way to organize various Windows (see windows.py)
around the screen at once.
A tab is then composed of multiple Buffer.
Each Tab object has different refresh() and resize() methods, defining how its
Windows are displayed, resized, etc
"""
MIN_WIDTH = 50
MIN_HEIGHT = 22
import logging
log = logging.getLogger(__name__)
from gettext import gettext as _
import windows
import curses
import difflib
import text_buffer
import string
import common
import core
import singleton
import xhtml
import weakref
import timed_events
import multiuserchat as muc
from theming import get_theme
from sleekxmpp.xmlstream.stanzabase import JID
from config import config
from roster import RosterGroup, roster
from contact import Contact, Resource
from user import User
from os import getenv, path
from logger import logger
from datetime import datetime, timedelta
SHOW_NAME = {
'dnd': _('busy'),
'away': _('away'),
'xa': _('not available'),
'chat': _('chatty'),
'': _('available')
}
NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
class Tab(object):
number = 0
tab_core = None
def __init__(self):
self.input = None
self._color_state = get_theme().COLOR_TAB_NORMAL
self.need_resize = False
self.nb = Tab.number
Tab.number += 1
self.need_resize = False
self.key_func = {} # each tab should add their keys in there
# and use them in on_input
self.commands = {} # and their own commands
@property
def core(self):
if not Tab.tab_core:
Tab.tab_core = singleton.Singleton(core.Core)
return Tab.tab_core
@property
def tab_win(self):
if not Tab.tab_core:
Tab.tab_core = singleton.Singleton(core.Core)
return Tab.tab_core.tab_win
@property
def info_win(self):
return self.core.information_win
@staticmethod
def resize(scr):
Tab.size = (Tab.height, Tab.width) = scr.getmaxyx()
if Tab.height < MIN_HEIGHT or Tab.width < MIN_WIDTH:
Tab.visible = False
else:
Tab.visible = True
def complete_commands(self, the_input):
"""
Does command completion on the specified input for both global and tab-specific
commands.
This should be called from the completion method (on tab, for example), passing
the input where completion is to be made.
It can completion the command name itself or an argument of the command.
Returns True if a completion was made, False else.
"""
txt = the_input.get_text()
# check if this is a command
if txt.startswith('/') and not txt.startswith('//'):
# check if we are in the middle of the command name
if len(txt.split()) > 1 or\
(txt.endswith(' ') and not the_input.last_completion):
command_name = txt.split()[0][1:]
if command_name in self.core.commands:
command = self.core.commands[command_name]
elif command_name in self.commands:
command = self.commands[command_name]
else: # Unknown command, cannot complete
return False
if command[2] is None:
return False # There's no completion function
else:
return command[2](the_input)
else:
# complete the command's name
words = ['/%s'% (name) for name in self.core.commands] +\
['/%s' % (name) for name in self.commands]
the_input.auto_completion(words, '')
return True
return False
def execute_command(self, provided_text):
"""
Execute the command in the input and return False if
the input didn't contain a command
"""
txt = provided_text or self.input.key_enter()
if txt.startswith('/') and not txt.startswith('//') and\
not txt.startswith('/me '):
command = txt.strip().split()[0][1:]
arg = txt[2+len(command):] # jump the '/' and the ' '
if command in self.commands: # check tab-specific commands
self.commands[command][0](arg)
elif command in self.core.commands: # check global commands
self.core.commands[command][0](arg)
else:
low = command.lower()
if low in self.commands:
self.commands[low][0](arg)
elif low in self.core.commands:
self.core.commands[low][0](arg)
else:
self.core.information(_("Unknown command (%s)") % (command), _('Error'))
return True
else:
return False
def refresh(self):
"""
Called on each screen refresh (when something has changed)
"""
raise NotImplementedError
def get_color_state(self):
"""
returns the color that should be used in the GlobalInfoBar
"""
return self._color_state
def set_color_state(self, color):
"""
set the color state
"""
pass
def get_name(self):
"""
get the name of the tab
"""
return self.__class__.__name__
def get_text_window(self):
"""
Returns the principal TextWin window, if there's one
"""
return None
def on_input(self, key):
pass
def on_lose_focus(self):
"""
called when this tab loses the focus.
"""
self._color_state = get_theme().COLOR_TAB_NORMAL
def on_gain_focus(self):
"""
called when this tab gains the focus.
"""
self._color_state = get_theme().COLOR_TAB_CURRENT
def add_message(self):
"""
Adds a message in the tab.
If the tab cannot add a message in itself (for example
FormTab, where text is not intented to be appened), it returns False.
If the tab can, it returns True
"""
return False
def on_scroll_down(self):
"""
Defines what happens when we scrol down
"""
pass
def on_scroll_up(self):
"""
Defines what happens when we scrol down
"""
pass
def on_info_win_size_changed(self):
"""
Called when the window with the informations is resized
"""
pass
def just_before_refresh(self):
"""
Method called just before the screen refresh.
Particularly useful to move the cursor at the
correct position.
"""
pass
def on_close(self):
"""
Called when the tab is to be closed
"""
if self.input:
self.input.on_delete()
def __del__(self):
log.debug('------ Closing tab %s' % self.__class__.__name__)
class ChatTab(Tab):
"""
A tab containing a chat of any type.
Just use this class instead of Tab if the tab needs a recent-words completion
Also, ^M is already bound to on_enter
And also, add the /say command
"""
def __init__(self, room):
Tab.__init__(self)
self._room = room
self.remote_wants_chatstates = None # change this to True or False when
# we know that the remote user wants chatstates, or not.
# None means we don’t know yet, and we send only "active" chatstates
self.chatstate = None # can be "active", "composing", "paused", "gone", "inactive"
# We keep a weakref of the event that will set our chatstate to "paused", so that
# we can delete it or change it if we need to
self.timed_event_paused = None
# if that’s None, then no paused chatstate was sent recently
# if that’s a weakref returning None, then a paused chatstate was sent
# since the last input
self.key_func['M-v'] = self.move_separator
self.key_func['M-/'] = self.last_words_completion
self.key_func['^M'] = self.on_enter
self.commands['say'] = (self.command_say,
_("""Usage: /say <message>\nSay: Just send the message.
Useful if you want your message to begin with a '/'"""), None)
self.chat_state = None
def last_words_completion(self):
"""
Complete the input with words recently said
"""
# build the list of the recent words
char_we_dont_want = string.punctuation+' '
words = list()
for msg in self._room.messages[:-40:-1]:
if not msg:
continue
txt = msg.txt
for char in char_we_dont_want:
txt = txt.replace(char, ' ')
for word in txt.split():
if len(word) >= 4 and word not in words:
words.append(word)
self.input.auto_completion(words, ' ')
def on_enter(self):
txt = self.input.key_enter()
if txt:
clean_text = xhtml.clean_text(txt)
if not self.execute_command(clean_text):
if txt.startswith('//'):
txt = txt[1:]
self.core.run_event('enter', line=txt)
self.command_say(txt)
self.cancel_paused_delay()
def send_chat_state(self, state, always_send=False):
"""
Send an empty chatstate message
"""
if not isinstance(self, MucTab) or self.get_room().joined:
if state in ('active', 'inactive', 'gone') and self.core.status.show in ('xa', 'away') and not always_send:
return
msg = self.core.xmpp.make_message(self.get_name())
msg['type'] = self.message_type
msg['chat_state'] = state
self.chat_state = state
msg.send()
def send_composing_chat_state(self, empty_after):
"""
Send the "active" or "composing" chatstate, depending
on the the current status of the input
"""
if config.get('send_chat_states', 'true') == 'true' and self.remote_wants_chatstates:
needed = 'inactive' if self.core.status.show in ('xa', 'away') else 'active'
self.cancel_paused_delay()
if not empty_after:
if self.chat_state != "composing":
self.send_chat_state("composing")
self.set_paused_delay(True)
elif empty_after and self.chat_state != needed:
self.send_chat_state(needed, True)
def set_paused_delay(self, composing):
"""
we create a timed event that will put us to paused
in a few seconds
"""
if config.get('send_chat_states', 'true') != 'true':
return
if self.timed_event_paused:
# check the weakref
event = self.timed_event_paused()
if event:
# the event already exists: we just update
# its date
event.change_date(datetime.now() + timedelta(seconds=4))
return
new_event = timed_events.DelayedEvent(4, self.send_chat_state, 'paused')
self.core.add_timed_event(new_event)
self.timed_event_paused = weakref.ref(new_event)
def cancel_paused_delay(self):
"""
Remove that event from the list and set it to None.
Called for example when the input is emptied, or when the message
is sent
"""
if self.timed_event_paused:
event = self.timed_event_paused()
if event:
self.core.remove_timed_event(event)
del event
self.timed_event_paused = None
def move_separator(self):
self.text_win.remove_line_separator()
self.text_win.add_line_separator()
self.text_win.refresh(self._room)
self.input.refresh()
def command_say(self, line):
raise NotImplementedError
class MucTab(ChatTab):
"""
The tab containing a multi-user-chat room.
It contains an userlist, an input, a topic, an information and a chat zone
"""
message_type = 'groupchat'
def __init__(self, room):
ChatTab.__init__(self, room)
self.remote_wants_chatstates = True
# We send active, composing and paused states to the MUC because
# the chatstate may or may not be filtered by the MUC,
# that’s not our problem.
self.topic_win = windows.Topic()
self.text_win = windows.TextWin()
room.add_window(self.text_win)
self.v_separator = windows.VerticalSeparator()
self.user_win = windows.UserList()
self.info_header = windows.MucInfoWin()
self.input = windows.MessageInput()
self.ignores = [] # set of Users
# keys
self.key_func['^I'] = self.completion
self.key_func['M-u'] = self.scroll_user_list_down
self.key_func['M-y'] = self.scroll_user_list_up
# commands
self.commands['ignore'] = (self.command_ignore, _("Usage: /ignore <nickname> \nIgnore: Ignore a specified nickname."), None)
self.commands['unignore'] = (self.command_unignore, _("Usage: /unignore <nickname>\nUnignore: Remove the specified nickname from the ignore list."), self.completion_unignore)
self.commands['kick'] = (self.command_kick, _("Usage: /kick <nick> [reason]\nKick: Kick the user with the specified nickname. You also can give an optional reason."), None)
self.commands['topic'] = (self.command_topic, _("Usage: /topic <subject>\nTopic: Change the subject of the room"), self.completion_topic)
self.commands['query'] = (self.command_query, _('Usage: /query <nick> [message]\nQuery: Open a private conversation with <nick>. This nick has to be present in the room you\'re currently in. If you specified a message after the nickname, it will immediately be sent to this user'), None)
self.commands['part'] = (self.command_part, _("Usage: /part [message]\nPart: disconnect from a room. You can specify an optional message."), None)
self.commands['nick'] = (self.command_nick, _("Usage: /nick <nickname>\nNick: Change your nickname in the current room"), None)
self.commands['recolor'] = (self.command_recolor, _('Usage: /recolor\nRecolor: Re-assign a color to all participants of the current room, based on the last time they talked. Use this if the participants currently talking have too many identical colors.'), None)
self.commands['cycle'] = (self.command_cycle, _('Usage: /cycle [message]\nCycle: Leaves the current room and rejoin it immediately'), None)
self.commands['info'] = (self.command_info, _('Usage: /info <nickname>\nInfo: Display some information about the user in the MUC: his/here role, affiliation, status and status message.'), None)
self.commands['configure'] = (self.command_configure, _('Usage: /configure\nConfigure: Configure the current room, through a form.'), None)
self.commands['version'] = (self.command_version, _('Usage: /version <jid or nick>\nVersion: get the software version of the given JID or nick in room (usually its XMPP client and Operating System)'), None)
self.commands['names'] = (self.command_names, _('Usage: /names\nNames: get the list of the users in the room, and the list of the people assuming the different roles.'), None)
self.resize()
def scroll_user_list_up(self):
self.user_win.scroll_up()
self.user_win.refresh(self._room.users)
self.input.refresh()
def scroll_user_list_down(self):
self.user_win.scroll_down()
self.user_win.refresh(self._room.users)
self.input.refresh()
def command_info(self, arg):
args = common.shell_split(arg)
if len(args) != 1:
return self.core.information("Info command takes only 1 argument")
user = self.get_room().get_user_by_name(args[0])
if not user:
return self.core.information("Unknown user: %s" % args[0])
info = '%s%s: show: %s, affiliation: %s, role: %s%s' % (args[0],
' (%s)' % user.jid if user.jid else '',
user.show or 'Available',
user.role or 'None',
user.affiliation or 'None',
'\n%s' % user.status if user.status else '')
self.core.information(info, 'Info')
def command_configure(self, arg):
form = self.core.xmpp.plugin['xep_0045'].getRoomForm(self.get_name())
if not form:
return
self.core.open_new_form(form, self.cancel_config, self.send_config)
def cancel_config(self, form):
"""
The user do not want to send his/her config, send an iq cancel
"""
self.core.xmpp.plugin['xep_0045'].cancelConfig(self.get_name())
self.core.close_tab()
def send_config(self, form):
"""
The user sends his/her config to the server
"""
self.core.xmpp.plugin['xep_0045'].configureRoom(self.get_name(), form)
self.core.close_tab()
def command_cycle(self, arg):
if self.get_room().joined:
muc.leave_groupchat(self.core.xmpp, self.get_name(), self.get_room().own_nick, arg)
self.get_room().disconnect()
self.core.disable_private_tabs(self.get_room().name)
self.core.command_join('/%s' % self.core.get_bookmark_nickname(self.get_room().name), '0')
self.user_win.pos = 0
def command_recolor(self, arg):
"""
Re-assign color to the participants of the room
"""
room = self.get_room()
compare_users = lambda x: x.last_talked
users = list(room.users)
# search our own user, to remove it from the room
for user in users:
if user.nick == room.own_nick:
users.remove(user)
nb_color = len(get_theme().LIST_COLOR_NICKNAMES)
for i, user in enumerate(sorted(users, key=compare_users, reverse=True)):
user.color = get_theme().LIST_COLOR_NICKNAMES[i % nb_color]
self.text_win.rebuild_everything(self._room)
self.text_win.refresh(self._room)
self.input.refresh()
def command_version(self, arg):
"""
/version <jid or nick>
"""
def callback(res):
if not res:
return self.core.information('Could not get the software version from %s' % (jid,), 'Warning')
version = '%s is running %s version %s on %s' % (jid,
res.get('name') or _('an unknown software'),
res.get('version') or _('unknown'),
res.get('os') or _('on an unknown platform'))
self.core.information(version, 'Info')
args = common.shell_split(arg)
if len(args) < 1:
return
if args[0] in [user.nick for user in self.get_room().users]:
jid = self._room.name + '/' + args[0]
else:
jid = args[0]
self.core.xmpp.plugin['xep_0092'].get_version(jid, callback=callback)
def command_nick(self, arg):
"""
/nick <nickname>
"""
args = common.shell_split(arg)
if len(args) != 1:
return
nick = args[0]
room = self.get_room()
if not room.joined:
return
current_status = self.core.get_status()
muc.change_nick(self.core.xmpp, room.name, nick, current_status.message, current_status.show)
def command_part(self, arg):
"""
/part [msg]
"""
args = arg.split()
room = self.get_room()
if len(args):
msg = ' '.join(args)
else:
msg = None
if self.get_room().joined:
muc.leave_groupchat(self.core.xmpp, room.name, room.own_nick, arg)
self.core.close_tab()
self.core.disable_private_tabs(self.get_room().name)
def command_query(self, arg):
"""
/query <nick> [message]
"""
args = common.shell_split(arg)
if len(args) < 1:
return
nick = args[0]
room = self.get_room()
r = None
for user in room.users:
if user.nick == nick:
r = self.core.open_private_window(room.name, user.nick)
if r and len(args) > 1:
msg = arg[len(nick)+1:]
self.core.current_tab().command_say(msg)
if not r:
self.core.information(_("Cannot find user: %s" % nick), 'Error')
def command_topic(self, arg):
"""
/topic [new topic]
"""
if not arg.strip():
self.core.add_message_to_text_buffer(self.get_room(),
_("The subject of the room is: %s") % self.get_room().topic)
self.text_win.refresh(self.get_room())
self.input.refresh()
return
subject = arg
muc.change_subject(self.core.xmpp, self.get_room().name, subject)
def command_names(self, arg=None):
"""
/names
"""
room = self.get_room()
if not room.joined:
return
users, visitors, moderators, participants, others = [], [], [], [], []
for user in room.users:
if user.role == 'visitor':
visitors.append(user.nick)
elif user.role == 'participant':
participants.append(user.nick)
elif user.role == 'moderator':
moderators.append(user.nick)
else:
others.append(user.nick)
users.append(user.nick)
message = ''
roles = (('Users', users), ('Visitors', visitors), ('Participants', participants), ('Moderators', moderators), ('Others', others))
for role in roles:
if role[1]:
role[1].sort()
message += '%s: %i\n ' % (role[0], len(role[1]))
last = role[1].pop()
for item in role[1]:
message += '%s, ' % item
message += '%s\n' % last
self.core.add_message_to_text_buffer(room, message)
self.text_win.refresh(self.get_room())
self.input.refresh()
def completion_topic(self, the_input):
current_topic = self.get_room().topic
return the_input.auto_completion([current_topic], ' ')
def command_kick(self, arg):
"""
/kick <nick> [reason]
"""
args = common.shell_split(arg)
if len(args) < 1:
self.core.command_help('kick')
return
nick = args[0]
if len(args) >= 2:
reason = ' '.join(args[1:])
else:
reason = ''
if not self.get_room().joined:
return
res = muc.eject_user(self.core.xmpp, self.get_name(), nick, reason)
if res['type'] == 'error':
self.core.room_error(res, self.get_name())
def command_say(self, line):
needed = 'inactive' if self.core.status.show in ('xa', 'away') else 'active'
msg = self.core.xmpp.make_message(self.get_name())
msg['type'] = 'groupchat'
if line.find('\x19') == -1:
msg['body'] = line
else:
msg['body'] = xhtml.clean_text(line)
msg['xhtml_im'] = xhtml.poezio_colors_to_html(line)
if config.get('send_chat_states', 'true') == 'true' and self.remote_wants_chatstates is not False:
msg['chat_state'] = needed
self.cancel_paused_delay()
msg.send()
self.chat_state = needed
def command_ignore(self, arg):
"""
/ignore <nick>
"""
args = common.shell_split(arg)
if len(args) != 1:
self.core.command_help('ignore')
return
nick = args[0]
user = self._room.get_user_by_name(nick)
if not user:
self.core.information(_('%s is not in the room') % nick)
elif user in self.ignores:
self.core.information(_('%s is already ignored') % nick)
else:
self.ignores.append(user)
self.core.information(_("%s is now ignored") % nick, 'info')
def command_unignore(self, arg):
"""
/unignore <nick>
"""
args = common.shell_split(arg)
if len(args) != 1:
self.core.command_help('unignore')
return
nick = args[0]
user = self._room.get_user_by_name(nick)
if not user:
self.core.information(_('%s is not in the room') % nick)
elif user not in self.ignores:
self.core.information(_('%s is not ignored') % nick)
else:
self.ignores.remove(user)
self.core.information(_('%s is now unignored') % nick)
def completion_unignore(self, the_input):
return the_input.auto_completion([user.nick for user in self.ignores], ' ')
def resize(self):
"""
Resize the whole window. i.e. all its sub-windows
"""
if not self.visible:
return
self.need_resize = False
text_width = (self.width//10)*9
self.topic_win.resize(1, self.width, 0, 0)
self.v_separator.resize(self.height-3, 1, 1, 9*(self.width//10))
self.text_win.resize(self.height-4-self.core.information_win_size, text_width, 1, 0)
self.text_win.rebuild_everything(self._room)
self.user_win.resize(self.height-3-self.core.information_win_size-1, self.width-text_width-1, 1, text_width+1)
self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0)
self.input.resize(1, self.width, self.height-1, 0)
def refresh(self):
if self.need_resize:
self.resize()
log.debug(' TAB Refresh: %s'%self.__class__.__name__)
self.topic_win.refresh(self._room.get_single_line_topic())
self.text_win.refresh(self._room)
self.v_separator.refresh()
self.user_win.refresh(self._room.users)
self.info_header.refresh(self._room, self.text_win)
self.tab_win.refresh()
self.info_win.refresh(self.core.informations)
self.input.refresh()
def on_input(self, key):
if key in self.key_func:
self.key_func[key]()
return False
self.input.do_command(key)
empty_after = self.input.get_text() == '' or (self.input.get_text().startswith('/') and not self.input.get_text().startswith('//'))
self.send_composing_chat_state(empty_after)
return False
def completion(self):
"""
Called when Tab is pressed, complete the nickname in the input
"""
if self.complete_commands(self.input):
return
# If we are not completing a command or a command's argument, complete a nick
compare_users = lambda x: x.last_talked
word_list = [user.nick for user in sorted(self._room.users, key=compare_users, reverse=True)\
if user.nick != self._room.own_nick]
after = config.get('after_completion', ',')+" "
if ' ' not in self.input.get_text() or (self.input.last_completion and\
self.input.get_text()[:-len(after)] == self.input.last_completion):
add_after = after
else:
add_after = ' '
self.input.auto_completion(word_list, add_after)
empty_after = self.input.get_text() == '' or (self.input.get_text().startswith('/') and not self.input.get_text().startswith('//'))
self.send_composing_chat_state(empty_after)
def get_color_state(self):
return self._room.color_state
def set_color_state(self, color):
self._room.set_color_state(color)
def get_name(self):
return self._room.name
def get_text_window(self):
return self.text_win
def get_room(self):
return self._room
def on_lose_focus(self):
self._room.set_color_state(get_theme().COLOR_TAB_NORMAL)
self.text_win.remove_line_separator()
self.text_win.add_line_separator()
if config.get('send_chat_states', 'true') == 'true' and not self.input.get_text():
self.send_chat_state('inactive')
def on_gain_focus(self):
self._room.set_color_state(get_theme().COLOR_TAB_CURRENT)
if self.text_win.built_lines and self.text_win.built_lines[-1] is None:
self.text_win.remove_line_separator()
curses.curs_set(1)
if self.get_room().joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text():
self.send_chat_state('active')
def on_scroll_up(self):
self.text_win.scroll_up(self.text_win.height-1)
def on_scroll_down(self):
self.text_win.scroll_down(self.text_win.height-1)
def on_info_win_size_changed(self):
if self.core.information_win_size >= self.height-3:
return
text_width = (self.width//10)*9
self.text_win.resize(self.height-4-self.core.information_win_size, text_width, 1, 0)
self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0)
self.user_win.resize(self.height-3-self.core.information_win_size-1, self.width-text_width-1, 1, text_width+1)
def handle_presence(self, presence):
from_nick = presence['from'].resource
from_room = presence['from'].bare
status_codes = set([s.attrib['code'] for s in presence.findall('{%s}x/{%s}status' % (NS_MUC_USER, NS_MUC_USER))])
# Check if it's not an error presence.
if presence['type'] == 'error':
return self.core.room_error(presence, from_room)
affiliation = presence['muc']['affiliation']
show = presence['show']
status = presence['status']
role = presence['muc']['role']
jid = presence['muc']['jid']
typ = presence['type']
room = self.get_room()
if not room.joined: # user in the room BEFORE us.
# ignore redondant presence message, see bug #1509
if from_nick not in [user.nick for user in room.users] and typ != "unavailable":
new_user = User(from_nick, affiliation, show, status, role, jid)
room.users.append(new_user)
if from_nick == room.own_nick:
room.joined = True
if self.core.current_tab() == self and self.core.status.show not in ('xa', 'away'):
self.send_chat_state('active')
new_user.color = get_theme().COLOR_OWN_NICK
room.add_message(_("\x195}Your nickname is \x193}%s") % (from_nick))
if '170' in status_codes:
room.add_message('\x191}Warning: \x195}this room is publicly logged')
else:
change_nick = '303' in status_codes
kick = '307' in status_codes and typ == 'unavailable'
ban = '301' in status_codes and typ == 'unavailable'
user = room.get_user_by_name(from_nick)
# New user
if not user:
self.on_user_join(room, from_nick, affiliation, show, status, role, jid)
# nick change
elif change_nick:
self.on_user_nick_change(room, presence, user, from_nick, from_room)
elif ban:
self.on_user_banned(room, presence, user, from_nick)
# kick
elif kick:
self.on_user_kicked(room, presence, user, from_nick)
# user quit
elif typ == 'unavailable':
self.on_user_leave_groupchat(room, user, jid, status, from_nick, from_room)
# status change
else:
self.on_user_change_status(room, user, from_nick, from_room, affiliation, role, show, status)
if self.core.current_tab() is self:
self.text_win.refresh(self._room)
self.user_win.refresh(self._room.users)
self.info_header.refresh(self._room, self.text_win)
self.input.refresh()
self.core.doupdate()
def on_user_join(self, room, from_nick, affiliation, show, status, role, jid):
"""
When a new user joins the groupchat
"""
room.users.append(User(from_nick, affiliation,
show, status, role, jid))
hide_exit_join = config.get('hide_exit_join', -1)
if hide_exit_join != 0:
if not jid.full:
room.add_message('\x194}%(spec)s \x193}%(nick)s\x195} joined the room' % {'nick':from_nick, 'spec':get_theme().CHAR_JOIN})
else:
room.add_message('\x194}%(spec)s \x193}%(nick)s \x195}(\x194}%(jid)s\x195}) joined the room' % {'spec':get_theme().CHAR_JOIN, 'nick':from_nick, 'jid':jid.full})
self.core.on_user_rejoined_private_conversation(room.name, from_nick)
def on_user_nick_change(self, room, presence, user, from_nick, from_room):
new_nick = presence.find('{%s}x/{%s}item' % (NS_MUC_USER, NS_MUC_USER)).attrib['nick']
if user.nick == room.own_nick:
room.own_nick = new_nick
# also change our nick in all private discussion of this room
for _tab in self.core.tabs:
if isinstance(_tab, PrivateTab) and JID(_tab.get_name()).bare == room.name:
_tab.get_room().own_nick = new_nick
user.change_nick(new_nick)
room.add_message('\x193}%(old)s\x195} is now known as \x193}%(new)s' % {'old':from_nick, 'new':new_nick})
# rename the private tabs if needed
self.core.rename_private_tabs(room.name, from_nick, new_nick)
def on_user_banned(self, room, presence, user, from_nick):
"""
When someone is banned from a muc
"""
room.users.remove(user)
by = presence.find('{%s}x/{%s}item/{%s}actor' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
reason = presence.find('{%s}x/{%s}item/{%s}reason' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
by = by.attrib['jid'] if by is not None else None
if from_nick == room.own_nick: # we are banned
room.disconnect()
self.core.disable_private_tabs(room.name)
self.tab_win.refresh()
self.core.doupdate()
if by:
kick_msg = _('\x191}%(spec)s \x193}You\x195} have been banned by \x194}%(by)s') % {'spec': get_theme().CHAR_KICK, 'by':by}
else:
kick_msg = _('\x191}%(spec)s \x193}You\x195} have been banned.') % {'spec':get_theme().CHAR_KICK}
else:
if by:
kick_msg = _('\x191}%(spec)s \x193}%(nick)s\x195} has been banned by \x194}%(by)s') % {'spec':get_theme().CHAR_KICK, 'nick':from_nick, 'by':by}
else:
kick_msg = _('\x191}%(spec)s \x193}%(nick)s\x195} has been banned') % {'spec':get_theme().CHAR_KICK, 'nick':from_nick.replace('"', '\\"')}
if reason is not None and reason.text:
kick_msg += _('\x195} Reason: \x196}%(reason)s\x195}') % {'reason': reason.text}
room.add_message(kick_msg)
def on_user_kicked(self, room, presence, user, from_nick):
"""
When someone is kicked from a muc
"""
room.users.remove(user)
by = presence.find('{%s}x/{%s}item/{%s}actor' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
reason = presence.find('{%s}x/{%s}item/{%s}reason' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
by = by.attrib['jid'] if by is not None else None
if from_nick == room.own_nick: # we are kicked
room.disconnect()
self.core.disable_private_tabs(room.name)
self.tab_win.refresh()
self.core.doupdate()
if by:
kick_msg = _('\x191}%(spec)s \x193}You\x195} have been kicked by \x193}%(by)s') % {'spec': get_theme().CHAR_KICK, 'by':by}
else:
kick_msg = _('\x191}%(spec)s \x193}You\x195} have been kicked.') % {'spec':get_theme().CHAR_KICK}
# try to auto-rejoin
if config.get('autorejoin', 'false') == 'true':
muc.join_groupchat(self.core.xmpp, room.name, room.own_nick)
else:
if by:
kick_msg = _('\x191}%(spec)s \x193}%(nick)s\x195} has been kicked by \x193}%(by)s') % {'spec':get_theme().CHAR_KICK.replace('"', '\\"'), 'nick':from_nick.replace('"', '\\"'), 'by':by.replace('"', '\\"')}
else:
kick_msg = _('\x191}%(spec)s \x193}%(nick)s\x195} has been kicked') % {'spec':get_theme().CHAR_KICK, 'nick':from_nick.replace('"', '\\"')}
if reason is not None and reason.text:
kick_msg += _('\x195} Reason: \x196}%(reason)s') % {'reason': reason.text}
room.add_message(kick_msg)
def on_user_leave_groupchat(self, room, user, jid, status, from_nick, from_room):
"""
When an user leaves a groupchat
"""
room.users.remove(user)
if room.own_nick == user.nick:
# We are now out of the room. Happens with some buggy (? not sure) servers
room.disconnect()
self.core.disable_private_tabs(from_room)
self.tab_win.refresh()
self.core.doupdate()
hide_exit_join = config.get('hide_exit_join', -1) if config.get('hide_exit_join', -1) >= -1 else -1
if hide_exit_join == -1 or user.has_talked_since(hide_exit_join):
if not jid.full:
leave_msg = _('\x191}%(spec)s \x193}%(nick)s\x195} has left the room') % {'nick':from_nick, 'spec':get_theme().CHAR_QUIT}
else:
leave_msg = _('\x191}%(spec)s \x193}%(nick)s\x195} (\x194}%(jid)s\x195}) has left the room') % {'spec':get_theme().CHAR_QUIT, 'nick':from_nick, 'jid':jid.full}
if status:
leave_msg += ' (%s)' % status
room.add_message(leave_msg)
self.core.refresh_window()
self.core.on_user_left_private_conversation(from_room, from_nick, status)
def on_user_change_status(self, room, user, from_nick, from_room, affiliation, role, show, status):
"""
When an user changes her status
"""
# build the message
display_message = False # flag to know if something significant enough
# to be displayed has changed
msg = _('\x193}%s\x195} changed: ')% from_nick.replace('"', '\\"')
if show not in SHOW_NAME:
self.core.information("%s from room %s sent an invalid show: %s" %\
(from_nick, from_room, show), "warning")
if affiliation != user.affiliation:
msg += _('affiliation: %s, ') % affiliation
display_message = True
if role != user.role:
msg += _('role: %s, ') % role
display_message = True
if show != user.show and show in SHOW_NAME:
msg += _('show: %s, ') % SHOW_NAME[show]
display_message = True
if status != user.status:
# if the user sets his status to nothing
if not status and show in SHOW_NAME:
msg += _('show: %s, ') % SHOW_NAME[show]
else:
msg += _('status: %s, ') % status
display_message = True
if not display_message:
return
msg = msg[:-2] # remove the last ", "
hide_status_change = config.get('hide_status_change', -1) if config.get('hide_status_change', -1) >= -1 else -1
if (hide_status_change == -1 or \
user.has_talked_since(hide_status_change) or\
user.nick == room.own_nick)\
and\
(affiliation != user.affiliation or\
role != user.role or\
show != user.show or\
status != user.status):
# display the message in the room
room.add_message(msg)
self.core.on_user_changed_status_in_private('%s/%s' % (from_room, from_nick), msg)
# finally, effectively change the user status
user.update(affiliation, show, status, role)
class PrivateTab(ChatTab):
"""
The tab containg a private conversation (someone from a MUC)
"""
message_type = 'chat'
def __init__(self, room):
ChatTab.__init__(self, room)
self.text_win = windows.TextWin()
room.add_window(self.text_win)
self.info_header = windows.PrivateInfoWin()
self.input = windows.MessageInput()
# keys
self.key_func['^I'] = self.completion
# commands
self.commands['info'] = (self.command_info, _('Usage: /info\nInfo: Display some information about the user in the MUC: '), None)
self.commands['unquery'] = (self.command_unquery, _("Usage: /unquery\nUnquery: close the tab"), None)
self.commands['part'] = (self.command_unquery, _("Usage: /part\nPart: close the tab"), None)
self.commands['version'] = (self.command_version, _('Usage: /version\nVersion: get the software version of the current interlocutor (usually its XMPP client and Operating System)'), None)
self.resize()
self.parent_muc = self.core.get_tab_by_name(JID(room.name).bare, MucTab)
self.on = True
def completion(self):
self.complete_commands(self.input)
def command_say(self, line):
if not self.on:
return
msg = self.core.xmpp.make_message(self.get_name())
msg['type'] = 'chat'
if line.find('\x19') == -1:
msg['body'] = line
else:
msg['body'] = xhtml.clean_text(line)
msg['xhtml_im'] = xhtml.poezio_colors_to_html(line)
if config.get('send_chat_states', 'true') == 'true' and self.remote_wants_chatstates is not False:
needed = 'inactive' if self.core.status.show in ('xa', 'away') else 'active'
msg['chat_state'] = needed
msg.send()
self.core.add_message_to_text_buffer(self.get_room(), line, None, self.core.own_nick or self.get_room().own_nick)
logger.log_message(JID(self.get_name()).bare, self.core.own_nick, line)
self.cancel_paused_delay()
self.text_win.refresh(self._room)
self.input.refresh()
def command_unquery(self, arg):
"""
/unquery
"""
self.core.close_tab()
def command_version(self, arg):
"""
/version
"""
def callback(res):
if not res:
return self.core.information('Could not get the software version from %s' % (jid,), 'Warning')
version = '%s is running %s version %s on %s' % (jid,
res.get('name') or _('an unknown software'),
res.get('version') or _('unknown'),
res.get('os') or _('on an unknown platform'))
self.core.information(version, 'Info')
jid = self.get_room().name
self.core.xmpp.plugin['xep_0092'].get_version(jid, callback=callback)
def command_info(self, arg):
"""
/info
"""
if arg:
self.parent_muc.command_info(arg)
else:
user = JID(self.get_room().name).resource
self.parent_muc.command_info(user)
def resize(self):
if self.core.information_win_size >= self.height-3 or not self.visible:
return
self.need_resize = False
self.text_win.resize(self.height-3-self.core.information_win_size, self.width, 0, 0)
self.text_win.rebuild_everything(self._room)
self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0)
self.input.resize(1, self.width, self.height-1, 0)
def refresh(self):
if self.need_resize:
self.resize()
log.debug(' TAB Refresh: %s'%self.__class__.__name__)
self.text_win.refresh(self._room)
self.info_header.refresh(self._room, self.text_win, self.chatstate)
self.info_win.refresh(self.core.informations)
self.tab_win.refresh()
self.input.refresh()
def refresh_info_header(self):
self.info_header.refresh(self._room, self.text_win, self.chatstate)
self.input.refresh()
def get_color_state(self):
if self._room.color_state == get_theme().COLOR_TAB_NORMAL or\
self._room.color_state == get_theme().COLOR_TAB_CURRENT:
return self._room.color_state
return get_theme().COLOR_TAB_PRIVATE
def set_color_state(self, color):
self._room.color_state = color
def get_name(self):
return self._room.name
def on_input(self, key):
if key in self.key_func:
self.key_func[key]()
return False
self.input.do_command(key)
if not self.on:
return False
empty_after = self.input.get_text() == '' or (self.input.get_text().startswith('/') and not self.input.get_text().startswith('//'))
tab = self.core.get_tab_by_name(JID(self.get_room().name).bare, MucTab)
if tab and tab.get_room().joined:
self.send_composing_chat_state(empty_after)
return False
def on_lose_focus(self):
self._room.set_color_state(get_theme().COLOR_TAB_NORMAL)
self.text_win.remove_line_separator()
self.text_win.add_line_separator()
if self.get_room().joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text():
self.send_chat_state('inactive')
def on_gain_focus(self):
self._room.set_color_state(get_theme().COLOR_TAB_CURRENT)
curses.curs_set(1)
if self.get_room().joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text():
self.send_chat_state('active')
def on_scroll_up(self):
self.text_win.scroll_up(self.text_win.height-1)
def on_scroll_down(self):
self.text_win.scroll_down(self.text_win.height-1)
def on_info_win_size_changed(self):
if self.core.information_win_size >= self.height-3:
return
self.text_win.resize(self.height-3-self.core.information_win_size, self.width, 0, 0)
self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0)
def get_room(self):
return self._room
def get_text_window(self):
return self.text_win
def rename_user(self, old_nick, new_nick):
"""
The user changed her nick in the corresponding muc: update the tab’s name and
display a message.
"""
self.get_room().add_message(_('"[%(old_nick)s]" is now known as "[%(new_nick)s]"') % {'old_nick':old_nick.replace('"', '\\"'), 'new_nick':new_nick.replace('"', '\\"')})
new_jid = JID(self.get_room().name).bare+'/'+new_nick
self.get_room().name = new_jid
def user_left(self, status_message, from_nick):
"""
The user left the associated MUC
"""
self.deactivate()
if not status_message:
self.get_room().add_message(_('\x191}%(spec)s \x193}%(nick)s\x195} has left the room') % {'nick':from_nick.replace('"', '\\"'), 'spec':get_theme().CHAR_QUIT.replace('"', '\\"')})
else:
self.get_room().add_message(_('\x191}%(spec)s \x193}%(nick)s\x195} has left the room (%(status)s)"') % {'nick':from_nick.replace('"', '\\"'), 'spec':get_theme().CHAR_QUIT, 'status': status_message.replace('"', '\\"')})
if self.core.current_tab() is self:
self.refresh()
self.core.doupdate()
def user_rejoined(self, nick):
"""
The user (or at least someone with the same nick) came back in the MUC
"""
self.activate()
self.get_room().add_message('\x194}%(spec)s \x193}%(nick)s\x195} joined the room' % {'nick':nick, 'spec':get_theme().CHAR_JOIN})
if self.core.current_tab() is self:
self.refresh()
self.core.doupdate()
def activate(self):
self.on = True
def deactivate(self):
self.on = False
class RosterInfoTab(Tab):
"""
A tab, splitted in two, containing the roster and infos
"""
def __init__(self):
Tab.__init__(self)
self.name = "Roster"
self.v_separator = windows.VerticalSeparator()
self.information_win = windows.TextWin()
self.core.information_buffer.add_window(self.information_win)
self.roster_win = windows.RosterWin()
self.contact_info_win = windows.ContactInfoWin()
self.default_help_message = windows.HelpText("Enter commands with “/”. “o”: toggle offline show")
self.input = self.default_help_message
self.set_color_state(get_theme().COLOR_TAB_NORMAL)
self.key_func['^I'] = self.completion
self.key_func[' '] = self.on_space
self.key_func["/"] = self.on_slash
self.key_func["KEY_UP"] = self.move_cursor_up
self.key_func["KEY_DOWN"] = self.move_cursor_down
self.key_func["M-u"] = self.move_cursor_to_next_group
self.key_func["M-y"] = self.move_cursor_to_prev_group
self.key_func["M-[1;5B"] = self.move_cursor_to_next_group
self.key_func["M-[1;5A"] = self.move_cursor_to_prev_group
self.key_func["o"] = self.toggle_offline_show
self.key_func["s"] = self.start_search
self.key_func["S"] = self.start_search_slow
self.commands['deny'] = (self.command_deny, _("Usage: /deny [jid]\nDeny: Use this command to remove and deny your presence to the provided JID (or the selected contact in your roster), who is asking you to be in his/here roster"), self.completion_deny)
self.commands['accept'] = (self.command_accept, _("Usage: /accept [jid]\nAccept: Use this command to authorize the provided JID (or the selected contact in your roster), to see your presence, and to ask to subscribe to it (mutual presence subscription)."), self.completion_deny)
self.commands['add'] = (self.command_add, _("Usage: /add <jid>\nAdd: Use this command to add the specified JID to your roster. The reverse authorization will automatically be accepted if the remote JID accepts your subscription, leading to a mutual presence subscription."), None)
self.commands['remove'] = (self.command_remove, _("Usage: /remove [jid]\nRemove: Use this command to remove the specified JID from your roster. This wil unsubscribe you from its presence, cancel its subscription to yours, and remove the item from your roster"), self.completion_remove)
self.commands['export'] = (self.command_export, _("Usage: /export [/path/to/file]\nExport: Use this command to export your contacts into /path/to/file if specified, or $HOME/poezio_contacts if not."), None)
self.commands['import'] = (self.command_import, _("Usage: /import [/path/to/file]\nImport: Use this command to import your contacts from /path/to/file if specified, or $HOME/poezio_contacts if not."), None)
self.resize()
def resize(self):
if not self.visible:
return
self.need_resize = False
roster_width = self.width//2
info_width = self.width-roster_width-1
self.v_separator.resize(self.height-2, 1, 0, roster_width)
self.information_win.resize(self.height-2-4, info_width, 0, roster_width+1, self.core.information_buffer)
self.roster_win.resize(self.height-2, roster_width, 0, 0)
self.contact_info_win.resize(4, info_width, self.height-2-4, roster_width+1)
self.input.resize(1, self.width, self.height-1, 0)
def completion(self):
# Check if we are entering a command (with the '/' key)
if isinstance(self.input, windows.CommandInput) and\
not self.input.help_message:
self.complete_commands(self.input)
def command_deny(self, args):
"""
Denies a JID from our roster
"""
args = args.split()
if not args:
item = self.roster_win.selected_row
if isinstance(item, Contact) and item.get_ask() == 'asked':
jid = item.get_bare_jid()
else:
self.core.information('No subscription to deny')
return
else:
jid = JID(args[0]).bare
self.core.xmpp.sendPresence(pto=jid, ptype='unsubscribed')
if self.core.xmpp.update_roster(jid, subscription='remove'):
roster.remove_contact(jid)
def command_add(self, args):
"""
Add the specified JID to the roster, and set automatically
accept the reverse subscription
"""
jid = JID(args.strip()).bare
if not jid:
self.core.information(_('No JID specified'), 'Error')
return
self.core.xmpp.sendPresence(pto=jid, ptype='subscribe')
def command_remove(self, args):
"""
Remove the specified JID from the roster. i.e. : unsubscribe
from its presence, and cancel its subscription to our.
"""
if args.strip():
jid = JID(args.strip()).bare
else:
item = self.roster_win.selected_row
if isinstance(item, Contact):
jid = item.get_bare_jid()
else:
self.core.information('No roster item to remove')
return
self.core.xmpp.sendPresence(pto=jid, ptype='unsubscribe')
self.core.xmpp.sendPresence(pto=jid, ptype='unsubscribed')
self.core.xmpp.del_roster_item(jid=jid)
def command_import(self, arg):
"""
Import the contacts
"""
args = common.shell_split(arg)
if len(args):
if args[0].startswith('/'):
filepath = args[0]
else:
filepath = path.join(getenv('HOME'), args[0])
else:
filepath = path.join(getenv('HOME'), 'poezio_contacts')
if not path.isfile(filepath):
self.core.information('The file %s does not exist' % filepath, 'Error')
return
try:
handle = open(filepath, 'r')
lines = handle.readlines()
handle.close()
except IOError:
self.core.information('Could not open %s' % filepath, 'Error')
return
for jid in lines:
self.command_add(jid.lstrip('\n'))
self.core.information('Contacts imported from %s' % filepath, 'Info')
def command_export(self, arg):
"""
Export the contacts
"""
args = common.shell_split(arg)
if len(args):
if args[0].startswith('/'):
filepath = args[0]
else:
filepath = path.join(getenv('HOME'), args[0])
else:
filepath = path.join(getenv('HOME'), 'poezio_contacts')
if path.isfile(filepath):
self.core.information('The file already exists', 'Error')
return
elif not path.isdir(path.dirname(filepath)):
self.core.information('Parent directory not found', 'Error')
return
if roster.export(filepath):
self.core.information('Contacts exported to %s' % filepath, 'Info')
else:
self.core.information('Failed to export contacts to %s' % filepath, 'Info')
def completion_remove(self, the_input):
"""
From with any JID presence in the roster
"""
jids = [contact.get_bare_jid() for contact in roster.get_contacts()]
return the_input.auto_completion(jids, '')
def completion_deny(self, the_input):
"""
Complete the first argument from the list of the
contact with ask=='subscribe'
"""
jids = [contact.get_bare_jid() for contact in roster.get_contacts()\
if contact.get_ask() == 'asked']
return the_input.auto_completion(jids, '')
def command_accept(self, args):
"""
Accept a JID from in roster. Authorize it AND subscribe to it
"""
args = args.split()
if not args:
item = self.roster_win.selected_row
if isinstance(item, Contact) and item.get_ask() == 'asked':
jid = item.get_bare_jid()
else:
self.core.information('No subscription to accept')
return
else:
jid = args[0]
self.core.xmpp.sendPresence(pto=jid, ptype='subscribed')
self.core.xmpp.sendPresence(pto=jid, ptype='subscribe')
def refresh(self):
if self.need_resize:
self.resize()
log.debug(' TAB Refresh: %s'%self.__class__.__name__)
self.v_separator.refresh()
self.roster_win.refresh(roster)
self.contact_info_win.refresh(self.roster_win.get_selected_row())
self.information_win.refresh(self.core.informations)
self.tab_win.refresh()
self.input.refresh()
def get_name(self):
return self.name
def get_color_state(self):
return self._color_state
def set_color_state(self, color):
self._color_state = color
def on_input(self, key):
if key == '^M':
selected_row = self.roster_win.get_selected_row()
res = self.input.do_command(key)
if res:
return True
if key == '^M':
self.core.on_roster_enter_key(selected_row)
return selected_row
elif key in self.key_func:
return self.key_func[key]()
def toggle_offline_show(self):
"""
Show or hide offline contacts
"""
option = 'roster_show_offline'
if config.get(option, 'false') == 'false':
config.set_and_save(option, 'true')
else:
config.set_and_save(option, 'false')
return True
def on_slash(self):
"""
'/' is pressed, we enter "input mode"
"""
curses.curs_set(1)
self.input = windows.CommandInput("", self.reset_help_message, self.execute_slash_command)
self.input.resize(1, self.width, self.height-1, 0)
self.input.do_command("/") # we add the slash
def reset_help_message(self, _=None):
curses.curs_set(0)
self.input = self.default_help_message
self.input.refresh()
self.core.doupdate()
return True
def execute_slash_command(self, txt):
if txt.startswith('/'):
self.input.key_enter()
self.execute_command(txt)
return self.reset_help_message()
def on_lose_focus(self):
self._color_state = get_theme().COLOR_TAB_NORMAL
def on_gain_focus(self):
self._color_state = get_theme().COLOR_TAB_CURRENT
if isinstance(self.input, windows.HelpText):
curses.curs_set(0)
else:
curses.curs_set(1)
def add_message(self):
return False
def move_cursor_down(self):
self.roster_win.move_cursor_down()
self.roster_win.refresh(roster)
self.contact_info_win.refresh(self.roster_win.get_selected_row())
self.input.refresh()
self.core.doupdate()
def move_cursor_up(self):
self.roster_win.move_cursor_up()
self.roster_win.refresh(roster)
self.contact_info_win.refresh(self.roster_win.get_selected_row())
self.input.refresh()
self.core.doupdate()
def move_cursor_to_prev_group(self):
self.roster_win.move_cursor_up()
while not isinstance(self.roster_win.get_selected_row(), RosterGroup):
if not self.roster_win.move_cursor_up():
break
self.roster_win.refresh(roster)
self.contact_info_win.refresh(self.roster_win.get_selected_row())
self.input.refresh()
self.core.doupdate()
def move_cursor_to_next_group(self):
self.roster_win.move_cursor_down()
while not isinstance(self.roster_win.get_selected_row(), RosterGroup):
if not self.roster_win.move_cursor_down():
break
self.roster_win.refresh(roster)
self.contact_info_win.refresh(self.roster_win.get_selected_row())
self.input.refresh()
self.core.doupdate()
def on_scroll_down(self):
for i in range(self.height-1):
self.roster_win.move_cursor_down()
return True
def on_scroll_up(self):
for i in range(self.height-1):
self.roster_win.move_cursor_up()
return True
def on_space(self):
selected_row = self.roster_win.get_selected_row()
if isinstance(selected_row, RosterGroup) or\
isinstance(selected_row, Contact):
selected_row.toggle_folded()
return True
def start_search(self):
"""
Start the search. The input should appear with a short instruction
in it.
"""
curses.curs_set(1)
self.input = windows.CommandInput("[Search]", self.on_search_terminate, self.on_search_terminate, self.set_roster_filter)
self.input.resize(1, self.width, self.height-1, 0)
return True
def start_search_slow(self):
curses.curs_set(1)
self.input = windows.CommandInput("[Search]", self.on_search_terminate, self.on_search_terminate, self.set_roster_filter_slow)
self.input.resize(1, self.width, self.height-1, 0)
return True
def set_roster_filter_slow(self, txt):
roster._contact_filter = (jid_and_name_match_slow, txt)
self.roster_win.refresh(roster)
return False
def set_roster_filter(self, txt):
roster._contact_filter = (jid_and_name_match, txt)
self.roster_win.refresh(roster)
return False
def on_search_terminate(self, txt):
curses.curs_set(0)
roster._contact_filter = None
self.reset_help_message()
return False
def on_close(self):
return
class ConversationTab(ChatTab):
"""
The tab containg a normal conversation (not from a MUC)
"""
message_type = 'chat'
def __init__(self, jid):
txt_buff = text_buffer.TextBuffer()
ChatTab.__init__(self, txt_buff)
self.color_state = get_theme().COLOR_TAB_NORMAL
self._name = jid # a conversation tab is linked to one specific full jid OR bare jid
self.text_win = windows.TextWin()
txt_buff.add_window(self.text_win)
self.upper_bar = windows.ConversationStatusMessageWin()
self.info_header = windows.ConversationInfoWin()
self.input = windows.MessageInput()
# keys
self.key_func['^I'] = self.completion
# commands
self.commands['unquery'] = (self.command_unquery, _("Usage: /unquery\nUnquery: close the tab"), None)
self.commands['part'] = (self.command_unquery, _("Usage: /part\Part: close the tab"), None)
self.resize()
def completion(self):
self.complete_commands(self.input)
def command_say(self, line):
msg = self.core.xmpp.make_message(self.get_name())
msg['type'] = 'chat'
if line.find('\x19') == -1:
msg['body'] = line
else:
msg['body'] = xhtml.clean_text(line)
msg['xhtml_im'] = xhtml.poezio_colors_to_html(line)
if config.get('send_chat_states', 'true') == 'true' and self.remote_wants_chatstates is not False:
needed = 'inactive' if self.core.status.show in ('xa', 'away') else 'active'
msg['chat_state'] = needed
msg.send()
self.core.add_message_to_text_buffer(self.get_room(), line, None, self.core.own_nick)
logger.log_message(JID(self.get_name()).bare, self.core.own_nick, line)
self.cancel_paused_delay()
self.text_win.refresh(self._room)
self.input.refresh()
def command_unquery(self, arg):
self.core.close_tab()
def resize(self):
if self.core.information_win_size >= self.height-3 or not self.visible:
return
self.need_resize = False
self.text_win.resize(self.height-4-self.core.information_win_size, self.width, 1, 0)
self.text_win.rebuild_everything(self._room)
self.upper_bar.resize(1, self.width, 0, 0)
self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0)
self.input.resize(1, self.width, self.height-1, 0)
def refresh(self):
if self.need_resize:
self.resize()
log.debug(' TAB Refresh: %s'%self.__class__.__name__)
self.text_win.refresh(self._room)
self.upper_bar.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()))
self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self._room, self.text_win, self.chatstate)
self.info_win.refresh(self.core.informations)
self.tab_win.refresh()
self.input.refresh()
def refresh_info_header(self):
self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self._room, self.text_win, self.chatstate)
self.input.refresh()
def get_color_state(self):
if self.color_state == get_theme().COLOR_TAB_NORMAL or\
self.color_state == get_theme().COLOR_TAB_CURRENT:
return self.color_state
return get_theme().COLOR_TAB_PRIVATE
def set_color_state(self, color):
self.color_state = color
def get_name(self):
return self._name
def on_input(self, key):
if key in self.key_func:
self.key_func[key]()
return False
self.input.do_command(key)
empty_after = self.input.get_text() == '' or (self.input.get_text().startswith('/') and not self.input.get_text().startswith('//'))
self.send_composing_chat_state(empty_after)
return False
def on_lose_focus(self):
self.set_color_state(get_theme().COLOR_TAB_NORMAL)
self.text_win.remove_line_separator()
self.text_win.add_line_separator()
if config.get('send_chat_states', 'true') == 'true' and not self.input.get_text() or not self.input.get_text().startswith('//'):
self.send_chat_state('inactive')
def on_gain_focus(self):
self.set_color_state(get_theme().COLOR_TAB_CURRENT)
curses.curs_set(1)
if config.get('send_chat_states', 'true') == 'true' and not self.input.get_text() or not self.input.get_text().startswith('//'):
self.send_chat_state('active')
def on_scroll_up(self):
self.text_win.scroll_up(self.text_win.height-1)
def on_scroll_down(self):
self.text_win.scroll_down(self.text_win.height-1)
def on_info_win_size_changed(self):
if self.core.information_win_size >= self.height-3:
return
self.text_win.resize(self.height-4-self.core.information_win_size, self.width, 1, 0)
self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0)
def get_room(self):
return self._room
def get_text_window(self):
return self.text_win
def on_close(self):
Tab.on_close(self)
if config.get('send_chat_states', 'true') == 'true':
self.send_chat_state('gone')
class MucListTab(Tab):
"""
A tab listing rooms from a specific server, displaying various information,
scrollable, and letting the user join them, etc
"""
def __init__(self, server):
Tab.__init__(self)
self._color_state = get_theme().COLOR_TAB_NORMAL
self.name = server
self.upper_message = windows.Topic()
self.upper_message.set_message('Chatroom list on server %s (Loading)' % self.name)
columns = ('node-part', 'name', 'users')
self.list_header = windows.ColumnHeaderWin(columns)
self.listview = windows.ListWin(columns)
self.default_help_message = windows.HelpText("“j”: join room.")
self.input = self.default_help_message
self.key_func["KEY_DOWN"] = self.listview.move_cursor_down
self.key_func["KEY_UP"] = self.listview.move_cursor_up
self.key_func['^I'] = self.completion
self.key_func["/"] = self.on_slash
self.key_func['j'] = self.join_selected
self.key_func['J'] = self.join_selected_no_focus
self.key_func['^M'] = self.join_selected
self.commands['close'] = (self.close, _("Usage: /close\nClose: Just close this tab"), None)
self.resize()
def refresh(self):
if self.need_resize:
self.resize()
log.debug(' TAB Refresh: %s'%self.__class__.__name__)
self.upper_message.refresh()
self.list_header.refresh()
self.listview.refresh()
self.tab_win.refresh()
self.input.refresh()
def resize(self):
if not self.visible:
return
self.need_resize = False
self.upper_message.resize(1, self.width, 0, 0)
column_size = {'node-part': (self.width-5)//4,
'name': (self.width-5)//4*3,
'users': 5}
self.list_header.resize_columns(column_size)
self.list_header.resize(1, self.width, 1, 0)
self.listview.resize_columns(column_size)
self.listview.resize(self.height-4, self.width, 2, 0)
self.input.resize(1, self.width, self.height-1, 0)
def on_slash(self):
"""
'/' is pressed, activate the input
"""
curses.curs_set(1)
self.input = windows.CommandInput("", self.reset_help_message, self.execute_slash_command)
self.input.resize(1, self.width, self.height-1, 0)
self.input.do_command("/") # we add the slash
def close(self, arg=None):
self.input.on_delete()
self.core.close_tab(self)
def join_selected_no_focus(self):
return
def set_error(self, msg, code, body):
"""
If there's an error (retrieving the values etc)
"""
self._error_message = _('Error: %(code)s - %(msg)s: %(body)s') % {'msg':msg, 'body':body, 'code':code}
self.upper_message.set_message(self._error_message)
self.upper_message.refresh()
curses.doupdate()
def on_muc_list_item_received(self, iq):
"""
Callback called when a disco#items result is received
Used with command_list
"""
if iq['type'] == 'error':
self.set_error(iq['error']['type'], iq['error']['code'], iq['error']['text'])
return
items = [{'node-part':JID(item[0]).user,
'jid': item[0],
'name': item[2]} for item in iq['disco_items'].get_items()]
self.listview.add_lines(items)
self.upper_message.set_message('Chatroom list on server %s' % self.name)
self.upper_message.refresh()
curses.doupdate()
def join_selected(self):
row = self.listview.get_selected_row()
if not row:
return
self.core.command_join(row['jid'])
def reset_help_message(self, _=None):
curses.curs_set(0)
self.input = self.default_help_message
return True
def execute_slash_command(self, txt):
self.execute_command(txt)
return self.reset_help_message()
def get_name(self):
return self.name
def completion(self):
if isinstance(self.input, windows.CommandInput):
self.complete_commands(self.input)
def on_input(self, key):
res = self.input.do_command(key)
if res:
return True
if key in self.key_func:
return self.key_func[key]()
def on_lose_focus(self):
self._color_state = get_theme().COLOR_TAB_NORMAL
def on_gain_focus(self):
self._color_state = get_theme().COLOR_TAB_CURRENT
curses.curs_set(0)
def get_color_state(self):
return self._color_state
def on_scroll_up(self):
self.listview.scroll_up()
def on_scroll_down(self):
self.listview.scroll_down()
class SimpleTextTab(Tab):
"""
A very simple tab, with just a text displaying some
information or whatever.
For example used to display tracebacks
"""
def __init__(self, text):
Tab.__init__(self)
self._color_state = get_theme().COLOR_TAB_NORMAL
self.text_win = windows.SimpleTextWin(text)
self.default_help_message = windows.HelpText("“Ctrl+q”: close")
self.input = self.default_help_message
self.key_func['^T'] = self.close
self.key_func["/"] = self.on_slash
self.resize()
def on_slash(self):
"""
'/' is pressed, activate the input
"""
curses.curs_set(1)
self.input = windows.CommandInput("", self.reset_help_message, self.execute_slash_command)
self.input.resize(1, self.width, self.height-1, 0)
self.input.do_command("/") # we add the slash
def on_input(self, key):
res = self.input.do_command(key)
if res:
return True
if key in self.key_func:
return self.key_func[key]()
def close(self):
self.core.close_tab()
def resize(self):
if not self.visible:
return
self.need_resize = False
self.text_win.resize(self.height-2, self.width, 0, 0)
self.input.resize(1, self.width, self.height-1, 0)
def refresh(self):
if self.need_resize:
self.resize()
log.debug(' TAB Refresh: %s'%self.__class__.__name__)
self.text_win.refresh()
self.tab_win.refresh()
self.input.refresh()
def on_lose_focus(self):
self._color_state = get_theme().COLOR_TAB_NORMAL
def on_gain_focus(self):
self._color_state = get_theme().COLOR_TAB_CURRENT
curses.curs_set(0)
def get_color_state(self):
return self._color_state
def diffmatch(search, string):
"""
Use difflib and a loop to check if search_pattern can
be 'almost' found INSIDE a string.
'almost' being defined by difflib
"""
l = len(search)
ratio = 0.7
for i in range(len(string) - l + 1):
if difflib.SequenceMatcher(None, search, string[i:i+l]).ratio() >= ratio:
return True
return False
def jid_and_name_match(contact, txt):
"""
Match jid with text precisely
"""
if not txt:
return True
if txt in JID(contact.get_bare_jid()).user:
return True
return False
def jid_and_name_match_slow(contact, txt):
"""
A function used to know if a contact in the roster should
be shown in the roster
"""
if not txt:
return True # Everything matches when search is empty
user = JID(contact.get_bare_jid()).user
if diffmatch(txt, user):
return True
if contact.get_name() and diffmatch(txt, contact.get_name()):
return True
return False
|