OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / hyper-social / bridge-irc / lib / adapter.js

68 lines · javascript ref:
import { createEnvelope, TYPES, PLATFORMS } from 'bridge-envelope'

function ircToEnvelope (nick, channel, text) {
  // Detect ACTION (/me)
  const actionMatch = text.match(/^\x01ACTION (.+)\x01$/)
  if (actionMatch) {
    return createEnvelope({
      type: TYPES.TEXT,
      platform: PLATFORMS.IRC,
      author: nick,
      content: `* ${nick} ${actionMatch[1]}`,
      channel
    })
  }

  return createEnvelope({
    type: TYPES.TEXT,
    platform: PLATFORMS.IRC,
    author: nick,
    content: text,
    channel
  })
}

function keetToIrc (keetMsg, bridgeIdentity) {
  // Skip messages from bridge identity (prevents echo)
  if (keetMsg.memberId === bridgeIdentity) return null
  // Skip system messages
  if (keetMsg.system) return null

  const name = keetMsg.name || 'anonymous'
  const text = keetMsg.text || ''

  if (!text.trim()) return null

  return {
    nick: name,
    text: `<${name}> ${text}`
  }
}

function envelopeToIrcText (envelope) {
  // Format envelope for IRC display
  const platform = platformName(envelope.platform)
  const prefix = platform !== 'irc' ? `[${platform}] ` : ''
  const author = envelope.author || 'anonymous'

  switch (envelope.type) {
    case TYPES.TEXT:
      return `${prefix}<${author}> ${envelope.content}`
    case TYPES.MEDIA:
      return `${prefix}<${author}> ${envelope.content || ''} ${envelope.mediaUrl || '[media]'}`
    case TYPES.REACTION:
      return `${prefix}* ${author} reacted: ${envelope.content}`
    case TYPES.SYSTEM:
      return `${prefix}** ${envelope.content}`
    default:
      return `${prefix}<${author}> ${envelope.content}`
  }
}

function platformName (id) {
  const names = ['keet', 'irc', 'discord', 'matrix', 'mastodon', 'email', 'android']
  return names[id] || 'unknown'
}

export { ircToEnvelope, keetToIrc, envelopeToIrcText }