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 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
|
#!/usr/bin/env python
# Time-stamp: <2004-04-30 13:25:21 crabbkw>
# Code and design by Casey Crabb (crabbkw@nafai.dyndns.org)
# This code is licensed under the BSD license.
# See the LICENSE file for details
#
# Copyright Casey Crabb (crabbkw@nafai.dyndns.org) July 2001
#
# from __future__ import nested_scopes
from threading import *
from IMCom import IMCom, errorCodes
from Preferences import Preferences
from LogHandler import LogHandler
from AutoStatus import AutoStatus
import CICommands
from Colors import *
import types
import string
import socket
import os
import time
import sys
import getpass
import operator
import signal
import traceback
import SocketWrapper
import pprint
import getopt
import math
try:
import fcntl
import termios
import struct
TERMWIDTH=1
except:
TERMWIDTH=0
try:
import codecs
CODECS = 1
except:
CODECS = 0
try:
import locale
LOCALE = 1
except:
LOCALE = 0
try:
import readline
READLINE = 1
except:
READLINE = 0
if sys.version_info[0] >= 2 and sys.version_info[1] >= 2:
METACLASSES = 1
else:
METACLASSES = 0
VERSION = "1.33 final (less bugfixes)"
tabCompleteToLastUser=0
terminalWidth = 80
pp = pprint.PrettyPrinter(indent=4)
def mySort(x, y):
a,b = x
c,d = y
return cmp(b,d)
def getTerminalWidth():
height, width = getheightwidth()
#print "(width,height) (",width,",",height,")"
return width
def getTerminalHeight():
height, width = getheightwidth()
#print "(width,height) (",width,",",height,")"
return height
def getheightwidth():
"""Return the height and width of the console in characters """
if not TERMWIDTH:
return 24, 80
try:
return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
except KeyError:
height, width = struct.unpack("hhhh", fcntl.ioctl(0, termios.TIOCGWINSZ ,"\000"*8))[0:2]
if not height: return 25, 80
return int(height), int(width)
class WrapperMetaclass:
def __init__(self, prefix, wrapper):
self.prefix = prefix
self.wrapper = wrapper
def __call__(self, name, bases, attrs):
methodNames = []
additionalMethods = []
for k in attrs:
if k.startswith(self.prefix):
methodNames.append(k)
additionalMethods.append(["_"+k, attrs[k]])
attrs[k] = self.wrapper(attrs[k])
for k in additionalMethods:
attrs[k[0]]=k[1]
attrs['methodNames'] = methodNames
return type(name, bases, attrs)
def methodWrapper(aMethod):
def wrappingMethod(*args, **kw):
tmp = args[0] # tmp is the object itself, hopefully a CLI instance
if tmp.callbacks.has_key(aMethod.__name__):
for func in tmp.callbacks[aMethod.__name__][0]: # call all the pre-method modules in order.
res = func(*args, **kw)
if(res == -1):
return res
if type(res) is types.ListType and len(res) == len(args):
args = res
aResult = aMethod(*args, **kw) # call the method itself
if tmp.callbacks.has_key(aMethod.__name__):
for func in tmp.callbacks[aMethod.__name__][1]: # call all the post-method modules in order.
res = func(*args, **kw)
if(res == -1):
return res
if type(res) is types.ListType and len(res) == len(args):
args = res
return aResult
return wrappingMethod
class CLI:
"The Main thread and heart of the IMCom CLI interface."
if METACLASSES:
__metaclass__ = WrapperMetaclass('handle', methodWrapper)
def __init__(self, debugging, profileName):
#Thread.__init__(self)
self.PRE = 0 # constant for the handle* mixins
self.POST = 1 # constant for the handle* mixins
self.callbacks = {} # keys are string names of methods, values are callable objects.
self.importedModules = {} # keys are strings of module names, values are their imported contents.
self.VERSION = VERSION
self.threadHash = {}
self.prompt = "IMCom> "
self.lastMessaged = None
self.lastReceived = None
self.lastToDict = {}
self.lastFromDict = {}
self.showPresenceUpdates = 1
self.ringBell = 1
self.confRingBell = 1
self.currentStatus = "online"
self.currentStatusType = "auto"
self.currentStatusReason = ""
self.debug = debugging
self.debugdetails = {
"autostatus" : 0,
}
self.mode = 0
self.MULTILINE = 1
self.outputQueue = []
self.gettingCommand = 0
self.pendingCommand = 0
self.userQueue = []
self.commandQueue = []
self.socketlisteners = []
self.socketserver = ''
self.autostatus = None
self.colorscheme = {}
self.prefs = Preferences()
if profileName == "":
self.profile = self.prefs.getDefaultProfile()
else:
if self.prefs.getProfile(profileName) != None:
self.profile = self.prefs.getProfile(profileName)
else:
print "The profile '"+profileName+"' doesn't exist."
print "These are the profiles you have: "
for key in self.prefs.profiles.keys():
print key
print
sys.exit(-1)
self.imcom = IMCom(self)
self.registerCallbacks()
self.loghandler = LogHandler( self, self.profile )
if(READLINE):
readline.parse_and_bind("tab: complete")
readline.parse_and_bind("set bell-style none")
readline.set_completer_delims(" ")
readline.set_completer(tabCompleter)
def registerCallbacks(self):
self.imcom.cbHandleConferenceMessage = self.handleConferenceMessage
self.imcom.cbHandleDisconnected = self.handleDisconnected
self.imcom.cbHandleAdminWho = self.handleAdminWho
self.imcom.cbHandlePresenceUpdate = self.handlePresenceUpdate
self.imcom.cbHandlePresenceError = self.handlePresenceError
self.imcom.cbHandleMessageReceive = self.handleMessageReceive
self.imcom.cbHandleMessageError = self.handleMessageError
self.imcom.cbHandleIQError = self.handleIQError
self.imcom.cbHandleInfoError = self.handleInfoError
self.imcom.cbHandleFileReceive = self.handleFileReceive
self.imcom.cbHandleFileReceived = self.handleFileReceived
self.imcom.cbHandleFileErrorReceived = self.handleFileErrorReceived
self.imcom.cbHandleAgentList = self.handleAgentList
self.imcom.cbHandleAgentRegister = self.handleAgentRegister
self.imcom.cbHandleAgentRegistered = self.handleAgentRegistered
self.imcom.cbHandleSubscribe = self.handleSubscribe
self.imcom.cbHandleSubscribed = self.handleSubscribed
self.imcom.cbHandleUnsubscribed = self.handleUnsubscribed
self.imcom.cbHandleUnsubscribe = self.handleUnsubscribe
self.imcom.cbHandleRosterUpdateCheck = self.handleRosterUpdateCheck
self.imcom.cbHandleRosterUpdate = self.handleRosterUpdate
self.imcom.cbHandleVCardSubmit = self.handleVCardSubmit
self.imcom.cbHandleVCard = self.handleVCard
self.imcom.cbHandleNoVCard = self.handleNoVCard
self.imcom.cbHandleLogin = self.login
self.imcom.cbHandleGrabRoster = self.grabRoster
self.imcom.cbHandleAgentUnRegistered = self.handleAgentUnRegistered
self.imcom.cbHandleNegotiationRequest = self.handleNegotiationRequest
self.imcom.cbHandleNegotiationResult = self.handleNegotiationResult
self.imcom.cbHandleVersionResponse = self.handleVersionResponse
self.imcom.cbHandleChangePassword = self.handleChangePassword
self.imcom.cbHandleConferenceMessage = self.handleConferenceMessage
self.imcom.cbHandleConferencePresence = self.handleConferencePresence
self.imcom.cbHandleConferenceCreated = self.handleConferenceCreated
self.imcom.cbHandleConferenceNicknameChange = self.handleConferenceNicknameChange
self.imcom.cbHandleConferenceNicknameChangeSuccess = self.handleConferenceNicknameChangeSuccess
self.imcom.cbHandleConferenceInvite = self.handleConferenceInvite
self.imcom.cbHandleConferenceSubject = self.handleConferenceSubject
self.imcom.cbHandleConferenceKicked = self.handleConferenceKicked
self.imcom.cbHandleConferenceUserKicked = self.handleConferenceUserKicked
self.imcom.cbHandleConferenceBanned = self.handleConferenceBanned
self.imcom.cbHandleConferenceUserBanned = self.handleConferenceUserBanned
self.imcom.cbHandleConferenceDestroyed = self.handleConferenceDestroyed
self.imcom.cbHandleConferenceConfig = self.handleConferenceConfig
self.imcom.cbHandleConferenceConfigSet = self.handleConferenceConfigSet
self.imcom.cbHandleNickCollision = self.handleNickCollision
self.imcom.cbHandleNickHasSlash = self.handleNickHasSlash
self.imcom.cbHandleStreamError = self.handleStreamError
self.imcom.cbHandleStreamClose = self.handleStreamClose
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# -------------------------------------------------------------------------
# Functions for module support and mixins
# -------------------------------------------------------------------------
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def registerCallback(self, name, preOrPost, func):
# check preOrPost to validate it exists in the appropriate range
# check function to make sure its a valid, existing function on CLI
# check to make sure callback is not none
# check to see if there is an enter for function in our hash
# if there isn't add a default one of [[][]]
# if there is, pull out preOrPost.
# append callback to it.
if not METACLASSES:
return -1
if not preOrPost in [0, 1]:
print "registerCallback, cannot register your callback\n ", "preOrPost passed in (",preOrPost, ") isn't a valid, not in list: ", [0,1]
return -1
if not callable(func):
print "registerCallback, cannot register your callback\n ", "the function you gave me isn't callable."
return -1
if not name in self.methodNames:
print "registerCallback, cannot register your callback\n ", name, "isn't a valid method name in list: ", self.methodNames
return -1
if not self.callbacks.has_key(name) or self.callbacks[name] == None:
self.callbacks[name] = [[], []]
self.callbacks[name][preOrPost].append(func)
return 0
def appendCallback(self, name, preOrPost, func):
return self.registerCallback(name, preOrPost, func)
def unRegisterCallback(self, name, preOrPost, func):
# check preOrPost to validate it exists in the appropriate range
# check function to make sure its a valid, existing function on CLI
# check to make sure callback is not none
# check to see if there is an enter for function in our hash
# if there isn't just return
# if there is, pull out preOrPost.
# remove callback from it.
if not METACLASSES:
return -1
if not preOrPost in [0, 1] or not name in self.methodNames:
print name, "isn't a valid method name in list: ", self.methodNames
return -1
if(self.callbacks.has_key(name) and self.callbacks[name] != None):
try:
self.callbacks[name][preOrPost].remove(func)
except:
return -1
return 0
def removeCallback(self, function, preOrPost, callback):
return self.registerCallback(function, preOrPost, callback)
def registerCommand(self, commandText, commandName, commandHelp, commandFunction):
if commandText == None or commandName == None or commandHelp == None or not callable(commandFunction):
return -1
if self.prefs.checkCommandCollision(commandName, commandText, self.profile.moduleCommands) or self.profile.sessioncommands.has_key(commandName):
self.output("Warning, a naming collision has occured; not allowing module to register command: " + commandName)
return -1
self.profile.moduleCommands[commandName] = [commandText.lower(), commandFunction]
self.profile.sessioncommands[commandName] = commandText.lower()
self.prefs.commands.registerModuleCommandHelp(commandName, commandHelp)
return 0
def unRegisterCommand(self, commandName):
try:
del self.profile.moduleCommands[commandName]
self.prefs.commands.unRegisterModuleCommandHelp(commandName)
except:
cli.output("Problems unregistering command")
def reloadModule(self, module):
if not METACLASSES:
return -1
if not self.importedModules.has_key(module):
return self.loadModule(module)
result = self.unloadModule(module)
theobj = getattr(self.importedModules[module], module)
reload(theobj)
return self.loadModule(module)
def loadModule(self, module):
# initialize the module
# hand it it's config
# call its setup procedure so it can register callbacks
if not METACLASSES:
return -1
if module in self.profile.loadedModules:
return -1
if not self.importedModules.has_key(module):
try:
self.importedModules[module] = __import__("usermodules."+module)
if(cli.debug):
print "loaded from user"
except ImportError:
self.importedModules[module] = __import__("modules."+module)
if(cli.debug):
print "loaded from dist"
moduleInstance = self.importedModules[module]
XMLDOMNode = self.profile.getModuleConfig(module)
modobj = getattr(getattr(moduleInstance, module), module)(XMLDOMNode)
#eval("moduleInstance." + module + "." + module + "(XMLDOMNode)")
modobj.printShortDescription()
modobj.registerCallbacks(self)
self.profile.modules[module] = modobj
self.profile.loadedModules.append(modobj)
return modobj
def unloadModule(self, aMod):
if not METACLASSES:
return -1
if(self == None or not self.profile.modules.has_key(aMod)):
return -1
aModObj = self.profile.modules[aMod]
if aModObj == None:
return -1
retval = aModObj.unRegisterCallbacks(self)
try:
self.profile.loadedModules.remove(aModObj)
self.profile.modules[aMod] = None
except:
pass
return 0
def initializeAllModules(self):
if not METACLASSES or len(self.profile.loadedModules) > 0:
return -1
for aMod in self.profile.modules.keys():
aModObj = self.loadModule(aMod)
def configureModule(self, aMod):
if not METACLASSES:
return -1
if not self.profile.modules.has_key(aMod):
return -1
aModObj = self.profile.modules[aMod]
if aModObj == None:
return -1
self.pendingCommand = 1
self.gettingCommand = 1
self.mode = self.MULTILINE
aModObj.configureModuleCLI(self)
self.gettingCommand = 0
self.pendingCommand = 0
self.mode = 0
self.dumpQueue()
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# -------------------------------------------------------------------------
# Functions for the login procedure
# -------------------------------------------------------------------------
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def grabRoster(self):
"""This function is called by the IMCom library when the roster
has been downloaded. Because the presence events for people
can come before the roster is downloaded this function needs
to print out people for whom we've already gotten presence
updates from. This is because the presence code doesn't show
us presence information for anyone who is not on our roster."""
self.output("Roster has been downloaded")
if(self.readyToRoll):
self.imcom.sendOnline()
self.readyToRoll = 1
keys = self.imcom.jidhash.keys()
for item in keys:
if(self.imcom.preshash.has_key(item)):
available, status, show = self.imcom.preshash[item]
if(not available):
continue
if(self.showPresenceUpdates):
if( self.imcom.gjidhash.has_key(item) and
(operator.contains(self.imcom.gjidhash[item],
"lurker") or
operator.contains(self.imcom.gjidhash[item],
"ignore"))):
return
a,show,status = self.imcom.preshash[item]
nick = self.getNick(item)
ttime = self.getTime()
ddate = self.getDate()
self.output(self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] +
" changed status to " +
self.colorscheme["statuscolor"] + show + self.colorscheme["defaultcolor"] + " (" +
self.colorscheme["desccolor"] + status + self.colorscheme["defaultcolor"] + ") at " +
self.colorscheme["timecolor"] + ttime + self.colorscheme["defaultcolor"])
else:
self.imcom.preshash[item] = (0, "unavailable", "offline")
def login(self, successful):
"""This function is called by the IMCom library. It lets us
know whether the login was successful or not."""
if(successful):
self.output(self.colorscheme["usercolor"] + "Logged on" + self.colorscheme["defaultcolor"])
else:
self.output(self.colorscheme["errorcolor"] + "Login FAILED!" + self.colorscheme["defaultcolor"])
def applyProfileWrapper(self):
result = (-1, "No Error")
iter = 0
for iter in range(5):
result = cli.applyProfile()
if result[0] == 0:
return result
if result[0] == 1:
print result[1]
sys.exit(-1)
if result[0] == 3:
print "Check your configuration file with regards to the current profile."
sys.exit(-1)
print result[1]
print "Sleeping for 5 seconds."
time.sleep(5)
print "Giving up."
return result
def applyProfile(self):
"""This function actually begins the login process by
selecting a profile and calling the IMCom library's
changeProfile command. If no profile is available we allow
the user to create one. This does not let the user create an
account, only lets them to create a profile for an account
pre-existing."""
if(self.profile!=None):
p = self.profile
useSSL = 1
if(p.switches['ssl'][0] == "false" or
p.switches['ssl'][0] == "no" or
p.switches['ssl'][0] == "off" or
p.switches['ssl'][0] == 0):
useSSL = 0
self.readyToRoll = 0
self.imcom.DTCPPORT = p.DTCPPort
self.initializeAllModules()
result = self.imcom.changeProfile(p.server, p.port, p.user, p.password, p.resource, useSSL, p.priority, p.encoding)
if result[0] != 0:
return result
self.loghandler.initLogHandler(p)
self.autostatus = AutoStatus( self, self.profile )
self.autostatus.start()
self.loghandler.setProfile( self.profile )
self.autostatus.setProfile( self.profile )
if(self.readyToRoll):
self.imcom.sendOnline()
self.readyToRoll = 1
return result
else:
# I should create a new profile here.
print("No profiles available or default profile not set.")
self.createNewProfile()
self.profile = self.prefs.getDefaultProfile()
return self.applyProfile()
def applyPrefs(self, debugging):
"""This function sets all the preference switches."""
if(self.profile == None):
return
p = self.profile
if(p.switches['ringbell'][0] == "true"):
self.ringBell = 1
else:
self.ringBell = 0
if(p.switches['confringbell'][0] == "true"):
self.confRingBell = 1
else:
self.confRingBell = 0
if( p.switches['colors'][0] == "true"):
self.setColors()
else:
self.setNoColors()
if( debugging != 0 or not p.switches['debug'][0] == "false" ):
self.debug = 1
self.imcom.setDebug(1)
else:
self.debug = 0
self.imcom.setDebug(0)
if( p.switches['nickprompt'][0] == "false"):
self.useNickAsPrompt = 0
else:
self.useNickAsPrompt = 1
if( p.switches['allowinterrupt'][0] == "false" ):
self.allowInterrupt = 0
else:
self.allowInterrupt = 1
if( p.switches['statusshow'][0] == "false" or
p.switches['statusshow'][0] == "no"):
self.showPresenceUpdates = 0
else:
self.showPresenceUpdates = 1
def createNewProfile(self):
"""This function creates a new profile and overwrites the
preferences file ~/.imcom/imcomrc"""
print("You don't seem to have a profile, lets make a new one")
name = raw_input("Enter a profile name > ")
server = raw_input("Enter the server name > ")
useSSL = raw_input("Do you want to use SSL? (yes) > ")
defaultPort = "5222"
if useSSL == None or len(useSSL) == 0 or useSSL == "yes" or useSSL == "y":
defaultPort = "5223"
useSSL = 1
else:
useSSL = 0
port = raw_input("Enter the port (" + defaultPort + ") > ")
if(port == None or len(port) == 0):
port = defaultPort
user = raw_input("Enter your login > " )
password = getpass.getpass("Enter your password > ")
resource = raw_input("Enter the resource (imcom) > ")
if(resource == None or len(resource) == 0):
resource = "imcom"
encoding = raw_input("Enter the encoding you want to use (iso-8859-1) > ")
if(encoding == None or len(encoding) == 0):
encoding = "iso-8859-1"
self.prefs.createInitialProfile(name,server,useSSL,port,
user,password,resource, encoding)
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# -------------------------------------------------------------------------
# The functions which begin with handle are callbacks from the
# IMCom library
# -------------------------------------------------------------------------
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def handleStreamClose(self):
self.output("The stream closed on us?")
self.imcom = None
sys.exit(-1)
def handleStreamError(self, text):
self.output(self.colorscheme["errorcolor"] + "There was a stream error: " + text + self.colorscheme["defaultcolor"])
def handleQuit(self):
"""This function is called by the quit command on a clean quit. Its only purpose is for modules to be able to do things on quit."""
def handleDisconnected(self):
"""This is called by the IMCom library when the server has
disconnected us unexpectantly. In theory it should go into
EVAL mode essentially dumped a developer onto a Python console
with the state of IMCom preserved. This however, does not work
well yet."""
self.output(self.colorscheme["errorcolor"] + "Server Disconnected us at " +
self.colorscheme["timecolor"] + self.getTime() + self.colorscheme["defaultcolor"])
if(self.debug):
self.output(self.colorscheme["errorcolor"] + "Going into EVAL mode, EOF (Control-d) to quit." + self.colorscheme["defaultcolor"])
line = "blah"
while line:
try:
line = raw_input("> ")
try:
pp.pprint(eval(line))
except:
print self.colorscheme["errorcolor"] + "That didn't eval well" + self.colorscheme["defaultcolor"]
except EOFError:
sys.exit(-1)
except KeyboardInterrupt:
sys.exit(-1)
sys.exit(-1)
self.output("Sleeping for 5 seconds before reconnecting.")
time.sleep(5)
self.output("Attempting to reconnect now.")
self.imcom = None
self.imcom = IMCom(self)
self.registerCallbacks()
result = self.applyProfileWrapper()
if result[0] != 0:
sys.exit(-1)
if(self.currentStatus == "chat"):
self.imcom.sendChat()
elif(self.currentStatus == "away"):
self.imcom.sendAway(self.currentStatusReason)
elif(self.currentStatus == "xa"):
self.imcom.sendXA(self.currentStatusReason)
elif(self.currentStatus == "dnd"):
self.imcom.sendDND(self.currentStatusReason)
def handleAdminWho(self, who, show, status, available, resource):
"""This function is called by the IMCom library when there has been a request for the list of users on the server. You
must be admin for this function to work."""
s = self.colorscheme["usercolor"] + who + "/" + resource + self.colorscheme["defaultcolor"] + " is " + self.colorscheme["statuscolor"] + show + self.colorscheme["defaultcolor"] + " as " + self.colorscheme["desccolor"] + \
status + self.colorscheme["defaultcolor"]
self.output(s)
def handlePresenceUpdate(self, who, show, status, available, resource,
duplicate):
"""This function is called by the IMCom library when there has
been a change in someone's precense. The user must be
subscribed to the presence."""
#if(self.debug):
# logDebug("Updating presence of " + who + " to " + show +
# " as " + status)
# don't print the status of anyone not in our jidlist and not
# a transport
#if( (not self.imcom.jidhash.has_key(who) or
# not self.imcom.gjidhash.has_key(who) ) and
# ( string.find( who, "@" ) != -1 ) ):
# if(self.debug):
# logDebug("jidhash doesn't have a " + who)
# return
nick = self.getNick(who)
# these handle error conditions in transports.
if( ( string.find( who, "@" ) == -1 )
and show and show == "offline" ):
s = self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["defaultcolor"] + \
nick + " is offline"
if(status):
s = s + " : " + status
self.output(s)
return
if( ( string.find( who, "@" ) == -1 )
and show and ( string.find( show, " " ) != -1 ) ):
s = self.colorscheme["errorcolor"] + "Notice: " + self.colorscheme["defaultcolor"] + \
nick + ": " + show
self.output(s)
return
# this is in response to a bug in trillian (icq program)
# yes, the fix should be server-side but you know....
if( ( self.profile.switches['igndupstatus'][0] == "true" )
and duplicate ):
return
# commented out for testing/reimplementation of how IMCom
# deals with transports.
#if( string.find( who, "@" ) == -1 and not self.imcom.agenthash.has_key(who)):
# return
# don't display the presence of people in the lurker group.
if( self.imcom.gjidhash.has_key(who) and
(operator.contains(self.imcom.gjidhash[who],"lurker") or
operator.contains(self.imcom.gjidhash[who],"ignore"))):
if(self.debug):
logDebug("Skipping lurker: " + who)
return
# only display status updates if we are set to display status updates.
if(self.showPresenceUpdates):
ttime = self.getTime()
ddate = self.getDate()
if(available):
s = self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"]
# if they have a resource, and they're not a transport
# then add the resource string.
if(resource != None and resource != "" and not
self.imcom.agenthash.has_key(who)):
s = s + "/" + self.colorscheme["keycolor"] + resource + self.colorscheme["defaultcolor"]
# append the rest of the information
s = s + " changed status to " + \
self.colorscheme["statuscolor"] + show + self.colorscheme["defaultcolor"] + " (" + \
self.colorscheme["desccolor"] + status + self.colorscheme["defaultcolor"] + ") at " + \
self.colorscheme["timecolor"] + ttime + self.colorscheme["defaultcolor"]
self.output(s)
else:
s= self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"]
# if they have a resource, and they're not a transport
# then add the resource string.
if(resource != None and resource != "" and not
self.imcom.agenthash.has_key(who)):
s = s + "/" + self.colorscheme["keycolor"] + resource + self.colorscheme["defaultcolor"]
# append the rest of the information
s = s + " is unavailable : " + \
self.colorscheme["desccolor"] + status + self.colorscheme["defaultcolor"] + \
" at " + self.colorscheme["timecolor"] + ttime + self.colorscheme["defaultcolor"]
self.output(s)
def handlePresenceError(self, ffrom, code, text):
towrite = self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] + " says " + text + "(" + code + ")."
if self.imcom.conferences.has_key(ffrom):
del self.imcom.confnick[self.imcom.conferences[ffrom][0]]
del self.imcom.conferences[ffrom]
if (not self.imcom.subscriptions.has_key(ffrom) or
self.imcom.subscriptions[ffrom] == 'from' or
self.imcom.subscriptions[ffrom] == 'none'):
return
self.output(towrite)
return
def handleUserPresenceChange(self, oldStatus, oldReason, newStatus, newReason):
# this function is here simply to allow modules to know when the user's presence has changed.
pass
def handleConferenceMessage(self, conf, nick, body, delay):
"This function is called from the IMCom library when a message has been received from a conference"
if(self.profile.switches['confsuppress'][0] == "false"):
towrite = self.colorscheme["keycolor"] + conf + self.colorscheme["defaultcolor"] + "/" + self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"]
ttime = self.getTime()
ddate = self.getDate()
towrite = towrite + " - Conference Message - "
# append the timestamp and the rest of the information
towrite = towrite + self.colorscheme["timecolor"] + ddate + " | " + ttime + self.colorscheme["defaultcolor"]
towrite = towrite + "\n" + self.colorscheme["messagebodycolor"] + body + self.colorscheme["defaultcolor"]
else:
towrite = self.colorscheme["keycolor"] + conf + self.colorscheme["defaultcolor"] + "/" + self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] + " : "
towrite = towrite + self.colorscheme["messagebodycolor"] + body + self.colorscheme["defaultcolor"]
# ring the terminal bell if configured to
if(self.confRingBell):
self.printText(chr(7))
self.output(towrite)
def handleConferenceCreated(self, conf):
towrite = self.colorscheme["usercolor"] + conf + self.colorscheme["defaultcolor"] + " was just created by you. You need to set it up. I am requesting the form."
self.output(towrite)
self.imcom.sendConferenceConfigRequest(conf)
return
def handleConferenceConfigSet(self, conf):
towrite = self.colorscheme["usercolor"] + conf + self.colorscheme["defaultcolor"] + " configuration was set successfully."
self.output(towrite)
return
def handleConferenceConfig(self, conf, xmlform):
towrite = self.colorscheme["messagebodycolor"] + xmlform.instructions + self.colorscheme["defaultcolor"]
self.output(towrite)
answers = self.getXDataAnswers(xmlform.fields)
self.imcom.sendConferenceConfigResults(conf, answers)
def handleConferenceNicknameChange(self, conf, oldnick, newnick):
towrite = self.colorscheme["keycolor"] + self.imcom.conferences[conf][0] + self.colorscheme["defaultcolor"] + "/" + self.colorscheme["usercolor"] + oldnick + self.colorscheme["defaultcolor"]
towrite = towrite + " has changed their nickname to " + self.colorscheme["usercolor"] + newnick + self.colorscheme["defaultcolor"] + "."
self.output(towrite)
def handleConferenceNicknameChangeSuccess(self, conf, oldnick, newnick):
towrite = "You successfully changed your nick name from " + self.colorscheme["usercolor"] + oldnick + self.colorscheme["defaultcolor"]
towrite = towrite + " to " + self.colorscheme["usercolor"] + newnick + self.colorscheme["defaultcolor"] + " in conference "
towrite = towrite + self.colorscheme["keycolor"] + self.imcom.conferences[conf][0] + self.colorscheme["defaultcolor"] + "."
self.output(towrite)
def handleConferencePresence(self, conf, nick, show, reason, affiliation, role, jid):
"This function is called from the IMCom library when someone in a conference has changed status"
if(self.showPresenceUpdates):# and 0):
if reason == None:
reason = show
towrite = self.colorscheme["keycolor"] + self.imcom.conferences[conf][0] + self.colorscheme["defaultcolor"] + "/" + self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"]
towrite = towrite + " changed status to " + self.colorscheme["statuscolor"] + show + self.colorscheme["defaultcolor"]
towrite = towrite + " as " + self.colorscheme["desccolor"] + reason + self.colorscheme["defaultcolor"]
if affiliation != None:
towrite = towrite + " (" + self.colorscheme["keycolor"] + affiliation + self.colorscheme["defaultcolor"] + ") "
if role != None:
towrite = towrite + " (" + self.colorscheme["keycolor"] + role + self.colorscheme["defaultcolor"] + ") "
ttime = self.getTime()
towrite = towrite + " at " + self.colorscheme["timecolor"] + ttime + self.colorscheme["defaultcolor"]
self.output(towrite)
def handleConferenceInvite(self, conf, ffrom, reason, password):
towrite = self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] + " invites you to join " + self.colorscheme["keycolor"] + conf + self.colorscheme["defaultcolor"] + "\n"
towrite = towrite + " -- " + reason
if password != None:
towrite = towrite + "\nThe password is: " + self.colorscheme["keycolor"] + password + self.colorscheme["defaultcolor"]
self.output(towrite)
def handleConferenceSubject(self, conf, ffrom, subject):
towrite = self.colorscheme["keycolor"] + conf + self.colorscheme["defaultcolor"] + "/" + self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] + " set the subject to "
towrite = towrite + self.colorscheme["desccolor"] + subject + self.colorscheme["defaultcolor"]
self.output(towrite)
def handleConferenceKicked(self, conf, reason, jidOfKicker):
towrite = self.colorscheme["errorcolor"] + "KICKED: " + self.colorscheme["defaultcolor"] + " from " + self.colorscheme["keycolor"] + conf + self.colorscheme["defaultcolor"]
if jidOfKicker != None:
towrite = towrite + " by " + self.colorscheme["usercolor"] + jidOfKicker + self.colorscheme["defaultcolor"]
if reason != None:
towrite = towrite + " : " + reason
self.output(towrite)
def handleConferenceUserKicked(self, conf, person, reason, jidOfKicker):
towrite = self.colorscheme["keycolor"] + self.imcom.conferences[conf][0] + self.colorscheme["defaultcolor"] + "/" + self.colorscheme["usercolor"] + person + self.colorscheme["defaultcolor"]
towrite = towrite + " was kicked"
if jidOfKicker != None:
towrite = towrite + " by " + self.colorscheme["usercolor"] + jidOfKicker + self.colorscheme["defaultcolor"]
if reason != None:
towrite = towrite + " for : " + self.colorscheme["desccolor"] + reason + self.colorscheme["defaultcolor"]
towrite = towrite + "."
self.output(towrite)
def handleConferenceBanned(self, conf, reason, jidOfKicker):
towrite = self.colorscheme["errorcolor"] + "BANNED: " + self.colorscheme["defaultcolor"] + " from " + self.colorscheme["keycolor"] + conf + self.colorscheme["defaultcolor"]
if jidOfKicker != None:
towrite = towrite + " by " + self.colorscheme["usercolor"] + jidOfKicker + self.colorscheme["defaultcolor"]
if reason != None:
towrite = towrite + " : " + reason
self.output(towrite)
def handleConferenceUserBanned(self, conf, person, reason, jidOfKicker):
towrite = self.colorscheme["keycolor"] + self.imcom.conferences[conf][0] + self.colorscheme["defaultcolor"] + "/" + self.colorscheme["usercolor"] + person + self.colorscheme["defaultcolor"]
towrite = towrite + " was banned"
if jidOfKicker != None:
towrite = towrite + " by " + self.colorscheme["usercolor"] + jidOfKicker + self.colorscheme["defaultcolor"]
if reason != None:
towrite = towrite + " for : " + self.colorscheme["desccolor"] + reason + self.colorscheme["defaultcolor"]
towrite = towrite + "."
self.output(towrite)
def handleConferenceDestroyed(self, conf):
towrite = self.colorscheme["keycolor"] + conf + self.colorscheme["defaultcolor"] + " has been " + self.colorscheme["errorcolor"] + "destroyed" + self.colorscheme["defaultcolor"] + "."
self.output(towrite)
def handleMessageReceive(self, ffrom, body, thread, delay,
resource):
"""This function is called from the IMCom library when a
message has been received from a user or transport/service."""
# don't display messages from people who belong to the ignore
# group.
if( self.imcom.gjidhash.has_key(ffrom) and
operator.contains(self.imcom.gjidhash[ffrom],"ignore")):
if(self.debug):
logDebug("Skipping message from ignored user: " + ffrom)
return
# append the jid to the top of the stack of last users to
# event us.
self.appendUserQueue(ffrom)
# try and get their nickname. otherwise their nickname is
# their jid.
try:
nick = self.imcom.jidhash[ffrom]
except:
nick = ffrom
ttime = None
ddate = None
towrite = None
towrite = self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"]
if(resource != ""):
towrite = towrite + "/" + self.colorscheme["keycolor"] + resource + self.colorscheme["defaultcolor"]
# if the delay timestamp exists, format it properly, otherwise
# the current time.
if(delay != None):
# parse the delay, attach the timestamp in good format.
# tuple is (year, month, day, hour, minutes, seconds,
# daysintotheyear, dst)
mytup = (int(delay[:4]),int(delay[4:6]),int(delay[6:8]),
int(delay[9:11]),int(delay[12:14]),int(delay[15:17]),0,0,0)
thet = time.localtime( time.mktime(mytup) - time.timezone)
ttime = time.strftime('%H:%M:%S',thet)
ddate = time.strftime('%m/%d/%Y',thet)
towrite = towrite + " - " + self.colorscheme["errorcolor"] + \
"DELAYED " + self.colorscheme["defaultcolor"] + "Message - "
else:
ttime = self.getTime()
ddate = self.getDate()
towrite = towrite + " - Message - "
# append the timestamp and the rest of the information
towrite = towrite + self.colorscheme["timecolor"] + ttime + self.colorscheme["defaultcolor"]
if(delay != None):
towrite = towrite + " on " + self.colorscheme["timecolor"] + ddate
towrite = towrite + "\n" + self.colorscheme["messagebodycolor"] + body + self.colorscheme["defaultcolor"]
# ring the terminal bell if configured to
if(self.ringBell):
if(self.profile.audiocmd != None):
os.spawnv(os.P_NOWAIT,self.profile.audiocmd,(self.profile.audiocmd,self.profile.audioargs,))
self.printText(chr(7))
self.lastReceived = ffrom
self.output(towrite)
# threadhash is used so that if someone is seeing this as a
# particular conversation we continue the conversation. (Silly
# GUI apps)
self.threadHash[ffrom] = thread
self.logMessage(ffrom,ffrom,ddate,ttime,body)
def handleMessageError(self, ffrom, body, code):
"""This function is called by the IMCom library when there has
been some sort of error related to sending or receiving a
message. CLI just prints out the information for the user."""
if ffrom == None:
ffrom = "from-value-not-set"
if body == None:
body = ""
if code == None:
code = "0"
if( ( code == "0" or code == 0 ) and body != "" ):
towrite = self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["defaultcolor"] + body
else:
towrite = self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["defaultcolor"] + \
"sending message to"\
" " + self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] + " :"\
" " + self.colorscheme["errorcolor"] + errorCodes[code] + self.colorscheme["defaultcolor"]
self.output(towrite)
def handleIQError(self, ffrom, code, body):
"""This function is called by the IMCom library when there has
been some sort of error related to information request or
submission. CLI just prints out the information for the user."""
towrite = self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["defaultcolor"] + "InfoQuery event"\
" " + self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] + " :"\
" " + self.colorscheme["errorcolor"] + errorCodes[code] + self.colorscheme["defaultcolor"]
if body != None:
towrite = towrite + " : " + self.colorscheme["errorcolor"] + body + self.colorscheme["defaultcolor"]
self.output(towrite)
def handleChangePassword(self, success, newPassword, errorCode, errorText):
towrite = None
if success == 1:
towrite = "Your password has been successfully change."
self.profile.password = newPassword
self.prefs.writePreferences()
else:
towrite = self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["defaultcolor"] + " Your password could not be changed."
towrite = towrite + self.colorscheme["errorcolor"] + errorCodes[code] + self.colorscheme["defaultcolor"] + errorText
self.output(towrite)
return
def handleInfoError(self, code, text):
"""This function is called by the IMCom library when there has
been some sort of error related to information request.
CLI just prints out the information for the user."""
towrite = self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["defaultcolor"] + "getting info"\
" Code: " + self.colorscheme["errorcolor"] + code + self.colorscheme["defaultcolor"] + " "\
" Text: " + self.colorscheme["errorcolor"] + text + self.colorscheme["defaultcolor"]
self.output(towrite)
def handleFileReceive(self, jid, url):
"""This function is called by the IMCom library when someone
has requested to send us a file."""
self.appendUserQueue(jid)
nick = self.getNick(jid)
towrite = self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] + " wants to send you"\
" a file at URL:\n" + self.colorscheme["keycolor"] + url + self.colorscheme["defaultcolor"] +\
"\nTo receive the file type: \n" + \
self.prefs.getCommand(self.profile, "getfile") + \
" " + nick
self.output(towrite)
def handleFileReceived(self, jid, url, id):
"""This function is called by the IMCom library when we have
successfully received the file."""
self.appendUserQueue(jid)
nick = self.getNick(jid)
file = url[url.rfind('/')+1:]
towrite = "Successfully receieved a file from " + self.colorscheme["usercolor"] +\
nick + self.colorscheme["defaultcolor"] + \
"\nThe file is saved in " + self.colorscheme["keycolor"] + "~/.imcom/files/" + \
file + self.colorscheme["defaultcolor"]
self.output(towrite)
def handleFileErrorReceived(self, jid, url, id, type, text):
"""This function is called by the IMCom library when there has
been some sort of error in file transmission."""
self.appendUserQueue(jid)
towrite = self.colorscheme["errorcolor"] + "Error: " + self.colorscheme["defaultcolor"] + \
"An error occured trying to get a file from " + \
self.colorscheme["usercolor"] + jid + self.colorscheme["defaultcolor"] + ".\nThe URL: " + \
self.colorscheme["keycolor"] + url + self.colorscheme["defaultcolor"] + "\n" + \
"Errortype: " + str(type) + "\n" + \
"Errorvalue: " + str(text)
self.output(towrite)
def handleAgentList(self, theList):
"""This function is called by the IMCom library when we have
received a list of agents from the server. Agents is another
word for Transport or Server in Jabber terminology."""
self.output("The following is a list of transports available on" +
" your Jabber server.")
for i in theList:
if(i.name):
self.output(self.colorscheme["keycolor"] + i.jid + self.colorscheme["defaultcolor"] +
" : " + self.colorscheme["defaultcolor"] + i.name)
def handleAgentRegister(self, id, ffrom, instructions, fields):
#
# this will cause issues if gettingCommand is already set to 1
# however, in that case things get *really* complicated anyway...
# and it's not likely to happen unless the user is playing with us
#
self.output("Please finish whatever command you're currently entering (or just hit enter) to begin registration process.")
self.pendingCommand = 1
while(self.gettingCommand == 1):
time.sleep(0.01)
self.output("\n\nThe following are instructions to complete the "\
"registration process, they may be of use, then "\
"again, don't count on it.\n\n")
self.output(self.colorscheme["desccolor"] + instructions + self.colorscheme["defaultcolor"] + "\n\n")
self.gettingCommand = 1
params = ffrom
for i in fields:
params += "<" + i
CICommands.interactiveregCommand(self, params)
self.dumpQueue()
self.pendingCommand = 0
self.gettingCommand = 0
def handleAgentRegistered(self, ffrom):
"""This function is called by the IMCom library when an agent
has been successfully registered."""
self.output("Your registration request to " + ffrom +
" was successful. I will authorize the transport now "
"for you.")
addcommand = self.prefs.getCommand( self.profile, "add" )
self.output( "To add contacts who use the " + self.getNick( ffrom ) +
" use the command:\n" + addcommand +
" id@" + ffrom + " nick\n" +
"for instance, " + addcommand + " airog@" + ffrom + " airog" )
self.imcom.sendSubscribed(ffrom) # + "/registered") is it needed?
return
def handleAgentUnRegistered(self, ffrom):
"""This function is called by the IMCom library when an agent has been successfully removed."""
self.output("Your registration to " + ffrom + " was cancelled successfully.")
return
def handleSubscribe(self, ffrom, ask):
"""This function is called by the IMCom library when someone
is requesting to add us to their roster."""
self.appendUserQueue(ffrom)
if(self.debug):
logDebug("Handling a subscribe request, from : " + ffrom)
ffrom = self.getNick(ffrom)
self.output(self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] +
" requests permission to subscribe to your presence")
def handleSubscribed(self, ffrom):
"""This function is called by the IMCom library when someone
authorized you to add them to your roster."""
self.appendUserQueue(ffrom)
nick = self.getNick(ffrom)
self.output(self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] +
" authorized your subscription request")
def handleUnsubscribed(self, ffrom, success):
"""This function is called by the IMCom library when the
unsubscribe process is complete."""
self.appendUserQueue(ffrom)
if(success):
nick = self.getNick(ffrom)
if self.imcom.asks[ffrom] == 'subscribe':
self.output(self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] +
" refused to grant authorization for you to subscribe to their presence. ")
return
self.output("Unsubscribed from " + self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] +
"'s presence")
else:
self.output("Yowza! Unsubscription process failed!")
def handleUnsubscribe(self, ffrom):
"""This function is called by the IMCom library when someone
is requesting to remove us from their roster."""
self.output(self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] + " is unsubscribing from your presence.")
# Reports whether a change to the roster was successful or not
def handleRosterUpdateCheck(self, successful, message):
"""This function is called by the IMCom library after a roster
change has been made, telling us whether or not it was successful"""
if(successful):
self.output("Roster update successful: " + message)
else:
self.output("Roster update " + self.colorscheme["errorcolor"] + " Error: " +
message + self.colorscheme["defaultcolor"])
# Called when there has been a change to the roster
def handleRosterUpdate(self, jid, name, subs, ask, success):
"""This function is called by the IMCom library when another
resource has made a change to our roster.
"""
# update our roster lists based upon information in jid, name
if(subs == "remove"):
if(success):
self.output("Removed " + self.colorscheme["usercolor"] + jid + self.colorscheme["defaultcolor"] +
" from our lists")
return
else:
self.output("Removal process " + self.colorscheme["errorcolor"] +
"failed!" + self.colorscheme["defaultcolor"])
return
def handleNickCollision(self, nickname, newjid, oldjid):
if nickname == None:
nickname = ""
if newjid == None:
newjid = ""
if oldjid == None:
oldjid = ""
self.output(self.colorscheme["errorcolor"] + "WARNING:" + self.colorscheme["defaultcolor"] + " A nickname collision occured.\n" +
"Nickname: " + self.colorscheme["keycolor"] + nickname + self.colorscheme["defaultcolor"] + " is now linked to JID: " +
self.colorscheme["keycolor"] + newjid + self.colorscheme["defaultcolor"] + " instead of " + self.colorscheme["keycolor"] + oldjid + self.colorscheme["defaultcolor"])
def handleNickHasSlash(self, nickname, newnick, jid):
self.output(self.colorscheme["errorcolor"] + "WARNING:" + self.colorscheme["defaultcolor"] + " The nickname " +
self.colorscheme["usercolor"] + nickname + self.colorscheme["defaultcolor"] +
" contains a slash. IMCom doesn't allow for this. For the duration of this session the user's nickname will be " +
self.colorscheme["usercolor"] + newnick + self.colorscheme["defaultcolor"] + ".")
def handleVCardSubmit(self, success):
if success:
self.output("Successfully submitted your vcard")
else:
self.output("Your VCard submission was unsuccessful.")
def handleVCard(self, ffrom, fn, given, family, nickname, email):
self.appendUserQueue(ffrom)
if(ffrom == None):
return
self.output("Information on " + self.colorscheme["usercolor"] + ffrom +
self.colorscheme["defaultcolor"] + ": ")
if(fn != None):
self.output(self.colorscheme["keycolor"] + "Fullname: " + self.colorscheme["desccolor"] + fn +
self.colorscheme["defaultcolor"])
if(given != None):
self.output(self.colorscheme["keycolor"] + "Given: " + self.colorscheme["desccolor"] + given +
self.colorscheme["defaultcolor"])
if(family != None):
self.output(self.colorscheme["keycolor"] + "Family: " + self.colorscheme["desccolor"] +
family + self.colorscheme["defaultcolor"])
if(nickname != None):
self.output(self.colorscheme["keycolor"] + "Nickname: " + self.colorscheme["desccolor"] +
nickname + self.colorscheme["defaultcolor"])
if(email != None):
self.output(self.colorscheme["keycolor"] + "Email: " + self.colorscheme["desccolor"] +
email + self.colorscheme["defaultcolor"])
def handleNoVCard(self, ffrom):
self.appendUserQueue(ffrom)
if(ffrom == None):
return
self.output("There is no information (no vcard filed) for " +
self.colorscheme["usercolor"] + ffrom + self.colorscheme["defaultcolor"] + ". ")
return
def handleNegotiationRequest(self, ffrom, type, options, id):
nick = self.getNick(ffrom)
text = self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] + " is requesting (id: " + self.colorscheme["keycolor"] + id + self.colorscheme["defaultcolor"] + ") "
text = text + "which of the following works for you for " + self.colorscheme["desccolor"] + type + self.colorscheme["defaultcolor"] + ".\n"
for option in options:
text = text + option + "\n"
self.output(text)
def handleNegotiationResult(self, ffrom, type, options, id):
nick = self.getNick(ffrom)
text = self.colorscheme["usercolor"] + nick + self.colorscheme["defaultcolor"] + " has specified (id: " + self.colorscheme["keycolor"] + id + self.colorscheme["defaultcolor"] + ") "
text = text + "that " + self.colorscheme["keycolor"] + options[0] + self.colorscheme["defaultcolor"] + " works for them for " + self.colorscheme["desccolor"] + type + self.colorscheme["defaultcolor"]
self.output(text)
def handleVersionResponse(self, ffrom, fromResource, name, version, os):
nick = self.getNick(ffrom)
text = self.colorscheme["usercolor"] + nick + "/" + fromResource + self.colorscheme["defaultcolor"] + " is using client: " + self.colorscheme["desccolor"] + name + self.colorscheme["defaultcolor"]
if version != None:
text = text + " version: " + self.colorscheme["keycolor"] + version + self.colorscheme["defaultcolor"]
if os != None:
text = text + " on os: " + self.colorscheme["keycolor"] + os + self.colorscheme["defaultcolor"]
self.output(text)
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# -------------------------------------------------------------------------
# Utility Functions
# -------------------------------------------------------------------------
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def addSocketWrapper(self, sw):
self.socketlisteners.append(sw)
self.output("A client socket started.")
def removeSocketWrapper(self, sw):
self.socketlisteners.remove(sw)
self.output("A client socket disconnected.")
def getColor(self, fg, bg):
if bg != "":
retval = fg[:-1] + ";" + bg[4:]
return retval
return fg
def overrideColor(self, colorname, newfgcolor, newbgcolor=""):
if not colorname in self.colorscheme.keys():
return None
fcolor = self.getColor(newfgcolor, newbgcolor)
ocolor = self.colorscheme[colorname]
self.colorscheme[colorname] = fcolor
return ocolor
def setColors(self):
g = self.profile.sessioncolors
self.colorscheme["backgroundcolor"] = g["background"]
self.colorscheme["usercolor"] = self.getColor(g["user"], self.colorscheme["backgroundcolor"])
self.colorscheme["statuscolor"] = self.getColor(g["status"], self.colorscheme["backgroundcolor"])
self.colorscheme["errorcolor"] = self.getColor(g["error"], self.colorscheme["backgroundcolor"])
self.colorscheme["messagebodycolor"] = self.getColor(g["messagebody"], self.colorscheme["backgroundcolor"])
self.colorscheme["timecolor"] = self.getColor(g["time"], self.colorscheme["backgroundcolor"])
self.colorscheme["desccolor"] = self.getColor(g["desc"], self.colorscheme["backgroundcolor"])
self.colorscheme["sepcolor"] = self.getColor(g["sep"], self.colorscheme["backgroundcolor"])
self.colorscheme["defaultcolor"] = self.getColor(g["default"], self.colorscheme["backgroundcolor"])
self.colorscheme["keycolor"] = self.getColor(g["key"], self.colorscheme["backgroundcolor"])
self.profile.switches['colors'][0] = "true"
CICommands.setCIColors(self.prefs, self.profile)
def setNoColors(self):
self.colorscheme["usercolor"] = ""
self.colorscheme["statuscolor"] = ""
self.colorscheme["errorcolor"] = ""
self.colorscheme["messagebodycolor"] = ""
self.colorscheme["timecolor"] = ""
self.colorscheme["desccolor"] = ""
self.colorscheme["sepcolor"] = ""
self.colorscheme["defaultcolor"] = ""
self.colorscheme["keycolor"] = ""
self.colorscheme["backgroundcolor"] = ""
self.profile.switches['colors'][0] = "false"
CICommands.setCIColors(self.prefs, self.profile)
def beginLine(self):
if(self.profile.switches['clearline'][0] == "true"):
if TERMWIDTH and self.gettingCommand:
myStrlen = len(self.currentStatus + ":" + self.prompt)
if(READLINE == 1):
myStrlen = myStrlen + len(readline.get_line_buffer())
myHeight = int(math.floor(myStrlen / getTerminalWidth()))
myWidth = myStrlen % getTerminalWidth()
for n in range(myHeight+1):
output=chr(27)+"[%dD"%getTerminalWidth() + self.colorscheme["backgroundcolor"] + chr(27)+"[K" + self.colorscheme["backgroundcolor"]
self.printText(output)
if n != myHeight:
output=chr(27)+"[1A"
self.printText(output)
else:
output=chr(27)+"[%dD"%getTerminalWidth() + self.colorscheme["backgroundcolor"]
self.printText(output)
def clearCommand(self):
self.beginLine()
if(self.profile.switches['clearline'][0] == "true"):
output=chr(27)+"[K" + self.colorscheme["backgroundcolor"]# + chr(27)+"[%dD"%getTerminalWidth()
self.printText(output)
else:
self.printText("\n")
def clearScreen(self):
if(self.profile.switches['colors'][0] == "true"):
output = self.colorscheme["defaultcolor"] + chr(27)+"[J" + self.colorscheme["defaultcolor"]
sys.stdout.write(output)
sys.stdout.flush()
def statusNag(self):
if( self.currentStatus != "online" ):
self.output( "Your current status is " +
self.colorscheme["statuscolor"] + self.currentStatus + self.colorscheme["defaultcolor"] +
": " + self.colorscheme["desccolor"] +
self.currentStatusReason + self.colorscheme["defaultcolor"] )
def printText(self, st):
try:
sys.stdout.write(st.encode(self.profile.encoding, 'replace'))
sys.stdout.flush()
except:
traceback.print_exc()
print "An exception occuring trying to print to terminal."
print "This is most likely due to an extended ascii character."
print "You may have missed a message or query response."
def dumpQueue(self):
for item in self.outputQueue:
self.clearCommand()
self.printText(item+"\n")
self.outputQueue = []
def output(self, st):
st = self.colorscheme["defaultcolor"] + st
for key in self.socketlisteners:
key.sendData(st+"\n.\n")
if(( self.mode == self.MULTILINE) and ( not self.allowInterrupt )):
self.outputQueue.append(st)
return
self.clearCommand()
self.printText(st+"\n")
if(READLINE == 1 and self.gettingCommand):
self.printText(self.currentStatus + ":" + self.prompt + unicode(readline.get_line_buffer(),self.profile.encoding))
elif(self.gettingCommand):
self.printText(self.currentStatus + ":" + self.prompt)
def checkto(self, nick):
if(nick == None):
return 0
if(-1 != string.find(nick,"@")):
return 1
if self.imcom.nickhash.has_key(nick):
return 1
if self.imcom.confnick.has_key(nick):
return 1
tmp = string.split(nick,".")
if(len(tmp) > 1):
return 1
return 0
def getTime(self):
return time.strftime('%H:%M:%S',time.localtime(time.time()))
def getDate(self):
return time.strftime('%m/%d/%Y',time.localtime(time.time()))
# begin add by ted
def logMultiMessage(self, fnames, sender, ddate, ttime, text):
for fname in fnames:
self.logMessage(fname, sender, ddate, ttime, text)
# end add by ted
def logMessage(self, fname, sender, ddate, ttime, text):
path = os.environ['HOME']+"/.imcom/"+self.profile.name+"/"
if( sender == self.profile.user+"@"+self.profile.server ):
self.lastToDict[fname] = text
else:
self.lastFromDict[sender] = text
# strip filename of /'s from resource string..
if(-1 != fname.find("/")):
fname = fname[:fname.find("/")]
# fix the text by replacing < with < etc.
text = self.imcom.normalize(text)
# make sure path exists.
try:
os.makedirs(path[:-1],0700)
except:
pass
try:
if (fname != self.getNick(fname)):
os.symlink(fname,path+self.getNick(fname))
except:
pass
try:
aFile = path+fname
aFile = aFile.encode(self.profile.encoding)
if(CODECS):
f = codecs.open(aFile,"ab+", self.profile.encoding, 'replace')
else:
f = open(aFile,"ab+")
f.write("<message from='"+sender+"' date='"+ddate+
"' time='"+ttime+"'>")
f.write(text)
f.write("</message>\n")
f.flush()
f.close()
except:
print "There was an exception attempting to log the message"
traceback.print_exc()
def getJID(self, nick):
result = nick
nick2 = nick # keep a copy of the original nick
resource = None
if nick == None or len(nick) == 0:
return (None, None)
if(nick != None and -1 != string.find(nick,"/")):
resource = nick[string.find(nick,'/')+1:]
# this is not a valid transformation on agents.
nick = nick[:string.find(nick,'/')]
else:
resource = None
if nick == None or len(nick) == 0:
return (None, None)
if(nick[0] == "$" and len(nick) == 2):
n = int(nick[1])
if(len(self.userQueue)>n):
result = self.userQueue[n]
if(self.debug):
logDebug("$? Result is: " + result)
return (result, self.imcom.normalize(resource))
else:
result = nick
if(self.imcom.nickhash.has_key(nick)):
result = self.imcom.nickhash[nick]
elif(self.imcom.confnick.has_key(nick)):
result = self.imcom.confnick[nick]
else:
for key in self.imcom.agenthash.keys():
if self.imcom.agenthash[key] == nick or self.imcom.agenthash[key] == nick2:
result = key
if not self.imcom.preshash.has_key(result):
result = result + "/registered"
break
if(self.imcom.jidhash.has_key(result) and self.imcom.reshash.has_key(result) and resource == None):
resource = self.imcom.reshash[result][0]
return (result, self.imcom.normalize(resource))
def isAgent(self, jid):
return ( self.imcom.agenthash.has_key(jid) or
self.imcom.agenthash.has_key(jid[:-11]))
def getNick(self, jid):
result = jid
if jid.find("/") != -1:
jid = jid[:jid.find("/")]
if( string.find( jid, "@" ) == -1 ):
if( self.imcom.agenthash.has_key(jid) ):
return self.imcom.agenthash[jid]
if(self.imcom.jidhash.has_key(jid) and
self.imcom.jidhash[jid] != None):
result = self.imcom.jidhash[jid]
else:
result = jid
return result
def updateKeyInHash(self, hash, oldkey, newkey):
keys = hash.keys()
for item in keys:
if(item == oldkey):
value = hash[item]
hash[newkey]=value
return
def updateHash(self, hash, oldkey, newkey, value):
keys = hash.keys()
for item in keys:
if(item == oldkey):
del hash[item]
hash[newkey] = value
def appendUserQueue(self, ffrom):
self.userQueue.insert(0,ffrom)
if(len(self.userQueue) > 10):
self.userQueue = self.userQueue[:10]
return
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# -------------------------------------------------------------------------
# XData Functions
# -------------------------------------------------------------------------
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def createXDataLabel(self, var, label, required, default):
myLabel = self.colorscheme["desccolor"]
if label != None:
myLabel = myLabel + label
else:
myLabel = myLabel + var
if default != None:
myLabel = myLabel + self.colorscheme["defaultcolor"] + " [" + self.colorscheme["keycolor"] + str(default) + self.colorscheme["defaultcolor"] + "] : "
else:
myLabel = myLabel + self.colorscheme["defaultcolor"] + " : "
return myLabel
def getXDataListSingleAnswer(self, var, label, default, options, required):
lstlen = len(options)
print "You may answer with a number between 1 and", lstlen
myLabel = self.createXDataLabel(var, label, required, None)
n = 1
for option in options:
print self.colorscheme["keycolor"], n, self.colorscheme["defaultcolor"] + ":" + self.colorscheme["desccolor"], option[0], self.colorscheme["defaultcolor"]
n = n + 1
result = None
print myLabel
result = raw_input( "> " )
if result == '' and not required:
return [var, '']
answer = None
try:
answer = int(result)
except:
pass
while answer == None or answer > lstlen or answer < 1:
print "Please answer with a number between 1 and", lstlen
print myLabel
result = raw_input("> " )
if result == '' and not required:
return [var, '']
answer = None
try:
answer = int(result)
except:
pass
return [var, options[answer-1][1]]
def getXDataBooleanAnswer(self, var, label, value, required):
print "You may answer with (yes, true, 1, y, t), (no, false, 0, n, f)\nJust hit enter if you don't want to answer at all."
myLabel = self.createXDataLabel(var, label, required, value)
result = None
answer = None
print myLabel
result = raw_input( "> " )
if value != None and result == '':
result = value
if result.lower() in ("yes", "true", "1", "y", "t"):
answer = 1
if result.lower() in ("no", "false", "0", "n", "f"):
answer = 0
if result == '':
answer = ""
while (required and result == None) or answer == None:
if required:
print "This is a required field, please enter a valid answer."
else:
print "Please answer with (yes, true, 1, y, t), (no, false, 0, n, f)\nJust hit enter if you don't want to answer at all."
print myLabel
result = raw_input( "> " )
if result.lower() in ("yes", "true", "1", "y", "t"):
answer = 1
if result.lower() in ("no", "false", "0", "n", "f"):
answer = 0
if result == '':
answer = ""
return [var, str(answer)]
def getXDataTextSingleAnswer(self, var, label, value, required, private):
myLabel = self.createXDataLabel(var, label, required, value)
result = None
print myLabel
if private:
result = getpass.getpass( "> " )
else:
result = raw_input( "> " )
if value != None and result == '':
result = value
while required and result == '':
print "This is a required field, please enter it correctly."
print myLabel
if private:
result = getpass.getpass( "> " )
else:
result = raw_input( "> " )
return [var, result]
def getXDataTextMultiAnswer(self, var, label, value, required):
myLabel = self.createXDataLabel(var, label, required, value)
result = ""
done = None
print "Please enter information, finishing with a single period on a line of its own."
print myLabel
while(not done):
tmp = raw_input( "> " )
if tmp == ".":
done = 1
continue
result = result + "\n" + tmp
return [var, result]
def getXDataListMultiAnswer(self, var, label, default, options, required):
myLabel = self.createXDataLabel(var, label, required, None)
result = ''
lstlen = len(options)
print "Please select as many options as you want 1-"+str(lstlen)+" delimiting by commas only, no spaces."
n = 1
for option in options:
print self.colorscheme["keycolor"], n, self.colorscheme["defaultcolor"] + ":" + self.colorscheme["desccolor"], option[0], self.colorscheme["defaultcolor"]
n = n + 1
print myLabel
tmp = raw_input( "> " )
tmps = tmp.split(",")
for num in tmp:
try:
i = int(num)
if i < 1 or i > lstlen:
continue
result = result + "\n" + options[i][1]
except:
pass
return result
def getXDataJIDSingleAnswer(self, var, label, value, required):
myLabel = self.createXDataLabel(var, label, required, value)
result = None
gaveGarbage = 0
print "You may either use a fully qualified JID, or the nick name of someone on your roster."
print myLabel
result = raw_input( "> " )
if value != None and result == '':
result = value
# validate jid here by nick or jid validation routine
jid = self.getJID(result)[0]
if(not self.checkTo(jid)):
gaveGarbage = 1
while (required and result == '') or gaveGarbage:
print "This is a required field, please enter it correctly."
print myLabel
result = raw_input( "> " )
# validate jid here by nick or jid validation routine
jid = self.getJID(result)[0]
if(not self.checkTo(jid)):
gaveGarbage = 1
else:
gaveGarbage = 0
return [var, jid]
def getXDataJIDMultiAnswer(self, var, label, value, required):
myLabel = self.createXDataLabel(var, label, required, value)
result = ''
print "You may either use a fully qualified JID, or the nick name of someone on your roster."
print "Separate JIDs with commas only."
print myLabel
tmp = raw_input( "> " )
if value != None and tmp == '':
result = value
tmps = tmp.split(',')
for maybeJid in tmps:
# validate jid here by nick or jid validation routine
jid = self.getJID(maybeJid)[0]
if(self.checkTo(jid)):
result = result + "\n" + jid
return [var, result]
def getXDataAnswers(self, xdataInformation):
self.output("Please finish whatever command you're currently entering (or just hit enter) to begin filling out this form.")
self.pendingCommand = 1
while(self.gettingCommand == 1):
time.sleep(0.01)
self.mode = self.MULTILINE
if(READLINE == 1):
readline.parse_and_bind("set disable-completion on")
self.gettingCommand = 1
answers = []
for field in xdataInformation:
print
required = None
if field[5] != None:
required = int(field[5])
if field[6] != None:
print self.colorscheme["desccolor"] + field[6] + self.colorscheme["defaultcolor"]
if field[0] == "hidden":
answers.append([field[1], field[3]])
continue
if field[0] == "fixed":
print field[3]
continue
if field[0] == "text-single":
answers.append(self.getXDataTextSingleAnswer(field[1], field[2], field[3], required, 0))
continue
if field[0] == "list-single":
answers.append(self.getXDataListSingleAnswer(field[1], field[2], field[3], field[4], required))
continue
if field[0] == "list-multi":
answers.append(self.getXDataListMultiAnswer(field[1], field[2], field[3], field[4], required))
continue
if field[0] == "boolean":
answers.append(self.getXDataBooleanAnswer(field[1], field[2], field[3], required))
continue
if field[0] == "text-private":
answers.append(self.getXDataTextSingleAnswer(field[1], field[2], field[3], required, 1))
continue
if field[0] == "text-multi":
answers.append(self.getXDataTextMultiAnswer(field[1], field[2], field[3], required))
continue
if field[0] == "jid-single":
answers.append(self.getXDataJIDSingleAnswer(field[1], field[2], field[3], required))
continue
if field[0] == "jid-multi":
answers.append(self.getXDataJIDMultiAnswer(field[1], field[2], field[3], required))
continue
self.gettingCommand = 0
self.pendingCommand = 0
if(READLINE == 1):
# turn completions back on
readline.parse_and_bind("set disable-completion off")
# show the messages that came in while they were busy
self.dumpQueue()
self.mode = 0
return answers
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# -------------------------------------------------------------------------
# Command Line Input, Execution Functions
# -------------------------------------------------------------------------
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def run(self):
self.running = 1
self.clearScreen()
try:
while(self.running):
if( len( self.commandQueue ) == 0 ):
while self.pendingCommand != 0:
self.gettingCommand = 0
time.sleep(0.1)
self.gettingCommand = 1
self.line = raw_input(self.currentStatus + ":" + self.prompt)
self.gettingCommand = 0
else:
self.line = self.commandQueue.pop( 0 )
self.executeCommand(self.line)
except EOFError:
print
self.executeCommand(self.prefs.getCommand(self.profile,"quit"))
except KeyboardInterrupt:
print
self.executeCommand(self.prefs.getCommand(self.profile,"quit"))
#except:
# print "something bad happened"
# print "Unexpected error:", sys.exc_info()[0]
# self.executeCommand(self.profile.commands.quit)
def getAllText(self,prompt):
l = ""
t = ""
saveprompt = ""
self.mode = self.MULTILINE
if(READLINE == 1):
readline.parse_and_bind("set disable-completion on")
self.gettingCommand = 1
# save the current prompt before we change it
saveprompt = self.prompt
self.prompt = "[" + prompt + "]: "
l = raw_input( self.prompt )
while(l != "."):
if(l == "#"):
t = None
break
t = t + l + "\n"
l = raw_input("[" + prompt + "]: ")
self.gettingCommand = 0
if(READLINE == 1):
# turn completions back on
readline.parse_and_bind("set disable-completion off")
# show the messages that came in while they were busy
self.dumpQueue()
self.mode = 0
# repair the prompt
self.prompt = saveprompt
# remove the last newline?
if( t != None ):
t = t[:-1]
t = unicode(t, self.profile.encoding)
return t
def checkAlias(self, command, line, profile):
if(profile.aliases.has_key(command)):
return profile.aliases[command] + " " + line
return None
def executeCommand(self, line):
p = self.prefs
pr = self.profile
if not isinstance(line,types.UnicodeType):
line = unicode(line, pr.encoding)
temp = string.split(line,None,1)
if self.autostatus != None:
self.autostatus.setIdleTime( 0 )
self.autostatus.doAutoStatus()
if(len(temp)==0):
return
command = string.lower(temp[0])
if(len(temp)>1):
line = temp[1]
else:
line = ""
line = string.strip(line)
a = self.checkAlias(command, line, pr)
if(a != None):
self.executeCommand(a)
return
if(command == "/startserver"):
self.socketserver = SocketWrapper.listener(self)
self.socketserver.start()
print "Started ths socket server"
return
if(command == "/stopserver"):
self.socketserver.cli = None
self.socketserver.running = 0
print "Stopped the socket server"
return
if(command == "/eval"):
try:
pp.pprint(eval(line))
except:
traceback.print_exc()
a,b,c = sys.exc_info()
print "An exception occured"
print(a)
print(b)
print(c)
print self.colorscheme["errorcolor"] + "ERROR: " + self.colorscheme["defaultcolor"] + \
"This doesn't evaluate well: <" + line + ">"
return
for key in self.profile.moduleCommands.keys():
if command == self.profile.moduleCommands[key][0].lower():
self.profile.moduleCommands[key][1](line)
return
keys = pr.sessioncommands.keys()
for key in keys:
if command == pr.sessioncommands[key].lower():
#callstring = key + "Command(self, line)"
#eval(callstring)
getattr(CICommands, key+"Command")(self,line)
return
print "What you say?? (" + self.prefs.getCommand(self.profile, "help") + " for help.)"
def findItem(self, dict, item):
keys = dict.keys()
for blah in keys:
if(dict[blah] == item):
return blah
return None
def printRoster(self):
self.printRosterBackend( 0 );
def printRosterGetLists(self, keys, doCulling):
if(keys == None):
keys = self.imcom.jidhash.keys()
chatPeople = []
onlinePeople = []
awayPeople = []
xaPeople = []
dndPeople = []
offlinePeople = []
mxn = 0 # max nick length
mxs = 0 # max show length
mxt = 0 # max status length
for person in keys:
# cull only if we're told to.
if(doCulling):
# don't bother with people we're not subscribed to
# unless we've asked for permission or they are a transport
if((not self.imcom.subscriptions.has_key(person) or
self.imcom.subscriptions[person] == 'from' or
self.imcom.subscriptions[person] == 'none')
and
(self.imcom.asks[person] == 'none' or
self.imcom.asks[person] == None) and
not self.isAgent(person)):
continue
# if the person is in the ignore or lurker group, skip them
if(not self.imcom.gjidhash.has_key(person)):
continue
glist = self.imcom.gjidhash[person]
if(operator.contains(glist, "lurker") or
operator.contains(glist, "ignore")):
continue
# if we don't have a presence entry for this person, skip them
if not self.imcom.preshash.has_key(person):
continue
# get the nick and calculate max lengths
nick = self.getNick(person)
available,show,status = self.imcom.preshash[person]
if(len(nick) > mxn):
mxn = len(nick)
if(len(show) > mxs):
mxs = len(show)
if(len(status) > mxt):
mxt = len(status)
if show == "chat":
chatPeople.append(nick)
if show == "online":
onlinePeople.append(nick)
if show == "away":
awayPeople.append(nick)
if show == "xa":
xaPeople.append(nick)
if show == "dnd":
dndPeople.append(nick)
if not available:
offlinePeople.append(nick)
chatPeople.sort()
onlinePeople.sort()
awayPeople.sort()
xaPeople.sort()
dndPeople.sort()
offlinePeople.sort()
return [mxn, mxs, mxt, chatPeople, onlinePeople, awayPeople, xaPeople, dndPeople, offlinePeople]
def printRosterPrintList(self, aList, mxn, mxs, mxt):
trs = "" # trs is theReturnString
for nick in aList:
jid = self.getJID(nick)[0]#self.imcom.nickhash[nick]
if not self.imcom.preshash.has_key(jid):
continue
available,show,status = self.imcom.preshash[jid]
asks = ""
if(self.imcom.asks.has_key(jid) and
self.imcom.asks[jid] == 'subscribe'):
asks = " awaiting authorization"
if not available:
line = self.colorscheme["usercolor"] + string.ljust(nick,mxn+4) + \
self.colorscheme["defaultcolor"] + self.colorscheme["statuscolor"] + \
string.ljust(show,mxs+2) + self.colorscheme["defaultcolor"] +\
self.colorscheme["desccolor"] + status + self.colorscheme["defaultcolor"] + asks
trs = trs + line + "\n"
else:
resources = ""
if(self.imcom.reshash.has_key(jid)):
for res in self.imcom.reshash[jid][2].keys():
ress = self.imcom.reshash[jid][2][res][1][0]
if(resources == "" and res != ""):
resources = " from resources: (" + self.colorscheme["keycolor"] + res + \
self.colorscheme["defaultcolor"] + ", " + self.colorscheme["statuscolor"] + ress + \
self.colorscheme["defaultcolor"] + ")"
elif(resources != ""):
resources = resources + ", (" + self.colorscheme["keycolor"] + res + \
self.colorscheme["defaultcolor"] + ", " + self.colorscheme["statuscolor"] + ress + \
self.colorscheme["defaultcolor"] + ")"
line = self.colorscheme["usercolor"] + string.ljust(nick,mxn+4) +\
self.colorscheme["defaultcolor"] + self.colorscheme["statuscolor"] +\
string.ljust(show,mxs+2) + self.colorscheme["defaultcolor"] +\
self.colorscheme["desccolor"] + status + self.colorscheme["defaultcolor"] + resources + asks
trs = trs + line + "\n"
return trs
def printRosterBackend(self, onlineonly):
mxn, mxs, mxt, chatPeople, onlinePeople, awayPeople, xaPeople, dndPeople, offlinePeople = self.printRosterGetLists(None, 1)
trs = "" # trs is theReturnString
trs = trs + self.colorscheme["sepcolor"] + \
"----------------------------------------" + self.colorscheme["defaultcolor"] + "\n"
trs = trs + "You are currently " + self.colorscheme["statuscolor"] + self.imcom.status + \
self.colorscheme["defaultcolor"] + ":" + self.currentStatusReason + "\n"
trs = trs + self.colorscheme["sepcolor"] + \
"---------------Your Roster--------------" + self.colorscheme["defaultcolor"] + "\n"
statList = []
if( not onlineonly):
statList.append(offlinePeople)
statList = statList + [dndPeople, xaPeople, awayPeople, onlinePeople, chatPeople]
for aList in statList:
trs = trs + self.printRosterPrintList(aList, mxn, mxs, mxt)
self.output(trs)
def printOnlineRoster(self):
self.printRosterBackend( 1 );
def showGroupMembers(self, groupname):
keys = self.imcom.grouphash[groupname]
mxn, mxs, mxt, chatPeople, onlinePeople, awayPeople, xaPeople, dndPeople, offlinePeople = self.printRosterGetLists(keys, 0)
self.printText(self.colorscheme["sepcolor"] + "----------------------------------------" + self.colorscheme["defaultcolor"] + "\n")
self.printText(self.colorscheme["keycolor"] + string.center(groupname,40) + self.colorscheme["defaultcolor"] + "\n")
self.printText(self.colorscheme["sepcolor"] + "----------------------------------------" + self.colorscheme["defaultcolor"] + "\n")
statList = [offlinePeople, dndPeople, xaPeople, awayPeople, onlinePeople, chatPeople]
trs = ""
for aList in statList:
trs = trs + self.printRosterPrintList(aList, mxn, mxs, mxt)
self.output(trs)
def timeInRange( self, unixTime, beginTime, endTime ):
curtime = time.localtime( unixTime )
beginTime = 0, 0, 0, int( beginTime[0] ), int( beginTime[1] ), \
0, 0, 0, curtime[8]
endTime = 0, 0, 0, int( endTime[0] ), int( endTime[1] ), \
0, 0, 0, curtime[8]
curtime = 0, 0, 0, curtime[3], curtime[4], 0, 0, 0, curtime[8]
if( ( time.mktime( curtime ) >= time.mktime( beginTime ) ) and
( time.mktime( curtime ) <= time.mktime( endTime ) ) ):
return 1
return 0
def doAutoStatus( self, idleTime ):
self.output( "Wow, michael just made a serious error. Please "\
"message him at michael@www.cosby.dhs.org to "\
"mention this." )
return
def tabCompleter(text, state):
global cli, tabCompleteToLastUser
try:
if(text == None):
return None
if len(readline.get_line_buffer()) == 0 and cli.lastMessaged != None:
if tabCompleteToLastUser:
tabCompleteToLastUser = 0
return None
else:
tabCompleteToLastUser = 1
lastMessagedNick = cli.getNick(cli.lastMessaged)
if lastMessagedNick.find(' ') != -1:
lastMessagedNick = '"' + lastMessagedNick + '"'
return cli.prefs.getCommand(cli.profile, "msg") + " " + lastMessagedNick + " "
if (len(readline.get_line_buffer()) == 1 and readline.get_line_buffer()[0] != "") and cli.lastReceived != None:
if tabCompleteToLastUser:
tabCompleteToLastUser = 0
return None
else:
tabCompleteToLastUser = 1
lastReceivedNick = cli.getNick(cli.lastReceived)
if lastReceivedNick.find(' ') != -1:
lastReceivedNick = '"' + lastReceivedNick + '"'
return cli.prefs.getCommand(cli.profile, "msg") + " " + lastReceivedNick + " "
params = string.split(readline.get_line_buffer())
if(len(params) == 1 and readline.get_line_buffer()[-1] != ' '):
# We're tab completing a command
r = completeFromHash(cli.profile.aliases,text,state)
if(r == None):
return completeFromHashValue(cli.profile.sessioncommands,
text,state)
else:
return r
if((len(params) == 1 and readline.get_line_buffer()[-1] == ' ') or
(len(params) == 2 and readline.get_line_buffer()[-1] != ' ')):
# We're probably completing a nick
foo = 1
while foo:
val = completeFromHash(cli.imcom.nickhash,text,state)
if val == None:
foo = 0
elif not operator.contains(cli.getJID(val)[0],"ignore"):
val += " "
foo = 0
else:
state=state+1
return val
if((len(params) == 3 and readline.get_line_buffer()[-1] != ' ') or
(len(params) == 2 and readline.get_line_buffer()[-1] == ' ')):
# Command specific tab completion
return None
return None
except:
traceback.print_exc()
a,b,c = sys.exc_info()
print "An exception occured"
print(a)
print(b)
print(c)
def completeFromHash(hash, text, state):
keys = hash.keys()
i=0
if not isinstance(text,types.UnicodeType):
text = unicode(text, cli.profile.encoding)
if len(text) > 0 and text[0] in ("'", '"'):
text = text[1:]
for item in keys:
if (item == None):
continue
if (text == item[:len(text)]):
if(i == state):
#print "Returning " + item
if(item.find(' ') != -1):
return '"' + item + '"'
return item
else:
i = i + 1
#print "Returning None"
return None
def completeFromHashValue(hash, text, state):
#print "In completeFromHashValue"
#print "hash is: ", hash
keys = hash.keys()
i = 0
for item in keys:
val = hash[item]
if(text == val[:len(text)]):
if(i == state):
return val
else:
i = i + 1
return None
def printStartup():
print("IMCom version " + str(VERSION) + " is released under the BSD license.\nSee LICENSE for details")
print("Written by Casey Crabb (JID: airog@floobin.cx)")
def logDebug(s):
#print s.encode(self.profile.encoding, "replace")
print s.encode('ASCII', "replace")
def printUsage():
printStartup()
print("Usage: imcom [opts]")
print("-d --debug Turn on debugging")
print("-p --profile Use the specified profile")
print("Example: imcom -p floobin.cx")
print("Example: imcom")
print("Example: imcom -d")
print("")
def handler(signum, frame):
#global cli
#if cli.debug:
print 'Signal handler called with signal', signum
print 'Frame is ', frame
pass
def quithandler(signum, frame):
cli.executeCommand(cli.prefs.getCommand(cli.profile,"quit"))
if(__name__ == "__main__"):
global cli
# if LOCALE:
# locale.setlocale(locale.LC_ALL, "de") #("US","ISO-8859-1"))
signal.signal(signal.SIGINT,quithandler)
getTerminalWidth()
# printStartup()
# remove the old debug log file
try:
os.remove(os.environ['HOME']+"/.imcom/debuglog.log")
except:
pass
args = "hdp:"
longargs = ["debug","profile=","help"]
try:
opts, args = getopt.getopt(sys.argv[1:], args, longargs)
except getopt.GetoptError:
printUsage()
sys.exit(2)
debugMode=0
profileName=""
for o, a in opts:
if o in ("-h", "--help"):
printUsage()
sys.exit(0)
if o in ("-d", "--debug"):
debugMode=1
if o in ("-p", "--profile"):
profileName=a
printStartup()
cli = CLI(debugMode, profileName)
cli.applyPrefs(debugMode)
result = cli.applyProfileWrapper()
cli.applyPrefs(debugMode)
if result[0] != 0:
sys.exit(-1)
#cli.start()
cli.run()
def yesnostr( val ):
if( val ):
return "yes"
else:
return "no"
def truefalsestr( val ):
if( val ):
return "true"
else:
return "false"
|