-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEnterPoint.java
executable file
·135 lines (115 loc) · 5.34 KB
/
EnterPoint.java
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
import java.io.IOException;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;
public class EnterPoint {
// Notice the username is NOT smack.test@gmail.com
private static String username = "technik7jobrobot@gmail.com";
private static String password = "robottechnik7";
public static void main( String[] args ) {
System.out.println("Starting IM client");
// gtalk requires this or your messages bounce back as errors
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(connConfig);
try {
connection.connect();
System.out.println("Connected to " + connection.getHost());
} catch (XMPPException ex) {
//ex.printStackTrace();
System.out.println("Failed to connect to " + connection.getHost());
System.exit(1);
}
try {
connection.login(username, password);
System.out.println("Logged in as " + connection.getUser());
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
} catch (XMPPException ex) {
//ex.printStackTrace();
// XMPPConnection only remember the username if login is succesful
// so we can''t use connection.getUser() unless we log in correctly
System.out.println("Failed to log in as " + username);
System.exit(1);
}
// ------------------------------------ this is direct connection
/* PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new MessageParrot(connection), filter);
if(args.length > 0) {
// google bounces back the default message types, you must use chat
Message msg = new Message(args[0], Message.Type.chat);
msg.setBody("Test");
connection.sendPacket(msg);
}else{
// google bounces back the default message types, you must use chat
Message msg = new Message("technik7job@gmail.com", Message.Type.chat);
msg.setBody("Test");
connection.sendPacket(msg);
}
System.out.println("Press enter to disconnect\n");
*/
// --------------------------------------------
ChatManager chatmanager = connection.getChatManager();
Chat newChat = chatmanager.createChat("technik7job@gmail.com", new MessageListener() {
public void processMessage(Chat chat, Message message) {
/*System.out.println("Received message: " + message);
System.out.println("Message Body: "+message.getBody());
System.out.println("Message Subject: "+message.getSubject());
System.out.println("Message From:"+message.getFrom());
System.out.println("Message Thread: "+message.getThread());
System.out.println("Message type:"+message.getType().toString());*/
try{
chat.sendMessage("server online");
}catch(Exception ex){};
}
});
try {
// ------------
newChat.sendMessage("Online!");
// ------------
/*Message newMessage=new Message();
newMessage.setBody("Howdy!");
newMessage.setProperty("favoriteColor", "red");
newChat.sendMessage(newMessage);*/
}
catch (XMPPException e) {
System.out.println("Error Delivering block");
}
try {
System.in.read();
} catch (IOException ex) {
//ex.printStackTrace();
}
Presence presence = new Presence(Presence.Type.unavailable);
connection.sendPacket(presence);
connection.disconnect();
System.out.println("END.");
}
}
/** listener for all chat's - listener for connection */
class MessageParrot implements PacketListener {
private XMPPConnection xmppConnection;
public MessageParrot(XMPPConnection conn) {
xmppConnection = conn;
}
public void processPacket(Packet packet) {
Message message = (Message)packet;
if(message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
System.out.println("Message from " + fromName + "\n" + message.getBody() + "\n");
Message reply = new Message();
reply.setTo(fromName);
reply.setBody("I am a Java bot. You said: " + message.getBody());
xmppConnection.sendPacket(reply);
}
}
};