When I first got jYMSG a few weeks ago, to create an aplication that I am working on, I noticed a few bugs. 1.) When someone added you as a friend, contactRequestReceived was not fired. 2.) There was no function for accepting said contact request. With modifications in Session.java and ServiceConstraints.java, and a packet sniffer, I was able to fix these errors.
ServiceConstraints.java
not much to do here. All you have to do is addthe constraint that identifies when an addFriend is recieved. Add this line allmost anywhere in the file:
public final static int SERVICE_FRIENDREQUEST = 214;
Session.java
A few functions were added. First of all, to get a friend request to fire the contactRequest, find void process(YMSG9Packet pkt), its near the bottom of the file, and within the switch, add:
case SERVICE_FRIENDREQUEST: receiveContactNew(pkt); break;
Then, find receiveContactNew(YMSG9Packet pkt), closer to the middle of the file, and change the contents of the else statemet to:
SessionEvent se = new SessionEvent
( this,
pkt.getValue("5"), // to
pkt.getValue("4"), // from
pkt.getValue("14"), // message
pkt.getValue("15") // timestamp
);
se.setStatus(pkt.status); // status!=0 means offline message
new FireEvent().fire(se,SERVICE_CONTACTNEW);
Now, every time someone requests you as a friend, it should fire ContactRequestRecieved with the appropreate values. Next lets move on to adding the friends. For this I added 2 functions. For the first function, find rejectContact(SessionEvent se, String msg) and below that function add:
public void acceptContact(SessionEvent se,String msg)
throws IllegalArgumentException,IllegalStateException,IOException
{ if(se.getFrom()==null || se.getTo()==null)
throw new IllegalArgumentException("Missing to or from field in event object.");
checkStatus(); transmitContactAccept(se.getFrom(),se.getTo(),msg);
}
Then, find the function transmitContactReject(String friend, String yid, String msg) and below that add:
protected void transmitContactAccept(String friend,String yid,String msg) throws IOException
{ PacketBodyBuffer body = new PacketBodyBuffer();
body.addElement("1" ,yid);
body.addElement("5" ,friend);
body.addElement("241","0");
body.addElement("13","1");
sendPacket(body,SERVICE_FRIENDREQUEST,(long)0);
System.out.println("sent contact accept");
}
This will allow you to accept their request.