Module: Messaging

Included in:
DrbDb
Defined in:
lib/DrbDB/Messaging.rb

Overview

this file is part of manqod manqod is distributed under the CDDL licence the owner of manqod is Dobai-Pataky Balint([email protected])

Instance Method Summary collapse

Instance Method Details

#delete_message(message_id, nick) ⇒ Object



70
71
72
73
# File 'lib/DrbDB/Messaging.rb', line 70

def delete_message(message_id,nick)
	admin.query("update messages set `state`='d' where id='#{message_id}'")
	send_check_messages(nick)
end

#messages(nick) ⇒ Object



66
67
68
# File 'lib/DrbDB/Messaging.rb', line 66

def messages(nick)
	admin.rows("select * from messages where recipient='#{nick}' and `state`!='d'")
end

#send_check_messages(nick) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/DrbDB/Messaging.rb', line 17

def send_check_messages(nick)
		main_server.connected_clients.each_key{|client| 
			begin
				c=String.new(client.to_s).split(/\@/)[0]
				client.rpc("Messaging.instance.check_messages") if c == nick
			rescue =>err
				eerror(err)
			end
		}
end

#send_message(nick, subject, body, sender = nil, sendmail = true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/DrbDB/Messaging.rb', line 6

def send_message(nick,subject,body,sender=nil,sendmail=true)
	nick=[nick] unless nick.class == Array
	Thread.new{
		nick.each{|n|
			admin.query("insert into messages (sender,recipient,subject,body,state,`date`) values('#{sender || "system"}','#{n}','#{admin.escape_string(subject)}','#{admin.escape_string(body)}','n',now())")
			send_check_messages(n)
		}
	}
	sendmail_to_nick(nick,subject,body) if sendmail
end

#sendmail_to_nick(nick, subject, body) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/DrbDB/Messaging.rb', line 28

def sendmail_to_nick(nick,subject,body)
	Thread.new{
		nick=[nick] unless nick.class == Array
		sent=0
		@users.each{|uid|
			user=@users.get_user(uid)
			nick.each{|n|
				if n.to_s == user["nick"].to_s && user["email"].to_s.length>3
					begin
						msg="From: manqod@#{Socket.gethostbyname(Socket.gethostname).first}\nTo: #{user['email']}\nContent-Type: text/plain; charset=UTF-8\nSubject: #{subject}\n#{body}"
						Net::SMTP.start('localhost'){ |smtp|
							smtp.send_message(msg, "manqod_server", user["email"])
							einfo("mail to #{user['email']} sent: #{subject}")
						}
						sent+=1
					rescue => err
						eerror("can't send error report via SMTP")
					end
				end
			}
		}
		einfo("sent total #{sent} emails to #{nick.inspect}")
	}
end

#set_message_read(message_id, nick) ⇒ Object



61
62
63
64
# File 'lib/DrbDB/Messaging.rb', line 61

def set_message_read(message_id,nick)
	admin.query("update messages set `state`='c' where id='#{message_id}'")
	send_check_messages(nick)
end

#unread_messages(nick) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/DrbDB/Messaging.rb', line 53

def unread_messages(nick)
	um=0
	if r=admin.qrow("select count(id) as cnt from messages where `state`='n' and recipient='#{nick}'")
		um=r["cnt"]
	end
	um
end