MCPcopy
hub / github.com/tinode/chat / dispatch

Method dispatch

server/session.go:465–614  ·  view source on GitHub ↗
(msg *ClientComMessage)

Source from the content-addressed store, hash-verified

463}
464
465func (s *Session) dispatch(msg *ClientComMessage) {
466 now := types.TimeNow()
467 atomic.StoreInt64(&s.lastAction, now.UnixNano())
468
469 // This should be the first block here, before any other checks.
470 var resp *ServerComMessage
471 if msg, resp = pluginFireHose(s, msg); resp != nil {
472 // Plugin provided a response. No further processing is needed.
473 s.queueOut(resp)
474 return
475 } else if msg == nil {
476 // Plugin requested to silently drop the request.
477 return
478 }
479
480 if msg.Extra == nil || (msg.Extra.AsUser == "" && msg.Extra.AuthLevel == "") {
481 // Use current user's ID and auth level.
482 msg.AsUser = s.uid.UserId()
483 msg.AuthLvl = int(s.authLvl)
484 } else if s.authLvl != auth.LevelRoot {
485 // Only root user can set alternative user ID and auth level values.
486 s.queueOut(ErrPermissionDenied("", "", now))
487 logs.Warn.Println("s.dispatch: non-root assigned asUser", s.sid)
488 return
489 } else if fromUid := types.ParseUserId(msg.Extra.AsUser); fromUid.IsZero() {
490 // Invalid msg.Extra.AsUser.
491 s.queueOut(ErrMalformed("", "", now))
492 logs.Warn.Println("s.dispatch: malformed asUser: ", msg.Extra.AsUser, s.sid)
493 return
494 } else {
495 // Use provided msg.Extra.AsUser
496 msg.AsUser = msg.Extra.AsUser
497
498 // Assign auth level, if one is provided. Ignore invalid strings.
499 if authLvl := auth.ParseAuthLevel(msg.Extra.AuthLevel); authLvl == auth.LevelNone {
500 // AuthLvl is not set by the caller, assign default LevelAuth.
501 msg.AuthLvl = int(auth.LevelAuth)
502 } else {
503 msg.AuthLvl = int(authLvl)
504 }
505 }
506
507 msg.Timestamp = now
508
509 var handler func(*ClientComMessage)
510 var uaRefresh bool
511
512 // Check if s.ver is defined
513 checkVers := func(handler func(*ClientComMessage)) func(*ClientComMessage) {
514 return func(m *ClientComMessage) {
515 if s.ver == 0 {
516 logs.Warn.Println("s.dispatch: {hi} is missing", s.sid)
517 s.queueOut(ErrCommandOutOfSequence(m.Id, m.Original, msg.Timestamp))
518 return
519 }
520 handler(m)
521 }
522 }

Callers 15

MessageLoopMethod · 0.95
TestDispatchHelloFunction · 0.95
TestDispatchLoginFunction · 0.95
TestDispatchAccNewFunction · 0.95
TestDispatchNoMessageFunction · 0.95
dispatchRawMethod · 0.95
TestDispatchSubscribeFunction · 0.80
TestDispatchLeaveFunction · 0.80

Calls 15

queueOutMethod · 0.95
getSubMethod · 0.95
TimeNowFunction · 0.92
ParseUserIdFunction · 0.92
ParseAuthLevelFunction · 0.92
pluginFireHoseFunction · 0.85
ErrPermissionDeniedFunction · 0.85
ErrMalformedFunction · 0.85
ErrCommandOutOfSequenceFunction · 0.85
ErrAuthRequiredReplyFunction · 0.85
UserIdMethod · 0.80

Tested by 15

TestDispatchHelloFunction · 0.76
TestDispatchLoginFunction · 0.76
TestDispatchAccNewFunction · 0.76
TestDispatchNoMessageFunction · 0.76
TestDispatchSubscribeFunction · 0.64
TestDispatchLeaveFunction · 0.64
TestDispatchLeaveUnsubMeFunction · 0.64