Skip to content

Commit

Permalink
added a loging and cacheing for the gateway and token
Browse files Browse the repository at this point in the history
  • Loading branch information
Pumuckl007 committed Aug 29, 2016
1 parent ec2c588 commit 904cce0
Show file tree
Hide file tree
Showing 468 changed files with 2,077 additions and 3,952 deletions.
92 changes: 85 additions & 7 deletions Cacophony/DiscordInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ var DMS_CHANGED = "DMS_CHANGED";
var VOICE_CONNECTION_UPDATE = "VOICE_CONNECTION_UPDATE";
//Fired when the ping is updated
var PING_UPDATED = "PING_UPDATED";
//Fired when a bad login was passed
var BAD_LOGIN = "BAD_LOGIN";
//Fired when the login completes
var LOGIN_SUCCESSFUL = "LOGIN_SUCCESSFUL";

var token = "";
var token = false;
var dataBase;
var websocket;
var websocketParrent;
var heartbeatTimer;
Expand All @@ -49,7 +54,6 @@ var currentVoiceConnection;
var voiceUDPConnection;

var init = function(parrent, voiceUDP){
getGateway(initContinue1);
websocketParrent = parrent;
addEventListener(NEW_GUILD, newGuildQuerry);
var typingTimer = Qt.createQmlObject("import QtQuick 2.0; Timer {}", websocketParrent);
Expand All @@ -68,12 +72,80 @@ var init = function(parrent, voiceUDP){
})
}

var setDB = function(db){
dataBase = db;
db.transaction(function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS cache(key TEXT UNIQUE, value TEXT)');
var table = tx.executeSql("SELECT * FROM cache WHERE key = 'token'");
if (table.rows.length > 0){
token = table.rows.item(0).value;
var connectingTimer = Qt.createQmlObject("import QtQuick 2.0; Timer {}", websocketParrent);
connectingTimer.triggered.connect(function(){
fireEvent(LOGIN_SUCCESSFUL);
getGateway(initContinue1);
});
connectingTimer.interval = 10;
connectingTimer.repeat = false;
connectingTimer.start();
}
});
}

var login = function(email, password){
if(password && password.trim() !== "" && email && email.trim() !== ""){
var msg = {
email: email,
password: password
};
aPIRequest("POST", BASE_URL + "/v6/auth/login", finishLogin, msg, true);
} else if(email && email.trim() !== "") {
token = email;
getGateway(initContinue1);
fireEvent(LOGIN_SUCCESSFUL);
dataBase.transaction(function(tx){
tx.executeSql("INSERT INTO cache VALUES(?, ?)", ["token", token]);
});
} else {
return -1;
}
return 0;
}

var finishLogin = function(json, status){
if(status){
fireEvent(BAD_LOGIN, status)
return;
}
fireEvent(LOGIN_SUCCESSFUL);
var message = JSON.parse(json);
token = message.token;
dataBase.transaction(function(tx){
tx.executeSql("INSERT INTO cache VALUES(?, ?)", ["token", token]);
});
getGateway(initContinue1);
}

var initContinue1 = function(url){
websocket = createWebsocket(JSON.parse(url).url);
}

var getGateway = function(callback){
aPIRequest("GET", BASE_URL + "/gateway", callback);
var getGateway = function(){
dataBase.transaction(function(tx){
var table = tx.executeSql("SELECT * FROM cache WHERE key = 'gateway'");
if (table.rows.length > 0){
var url = table.rows.item(0).value;
initContinue1(JSON.stringify({url:url}));
} else {
aPIRequest("GET", BASE_URL + "/gateway", cacheGateway);
}
});
}

var cacheGateway= function(url){
dataBase.transaction(function(tx){
tx.executeSql("INSERT INTO cache VALUES(?, ?)", ["gateway", JSON.parse(url).url]);
initContinue1(url);
});
}

var createWebsocket = function(url, stsChanged, msgRecived){
Expand Down Expand Up @@ -235,16 +307,22 @@ var handleEvent = function(object){
}
}

var aPIRequest = function(type, url, callback, fourth){
var aPIRequest = function(type, url, callback, fourth, callOnError){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
if(callback && typeof callback === 'function')
callback(xmlhttp.responseText);
} else if(callOnError && xmlhttp.readyState === 4){
callback("", xmlhttp.status);

} else if(xmlhttp.readyState ===4){
console.log(xmlhttp.statusText + " " + type + " " + url + " " + fourth + " " + JSON.stringify(fourth));
}
}
xmlhttp.open(type, url, true);
xmlhttp.setRequestHeader('user-agent', "DiscordBot (https://github.com/Pumuckl007/Cacophony, 0.1)" );
if(!callOnError)
xmlhttp.setRequestHeader('user-agent', "DiscordBot (https://github.com/Pumuckl007/Cacophony, 0.1)" );
xmlhttp.setRequestHeader('content-type', 'application/json');
if(token)
xmlhttp.setRequestHeader('authorization', (user ? (user.bot ? "Bot " : "") : "") + token);
Expand Down
79 changes: 63 additions & 16 deletions Cacophony/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.3
import Ubuntu.Components.ListItems 1.3 as ListItem
import QtQuick.LocalStorage 2.0
import "DiscordInterface.js" as Discord

import Cacophony 1.0 as Cacophony
Expand Down Expand Up @@ -53,6 +54,65 @@ MainView {
}
}

