Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xat-server 0.1.0 candidate #48

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6e174bc
bug: fails if user tries send 'p' when sender and receiver are in dif…
huumanoid Oct 5, 2016
d55e088
Merge remote-tracking branch 'iiegor/master'
huumanoid Oct 5, 2016
45ad817
fix: application doesn't shutdown on SIGTERM
huumanoid Oct 5, 2016
9e18855
Merge remote-tracking branch 'iiegor/master'
huumanoid Oct 9, 2016
9570688
npm test script update
huumanoid Oct 9, 2016
cef5567
private-message test slight improvement
huumanoid Oct 9, 2016
197c711
small refact
huumanoid Oct 7, 2016
7a1a6fb
fix #26 #25 bugs. fix: messages should be sent only to users in same …
huumanoid Oct 7, 2016
dce7dbc
fix: users have 0 (Number) pool by default, but pool from <w> package…
huumanoid Oct 7, 2016
87942f1
fix: should broadcast <l> when changing pool. refactored
huumanoid Oct 7, 2016
afd9ae3
fix: nf should pass even in same pool
huumanoid Oct 7, 2016
c49a331
onsuper should be called on pool change
huumanoid Oct 7, 2016
008425f
<jt p='pool'.. /> is not ignored now
huumanoid Oct 7, 2016
42e8de9
pools test: user pools specified, typo fixed
huumanoid Oct 9, 2016
d1ac0f9
more tests for pools
huumanoid Oct 9, 2016
d736a5c
more tests for pools
huumanoid Oct 9, 2016
d6b7e70
test/message: evil case added
huumanoid Oct 9, 2016
6c77363
compile scripts added
huumanoid Oct 10, 2016
6bae62a
chmod +x
huumanoid Oct 10, 2016
194a302
simple implementation
huumanoid Oct 6, 2016
4aaa4fa
improved implementation
huumanoid Oct 6, 2016
7b01ba4
lint
huumanoid Oct 6, 2016
160e839
damned swp files
huumanoid Oct 6, 2016
39f6c4e
fix: /at expanding
huumanoid Oct 10, 2016
8986f79
lint
huumanoid Oct 10, 2016
9c9bcce
npm test improved: runs compiled server, takes list of tests (all by …
huumanoid Oct 10, 2016
1016a09
mainowner rank test
huumanoid Oct 10, 2016
a1b7900
main owner recognized
huumanoid Oct 10, 2016
66b8eda
illegal owner test added
huumanoid Oct 10, 2016
33238bf
more reliable message test
huumanoid Oct 10, 2016
3b50631
test/rank: make member tests added
huumanoid Oct 10, 2016
ad86b0e
test/pools: delay fix
huumanoid Oct 10, 2016
812cb90
database execJoin added for queries with JOIN
huumanoid Oct 11, 2016
9c0728a
ranking first implementation
huumanoid Oct 11, 2016
4ede507
db-init script run before tests
huumanoid Oct 11, 2016
61ee62f
error descriptions added, control packet fix
huumanoid Oct 11, 2016
361f002
test/rank errors fixed
huumanoid Oct 11, 2016
d0a4fb3
more tests added, checkMake fixed
huumanoid Oct 11, 2016
6b2d83a
ranking: <i> <u> emitting added
huumanoid Oct 11, 2016
0a88c0d
ranking: mainowner moderating himself test
huumanoid Oct 11, 2016
7949f88
ranking: mainowner moderating himself fix
huumanoid Oct 11, 2016
8658f88
test/rank: message checkers added
huumanoid Oct 11, 2016
440b081
ranking: more tests
huumanoid Oct 11, 2016
312a73e
more sufficient 'v' in <i>
huumanoid Oct 11, 2016
ee3d04e
implemented: specifying fields for Profile.getById issue #7
huumanoid Oct 11, 2016
cb88d98
lint
huumanoid Oct 11, 2016
0a8902c
Merge remote-tracking branch 'iiegor/master'
huumanoid Oct 11, 2016
6737bbb
fix merge conflicts
huumanoid Oct 11, 2016
9fc9193
houndfix
huumanoid Oct 12, 2016
dd7f8a3
skipped unnesesary tests
huumanoid Oct 12, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
*.log
.*.sw*
/lib
Empty file modified bin/xat
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions bin/xat-production
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

var Application;

Application = new (require('../lib/bootstrap'))
1 change: 1 addition & 0 deletions bin/xat-test
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"scripts": {
"start": "node bin/xat",
"lint": "coffeelint src",
"test": "mocha --require coffee-script/register --compilers coffee:coffee-script --recursive test"
"test": "script/test",
"compile": "coffee --compile --output lib src",
"start-production": "node bin/xat-production"
}
}
Empty file modified script/install
100644 → 100755
Empty file.
Empty file modified script/run
100644 → 100755
Empty file.
6 changes: 4 additions & 2 deletions script/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash

