This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindypicdump.rb
executable file
·724 lines (703 loc) · 25.1 KB
/
indypicdump.rb
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#!/usr/bin/env ruby
# This file is part of indypicdump.
# indypicdump is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# indypicdump is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with indypicdump. If not, see <http://www.gnu.org/licenses/>.
# Copyright 2012-2015 Marcus Schommer <sabotrax@gmail.com>
$:.unshift("#{File.dirname(__FILE__)}/lib")
require 'date'
require 'mail'
require 'rmagick'
require 'sqlite3'
require 'stalker'
require 'ipdconfig'
require 'ipdpicture'
require 'ipdtest'
require 'ipduser'
require 'ipdmessage'
require 'ipddump'
require 'ipdrequest'
require 'ipdemail'
require 'ipderror'
include IPDConfig
IPDDump.load_dump_map
switch = ARGV.shift
if switch
case switch
when "test"
test = true
else
puts "WRONG MODE (try \"test\")"
exit
end
end
##############################
# get mail
unless test
Mail.defaults do
retriever_method :pop3,
:address => POP3_HOST,
:port => POP3_PORT,
:user_name => POP3_USER,
:password => POP3_PASS,
:enable_ssl => POP3_SSL
end
end
unless test
#mail = Mail.find_and_delete(:what => :first, :count => FETCH_MAILS, :order => :asc, :delete_after_find => true)
mail = Mail.find(:what => :first, :count => FETCH_MAILS, :order => :asc)
else
mail = []
mail.push(IPDTest.gen_mail)
end
LOG_HANDLE.info("MAILS #{mail.length}/#{FETCH_MAILS}")
picstack = []
mail.each do |m|
##############################
# process user management mail
if m.to[0] == EMAIL_USER_MGMT
case m.subject
# i am
when /(i am)\s+([a-z]+[\- ][a-z]+)/i
nick = $2.undash
if IPDUser.exists?(nick)
# check duplicate requests and ignore
request = IPDRequest.new
request.action = ["i am", "confirm owner", nick, m.from[0]].join(",")
next if request.exists?
# check if requesting email address is already bound to this username
user = IPDUser.load(nick)
if user.has_email?(m.from[0])
Stalker.enqueue("email.send", :to => m.from[0], :template => :i_am_already_are, :from => m.from[0], :nick => nick, :subject => "Notice")
next
end
# check if requesting email address is already bound to any username
result = DB_HANDLE.execute("SELECT u.nick FROM user u JOIN mapping_user_email_address m ON u.id = m.id_user JOIN email_address e ON m.id_address = e.id WHERE e.address = ?", [m.from[0]])
if result.any?
Stalker.enqueue("email.send", :to => m.from[0], :template => :i_am_some_is, :from => m.from[0], :nick => nick, :subject => "Notice")
next
end
request.save
# send request to owner of username
Stalker.enqueue("email.send", :to => user.email.first, :template => :i_am_confirm_owner, :code => request.code, :from => m.from[0], :nick => nick, :subject => "Request to add email address")
# send notification of non-existing user
else
Stalker.enqueue("email.send", :to => m.from[0], :template => :i_am_no_user, :from => m.from[0], :nick => nick, :subject => "Notice")
end
next
# accept/decline messages
when /\b(accept|decline)\s+messages?\b/i
order = $1.downcase
if IPDUser.exists?(m.from[0])
# check duplicate requests and ignore
request = IPDRequest.new
request.action = ["#{order} messages", m.from[0]].join(",")
# NOTICE
# #exists? checks for existing accepts _and_ declines
next if request.exists?
# check if requesting user is already accepting/declining messages
user = IPDUser.load(m.from[0])
if (order == "accept" and user.accept_external_messages?) or (order == "decline" and user.decline_external_messages?)
Stalker.enqueue("email.send", :to => m.from[0], :template => :messages_already_are, :nick => user.nick, :order => order, :subject => "Notice")
next
end
# send request
request.save
Stalker.enqueue("email.send", :to => m.from[0], :template => :messages_request_code, :code => request.code, :nick => user.nick, :order => order, :subject => "Request to #{order} messages")
end
next
# open dump
when /\bopen\s+([a-z0-9][a-z0-9\- ]*)(?<![\- ])\s+for\s+(#{REGEX_EMAIL})/i
address = $2.downcase
if IPDUser.exists?(m.from[0])
user = IPDUser.load(m.from[0])
# check if dump exists
dump = IPDDump.load($1.dash)
unless dump
Stalker.enqueue("email.send", :to => m.from[0], :template => :open_dump_no_dump, :nick => user.nick, :dump => $1.undash, :address => address, :subject => "Notice")
next
end
# check duplicate requests and ignore
request = IPDRequest.new
request.action = ["open dump", dump.alias, address].join(",")
next if request.exists?
# check if user is member of dump
unless dump.has_user?(user.id)
Stalker.enqueue("email.send", :to => m.from[0], :template => :open_dump_no_member, :nick => user.nick, :dump => dump.alias.undash, :address => address, :subject => "Notice")
next
end
# check if new address is already member of dump
if user.has_email?(address)
Stalker.enqueue("email.send", :to => m.from[0], :template => :bad_kitty, :nick => user.nick, :subject => "Notice")
next
end
if dump.has_user?(address)
Stalker.enqueue("email.send", :to => m.from[0], :template => :open_dump_already_are, :nick => user.nick, :dump => dump.alias.undash, :address => address, :subject => "Notice")
next
end
# send request
request.save
Stalker.enqueue("email.send", :to => m.from[0], :template => :open_dump_request_code, :code => request.code, :nick => user.nick, :dump => dump.alias.undash, :address => address, :subject => "Request to open dump")
end
next
# did read
when /\bdid\s+read\b/i
if IPDUser.exists?(m.from[0])
user = IPDUser.load(m.from[0])
# remove messages older than the time of mailing
IPDMessage.remove_old(user.id, m.date.to_time.to_i)
LOG_HANDLE.info("USER REQUEST DID READ #{m.from[0]}")
end
next
# stats please
when /\bstats\s+please\b/i
if IPDUser.exists?(m.from[0])
user = IPDUser.load(m.from[0])
user_hash = {
:nick => user.nick,
:time_created => user.time_created,
:accept_messages => user.accept_external_messages?,
}
email_list = user.email
result = DB_HANDLE.execute("SELECT d.id, d.alias FROM dump d JOIN mapping_dump_user m ON d.id = m.id_dump WHERE m.id_user= ? ORDER BY d.alias ASC", [user.id])
dump_list = []
pictures = 0
percent = 0
result.each do |row|
# no. of pics of user
result2 = DB_HANDLE.execute("SELECT COUNT(*) FROM \"#{row[0]}\" WHERE id_user = ?", [user.id])
# no. of all pics
result3 = DB_HANDLE.execute("SELECT COUNT(*) FROM \"#{row[0]}\"")
if result2[0][0] > 0
pictures += result2[0][0]
percent = (result2[0][0].to_f / result3[0][0].to_f * 100).round(2)
end
# member ranking
result4 = DB_HANDLE.execute("SELECT id_user, count(1) posts FROM \"#{row[0]}\" GROUP BY id_user ORDER BY posts DESC, id_user ASC")
ranking = 0
result4.each do |row2|
ranking += 1
break if user.id == row2[0]
end
dump_list.push({
:alias => row[1],
:pictures => result2[0][0],
:percent => percent,
:ranking => ranking,
:members => result4.size,
})
end
# find the most common color of the newest picture
acc = []
filename = ""
if pictures != 0
result = DB_HANDLE.execute("SELECT id FROM picture WHERE id_user = ? ORDER BY id DESC LIMIT 1", [user.id])
picture = IPDPicture.load(result[0][0])
begin
acc = picture.approx_common_color.slice(0, 2)
rescue PictureCommonColorMissing
LOG_HANDLE.error("COMMON COLOR MISSING ERROR ID #{picture.id}")
end
filename = picture.filename
end
Stalker.enqueue("email.send", :to => m.from[0], :template => :stats_please, :user => user_hash, :email => email_list, :dump => dump_list, :pictures => pictures, :filename => filename, :common_color => acc, :subject => "Your stats")
LOG_HANDLE.info("USER REQUEST STATS PLEASE #{m.from[0]}")
end
next
# remove pictures
when /\bremove\s+pictures?\b/i
if IPDUser.exists?(m.from[0])
user = IPDUser.load(m.from[0])
# find filenames in body
body = ""
if m.multipart?
m.parts.each do |p|
body += p.decoded if p.content_type.start_with?('text/')
end
else
body = m.body.decoded
end
remove_ids = []
picture_list = []
err = false
body.scan(/\b#{REGEX_FILENAME}\b/i).uniq.each do |f|
# check ownership of pictures
# TODO
# better downsize pictures in db and in fs to avoid misses
unless user.owns_picture?(f)
Stalker.enqueue("email.send", :to => m.from[0], :template => :remove_picture_not_owner, :nick => user.nick, :subject => "Notice")
err = true
break
end
picture = IPDPicture.load(f)
picture_hash = {
:path => picture.path,
:filename => picture.filename,
:in_group => false
}
if picture.in_group?
first = picture.group_ids.first
unless remove_ids.include?(first)
remove_ids << first
end
picture_hash[:in_group] = true
else
remove_ids << picture.id
end
picture_list << picture_hash
end
next if err
action = ["remove pictures", "confirm", user.nick] + remove_ids
# check duplicate requests and ignore
request = IPDRequest.new
request.action = action.join(",")
# NOTE
# IPDRequest#exists? needs the trailing "," for this action
request.action += ","
next if request.exists?
# send request
request.save
Stalker.enqueue("email.send", :to => m.from[0], :template => :remove_picture_confirm_deletion, :code => request.code, :nick => user.nick, :pictures => picture_list, :subject => "Request to remove pictures")
end
next
# keep pictures
when /\bkeep\s+pictures?\b/i
if IPDUser.exists?(m.from[0])
user = IPDUser.load(m.from[0])
now = Time.now
result = DB_HANDLE.execute("SELECT action FROM user_request WHERE action LIKE ? AND time_created >= ? ORDER BY time_created ASC", ["remove pictures,%,#{user.nick}%", now.to_i - 129600])
template = ""
# TODO FIX
# bei mehreren remove requests mit verschiedenen zustaenden (complete/cleared) werden falsche mails verschickt
result.each do |row|
if row[0] =~ /complete/
keep_pictures = []
picture = IPDPicture.load(row[0].split(',').last)
if picture.in_group?
keep_pictures += IPDPicture.load_group(picture.id)
else
keep_pictures << picture
end
keep_pictures.each do |p|
p.show!
p.save
end
template = :remove_picture_kept_pictures if template.empty?
IPDRequest.remove_by_action(row[0])
elsif row[0] =~ /cleared/
template = :remove_picture_already_gone if template.empty?
end
end
Stalker.enqueue("email.send", :to => m.from[0], :template => template, :nick => user.nick, :subject => "Request to keep pictures") if template != ""
end
next
# set dump
when /\bset\s+([a-z0-9][a-z0-9\- ]*)(?<![\- ])\s+(open|hidden|protected)\b/i
state = $2.downcase
if IPDUser.exists?(m.from[0])
user = IPDUser.load(m.from[0])
# check if dump exists
dump = IPDDump.load($1.dash)
unless dump
Stalker.enqueue("email.send", :to => m.from[0], :template => :set_dump_no_dump, :nick => user.nick, :dump => $1.undash, :state => state, :subject => "Notice")
next
end
# check duplicate requests and ignore
request = IPDRequest.new
request.action = ["set dump", dump.alias, state].join(",")
next if request.exists?
# check if user is admin of dump
unless user.admin_of_dump?(dump.id)
Stalker.enqueue("email.send", :to => m.from[0], :template => :set_dump_no_admin, :nick => user.nick, :dump => dump.alias.undash, :state => state, :subject => "Notice")
next
end
# check if dump is already in the wanted state
if dump.open? and state == "open" or dump.hidden? and state == "hidden" or dump.protected? and state == "protected"
Stalker.enqueue("email.send", :to => m.from[0], :template => :set_dump_already_is, :nick => user.nick, :dump => dump.alias.undash, :state => state, :subject => "Notice")
next
end
# find password in body
if state == "protected"
body = ""
if m.multipart?
m.parts.each do |p|
body += p.decoded if p.content_type.start_with?('text/')
end
else
body = m.body.decoded
end
if body !~ /\bpassword\s+"([a-z0-9\- ]+)"/i
Stalker.enqueue("email.send", :to => m.from[0], :template => :set_dump_no_password, :nick => user.nick, :dump => dump.alias.undash, :state => state, :subject => "Notice")
next
elsif $1.to_s.length < 8
Stalker.enqueue("email.send", :to => m.from[0], :template => :set_dump_short_password, :nick => user.nick, :dump => dump.alias.undash, :state => state, :subject => "Notice")
next
end
request.action += ",#{$1}"
end
# send request
request.save
Stalker.enqueue("email.send", :to => m.from[0], :template => :set_dump_request_code, :code => request.code, :nick => user.nick, :dump => dump.alias.undash, :state => state, :subject => "Request to set state of dump")
end
end
next
end
if m.to[0] == EMAIL_SELF
body = ""
if m.multipart?
m.parts.each do |p|
body += p.decoded if p.content_type.start_with?('text/')
end
else
body = m.body.decoded
end
case body
# work request codes
when /request code (\h+)/im
code = $1.downcase
result = DB_HANDLE.execute("SELECT action FROM user_request WHERE code LIKE ?", [code])
if result.any?
action = result[0][0].split(",")
case action[0].downcase
# i am
when "i am"
# create confirmation request for requestor
if action[1] == "confirm owner"
IPDRequest.remove_by_action(result[0][0])
request = IPDRequest.new
request.action = ["i am", "confirm requestor", action.slice(2,2)].join(",")
request.save
action = request.action.split(",")
Stalker.enqueue("email.send", :to => action[3], :template => :i_am_confirm_requestor, :code => request.code, :from => action[3], :subject => "Request to add email address")
# add address to requesting user
elsif action[1] == "confirm requestor"
user = IPDUser.load(action[2])
user.email = action[3]
user.save
IPDRequest.remove_by_action(result[0][0])
LOG_HANDLE.info("USER REQUEST BOUND ADDRESS TO USER #{action[3]} -> #{action[2]}")
end
next
# accept/decline messages
when /(accept|decline) messages/
order = $1
user = IPDUser.load(action[1])
if order == "accept"
user.accept_external_messages!
elsif order == "decline"
user.decline_external_messages!
end
user.save
IPDRequest.remove_by_action(result[0][0])
LOG_HANDLE.info("USER REQUEST #{user.nick} #{order.upcase}S MESSAGES")
next
# open dump
when /open dump/
dump = IPDDump.load(action[1])
user = IPDUser.load(action[2])
# add known users to dump
if user
dump.add_user(user.id, :admin => 0, :time_created => Time.now.to_i)
dump.save
Stalker.enqueue("email.send", :to => action[2], :template => :open_dump_notice_invited, :nick => user.nick, :dump => dump.alias.undash, :subject => "A new place to store pictures")
IPDRequest.remove_by_action(result[0][0])
LOG_HANDLE.info("USER REQUEST ADD USER #{user.nick} TO DUMP #{dump.alias}")
# invite new users
else
IPDRequest.remove_by_action(result[0][0])
request = IPDRequest.new
action.shift
request.action = ["new user", action].join(",")
request.save
action = request.action.split(",")
Stalker.enqueue("email.send", :to => action[2], :template => :new_user_request_code, :code => request.code, :subject => "indypicdump - collect and share")
end
next
# new user
when /new user/
user = IPDUser.new
user.email = action[2]
user.save
dump = IPDDump.load(action[1])
dump.add_user(user.id, :admin => 0, :time_created => Time.now.to_i)
dump.save
Stalker.enqueue("email.send", :to => action[2], :template => :new_user_notice_invited, :dump => dump.alias.undash, :subject => "Welcome to indypicdump")
IPDRequest.remove_by_action(result[0][0])
LOG_HANDLE.info("USER REQUEST NEW USER #{action[2]} IS #{user.nick} IN DUMP #{dump.alias}")
next
# create dump (and user)
when /create dump/
dump = IPDDump.new
dump.alias = action[1]
user = IPDUser.new
user.email = action[2]
user.save
dump.add_user(user.id, :admin => 1, :time_created => Time.now.to_i)
dump.save
Stalker.enqueue("email.send", :to => action[2], :template => :new_user_notice_invited, :dump => dump.alias.undash, :subject => "Welcome to indypicdump")
IPDRequest.remove_by_action(result[0][0])
LOG_HANDLE.info("USER REQUEST NEW DUMP #{dump.alias} BY #{action[2]} IS NEW USER #{user.nick}")
next
# remove pictures
when /remove pictures/
user = IPDUser.load(action[2])
# hide pictures up until their final deletion
remove_pictures = []
action[3..-1].each do |id|
picture = IPDPicture.load(id)
if picture.in_group?
remove_pictures += IPDPicture.load_group(picture.id)
else
remove_pictures << picture
end
end
remove_pictures.each do |p|
p.no_show!
p.save
end
# build final deletion request
IPDRequest.remove_by_action(result[0][0])
request = IPDRequest.new
request.action = ["remove pictures", "complete", action[2..-1]].join(",")
request.save
LOG_HANDLE.info("USER REQUEST DELETE PICTURES #{user.nick} -#{remove_pictures.size}")
next
# set dump
when /set dump/
dump = IPDDump.load(action[1])
if action[2] == "open"
dump.open!
dump.password = ""
elsif action[2] == "hidden"
dump.hide!
dump.password = ""
elsif action[2] == "protected"
dump.protect!
dump.password = Digest::RMD160::hexdigest(action[3].to_s)
end
dump.save
dump.user.each_key do |id|
user = IPDUser.load(id)
unless user.email[0].to_s.empty?
Stalker.enqueue("email.send", :to => user.email[0], :template => :set_dump_changed_state, :nick => user.nick, :dump => dump.alias.undash, :state => action[2], :password => action[3], :subject => "State of dump changed")
end
end
IPDRequest.remove_by_action(result[0][0])
LOG_HANDLE.info("USER REQUEST SET DUMP #{dump.alias} #{action[2]}")
next
end
else
# send error mail?
end
end
next
end
if m.subject =~ /\bgroup\b/i
group = true
else
group = false
end
noof_pictures = 0
m.attachments.each do | attachment |
##############################
# extract picture attachments
if (attachment.content_type.start_with?('image/'))
noof_pictures += 1 if group
# load user
# CAUTION
# "downcase" only works in the ASCII region
email = m.from[0].downcase
user = IPDUser.load(email)
break unless user
# drop pictures smaller than PICTURE_MIN_SIZE
img = Magick::Image::from_blob(attachment.body.decoded)[0]
if img.columns >= img.rows and img.columns < PICTURE_MIN_SIZE or img.rows >= img.columns and img.rows < PICTURE_MIN_SIZE
msg = IPDMessage.new
if group
msg.message_id = MSG_GROUP_PIC_TOO_SMALL
noof_pictures -= 1
else
msg.message_id = MSG_PIC_TOO_SMALL
end
msg.time_created = m.date.to_time.to_i
msg.id_user = user.id
msg.save
LOG_HANDLE.info("PICTURE #{group ? "(IN GROUP) " : ""}TOO SMALL FROM #{user.nick} SIZE #{img.columns}x#{img.rows}")
break
end
# check for existing dump
unless IPDDump.exists?(m.to[0].to_s)
msg = IPDMessage.new
if group
msg.message_id = MSG_GROUP_UNKNOWN_DUMP
noof_pictures -= 1
else
msg.message_id = MSG_UNKNOWN_DUMP
end
msg.time_created = m.date.to_time.to_i
msg.id_user = user.id
msg.save
unknown_dump = m.to[0].to_s.downcase
unknown_dump.sub!(/@.+$/, "")
LOG_HANDLE.info("UNKNOWN DUMP #{group ? "(IN GROUP) " : ""}#{unknown_dump} FROM #{user.nick}")
break
end
# check if user is member of dump
dump = IPDDump.load(IPDDump.id_dump(m.to[0].to_s))
unless dump.has_user?(user.id)
msg = IPDMessage.new
if group
msg.message_id = MSG_GROUP_NO_DUMP_MEMBER
noof_pictures -= 1
else
msg.message_id = MSG_NO_DUMP_MEMBER
end
msg.time_created = m.date.to_time.to_i
msg.id_user = user.id
msg.save
LOG_HANDLE.info("FORBIDDEN DUMP #{group ? "(IN GROUP) " : ""}#{dump.alias} FROM #{user.nick}")
break
end
# check for duplicate pictures
# TODO
# better use dump.has_picture?
picture_hash = Digest::RMD160::hexdigest(attachment.body.encoded)
# TODO
# can #id_dump be removed?
#id_dump = IPDDump.id_dump(m.to[0].to_s)
result = DB_HANDLE.execute("SELECT id FROM \"#{dump.id}\" WHERE original_hash = ?", [picture_hash])
if result.any?
msg = IPDMessage.new
if group
msg.message_id = MSG_GROUP_DUPLICATE_PICTURE
noof_pictures -= 1
else
msg.message_id = MSG_DUPLICATE_PICTURE
end
msg.time_created = m.date.to_time.to_i
msg.id_user = user.id
msg.save
LOG_HANDLE.info("DUPLICATE PICTURE #{group ? "(IN GROUP) " : ""}FROM #{user.nick} ORIGINAL ID #{result[0][0]} DUMP #{dump.alias}")
break
end
if group
LOG_HANDLE.info("SENDER #{email}") if noof_pictures == 1
else
LOG_HANDLE.info("SENDER #{email}")
end
# generate unique filename
now = Time.now
filename = now.to_f.to_s + File.extname(attachment.filename)
path = Time.new(now.year, now.month, now.day).to_i.to_s
picture = IPDPicture.new
# NOTE
# we use #id as "is part of group tag"
picture.id = noof_pictures if group
picture.filename = filename
picture.time_sent = m.date.to_time.to_i
picture.id_user = user.id
picture.original_hash = picture_hash
picture.id_dump = IPDDump.id_dump(m.to[0].to_s) if IPDDump.exists?(m.to[0].to_s)
picture.path = path
picstack.push(picture)
begin
File.open(TMP_DIR + "/" + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
rescue Exception => e
LOG_HANDLE.fatal("FILE SAVE ERROR #{user.email.to_s} / #{attachment.filename} / #{filename} / #{e.message} / #{e.backtrace.shift}")
end
end
# 3 pictures are OK in "group" mails
if group
break if noof_pictures == 3
# 1 otherwise
else
break
end
end
if group and noof_pictures < 3
noof_pictures.times { picstack.pop }
end
end
##############################
# process pictures
groupstack = []
picstack.each do |picture|
container = Magick::ImageList.new
container.from_blob IO.read(TMP_DIR + "/" + picture.filename)
container.each do |img|
# autoorient
img.auto_orient!
# read EXIF DateTime
# EXIF DateTime is local time w/o time zone information
# DateTime works for Android and iPhones
date = img.get_exif_by_entry('DateTime')[0][1]
# DateTimeOriginal is needed by Microsoft Lumia
unless date
date = img.get_exif_by_entry('DateTimeOriginal')[0][1]
end
if date =~ /^\d{4}:\d\d:\d\d \d\d:\d\d:\d\d$/
# CAUTION
# DateTime.to_time applies the local time zone
# so we subtract the time zone offset from it
time_taken = DateTime.strptime(date, '%Y:%m:%d %H:%M:%S').to_time
picture.time_taken = time_taken.to_i - time_taken.gmt_offset
end
# resize
if img.columns >= img.rows and img.columns > PICTURE_MAX_HORZ_SIZE
resize = PICTURE_MAX_HORZ_SIZE
elsif img.columns < img.rows and img.rows > PICTURE_MAX_VERT_SIZE
resize = PICTURE_MAX_VERT_SIZE
end
img.resize_to_fit!(resize) if resize
break unless picture.filename =~ /\.gif$/i
end
begin
unless Dir.exists?(PICTURE_DIR + "/" + picture.path)
Dir.mkdir(PICTURE_DIR + "/" + picture.path)
LOG_HANDLE.info("NEW DAY DIR #{picture.path}")
end
File.open(PICTURE_DIR + "/" + picture.path + "/" + picture.filename, 'wb') { |f| f.write container.to_blob }
rescue Exception => e
LOG_HANDLE.fatal("FILE COPY ERROR #{picture.filename} / #{e.message} / #{e.backtrace.shift}")
raise
end
# seems ok, so insert into db
if picture.id != 0
groupstack << picture
# remove fake id (see above)
picture.id = 0
end
picture.save
# TODO
# change quantize to use id
# quantize
Stalker.enqueue("picture.quantize", :filename => picture.filename)
# delete tmp files
begin
File.unlink(TMP_DIR + "/" + picture.filename)
rescue Exception => e
LOG_HANDLE.fatal("FILE DELETE ERROR #{picture.filename} / #{e.message} / #{e.backtrace.shift}")
raise
end
LOG_HANDLE.info("NEW GROUP") if groupstack.size == 1
LOG_HANDLE.info("ADD PICTURE #{picture.filename} DUMP #{picture.id_dump}")
end
# build group relationship
i = 0
while i < groupstack.size
groupstack[i].successor = groupstack[i + 1].id
groupstack[i].save
groupstack[i + 1].precursor = groupstack[i].id
groupstack[i + 1].successor = groupstack[i + 2].id
groupstack[i + 1].save
groupstack[i + 2].precursor = groupstack[i + 1].id
groupstack[i + 2].save
i += 3
end