otr: removed TLV parsing code (i still has to fix SMP queries in callback)
[dyskinesia.git] / src / chatjsapi.cpp
blob37f3b59a0d067115671b173df6241e00db7915b6
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://sam.zoy.org/wtfpl/COPYING for more details.
10 * we are the Borg.
12 #include <QDebug>
14 #include <ctype.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
19 #include <QTextCodec>
20 #include <QClipboard>
22 #include "chatjsapi.h"
24 #include "chatform.h"
25 #include "k8strutils.h"
26 #include "psyccontact.h"
29 extern bool debugOutput;
32 ///////////////////////////////////////////////////////////////////////////////
33 static void printQStr (const QString &str) {
34 //QTextCodec *codec = QTextCodec::codecForName("koi8-r");
35 QTextCodec *codec = QTextCodec::codecForLocale();
36 QByteArray ba;
37 if (codec) ba = codec->fromUnicode(str); else ba = str.toLatin1();
38 const char *s = ba.constData();
39 int sz = ba.size();
40 while (sz > 0) {
41 int len = strlen(s);
42 if (debugOutput) fprintf(stdout, "%s", s);
43 sz -= len;
44 if (sz > 0) {
45 if (debugOutput) fprintf(stdout, "\n ");
46 sz--;
47 s += len+1;
50 if (debugOutput) fprintf(stdout, "\n");
54 ///////////////////////////////////////////////////////////////////////////////
55 ChatJSAPI::ChatJSAPI (QWebFrame *frame, ChatForm *cform) : QObject(cform), mFrame(frame), mForm(cform) {
59 ChatJSAPI::~ChatJSAPI () {
63 QVariant ChatJSAPI::test (const QVariant &v) {
64 qDebug() << QVariant::typeToName(v.type());
65 qDebug() << v;
66 return QVariant(10);
70 void ChatJSAPI::print (const QString &str) {
71 printQStr(str);
72 if (debugOutput) fflush(stderr);
76 void ChatJSAPI::printLn (const QString &str) {
77 printQStr(str);
78 if (debugOutput) fprintf(stderr, "\n");
79 if (debugOutput) fflush(stderr);
83 void ChatJSAPI::refresh () {
84 mFrame->evaluateJavaScript("window.scrollTo(window.pageXOffset, window.pageYOffset);");
88 void ChatJSAPI::scrollToBottom () {
89 mFrame->evaluateJavaScript(
90 "do{var ___tmp___=window.pageYOffset;window.scrollTo(0,1000000);}while(window.pageYOffset!=___tmp___);"
95 void ChatJSAPI::startChat (const QString &uni) {
96 mForm->startChatWith(uni);
100 void ChatJSAPI::requestAuth (const QString &uni) {
101 PsycContact *cc = mForm->findContact(uni);
102 if (!cc) return;
103 mForm->mProto->requestAuth(uni, "");
107 void ChatJSAPI::cancelAuth (const QString &uni) {
108 PsycContact *cc = mForm->findContact(uni);
109 if (!cc) return;
110 cc->setAuthorized(false);
111 mForm->mProto->removeAuth(uni, "");
112 //mForm->knockAutosave();
113 mForm->saveOneContact(cc);
114 mForm->redrawContact(cc);
118 void ChatJSAPI::sendAuth (const QString &uni) {
119 PsycContact *cc = mForm->findContact(uni);
120 if (!cc) return;
121 cc->setAuthorized(true);
122 mForm->mProto->sendAuth(uni, "");
123 //mForm->knockAutosave();
124 mForm->saveOneContact(cc);
125 mForm->redrawContact(cc);
129 void ChatJSAPI::deleteContact (const QString &uni) {
130 PsycContact *cc = mForm->findContact(uni);
131 if (!cc) return;
132 mForm->deleteContact(cc);
136 void ChatJSAPI::enterPlace (const QString &uni) {
137 if (!mForm->mProto->isLoggedIn()) return;
138 mForm->mProto->enterPlace(uni);
142 void ChatJSAPI::leavePlace (const QString &uni) {
143 if (!mForm->mProto->isLoggedIn()) return;
144 PsycEntity place(uni);
145 mForm->mProto->leavePlace(place);
146 mForm->onPlaceLeaved(place);
150 bool ChatJSAPI::hasOption (const QString &name) {
151 return hasOption(name, QString());
155 bool ChatJSAPI::hasOption (const QString &name, const QString &uni) {
156 return mForm->hasKeyOpt(name, uni);
160 bool ChatJSAPI::removeOption (const QString &name) {
161 return removeOption(name, QString());
165 bool ChatJSAPI::removeOption (const QString &name, const QString &uni) {
166 return mForm->removeOpt(name, uni);
170 QVariant ChatJSAPI::getOption (const QString &name) {
171 return getOption(name, QString());
175 QVariant ChatJSAPI::getOption (const QString &name, const QString &uni) {
176 switch (mForm->typeOpt(name, uni)) {
177 case K8SDB::Boolean: return QVariant(mForm->toBoolOpt(name, false, uni));
178 case K8SDB::Integer:
179 //qDebug() << "!!" << mForm->toIntOpt(name, 0, uni);
180 return QVariant(mForm->toIntOpt(name, 0, uni));
181 //case K8SDB::KeySequence:
182 case K8SDB::String:
183 return QVariant(mForm->asStringOpt(name, uni));
184 default: ;
186 return QVariant();
190 bool ChatJSAPI::setOption (const QString &name, const QVariant &v) {
191 return setOption(name, QString(), v);
195 bool ChatJSAPI::setOption (const QString &name, const QString &uni, const QVariant &v) {
196 switch (v.type()) {
197 case QVariant::Bool: return mForm->setOpt(name, v.toBool(), uni);
198 case QVariant::Char:
199 case QVariant::String:
200 return mForm->setOpt(name, v.toString(), uni);
201 case QVariant::Int: return mForm->setOpt(name, v.toInt(), uni);
202 default: ;
204 return false;
208 QVariant ChatJSAPI::contactInfo (const QString &uni) {
209 PsycContact *cc = mForm->findContact(uni);
210 if (!cc) return QVariant(); // null
211 QVariantMap ci;
212 ci["place"] = QVariant(cc->isPlace());
213 ci["verbatim"] = QVariant(cc->verbatim());
214 ci["hidden"] = QVariant(cc->isHidden());
215 ci["uni"] = QVariant(cc->uni());
216 ci["proto"] = QVariant(cc->entity()->proto());
217 ci["nick"] = QVariant(cc->nick());
218 ci["host"] = QVariant(cc->entity()->host());
219 ci["port"] = QVariant(cc->entity()->port());
220 ci["channel"] = QVariant(cc->entity()->channel());
221 ci["status"] = QVariant((int)cc->status());
222 ci["statusName"] = QVariant(PsycProto::statusName(cc->status()));
223 ci["statusText"] = QVariant(cc->statusText());
224 ci["mood"] = QVariant((int)cc->mood());
225 ci["moodName"] = QVariant(PsycProto::moodName(cc->mood()));
226 ci["authorized"] = QVariant(cc->authorized());
227 ci["group"] = QVariant(cc->groupName());
228 ci["unread"] = QVariant(cc->messageCount());
229 ci["lastseen"] = QVariant(cc->lastSeenUnix());
230 ci["chatting"] = QVariant(mForm->mChatUser && mForm->mChatUser==cc);
231 ci["showAlways"] = mForm->toBoolOpt("/contactlist/showalways", false, cc->uni());
232 ci["skipUnreadCycle"] = mForm->toBoolOpt("/contactlist/skipunreadcycle", false, cc->uni());
233 ci["isOTR"] = QVariant(cc->isOTRActive());
234 ci["isOTRVerified"] = QVariant(cc->isOTRVerified());
235 return QVariant(ci);
239 void ChatJSAPI::setContactVerbatim (const QString &uni, const QString &v) {
240 PsycContact *cc = mForm->findContact(uni);
241 if (!cc) return;
242 if (cc->verbatim() != v) {
243 cc->setVerbatim(v);
244 mForm->saveOneContact(cc);
245 mForm->redrawContact(cc);
250 void ChatJSAPI::setContactHidden (const QString &uni, bool v) {
251 PsycContact *cc = mForm->findContact(uni);
252 if (!cc) return;
253 if (cc->isHidden() != v) {
254 cc->setHidden(v);
255 mForm->saveOneContact(cc);
256 mForm->redrawContact(cc);
261 void ChatJSAPI::setStatus (int newStatus) {
262 if (newStatus < 1 || newStatus > 9) return;
263 mForm->setStatus((PsycProto::Status)newStatus);
267 int ChatJSAPI::status () {
268 return (int)mForm->mProto->status();
272 void ChatJSAPI::markAsRead (const QString &aUNI) {
273 mForm->markAsRead(aUNI);
277 void ChatJSAPI::setWasBottom (bool bflag) {
278 mForm->mWasAtBottom = bflag;
282 void ChatJSAPI::selectedText (const QString &s) {
283 mForm->mSelText = s;
287 QString ChatJSAPI::editorText () {
288 return mForm->edChat->toPlainText();
292 void ChatJSAPI::setEditorText (const QString &s) {
293 mForm->edChat->setPlainText(s);
294 mForm->mStartingNickCompletion.clear();
295 mForm->mLatestNickCompletion.clear();
296 mForm->edChat->ensureCursorVisible();
300 void ChatJSAPI::insertEditorText (const QString &s) {
301 mForm->edChat->insertPlainText(s);
302 mForm->edChat->ensureCursorVisible();
306 int ChatJSAPI::editorCurPos () {
307 QTextCursor cur(mForm->edChat->textCursor());
308 return cur.position();
312 void ChatJSAPI::setEditorCurPos (int pos) {
313 QTextCursor cur(mForm->edChat->textCursor());
314 cur.setPosition(pos);
315 mForm->edChat->setTextCursor(cur);
319 void ChatJSAPI::setClipboardText (const QString &s) {
320 QClipboard *cb = QApplication::clipboard();
321 cb->setText(s);
325 void ChatJSAPI::otrStart (const QString &aUNI) {
326 mForm->otrConnect(aUNI);
330 void ChatJSAPI::otrEnd (const QString &aUNI) {
331 mForm->otrDisconnect(aUNI);
335 void ChatJSAPI::otrSetTrust (const QString &aUNI, bool tflag) {
336 mForm->otrSetTrust(aUNI, tflag);
340 void ChatJSAPI::otrForget (const QString &aUNI) {
341 mForm->otrForget(aUNI);
345 void ChatJSAPI::otrInitiateSMP (const QString &aUNI, const QString &secret, const QString &question) {
346 mForm->otrInitiateSMP(aUNI, secret, question);
350 void ChatJSAPI::doPSYC (const QString &cmdNargs) {
351 doPSYC(cmdNargs, "");
355 //FIXME! add 'focus' here!
356 void ChatJSAPI::doPSYC (const QString &cmdNargs, const QString &destUni) {
357 if (mForm->mProto->isLoggedIn()) mForm->mProto->doCommand(cmdNargs, destUni);
361 // this will return an UNI or ""
362 QString ChatJSAPI::chattingWith () {
363 if (!mForm->mChatUser) return QString();
364 return mForm->mChatUser->uni();
368 // this can be done in JS code, but who really cares?
369 bool ChatJSAPI::isInPlace () {
370 if (!mForm->mChatUser) return false;
371 return mForm->mChatUser->isPlace();
375 void ChatJSAPI::invite (const QString &uni, const QString &place) {
376 if (!mForm->mProto->isLoggedIn()) return;
377 mForm->mProto->invite(place, uni);
381 bool ChatJSAPI::addMessage (const QString &placeUni, const QString &userUni, uint time, const QString &text,
382 const QString &action)
384 PsycEntity place(placeUni);
385 PsycEntity user(userUni);
386 if (!user.isValid()) return false;
387 if (place.isValid() && !place.isPlace()) return false;
388 QDateTime dtm;
389 if (time == 0) dtm = QDateTime::currentDateTime(); else dtm.setTime_t(time);
390 mForm->onMessage(place, user, dtm, text, action, "\x1"+text);
391 return true;
395 bool ChatJSAPI::addMessageEx (const QString &placeUni, const QString &userUni, uint time, const QString &text,
396 const QString &action, const QString &popupMsg)
398 PsycEntity place(placeUni);
399 PsycEntity user(userUni);
400 if (!user.isValid()) return false;
401 if (place.isValid() && !place.isPlace()) return false;
402 QDateTime dtm;
403 if (time == 0) dtm = QDateTime::currentDateTime(); else dtm.setTime_t(time);
404 mForm->onMessage(place, user, dtm, text, action, popupMsg);
405 return true;