Component {
id: dialog
Dialog {
id: dialogue
title: "Login"
text: ""
TextField{
id:username
focus: true
placeholderText: i18n.tr("Username/Token");
}
TextField{
id:password
echoMode: TextInput.Password
placeholderText: i18n.tr("Password");
focus: true
}
Button {
id:login
text: "Login"
color:UbuntuColors.green
onClicked: {
if(discord().login(username.text, password.text) >= 0){
username.visible = false;
password.text = "";
password.visible = false;
login.visible = false;
activityIndicator.visible = true;
activityIndicator.running = true;
} else {
dialogue.text = i18n.tr("Invalid username, password, or token.");
password.text = "";
}
}
}
ActivityIndicator {
id: activityIndicator
running: false
visible:false
}
Component.onCompleted: {
var popUtil = PopupUtils;
Discord.addEventListener(Discord.BAD_LOGIN, function(){
dialogue.text = i18n.tr("Invalid username, password, or token.");
username.visible = true;
password.visible = true;
login.visible = true;
activityIndicator.visible = false;
activityIndicator.running = false;
});
Discord.addEventListener(Discord.LOGIN_SUCCESSFUL, function(){
activityIndicator.visible = false;
activityIndicator.running = false;
popUtil.close(dialogue);
});
}
}
}

function discord(){
return Discord
}
Expand All @@ -61,24 +121,11 @@ MainView {
PopupUtils.open(dialog);
}

Timer{
id: timerDebug
interval: 4000;
repeat: false;
onTriggered: {
voiceConnection.writePackets();
}
running: false;
}

Component.onCompleted: {
//myType.encodeDecodeTest();
//voiceConnection.ssrc = 10;
//voiceConnection.port= 39338;
//voiceConnection.url = "example.com";
Discord.init(mainView, voiceConnection);
//voiceConnection.decodeTest();
//Discord.addEventListener(Discord.CHANGE_CHANNEL, done);
var db = LocalStorage.openDatabaseSync("cacophony", "0.1", "Cacheing/Saving", 100000);
Discord.setDB(db)
done();
}
}

