1
|
package MyBot;
|
2
|
|
3
|
import java.text.SimpleDateFormat;
|
4
|
import java.util.Date;
|
5
|
import java.net.*;
|
6
|
import java.io.*;
|
7
|
|
8
|
import net.sf.jml.Email;
|
9
|
import net.sf.jml.MsnContact;
|
10
|
import net.sf.jml.MsnList;
|
11
|
import net.sf.jml.MsnMessenger;
|
12
|
import net.sf.jml.MsnProtocol;
|
13
|
import net.sf.jml.MsnSwitchboard;
|
14
|
import net.sf.jml.MsnUserStatus;
|
15
|
import net.sf.jml.event.MsnAdapter;
|
16
|
import net.sf.jml.impl.MsnContactImpl;
|
17
|
import net.sf.jml.impl.MsnMessengerFactory;
|
18
|
import net.sf.jml.message.MsnControlMessage;
|
19
|
import net.sf.jml.message.MsnDatacastMessage;
|
20
|
import net.sf.jml.message.MsnInstantMessage;
|
21
|
import net.sf.jml.message.MsnSystemMessage;
|
22
|
import net.sf.jml.message.MsnUnknownMessage;
|
23
|
import net.sf.jml.protocol.MsnOutgoingMessage;
|
24
|
|
25
|
|
26
|
|
27
|
|
28
|
|
29
|
|
30
|
|
31
|
public class ChatBot extends MsnAdapter {
|
32
|
private String email;
|
33
|
private String password;
|
34
|
private MsnMessenger messenger=null;
|
35
|
private int timeout=1000;
|
36
|
public static final int PORT = 8088;
|
37
|
SimpleDateFormat sdf = new SimpleDateFormat( " MM-dd HH:mm " );
|
38
|
PrintWriter out;
|
39
|
|
40
|
public ChatBot() throws IOException{
|
41
|
|
42
|
}
|
43
|
|
44
|
public static void main(String [] args)
|
45
|
throws java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException
|
46
|
{
|
47
|
String email,password;
|
48
|
ChatBot chatbot=(ChatBot) Class.forName( "MyBot.ChatBot" ).newInstance();
|
49
|
if(args.length==2){
|
50
|
email = args[0];
|
51
|
password = args[1];
|
52
|
}else{
|
53
|
email = "ask_answer2011@hotmail.com";
|
54
|
password = "chatbot";
|
55
|
}
|
56
|
chatbot.set(email,password);
|
57
|
chatbot.start();
|
58
|
}
|
59
|
|
60
|
|
61
|
public void start() {
|
62
|
messenger = MsnMessengerFactory.createMsnMessenger(email, password);
|
63
|
messenger.setSupportedProtocol(new MsnProtocol[]{MsnProtocol.MSNP12});
|
64
|
messenger.getOwner().setInitStatus(MsnUserStatus.ONLINE);
|
65
|
messenger.addListener( this );
|
66
|
messenger.login();
|
67
|
}
|
68
|
|
69
|
public void loginCompleted(MsnMessenger messenger){
|
70
|
System.out.println(messenger+sdf.format(new Date())+" login complete" );
|
71
|
messenger.getOwner().setDisplayName("I am bot");
|
72
|
String content= messenger+" login complete";
|
73
|
String fileName="login.log";
|
74
|
try{
|
75
|
writeFile(fileName,content);
|
76
|
}catch (Exception e){
|
77
|
e.printStackTrace();
|
78
|
System.out.println(content+"cannot be write to"+fileName);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
|
83
|
public void instantMessageReceived(MsnSwitchboard switchboard,
|
84
|
MsnInstantMessage message, MsnContact contact) {
|
85
|
System.out.println("message received");
|
86
|
|
87
|
String XMLstring;
|
88
|
XMLstring = XMLDisposal.XmlBuild("TVs", sdf.format(new Date()).toString(),
|
89
|
contact.getEmail().getEmailAddress().toString(), message.getContent().toString());
|
90
|
String emailstring=XMLDisposal.GetUser(XMLstring);
|
91
|
Email con=Email.parseStr(emailstring);
|
92
|
messenger.sendText(con, "I have login");
|
93
|
Socket socket=null;
|
94
|
try{
|
95
|
socket=new Socket("localhost",PORT);
|
96
|
System.out.println("socket:"+socket);
|
97
|
DataInputStream in = new DataInputStream(socket.getInputStream());
|
98
|
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
|
99
|
socket.setSoTimeout(timeout);
|
100
|
|
101
|
|
102
|
|
103
|
|
104
|
|
105
|
|
106
|
out.writeUTF(XMLstring);
|
107
|
String touser;
|
108
|
try{
|
109
|
touser = in.readUTF();
|
110
|
}catch (Exception e){
|
111
|
System.out.println(e);
|
112
|
touser = "problem occurs, please resend latter";
|
113
|
}
|
114
|
System.out.println(touser);
|
115
|
message.setContent(touser);
|
116
|
switchboard.sendMessage(message);
|
117
|
}catch(Exception e){
|
118
|
System.out.println("Exception"+e);
|
119
|
System.out.println("database cannot be connect,check it!");
|
120
|
String err = "problem occurs, please resend latter";
|
121
|
message.setContent(err);
|
122
|
switchboard.sendMessage(message);
|
123
|
}finally{
|
124
|
try{
|
125
|
socket.close();
|
126
|
}catch(Exception e){
|
127
|
System.out.println("Exception"+e);
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
|
132
|
|
133
|
public void set(String email, String password){
|
134
|
this.email=email;
|
135
|
this.password=password;
|
136
|
}
|
137
|
|
138
|
public void systemMessageReceived(MsnMessenger messenger,MsnSystemMessage message){
|
139
|
}
|
140
|
public void controlMessageReceived(MsnSwitchboard switchboard,MsnControlMessage message, MsnContact contact){
|
141
|
switchboard.sendMessage(message,false);
|
142
|
}
|
143
|
public void datacastMessageReceived(MsnSwitchboard switchboard,MsnDatacastMessage message, MsnContact friend){
|
144
|
switchboard.sendMessage(message,false);
|
145
|
}
|
146
|
public void unknownMessageReceived(MsnSwitchboard switchboard,MsnUnknownMessage message, MsnContact friend){
|
147
|
}
|
148
|
public void contactListInitCompleted(MsnMessenger messenger){
|
149
|
MsnContact [] mycontact = messenger.getContactList().getContactsInList(MsnList.AL);
|
150
|
String fileName="login.log";
|
151
|
String content = "I have "+ mycontact.length+ "contacts";
|
152
|
ListContact(messenger);
|
153
|
try{
|
154
|
writeFile(fileName,content);
|
155
|
}catch (Exception e){
|
156
|
e.printStackTrace();
|
157
|
System.out.println(content+"cannot be write to"+fileName);
|
158
|
}
|
159
|
}
|
160
|
public void ListContact(MsnMessenger messenger){
|
161
|
MsnContact [] mycontact = messenger.getContactList().getContactsInList(MsnList.AL);
|
162
|
for(int i=0;i<mycontact.length;i++){
|
163
|
String personal = mycontact[i].getDisplayName()+" is "+mycontact[i].getStatus() +
|
164
|
mycontact[i].getPersonalMessage();
|
165
|
System.out.println(personal);
|
166
|
}
|
167
|
}
|
168
|
public void contactListSyncCompleted(MsnMessenger messenger){
|
169
|
}
|
170
|
public void contactStatusChanged(MsnMessenger messenger,MsnContact friend){
|
171
|
}
|
172
|
public void ownerStatusChanged(MsnMessenger messenger){
|
173
|
}
|
174
|
public void contactAddedMe(MsnMessenger messenger, MsnContact friend){
|
175
|
String content= friend.getEmail() + " add " + messenger;
|
176
|
String fileName="contactAddedMe.log";
|
177
|
System.out.println(content);
|
178
|
messenger.addFriend(friend.getEmail(),friend.getFriendlyName());
|
179
|
try{
|
180
|
writeFile(fileName,content);
|
181
|
}catch (Exception e){
|
182
|
e.printStackTrace();
|
183
|
System.out.println(content+"cannot be write to"+fileName);
|
184
|
}
|
185
|
}
|
186
|
public void contactRemovedMe(MsnMessenger messenger, MsnContact friend){
|
187
|
String content= friend.getEmail() + " delete " + messenger;
|
188
|
String fileName="contactRemoveMe.log";
|
189
|
System.out.println(content);
|
190
|
try{
|
191
|
writeFile(fileName,content);
|
192
|
}catch (Exception e){
|
193
|
e.printStackTrace();
|
194
|
System.out.println(content+"cannot be write to"+fileName);
|
195
|
}
|
196
|
System.out.println(friend.getEmail() + " delete " + messenger);
|
197
|
messenger.removeFriend(friend.getEmail(),false);
|
198
|
}
|
199
|
public void writeFile(String fileName,String content) throws IOException
|
200
|
{
|
201
|
FileWriter fw = new FileWriter(fileName);
|
202
|
String time=sdf.format(new Date());
|
203
|
fw.write("at " + time + "\t"+content);
|
204
|
fw.close();
|
205
|
}
|
206
|
|
207
|
|
208
|
|
209
|
|
210
|
|
211
|
|
212
|
|
213
|
|
214
|
|
215
|
|
216
|
|
217
|
|
218
|
|
219
|
|
220
|
|
221
|
|
222
|
|
223
|
|
224
|
|
225
|
|
226
|
|
227
|
}
|