/** * 用户登录验证 * * @return */ public SipcRequest createUserAuthRequest( SipcHeader wwwHeader, int presence, boolean isSupportedMutiConnection, StoreVersion version) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.REGISTER); Pattern pt = Pattern.compile( "Digest algorithm=\"SHA1-sess-v4\",nonce=\"(.*?)\",key=\"(.*?)\",signature=\"(.*?)\""); Matcher mc = pt.matcher(wwwHeader.getValue()); if (mc.matches()) { String passHex = PasswordEncrypterV4.encryptV4(this.user.getUserId(), this.user.getPassword()); AuthGeneratorV4 auth = new AuthGeneratorV4(); String aeskey = ConvertHelper.byte2HexStringWithoutSpace(user.getAesKey()); String response = auth.generate(mc.group(2), passHex, mc.group(1), aeskey); String authString = "Digest response=\"" + response + "\",algorithm=\"SHA1-sess-v4\""; req.addHeader(SipcHeader.AUTHORIZATION, authString); req.addHeader("AK", "ak-value"); String body = MessageTemplate.TMPL_USER_AUTH; body = body.replace("{machineCode}", FetionConfig.getString("fetion.sip.machine-code")); body = body.replace("{sid}", Integer.toString(this.user.getFetionId())); body = body.replace("{userId}", Integer.toString(this.user.getUserId())); body = body.replace("{presence}", Integer.toString(presence)); body = body.replace("{personalVersion}", Integer.toString(version.getPersonalVersion())); body = body.replace("{contactVersion}", Integer.toString(version.getContactVersion())); req.setBody(new SipcBody(body)); } else { throw new IllegalStateException("parse wwwHeader failed. wwwHeader=" + wwwHeader.getValue()); } return req; }
/** 群消息 */ public SipcRequest createSendGroupChatMessageRequest(String uri, String message) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.MESSAGE); req.addHeader(SipcHeader.CONTENT_TYPE, "text/html-fragment"); req.addHeader(SipcHeader.CONTENT_TYPE, "text/plain"); req.addHeader(SipcHeader.SUPPORTED, "SaveHistory"); req.addHeader(SipcHeader.TO, uri); req.setBody(new SipcBody(message)); return req; }
/** * 发送手机短消息 * * @param uri * @param m * @return */ public SipcRequest createSendSMSRequest(String uri, Message m) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.MESSAGE); req.addHeader(SipcHeader.TO, uri); req.addHeader(SipcHeader.EVENT, "SendCatSMS"); req.setBody(new SipcBody(m.getText())); return req; }
/** 注册聊天服务器 */ public SipcRequest createRegisterChatRequest(String ticket) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.REGISTER); req.addHeader(SipcHeader.AUTHORIZATION, "TICKS auth=\"" + ticket + "\""); req.addHeader(SipcHeader.SUPPORTED, "text/html-fragment"); // req.addHeader(SipcHeader.SUPPORTED,"multiparty"); // req.addHeader(SipcHeader.SUPPORTED,"nudge"); // req.addHeader(SipcHeader.SUPPORTED,"share-background"); // req.addHeader(SipcHeader.FIELD_SUPPORTED,"fetion-show"); return req; }
/** 发送在线消息 */ public SipcRequest createSendChatMessageRequest(String toUri, Message m) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.MESSAGE); req.addHeader(SipcHeader.TO, toUri); // req.addHeader(SipcHeader.CONTENT_TYPE, "text/plain");text/html-fragment req.addHeader(SipcHeader.CONTENT_TYPE, m.getType()); req.addHeader(SipcHeader.EVENT, "CatMsg"); req.setBody(new SipcBody(m.getContent())); return req; }
/** * 获取个人详细信息 * * @return */ public SipcRequest createGetPersonalInfoRequest() { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "GetPersonalInfo"); req.setBody(new SipcBody(MessageTemplate.TMPL_GET_PERSONAL_INFO)); return req; }
/** 确认会话收到请求 */ public SipcRequest createAckRequest(String uri) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.ACK); req.addHeader(SipcHeader.TO, uri); req.setNeedReplyTimes(0); return req; }
/** 订阅异步通知 */ public SipcRequest createSubscribeRequest() { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SUBSCRIBE); req.addHeader(SipcHeader.EVENT, "PresenceV4"); req.setBody(new SipcBody(MessageTemplate.TMPL_SUBSCRIBE)); return req; }
/** 建立新的分组 */ public SipcRequest createCreateCordRequest(String title) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "CreateBuddyList"); req.setBody(new SipcBody(MessageTemplate.TMPL_CREATE_CORD.replace("{title}", title))); return req; }
/** 订阅群通知 */ public SipcRequest createSubscribeGroupNotifyRequest(String uri) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SUBSCRIBE); req.addHeader(SipcHeader.EVENT, "PGPresence"); req.setBody(new SipcBody(MessageTemplate.TMPL_SUBSCRIBE_GROUP_NOPTIFY.replace("{uri}", uri))); return req; }
/** 群在线请求 */ public SipcRequest createGroupKeepLiveRequest(String uri) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.INFO); req.addHeader(SipcHeader.TO, uri); req.setBody(new SipcBody(MessageTemplate.TMPL_GROUP_KEEP_LIVE)); return req; }
/** * 创建默认的SipcRequest * * @param m * @return */ public SipcRequest createDefaultSipcRequest(String m) { SipcRequest req = new SipcRequest(m, this.user.getDomain()); req.addHeader(SipcHeader.FROM, Integer.toString(this.user.getFetionId())); if (m.equals(this.lastMethod)) { req.addHeader(SipcHeader.CALLID, Integer.toString(this.globalCallId)); req.addHeader(SipcHeader.SEQUENCE, Integer.toString(this.getNextSequence()) + " " + m); } else { req.addHeader(SipcHeader.CALLID, Integer.toString(this.getNextCallID())); req.addHeader(SipcHeader.SEQUENCE, "1 " + m); this.gloalSequence = 1; } req.setAliveTime( (int) (FetionConfig.getInteger("fetion.sip.default-alive-time") + System.currentTimeMillis() / 1000)); this.lastMethod = m; return req; }
/** 将好友从黑名单移除 */ public SipcRequest createRemoveBuddyFromBlackList(int userId) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "RemoveFromBlacklistV4"); req.setBody( new SipcBody( MessageTemplate.TMPL_REMOVE_FROM_BLACKLIST.replace( "{userId}", Integer.toString(userId)))); return req; }
/** 删除分组 */ public SipcRequest createDeleteCordRequest(int cordId) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "DeleteBuddyList"); req.setBody( new SipcBody( MessageTemplate.TMPL_DELETE_CORD.replace("{cordId}", Integer.toString(cordId)))); return req; }
/** * 拒绝陌生人添加好友请求 * * @param userId * @return */ public SipcRequest createDeclineApplicationRequest(int userId) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); String body = MessageTemplate.TMPL_APPLICATION_DECLINED; body = body.replace("{userId}", Integer.toString(userId)); req.addHeader(SipcHeader.EVENT, "HandleContactRequestV4"); req.setBody(new SipcBody(body)); return req; }
/** 更新分组标题 */ public SipcRequest createSetCordTitleRequest(int cordId, String title) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "SetBuddyListInfo"); String body = MessageTemplate.TMPL_UPDATE_CORD; body = body.replace("{cordId}", Integer.toString(cordId)); body = body.replace("{title}", title); req.setBody(new SipcBody(body)); return req; }
/** 邀请好友加入会话 */ public SipcRequest createInvateBuddyRequest(String uri) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); String body = MessageTemplate.TMPL_INVATE_BUDDY; body = body.replace("{uri}", uri); req.addHeader(SipcHeader.EVENT, "InviteBuddy"); req.setBody(new SipcBody(body)); return req; }
/** 获取定时短信列表 */ public SipcRequest createGetScheduleSMSListRequest(int localVersion) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "SSGetScheduleSmsList"); String body = MessageTemplate.TMPL_GET_SCHEDULE_SMS_LIST; body = body.replace("{version}", Integer.toString(localVersion)); req.setBody(new SipcBody(body)); return req; }
/** * 删除好友 * * @param uri * @return */ public SipcRequest createDeleteBuddyRequest(int userId) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); String body = MessageTemplate.TMPL_DELETE_BUDDY; body = body.replace("{userId}", Integer.toString(userId)); req.addHeader(SipcHeader.EVENT, "DeleteBuddyV4"); req.setBody(new SipcBody(body)); return req; }
/** 设置群状态 */ public SipcRequest createSetGroupPresenceRequest(String uri, int presense) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "PGSetPresence"); String body = MessageTemplate.TMPL_GROUP_SET_PRESENCE; body = body.replace("{uri}", uri); body = body.replace("{presence}", Integer.toString(presense)); req.setBody(new SipcBody(body)); return req; }
/** * 获取联系人详细信息 * * @param buddyList * @return */ public SipcRequest createGetContactInfoRequest(String uri) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); String body = MessageTemplate.TMPL_GET_CONTACT_INFO; body = body.replace("{args}", " uri=\"" + uri + "\" version=\"0\" "); req.setBody(new SipcBody(body)); req.addHeader(SipcHeader.EVENT, "GetContactInfoV4"); return req; }
/** 获取群列表 */ public SipcRequest createGetGroupListRequest(int localVersion) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "PGGetGroupList"); req.setBody( new SipcBody( MessageTemplate.TMPL_GET_GROUP_LIST.replace( "{version}", Integer.toString(localVersion)))); return req; }
/** * 设置好友本地姓名 * * @param userId 好友用户ID * @param localName 本地显示名字 * @return */ public SipcRequest createSetBuddyLocalName(int userId, String localName) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); String body = MessageTemplate.TMPL_SET_BUDDY_LOCAL_NAME; body = body.replace("{userId}", Integer.toString(userId)); body = body.replace("{localName}", localName); req.addHeader(SipcHeader.EVENT, "SetContactInfoV4"); req.setBody(new SipcBody(body)); return req; }
/** * 设置好友分组信息 * * @param uri * @param cordId * @return */ public SipcRequest createSetBuddyCord(int userId, String cordId) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); String body = MessageTemplate.TMPL_SET_BUDDY_CORD; body = body.replace("{userId}", Integer.toString(userId)); body = body.replace("{cordId}", cordId != null ? cordId : ""); req.addHeader(SipcHeader.EVENT, "SetContactInfoV4"); req.setBody(new SipcBody(body)); return req; }
/** 开始群会话 */ public SipcRequest createInviteRequest(String uri, Port localPort) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.INVATE); req.addHeader(SipcHeader.TO, uri); req.addHeader(SipcHeader.SUPPORTED, "text/html-fragment"); // req.addHeader(SipcHeader.SUPPORTED, "text/plain"); req.addHeader(SipcHeader.SUPPORTED, "multiparty"); req.addHeader(SipcHeader.SUPPORTED, "nudge"); req.addHeader(SipcHeader.SUPPORTED, "share-background"); req.addHeader(SipcHeader.SUPPORTED, "fetion-show"); req.setNeedReplyTimes(2); // 需回复两次 // 正文是一些固定的参数 StringBuffer buffer = new StringBuffer(); buffer.append("v=0\r\n"); buffer.append("o=-0 0 IN " + localPort.toString() + "\r\n"); buffer.append("s=session\r\n"); buffer.append("c=IN IP4 " + localPort.toString() + "\r\n"); buffer.append("t=0 0\r\n"); buffer.append("m=message " + Integer.toString(localPort.getPort()) + " sip " + uri); req.setBody(new SipcBody(buffer.toString())); return req; }
/** * 添加飞信好友请求 * * @param uri * @param promptId * @param cordId * @param desc * @return */ public SipcRequest createAddBuddyRequest( String uri, int promptId, int cordId, String desc, String localName) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); String body = MessageTemplate.TMPL_ADD_BUDDY; body = body.replace("{uri}", uri); body = body.replace("{promptId}", Integer.toString(promptId)); body = body.replace("{cordId}", cordId == -1 ? "" : Integer.toString(cordId)); body = body.replace("{desc}", StringHelper.qouteHtmlSpecialChars(desc)); body = body.replace("{localName}", localName != null ? "local-name=\"" + localName + "\"" : ""); req.addHeader(SipcHeader.EVENT, "AddBuddyV4"); req.setBody(new SipcBody(body)); return req; }
/** * 更改个人资料 这里只支持更改昵称和个性签名 * * @param updateXML 更新的XML * @return */ public SipcRequest createSetPersonalInfoRequest() { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); NodeBuilder builder = new NodeBuilder(); // 因为用户可以改变自己的信息,这里权限改变了所以不使用BeanHelper来处理 builder.add("nickname", StringHelper.qouteHtmlSpecialChars(user.getNickName())); builder.add("impresa", StringHelper.qouteHtmlSpecialChars(user.getImpresa())); // 用户扩展信息。.TODO .. // BeanHelper.toUpdateXML(BuddyExtend.class, this.client.getFetionUser(), builder); String body = MessageTemplate.TMPL_SET_PERSONAL_INFO; body = body.replace("{personal}", builder.toXML("personal")); req.addHeader(SipcHeader.EVENT, "SetUserInfoV4"); req.setBody(new SipcBody(body)); return req; }
/** 删除定时短信 */ public SipcRequest createDeleteScheduleSMSRequest(Collection<ScheduleSMS> scheduleSMSList) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "SSDeleteScheduleSms"); String body = MessageTemplate.TMPL_DELETE_SCHEDULE_SMS; String scheduleSMSTmpl = "<schedule-sms id=\"{id}\" />"; StringBuffer buffer = new StringBuffer(); Iterator<ScheduleSMS> it = scheduleSMSList.iterator(); while (it.hasNext()) { ScheduleSMS s = it.next(); buffer.append(scheduleSMSTmpl.replace("{id}", Integer.toString(s.getId()))); } body = body.replace("{scheduleSMSList}", buffer.toString()); req.setBody(new SipcBody(body)); return req; }
/** 创建定时短信 */ public SipcRequest createCreateScheduleSMSRequest( Date sendDate, Message message, Collection<Buddy> receiverList) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "SSSetScheduleCatSms"); String body = MessageTemplate.TMPL_CREATE_SCHEDULE_SMS; SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d H:m:s"); df.setTimeZone(TimeZone.getTimeZone("GMT 0")); body = body.replace("{sendDate}", df.format(sendDate)); body = body.replace("{message}", message.getText()); String receiverTmpl = "<receiver uri=\"{uri}\" />"; StringBuffer buffer = new StringBuffer(); Iterator<Buddy> it = receiverList.iterator(); while (it.hasNext()) { Buddy b = it.next(); buffer.append(receiverTmpl.replace("{uri}", b.getUri())); } body = body.replace("{receiverList}", buffer.toString()); req.setBody(new SipcBody(body)); return req; }
/** 将好友移到黑名单中 */ public SipcRequest createAddBuddyToBlackList(String uri) { SipcRequest req = this.createDefaultSipcRequest(SipcMethod.SERVICE); req.addHeader(SipcHeader.EVENT, "AddToBlacklist"); req.setBody(new SipcBody(MessageTemplate.TMPL_ADD_TO_BLACKLIST.replace("{uri}", uri))); return req; }