Skip to content

Commit 1061b35

Browse files
committed
initial commit
0 parents  commit 1061b35

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

Diff for: lib/libtoxav.so

236 KB
Binary file not shown.

Diff for: lib/libtoxcore.so

1.02 MB
Binary file not shown.

Diff for: lib/libtoxencryptsave.so

24.9 KB
Binary file not shown.

Diff for: package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "matrix-puppet-tox",
3+
"version": "0.0.0",
4+
"main": "./dist/index.js",
5+
"types": "./dist/index.d.ts",
6+
"scripts": {
7+
"prepare": "tsc",
8+
"start": "DEBUG=matrix-puppet:* node index.js",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"bluebird": "^3.4.7",
16+
"matrix-puppet-bridge": "^2.0.0",
17+
"node-ffi": "https://github.com/node-ffi/node-ffi",
18+
"toxcore": "https://github.com/TokTok/js-toxcore-c"
19+
},
20+
"devDependencies": {
21+
"typescript": "^2.3.4"
22+
}
23+
}

Diff for: src/client.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as toxcore from 'toxcore';
2+
import * as Promise from 'bluebird';
3+
const debug = require('debug')('matrix-puppet:tox:client');
4+
export const EventEmitter = require('events').EventEmitter;
5+
6+
Promise.promisifyAll(toxcore);
7+
8+
const nodes = [
9+
{ maintainer: 'saneki',
10+
address: '96.31.85.154',
11+
port: 33445,
12+
key: '674153CF49616CD1C4ADF44B004686FC1F6C9DCDD048EF89B117B3F02AA0B778' },
13+
{ maintainer: 'Impyy',
14+
address: '178.62.250.138',
15+
port: 33445,
16+
key: '788236D34978D1D5BD822F0A5BEBD2C53C64CC31CD3149350EE27D4D9A2F9B6B' },
17+
{ maintainer: 'sonOfRa',
18+
address: '144.76.60.215',
19+
port: 33445,
20+
key: '04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F' }
21+
];
22+
23+
export class ToxClient extends (EventEmitter as { new(): any; }) {
24+
constructor() {
25+
super();
26+
this.tox = new toxcore.Tox({
27+
data: '/home/sorunome/.config/tox/tox_save_test.tox',
28+
path: '/home/sorunome/repos/matrix-puppet-tox/lib/libtoxcore.so',
29+
crypto: '/home/sorunome/repos/matrix-puppet-tox/lib/libtoxencryptsave.so',
30+
});
31+
}
32+
connect() {
33+
return Promise.map(nodes, (node) => {
34+
return this.tox.bootstrapAsync(node.address, node.port, node.key);
35+
}).then(this.tox.setNameAsync('Soruteeeest'))
36+
.then(this.tox.setStatusMessageAsync('testing tox'))
37+
.then(() => {
38+
// init callbacks
39+
40+
// Listen for friend requests
41+
this.tox.on('friendRequest', (e) => {
42+
console.log('Friend request from: ' + e.publicKeyHex());
43+
});
44+
45+
// Print received friend messages to console
46+
this.tox.on('friendMessage', (e) => {
47+
var friendName = this.getNameById(e.friend());
48+
this.emit('message', {
49+
name: friendName,
50+
id: this.tox.getFriendPublicKeyHexSync(e.friend()),
51+
message: e.message(),
52+
action: e._messageType == 1
53+
});
54+
});
55+
})
56+
.then(() => {
57+
this.tox.start();
58+
});
59+
}
60+
sendMessage(text, channel, action) {
61+
return this.tox.sendFriendMessageAsync(channel, text, action);
62+
}
63+
getSelfUserId() {
64+
return this.tox.getAddressHexAsync();
65+
}
66+
getNameById(id) {
67+
return this.tox.getFriendNameSync(id).replace(/\0/g, '');
68+
}
69+
}

Diff for: src/index.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
ThirdPartyAdapter,
3+
4+
download, entities,
5+
6+
ThirdPartyPayload, ThirdPartyMessagePayload, ThirdPartyImageMessagePayload,
7+
UserData, RoomData
8+
} from 'matrix-puppet-bridge';
9+
10+
const debug = require('debug')('matrix-puppet:tox');
11+
import { ToxClient } from './client'
12+
13+
export class Adapter extends ThirdPartyAdapter {
14+
public serviceName = 'Tox';
15+
private client: ToxClient;
16+
startClient(): Promise<void> {
17+
this.client = new ToxClient();
18+
return this.client.connect().then(() => {
19+
debug('waiting a little bit for initial self-messages to fire before listening for messages');
20+
setTimeout(()=>this.registerMessageListener(), 5000);
21+
});
22+
}
23+
registerMessageListener() {
24+
this.client.on('message', (data) => {
25+
this.puppetBridge.sendMessage({
26+
roomId: data.id,
27+
senderId: data.id,
28+
senderName: data.name,
29+
text: data.message,
30+
});
31+
})
32+
}
33+
getUserData(id): Promise<UserData>{
34+
return Promise.resolve(() => {
35+
let payload = <UserData>{
36+
name: this.client.getNameById(id),
37+
};
38+
return payload;
39+
});
40+
}
41+
getRoomData(id: string): Promise<RoomData> {
42+
return new Promise<RoomData>((resolve, reject) => {
43+
const name = this.client.getNameById(id);
44+
if (!name) {
45+
return reject();
46+
}
47+
let payload = <RoomData>{
48+
name: name,
49+
topic: 'Tox Chat ('+name+')',
50+
isDirect: true,
51+
};
52+
return resolve(payload);
53+
});
54+
}
55+
sendMessage(id, text) {
56+
return this.client.sendMessage(text, id, false);
57+
}
58+
sendImageMessage(id, data) {
59+
return this.client.sendMessage('['+data.text+'] '+data.url, id, false);
60+
}
61+
};

Diff for: tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"sourceMap": true,
4+
"declaration": true,
5+
"module": "commonjs",
6+
"target": "es2015",
7+
"outDir": "./dist"
8+
}
9+
}

0 commit comments

Comments
 (0)