Class: Nanobot::Channels::Email

Inherits:
BaseChannel show all
Defined in:
lib/nanobot/channels/email.rb

Overview

Email channel using IMAP polling for inbound messages and SMTP for replies. Requires explicit consent_granted=true and optional auto_reply_enabled flag.

Constant Summary collapse

MAX_PROCESSED_UIDS =

Maximum number of tracked UIDs before clearing to prevent unbounded growth.

100_000

Instance Attribute Summary

Attributes inherited from BaseChannel

#bus, #config, #logger, #name

Instance Method Summary collapse

Methods inherited from BaseChannel

#allowed?, #initialize, #running?

Constructor Details

This class inherits a constructor from Nanobot::Channels::BaseChannel

Instance Method Details

#send(message) ⇒ Object

Send an email reply via SMTP, respecting consent and auto-reply settings.

Parameters:



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
# File 'lib/nanobot/channels/email.rb', line 66

def send(message)
  unless @config.consent_granted
    @logger.warn 'Skip email send: consent_granted is false'
    return
  end

  force_send = (message. || {})['force_send']
  unless @config.auto_reply_enabled || force_send
    @logger.info 'Skip automatic email reply: auto_reply_enabled is false'
    return
  end

  to_addr = message.chat_id.to_s.strip
  return if to_addr.empty?

  base_subject = @last_subject_by_chat[to_addr] || 'nanobot reply'
  subject = reply_subject(base_subject)

  mail = ::Mail.new
  mail.to = to_addr
  mail.subject = subject
  mail.body = message.content || ''
  mail.from = @config.from_address || @config.smtp_username

  if (ref = @last_message_id_by_chat[to_addr])
    mail['In-Reply-To'] = ref
    mail['References'] = ref
  end

  smtp_send(mail)
end

#start ⇒ Object



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
# File 'lib/nanobot/channels/email.rb', line 20

def start
  unless @config.consent_granted
    @logger.warn 'Email channel disabled: consent_granted is false. ' \
                 'Set channels.email.consent_granted=true after explicit user permission.'
    return
  end

  return unless valid_config?

  @running = true
  @processed_uids = Set.new
  @last_subject_by_chat = {}
  @last_message_id_by_chat = {}

  poll_seconds = [@config.poll_interval_seconds, 5].max

  @logger.info "Starting Email channel (IMAP polling every #{poll_seconds}s)..."

  while @running
    begin
      fetch_new_messages.each do |item|
        sender = item[:sender]
        @last_subject_by_chat[sender] = item[:subject] if item[:subject]
        @last_message_id_by_chat[sender] = item[:message_id] if item[:message_id]

        handle_message(
          sender_id: sender,
          chat_id: sender,
          content: item[:content],
          metadata: item[:metadata] || {}
        )
      end
    rescue StandardError => e
      @logger.error "Email polling error: #{e.message}"
    end

    sleep poll_seconds
  end
end

#stop ⇒ Object



60
61
62
# File 'lib/nanobot/channels/email.rb', line 60

def stop
  @running = false
end