This repository has been archived by the owner on Oct 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchat.rb
executable file
·122 lines (104 loc) · 4.68 KB
/
chat.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
#!/usr/bin/env ruby
=begin
Application Name: Whiz-Chat
Version: 2.50
License: GPL V3.0
Developers: S.Sathianarayanan, S.Prasanna Venkadesh
Copyright 2010-2012 Prasanna Venkadesh & Sathianarayanan
=end
begin
require 'rubygems' #Rubygems gem
require 'colorize' #gem used to set colors to text and backgrounds
require 'xmpp4r-simple' #Simple XML Protocol for Jabber API
require "highline/import" #for password protection
require "mail_check" #for gmail access
require "Contacts"
system('clear')
system('espeak "Welcome. Dude" >/dev/null 2>&1')
puts "Whiz Chat Version - 2.50".colorize(:yellow)
puts "Developed by- S. Sathianarayanan (sathia2704@gmail.com)\nImproved by- S. Prasanna Venkadesh (prasmailme@gmail.com)\nGithub Repository: https://github.com/PrasannaVenkadesh/Whiz-Chat\n".colorize( :blue ).underline
print 'Enter your Gmail Username :'
username = gets.chomp #Get input from username for user-id
#gets password for mail account, ask from higline gem
password = ask("Enter your password :" ) { |p| p.echo = "*" }
def quit
#type 'bye' in terminal after logging in, you will be logged out.
system('reset')
puts "You are now Disconnected"
system('espeak "Bye Dude" >/dev/null 2>&1')
exit()
end
begin
puts "Want to Chat or check Mail?\nc - chat\nm - mail\nq - quit"
@opt = gets.chomp
if(@opt == 'm')
system('clear')
mailcheck = Check.new
mailcheck.do_check(username,password)
end
if(@opt == 'q')
quit
end
end while(@opt!='c')
login = Login.new
login.do_login(username,password)
puts "\n"
print 'To Username: '
@to_username = gets.chomp #prompt for userid to whom you want to chat with
puts "Connecting to jabber server.."
@jabber = Jabber::Simple.new(username+'@gmail.com',password) #using jabber api to connect with gmail account.
system('clear') #to clear the console screen to keep the screen clean.
system('espeak "Connected.." >/dev/null 2>&1')
puts "Do you want to set Status Message (y) or (n)"
@choic = gets.chomp
if(@choic == 'y')
puts 'Whats in your mind: '.colorize(:red)
temp = gets.chomp
@jabber.status(:chat,temp)
end
system('clear')
puts "You can now start chatting\nType bye to quit" #note you need to type 'bye' at terminal to quit the app.
@mess #a variable to get input message from you and also to quit from this chat.
#method to get input from you and to send to person you are in chat with.
def send
while @mess!="bye" do #repeat until the user want to quit
@mess = gets.chomp #to get input message from user to chat
#method to send the message of user to recipeint.
@jabber.deliver(@to_username, @mess)
sleep(1) #for multithreading
end
quit
end
#method to read and display the message from sender
def receive
#repeat until the user want to quit
while @mess!="bye" do
#method that reads the revceived message and puts in msg variable
@jabber.received_messages do |msg|
if msg.from.node+'@'+msg.from.domain == @to_username
puts "=============================================="
puts @to_username.colorize(:color => :black,:background => :yellow) +": " + msg.body.colorize(:green) #display message in screen
puts Time.now.to_s.colorize(:gray) #display the time of message received
system('espeak "Got message" > /dev/null 2>&1')
puts "**********************************************"
sleep(2) #for multithreading
end
end
end
#quit
end #end of receive method
t1=Thread.new { send() } #instance for sending thread
t2=Thread.new { receive() } #instance for receiving thread
t1.join #when encounters sleep, jumps to thread t2
t2.join #when encounters sleep, jumps to thread t1
#for first time users who have not installed the required gems, this method will be run only once
rescue LoadError
puts "Please execute the install.sh file before running this file.\nUse 'sudo sh install.sh' to execute"
rescue Jabber::ClientAuthenticationFailure
print "Invalid username / password\n".colorize(:red) + "Run again\n".colorize(:yellow)
rescue Net::IMAP::BadResponseError
print "Invalid username / password\n".colorize(:green) + "Run again\n".colorize(:yellow)
#To handle interrupts
rescue Interrupt
puts "\nAborted By User..."
end