├── configdefaults.json └── main.py /configdefaults.json: -------------------------------------------------------------------------------- 1 | { 2 | "username": "username", 3 | "login2": "gmail.com", 4 | "server": "talk.google.com", 5 | "passwd": "password", 6 | "port": 5223 7 | } -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from PyQt4 import QtCore, QtGui, QtWebKit 3 | from PyQt4.QtCore import QVariant, QTimer, QThread 4 | import xmpp 5 | from PyQt4.QtWebKit import QWebSettings 6 | import time 7 | import json 8 | 9 | 10 | """Html snippet.""" 11 | html = """ 12 | 13 |
14 |
15 |
16 | 18 | 19 |
20 |
21 | 62 | 63 | 64 |
65 | 66 | """ 67 | 68 | class QtJsBridge(QtCore.QObject): 69 | """connection between QT and Webkit.""" 70 | 71 | @QtCore.pyqtSlot(str) 72 | def showMessage(self, msg): 73 | """Open a message box and display the specified message.""" 74 | QtGui.QMessageBox.information(None, "Info", msg) 75 | 76 | def _pyVersion(self): 77 | """Return the Python version.""" 78 | return sys.version 79 | @QtCore.pyqtSlot() 80 | def uplinkButton(self):#unused 81 | self.mainframe.evaluateJavaScript("") 82 | pass 83 | 84 | def gotmsg(self,sess,mess): 85 | print 'MESSAGE'*3 86 | print "MESS", mess 87 | nick=str(mess.getFrom()) 88 | print "NICK", nick 89 | text=str(mess.getBody()) 90 | print text.__class__.__name__ 91 | if text == None or text == "None": 92 | print "blank message, ignoring" 93 | return 94 | print "TEXT", text 95 | try: 96 | self.mainframe.evaluateJavaScript("addchat('"+nick+"','"+text+"');") 97 | except: 98 | print "could not issue message" 99 | @QtCore.pyqtSlot(str, str) 100 | def sendMessage(self, to, message): 101 | to=str(to) 102 | message=str(message) 103 | print "sending: ", message, " to ", to 104 | message = xmpp.Message(to, message) 105 | message.setAttr('type', 'chat') 106 | self.send(message) 107 | 108 | @QtCore.pyqtSlot(result=QVariant) 109 | def getRoster(self): 110 | print "getting roster" 111 | return QVariant(self.rkeys) 112 | 113 | @QtCore.pyqtSlot(str) 114 | def printit(self, out): 115 | print out 116 | 117 | """Python interpreter version property.""" 118 | pyVersion = QtCore.pyqtProperty(str, fget=_pyVersion) 119 | 120 | import select 121 | 122 | 123 | def main(): 124 | app = QtGui.QApplication(sys.argv) 125 | QWebSettings.globalSettings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True); 126 | 127 | myObj = QtJsBridge() 128 | 129 | webView = QtWebKit.QWebView() 130 | # Make myObj exposed as JavaScript object named 'pyObj' 131 | webView.page().mainFrame().addToJavaScriptWindowObject("pyObj", myObj) 132 | myObj.mainframe=webView.page().mainFrame() 133 | webView.setHtml(html) 134 | 135 | window = QtGui.QMainWindow() 136 | window.setCentralWidget(webView) 137 | window.show() 138 | 139 | cf = json.load(open('config.json')) 140 | client = xmpp.Client(cf['login2']) 141 | client.connect(server=(cf['server'],int(cf['port']))) 142 | client.auth(cf['username'], cf['passwd'], 'botty') 143 | 144 | 145 | client.RegisterHandler('message', myObj.gotmsg) 146 | #client.RegisterHandler('chat', self.gotmsg) 147 | client.sendInitPresence() 148 | 149 | #need to call this later on too. 150 | roster = client.getRoster() 151 | myObj.rkeys = [str(r) for r in roster.keys()] 152 | 153 | #give js obj access to send. could wrap in another method if paranoid :P 154 | myObj.send = client.send 155 | myObj.mainframe.evaluateJavaScript("getRoster();") 156 | 157 | #this get messages section could be improved, or replaced! 158 | global cancheckmsgs 159 | cancheckmsgs = True 160 | def checkmsgs(): 161 | global cancheckmsgs 162 | if not cancheckmsgs: return 163 | cancheckmsgs = False 164 | socketlist = {client.Connection._sock:'xmpp',sys.stdin:'stdio'} 165 | 166 | (i , o, e) = select.select(socketlist.keys(),[],[],.01) 167 | for each in i: 168 | print each 169 | if socketlist[each] == 'xmpp': 170 | client.Process(.01) 171 | cancheckmsgs = True 172 | 173 | timer = QTimer() 174 | timer.timeout.connect(checkmsgs) 175 | timer.start(100) 176 | 177 | sys.exit(app.exec_()) 178 | 179 | if __name__ == "__main__": 180 | main() 181 | --------------------------------------------------------------------------------