coffee src/test/db-init.coffee
mocha --compilers coffee:coffee-script/register "$@"
coffee test/lib/db-init.coffee \
&& echo "Database init OK" \
&& npm run compile \
&& mocha --compilers coffee:coffee-script/register "$@"
29 changes: 29 additions & 0 deletions src/packet-builders/chat.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
builder = require '../utils/builder'

Rank = require '../structures/rank'

module.exports =
buildMeta: (client) ->
## Chat settings and info
## r: 1 - (All main owner) / 2 - (All moderator) / 3 - (All member) / 4 (All owner)
## v: 1 - (Normal) / 3 - (w_VIP) / 4 - (w_ALLP) / other - (All unregistered)
packet = builder.create('i')
packet.append('b', "#{client.chat.bg};=#{client.chat.attached.name || ''};=#{client.chat.attached.id || ''};=#{client.chat.language};=#{client.chat.radio};=#{client.chat.button}")
packet.append('r', client.chat.rank.toNumber()) if client.chat.rank != Rank.GUEST
packet.append('f', '21233728')
packet.append('v', client.user.registered? && 1 | 0)
packet.append('cb', '2387')

buildPowers: (client) ->
packet = builder.create('gp')
packet.append('p', '0|0|1163220288|1079330064|20975876|269549572|16645|4210689|1|4194304|0|0|0|')
packet.append('g180', "{'m':'','d':'','t':'','v':1}")
packet.append('g256', "{'rnk':'8','dt':120,'rc':'1','v':1}")
packet.append('g100', 'assistance,1lH2M4N,xatalert,1e7wfSx')
packet.append('g114', "{'m':'Lobby','t':'Staff','rnk':'8','b':'Jail','brk':'8','v':1}")
packet.append('g112', 'Welcome to the lobby! Visit assistance and help pages.')
packet.append('g246', "{'rnk':'8','dt':30,'rt':'10','rc':'1','tg':1000,'v':1}")
packet.append('g90', 'shit,faggot,slut,cum,nigga,niqqa,prostitute,ixat,azzhole,tits,dick,sex,fuk,fuc,thot')
packet.append('g80', "{'mb':'11','ubn':'8','mbt':24,'ss':'8','rgd':'8','prm':'14','bge':'8','mxt':60,'sme':'11','dnc':'11','bdg':'11','yl':'10','rc':'10','p':'7','ka':'7'}")
packet.append('g74', 'd,waiting,astonished,swt,crs,un,redface,evil,rolleyes,what,aghast,omg,smirk')
packet.append('g106', 'c#sloth')
22 changes: 22 additions & 0 deletions src/packet-builders/message.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
builder = require '../utils/builder'

module.exports =
# options:
# message
# client - instance of services.Client related to user-sender
# time - timestamp of message
# messageId - chat-unique (pool-unique?) id of message
buildNewMain: (options) ->
builder.create('m')
.append('u', options.client.user.id)
.append('t', options.message)
.append('E', options.time)
.append('r', options.client.chat.id)
.append('i', options.messageId || 0)

buildOldMain: (message) ->
builder.create('m')
.append('u', message.uid)
.append('t', message.message)
.append('i', message.mid)
.append('s', '1')
44 changes: 44 additions & 0 deletions src/packet-builders/user.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
profile = require '../workers/profile'
builder = require '../utils/builder'

expandPacket = (packet, user, fillType) ->
packet.append('u', user.id)
.append('n', user.nickname)
.append('a', user.avatar)
.append('h', user.url)
.append('q', user.q)
.append('v', '0')
.append('cb', '0')

packet.append('f', user.f) if user.f > 0

if user.registered
packet.append('N', user.username)
packet.append('d0', user.d0)
packet.append('d2', user.d2) if user.d2?
packet.appendRaw(user.pStr)

# This method works assuming user with id=userId is online.
# This method is sync.
expandPacketWithOnlineUserData = (packet, client, fillType) ->
expandPacket(packet, client.user, fillType)

expandPacketWithUserData = (packet, userId, fillType) ->
new Promise (resolve, reject) ->
client = global.Server.getClientById userId
if client?
expandPacketWithOnlineUserData(packet, client, fillType)
resolve(packet)
else
profile.getById(userId).then((data) ->
reject() if data.length < 1
expandPacket(packet, data[0], fillType)
resolve(packet)
).catch reject

module.exports =
expandPacketWithOnlineUserData: expandPacketWithOnlineUserData
buildU: (client) ->
packet = builder.create('u')
expandPacketWithOnlineUserData(packet, client)
return packet
46 changes: 24 additions & 22 deletions src/services/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parser = require '../utils/parser'
math = require '../utils/math'
logger = require '../utils/logger'
builder = require '../utils/builder'
userBuilder = require '../packet-builders/user'

