firer.js 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/**
 *
 * Wechaty: Wechat for Bot. Connecting ChatBots
 *
 * Class PuppetWeb Firer
 *
 * Process the Message to find which event to FIRE
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */

/**************************************
 *
 * Firer for Class PuppetWeb
 *
 * here `this` is a PuppetWeb Instance
 *
 ***************************************/
const util  = require('util')
const fs    = require('fs')
const co    = require('co')

const log = require('../brolog-env')
const Contact = require('../contact')
const Message = require('../message')
const FriendRequest = require('./friend-request')

const PuppetWebFirer = {
  fireFriendConfirm
  , fireFriendRequest

  , fireRoomJoin
  , fireRoomLeave

  // for testing
  , checkFriendConfirm
  , checkRoomJoin
  , checkRoomLeave
}

const regexConfig = {
  friendConfirm:    /^You have added (.+) as your WeChat contact. Start chatting!$/

  , roomJoin:  /^"?(.+?)"? invited "(.+)" to the group chat$/
  , roomLeave:  /^You removed "(.+)" from the group chat$/
}

function fireFriendRequest(m) {
  const info = m.rawObj.RecommendInfo
  log.verbose('PuppetWebEvent', 'fireFriendRequest(%s)', info)

  const request = new FriendRequest()
  request.receive(info)
  this.emit('friend', request.contact, request)
}

/**
 * try to find FriendRequest Confirmation Message
 */
function checkFriendConfirm(content) {
  const re = regexConfig.friendConfirm
  if (re.test(content)) {
    return true
  } else {
    return false
  }
}

function fireFriendConfirm(m) {
  const content = m.content()
  log.silly('PuppetWebEvent', 'fireFriendConfirm(%s)', content)

  if (!checkFriendConfirm(content)) {
    return
  }
  const request = new FriendRequest()
  const contact = Contact.load(m.get('from'))
  request.confirm(contact)

  this.emit('friend', contact)
}


/**
 * try to find 'join' event for Room
 *
1.
  You've invited "李卓桓" to the group chat
  You've invited "李卓桓.PreAngel、Bruce LEE" to the group chat
2.
   "李卓桓.PreAngel" invited "Bruce LEE" to the group chat
*/
function checkRoomJoin(content) {
  const re = regexConfig.roomJoin

  const found = content.match(re)
  if (!found) {
    return false
  }
  const [_, inviter, invitee] = found
  return [invitee, inviter]
}

function fireRoomJoin(m) {
  const room    = m.room()
  const content = m.content()

  const [invitee, inviter] = checkRoomJoin(cotent)
  if (!invitee || !inviter) {
    return
  }

  let inviterContact, inviteeContact

  if (inviter === "You've") {
    inviterContact = Contact.load(this.userId)
  } else {
    inviterContact = room.member(inviter)
  }

  inviteeContact = room.member(invitee)

  if (!inviterContact || !inviteeContact) {
    log.error('PuppetWebEvent', 'inivter or invitee not found for %s, %s', inviter, invitee)
    return
  }
  room.emit('join', inviteeContact, inviterContact)
}

function checkRoomLeave(content) {
  const re = regexConfig.roomLeave

  const found = content.match(re)
  if (!found) {
    return false
  }
  const [_, leaver] = found
  return leaver
}

/**
 * You removed "Bruce LEE" from the group chat
 */
function fireRoomLeave(m) {
  const leaver = checkRoomLeave(m.content())
  if (!leaver) {
    return
  }

  const room = m.room()
  leaverContact = room.member(leaver)

  if (!leaverContact) {
    log.error('PuppetWebEvent', 'leaver not found for %s', leaver)
    return
  }
  room.emit('leave', leaverContact)
}

module.exports = PuppetWebFirer