aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/pop3_server.rb
blob: 971adae2b285a8627b77c5e561a201a6a10e0fa0 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
require 'thin'
require 'mauve/mauve_thread'
require 'digest/sha1'

module Mauve
  # 
  # The POP3 server, where messages can also be read.
  #
  class Pop3Server < MauveThread

    include Singleton

    attr_reader :port, :ip

    # Initialize the server
    #
    # Default port is 1110
    # Default IP is 0.0.0.0
    #
    def initialize
      super
      self.port = 1110
      self.ip = "0.0.0.0"
    end
   
    #
    # Set the port
    #
    # @param [Integer] pr
    # @raise [ArgumentError] if the port is not sane
    # ~
    def port=(pr)
      raise ArgumentError, "port must be an integer between 0 and #{2**16-1}" unless pr.is_a?(Integer) and pr < 2**16 and pr > 0
      @port = pr
    end
    
    #
    # Set the IP address.  Unfortunately IPv6 is not OK.
    #
    # @param [String] i The IP address required.
    #
    def ip=(i)
      raise ArgumentError, "ip must be a string" unless i.is_a?(String)
      #
      # Use ipaddr to sanitize our IP.
      #
      IPAddr.new(i)

      @ip = i
    end
    
    # @return [Log4r::Logger]
    def logger
      @logger ||= Log4r::Logger.new(self.class.to_s)
    end

    #
    # This stops the server
    #
    def stop
      if @server.running?
        @server.stop
      else
        @server.stop!
      end

      super
    end

    #
    # This stops the server faster than stop
    #
    def join
      @server.stop! if @server

      super
    end

    #
    # Since Server.start doesn't return below, we can't check when the thread was last polled.
    #
    def last_polled_at
      Time.now
    end

    private

    #
    # This starts the server, and keeps it going.
    #
    def main_loop
      unless @server and @server.running?
        @server = Mauve::Pop3Backend.new(@ip.to_s, @port)
        logger.info "Listening on #{@server.to_s}"
        #
        # The next statment doesn't return.
        #
        @server.start
      end
    end

  end    

  #
  # This is the Pop3 Server itself.  It is based on the Thin HTTP server, and hence EventMachine.
  #
  class Pop3Backend < Thin::Backends::TcpServer

    #
    # @return [Log4r::Logger]
    def logger
      @logger ||= Log4r::Logger.new(self.class.to_s)
    end
        
    # Initialize a new connection to the server
    def connect
      @signature = EventMachine.start_server(@host, @port, Pop3Connection)
    end
        
    # Disconnect the server, but only if EventMachine is still going.
    def disconnect
      #
      # Only do this if EventMachine is still going.. The http_server may have
      # stopped it already.
      #
      EventMachine.stop_server(@signature) if EventMachine.reactor_running?
    end

  end

  #
  # This class represents and individual connection, and understands some POP3
  # commands.
  #
  class Pop3Connection < EventMachine::Connection

    # The username
    attr_reader :user

    # Default CR+LF combo.
    CRLF = "\r\n"

    # @return [Log4r::Logger]
    def logger
      @logger ||= Log4r::Logger.new(self.class.to_s)
    end

    # This is called once the connection has been established.  It says hello
    # to the client, and resets the state.
    def post_init
      logger.info "New connection"
      send_data "+OK #{self.class.to_s} started"
      @state = :authorization
      @user  = nil
      @messages = []
      @level    = nil
    end

    # This returns a list of commands allowed in a state.
    #
    # @param [Symbol] The state to query, defaults to the current state.
    # @return [Array] An array of permitted comands.
    #
    def permitted_commands(state=@state)
     case @state
        when :authorization
          %w(QUIT USER PASS CAPA)
        when :transaction
          %w(QUIT STAT LIST RETR DELE NOOP RSET UIDL CAPA)
        when :update
          %w(QUIT)
      end
    end

    # This returns a list of capabilities in a given state.
    #
    # @param [Symbol] The state to query, defaults to the current state.
    # @return [Array] An array of capabilities.
    def capabilities(state=@state)
     case @state
        when :transaction
          %w(CAPA UIDL)
        when :authorization
          %w(CAPA UIDL USER) 
        else
          []
      end
    end

    # This method handles a command, and parses it.
    #
    # The following POP3 commands are understood:
    #   QUIT
    #   USER
    #   PASS
    #   STAT
    #   LIST
    #   RETR
    #   DELE
    #   NOOP
    #   RSET
    #   CAPA
    #   UIDL
    #
    # The command is checked against a list of permitted commands, given the
    # state of the connection, and returns an error if the command is
    # forbidden.
    #
    # @param [String] data The data to process.
    #
    def receive_data (data)
      data.split(CRLF).each do |cmd|
        break if error?

        if cmd =~ Regexp.new('\A('+self.permitted_commands.join("|")+')\b')
          case $1
            when "QUIT"
              do_process_quit cmd
            when "USER"
              do_process_user cmd
            when "PASS"
              do_process_pass cmd
            when "STAT"
              do_process_stat cmd
            when "LIST"
              do_process_list cmd
            when "RETR"
              do_process_retr cmd
            when "DELE"
              do_process_dele cmd
            when "NOOP"
              do_process_noop cmd
            when "RSET"
              do_process_rset cmd
            when "CAPA"
              do_process_capa cmd
            when "UIDL"
              do_process_uidl cmd
            else
              do_process_error cmd
          end
        else
          do_process_error cmd
        end
      end
    end
 
    # This sends the data back to the user.  A CR+LF is joined to the end of
    # the data.
    #
    # @param [String] d The data to send back.
    def send_data(d)
      d += CRLF
      super unless error?
    end

    private

    # This deals with CAPA, returning a string of capabilities in the current
    # connection state.
    #
    # @param [String] a The complete CAPA command sent by the client.
    #
    def do_process_capa(a)
      send_data (["+OK Capabilities follow:"] + self.capabilities + ["."]).join(CRLF)
    end

    # This deals with the USER command.
    #
    # Any of low, normal, urgent can be appended to the username, to select
    # only alarms of that level to be shown.
    #
    # e.g.
    #   patrick+low
    #
    # will show only alerts of a LOW level.
    #
    # @param [String] s The complete USER command sent by the client.
    #
    def do_process_user(s)
      allowed_levels = Mauve::AlertGroup::LEVELS.collect{|l| l.to_s}

      if s =~ /\AUSER +(\w+)\+(#{allowed_levels.join("|")})/
        # Allow alerts to be shown by level.
        #
        @user  = $1
        @level = $2
        #
        send_data "+OK Only going to show #{@level} alerts."

     elsif s =~ /\AUSER +([\w]+)/
        @user =  $1

        send_data "+OK"
      else
        send_data "-ERR Username not understood."
      end
    end

    # This processes the PASS command.  It uses the Mauve::Authenticate class
    # to authenticate the user.  Once authenticated, the state is set to :transaction.
    #
    # @param [String] s The complete PASS command sent by the client.
    def do_process_pass(s)
      
      if @user and s =~ /\APASS +(\S+)/
        if Mauve::Authentication.authenticate(@user, $1)
          @state = :transaction
          send_data "+OK Welcome #{@user} (#{@level})." 
        else
          send_data "-ERR Authentication failed."
        end        
      else
        send_data "-ERR USER comes first."
      end
    end

    #
    # This just sends an "ERR Unknown command" string back to the user.
    #
    # @param [String] a The complete command from the client that caused this error.
    def do_process_error(a)
      send_data "-ERR Unknown comand."
    end

    # This does a NOOP.
    #
    # @param [String] a The complete NOOP command from the client.
    def do_process_noop(a)
      send_data "+OK Thanks."
    end

    # Delete is processed as a NOOP
    alias do_process_dele do_process_noop

    # This logs a user out, and closes the connection.  The state is set to :update.
    #
    # @param [String] a The complete QUIT command from the client.
    def do_process_quit(a)
      @state = :update

      send_data "+OK bye."

      close_connection_after_writing
    end

    # This sends the number of messages, and their size back to the client.
    #
    # @param [String] a The complete STAT command from the client.
    def do_process_stat(a)
      send_data "+OK #{self.messages.length} #{self.messages.inject(0){|s,m| s+= m[1].length}}"
    end

    # This sends a list of the messages back to the client.
    #
    # @param [String] a The complete LIST command from the client.
    #
    def do_process_list(a)
      d = []
      if a =~ /\ALIST +(\d+)\b/
        ind = $1.to_i
        if ind > 0 and ind <= self.messages.length
          d << "+OK #{ind} #{self.messages[ind-1][1].length}"
        else
          d << "-ERR Unknown message."
        end
      else
        d << "+OK #{self.messages.length} messages (#{self.messages.inject(0){|s,m| s+= m[1].length}} octets)."
        self.messages.each_with_index{|m,i| d << "#{i+1} #{m[1].length}"}
        d << "."
      end

      send_data d.join(CRLF)
    end

    # This sends the UID of a message back to the client.
    #
    # @param [String] a The complete UIDL command from the client.
    def do_process_uidl(a)
      if a =~ /\AUIDL +(\d+)\b/
        ind = $1.to_i
        if ind > 0 and ind <= self.messages.length
          m = self.messages[ind-1][0].id
          send_data "+OK #{ind} #{m}"
        else
          send_data "-ERR Message not found."
        end
      else
        d = ["+OK "]
        self.messages.each_with_index{|m,i| d << "#{i+1} #{m[0].id}"}
        d << "."

        send_data d.join(CRLF)
      end
    end

    # This retrieves a message for the client.
    #
    # @param [String] a The complete RETR command from the client.
    #
    def do_process_retr(a)
      if a =~ /\ARETR +(\d+)\b/
        ind = $1.to_i
        if ind > 0 and ind <= self.messages.length
          alert_changed, msg = self.messages[ind-1]
          send_data ["+OK #{msg.length} octets", msg, "."].join(CRLF)
          note =  "#{alert_changed.update_type.capitalize} notification downloaded via POP3 by #{@user}" 
          logger.info note+" about #{alert_changed}."
          h = History.new(:alerts => [alert_changed.alert_id], :type => "notification", :event => note, :user => @user)
          logger.error "Unable to save history due to #{h.errors.inspect}" if !h.save
        else
          send_data "-ERR Message not found."
        end
      else
        send_data "-ERR Boo."
      end

    end

    protected

    #
    # These are the messages in the mailbox.  It looks for the first 100 alert_changed, and formats them into emails, and returns an array of
    #
    #  [alert_changed, email]
    #
    # @return [Array] Array of alert_changeds and emails.
    #
    def messages
      if @messages.empty?
        @messages = []

        email = Configuration.current.notification_methods['email']

        alerts_seen = []

        #
        # A maximum of the 100 most recent alerts.
        #
        AlertChanged.first(100, :person => self.user, :was_relevant => true).each do |a|
          #
          # Not interested in alerts 
          #
          next unless @level.nil? or a.level.to_s == @level

          #
          # Only interested in alerts
          #
          next unless a.alert.is_a?(Mauve::Alert)

          #
          # Only one message per alert.
          #
          next if alerts_seen.include?([a.alert_id, a.update_type])

          relevant = case a.update_type
            when "raised"
              a.alert.raised?
            when "acknowledged"
              a.alert.acknowledged?
            when "cleared"
              a.alert.cleared?
            else
              false
          end

          next unless relevant

          alerts_seen << [a.alert_id, a.update_type]

          @messages << [a, email.prepare_message(self.user+"@"+Server.instance.hostname, a.alert, [])]
        end
      end
      
      @messages
    end

  end

end