User = require '../workers/user'
Chat = require '../workers/chat'
Expand Down Expand Up @@ -73,9 +74,11 @@ class Client
dupUser.socket.end()

@id = @user.id
@setSuper()

Chat.joinRoom.call(@)
pool = parseInt parser.getAttribute(packet, 'p')
rankPass = parser.getAttribute(packet, 'r')

Chat.joinRoom.call(@, pool, rankPass)
).catch((err) => @logger.log @logger.level.ERROR, err, null)
when packetTag == "v"
###
Expand Down Expand Up @@ -110,13 +113,18 @@ class Client
Save user profile data
@spec <c u="2" t="/b USER_ID(int),UNKNOWN(int),,USERNAME(str),AVATAR(str),HOME(str),0,0,0,0....." />
###
return if not @user.authenicated or @user.guest
return if not @maySendMessages()

type = parser.getAttribute(packet, 't')
destId = parser.getAttribute(packet, 'u')

return if type is '/KEEPALIVE'

@logger.log @logger.level.ERROR, "Unhandled user data update packet", null
switch
when type is '/KEEPALIVE'
return
when type.startsWith('/m') or type.startsWith('/e') or type.startsWith('/M') or type.startsWith('/r')
Chat.makeUser.call(@, {rank: type[1], userId: destId}).catch((err) =>
@logger.log @logger.level.ERROR, "makeUser error", err)
else
@logger.log @logger.level.ERROR, "Unhandled user data update packet", null
when packetTag == "z" and isSlash
###
User profile
Expand All @@ -142,33 +150,25 @@ class Client
@routeZ(packet.compose(), userProfileId)
else if type.substr(0, 2) is '/a' and userProfile != null
packet = builder.create('z')
packet.append('N', @user.username) if @user.registered

status = type.substr(2)

# TODO: Show only for friends
if status[0] == '_'
if status.substr(1) == 'NF' and not global.Server.rooms[@user.chat][userProfileId]
if status.substr(1) == 'NF'
status = '/a_NF'
else
status = '/a_'
else
status = "/a#{Chat.getLink(@user.chat)}"

packet.append('t', status)
userBuilder.expandPacketWithOnlineUserData packet, @

packet.append('b', '1')
packet.append('t', status)
# .append('b', '1') # what does 'b' means?
.append('d', userProfileId)
.append('u', @user.id)
.append('po', '0')
.appendRaw(@user.pStr)
.append('x', @user.xats || 0)
.append('y', @user.days || 0)
.append('q', '3')
.append('n', @user.nickname)
.append('a', @user.avatar)
.append('h', @user.url)
.append('v', '2')

@routeZ(packet.compose(), userProfileId)
else if type is '/l' or type.substr(0, 2) is '/a'
Expand Down Expand Up @@ -213,9 +213,11 @@ class Client
Room pools
@spec <w v="ACTUAL_POOL(int) POOLS(int,int..)" />
###
@chat.onPool = packetTag.substr(1)

Chat.joinRoom.call(@)
###
Change pool
@spec <wNEWPOOL />
###
Chat.changePool.call(@, parseInt packetTag.substr(1))
when packetTag == 'x'
###
App
Expand Down
28 changes: 15 additions & 13 deletions src/services/database.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ Pool = mysql.createPool(
database: config.mysql.database
)

_exec = (options, values) -> new Promise((resolve, reject) ->
Pool.getConnection (err, connection) ->
return reject(err) if err

values = values || []

connection.query(options, values, (err, rows) ->
connection.release()
return reject(err) if err
resolve(rows)
)
)

module.exports =
# Ping database
initialize: (cb) ->
Pool.getConnection (err) -> cb err

exec: (sql, values) -> new Promise((resolve, reject) ->
Pool.getConnection (err, connection) ->
reject(err) if err

values = values || []

connection.query(sql, values, (err, rows) ->
connection.release()
reject(err) if err
resolve(rows)
)
)
exec: (sql, values) -> _exec({sql: sql}, values)
execJoin: (sql, values) -> _exec({sql: sql, nestTables: true}, values)
28 changes: 28 additions & 0 deletions src/structures/rank.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports =
class Rank
_number: null
map = [0, 4, 2, 1, 3]
strmap = ['r', null, 'm', 'e', 'M']
rankPool = [new Rank(0), new Rank(1), new Rank(2), new Rank(3), new Rank(4)]

@GUEST = rankPool[0]
@MEMBER = rankPool[3]
@MODERATOR = rankPool[2]
@OWNER = rankPool[4]
@MAINOWNER = rankPool[1]

## For internal use.
constructor: (@_number) ->
undefined

toNumber: -> @_number

toString: -> strmap[@_number]

## Normal way to convert number to rank.
@fromNumber: (number) ->
return rankPool[number]
@fromString: (str) ->
return rankPool[strmap.indexOf str]

compareTo: (rank) -> map[@_number] - map[rank._number]
Loading