Binary file modified backend/opus/.libs/libopus.a
Binary file not shown.
Binary file removed backend/opus/.libs/libopus.so.0.5.3
Binary file not shown.
Binary file removed backend/opus/.libs/opus_demo
Binary file not shown.
Binary file removed backend/opus/.libs/repacketizer_demo
Binary file not shown.
6 changes: 3 additions & 3 deletions backend/opus/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,8 @@ AUTOCONF = ${SHELL} /home/max/src/ubuntuApps/Cacophony/backend/opus/missing auto
AUTOHEADER = ${SHELL} /home/max/src/ubuntuApps/Cacophony/backend/opus/missing autoheader
AUTOMAKE = ${SHELL} /home/max/src/ubuntuApps/Cacophony/backend/opus/missing automake-1.15
AWK = gawk
CC = arm-linux-gnueabihf-gcc
CCAS = arm-linux-gnueabihf-gcc
CC = arm-linux-gnueabihf-gcc -std=gnu99
CCAS = arm-linux-gnueabihf-gcc -std=gnu99
CCASDEPMODE = depmode=gcc3
CCASFLAGS = -fPIC
CCDEPMODE = depmode=gcc3
Expand Down Expand Up @@ -931,7 +931,7 @@ INSTALL_DATA = ${INSTALL} -m 644
INSTALL_PROGRAM = ${INSTALL}
INSTALL_SCRIPT = ${INSTALL}
INSTALL_STRIP_PROGRAM = $(install_sh) -c -s
LD = /usr/arm-linux-gnueabihf/bin/ld
LD = /var/lib/schroot/chroots/click-ubuntu-sdk-15.04-armhf/usr/arm-linux-gnueabihf/bin/ld
LDFLAGS =
LIBM = -lm
LIBOBJS =
Expand Down
27 changes: 11 additions & 16 deletions backend/opus/celt/.deps/bands.Plo
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ celt/bands.lo: celt/bands.c \
/usr/arm-linux-gnueabihf/include/bits/wordsize.h \
/usr/arm-linux-gnueabihf/include/gnu/stubs.h \
/usr/arm-linux-gnueabihf/include/gnu/stubs-hard.h \
/usr/arm-linux-gnueabihf/include/bits/math-vector.h \
/usr/arm-linux-gnueabihf/include/bits/libm-simd-decl-stubs.h \
/usr/arm-linux-gnueabihf/include/bits/huge_val.h \
/usr/arm-linux-gnueabihf/include/bits/huge_valf.h \
/usr/arm-linux-gnueabihf/include/bits/huge_vall.h \
Expand All @@ -16,14 +14,15 @@ celt/bands.lo: celt/bands.c \
/usr/arm-linux-gnueabihf/include/bits/mathdef.h \
/usr/arm-linux-gnueabihf/include/bits/mathcalls.h celt/bands.h \
celt/arch.h opus_types.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include/stdint.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include/stdint.h \
/usr/arm-linux-gnueabihf/include/stdint.h \
/usr/arm-linux-gnueabihf/include/bits/wchar.h opus_defines.h \
opus_types.h celt/modes.h celt/celt.h opus_custom.h opus_defines.h \
celt/entenc.h /usr/lib/gcc-cross/arm-linux-gnueabihf/5/include/stddef.h \
celt/entenc.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include/stddef.h \
celt/entcode.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include-fixed/limits.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include-fixed/syslimits.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include-fixed/limits.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include-fixed/syslimits.h \
/usr/arm-linux-gnueabihf/include/limits.h \
/usr/arm-linux-gnueabihf/include/bits/posix1_lim.h \
/usr/arm-linux-gnueabihf/include/bits/local_lim.h \
Expand Down Expand Up @@ -56,7 +55,7 @@ celt/bands.lo: celt/bands.c \
/usr/arm-linux-gnueabihf/include/libio.h \
/usr/arm-linux-gnueabihf/include/_G_config.h \
/usr/arm-linux-gnueabihf/include/wchar.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include/stdarg.h \
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include/stdarg.h \
/usr/arm-linux-gnueabihf/include/bits/stdio_lim.h \
/usr/arm-linux-gnueabihf/include/bits/sys_errlist.h celt/mathops.h \
celt/quant_bands.h celt/pitch.h celt/arm/pitch_arm.h celt/arm/armcpu.h
Expand All @@ -77,10 +76,6 @@ config.h:

/usr/arm-linux-gnueabihf/include/gnu/stubs-hard.h:

/usr/arm-linux-gnueabihf/include/bits/math-vector.h:

/usr/arm-linux-gnueabihf/include/bits/libm-simd-decl-stubs.h:

/usr/arm-linux-gnueabihf/include/bits/huge_val.h:

/usr/arm-linux-gnueabihf/include/bits/huge_valf.h:
Expand All @@ -101,7 +96,7 @@ celt/arch.h:

opus_types.h:

/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include/stdint.h:
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include/stdint.h:

/usr/arm-linux-gnueabihf/include/stdint.h:

Expand All @@ -121,13 +116,13 @@ opus_defines.h:

celt/entenc.h:

/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include/stddef.h:
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include/stddef.h:

celt/entcode.h:

/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include-fixed/limits.h:
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include-fixed/limits.h:

/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include-fixed/syslimits.h:
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include-fixed/syslimits.h:

/usr/arm-linux-gnueabihf/include/limits.h:

Expand Down Expand Up @@ -211,7 +206,7 @@ celt/os_support.h:

/usr/arm-linux-gnueabihf/include/wchar.h:

/usr/lib/gcc-cross/arm-linux-gnueabihf/5/include/stdarg.h:
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.9/include/stdarg.h:

/usr/arm-linux-gnueabihf/include/bits/stdio_lim.h:

Expand Down
Loading

0 comments on commit 904cce0

Please sign in to comment.