static int TlenMUCSendQuery(TlenProtocol *proto, int type, const char *parent, int page) { if (!proto->isOnline) { return 1; } if (type == 3) { // find chat room by name char serialId[32]; TLEN_LIST_ITEM *item; mir_snprintf(serialId, SIZEOF(serialId), TLEN_IQID"%d", TlenSerialNext(proto)); item = TlenListAdd(proto, LIST_SEARCH, serialId); item->roomName = mir_strdup(parent); TlenSend(proto, "<iq to='c' type='3' n='%s' id='%s'/>", parent, serialId); } else { if (parent == NULL) { TlenSend(proto, "<iq to='c' type='%d'/>", type); } else { // 1 - groups, 2 - chat rooms, 7 - user nicks, 8 - user rooms if (type == 1 || (type == 2 && page == 0) || type == 7 || type == 8) { TlenSend(proto, "<iq to='c' type='%d' p='%s'/>", type, parent); } else if (type == 2) { TlenSend(proto, "<iq to='c' type='%d' p='%s' n='%d'/>", type, parent, page); } else if (type == 6) { if (page) { TlenSend(proto, "<iq to='c' type='%d' n='%s' k='u'/>", type, parent); } else { TlenSend(proto, "<iq to='c' type='%d' n='%s'/>", type, parent); } } else if (type == 4) { // list of users, admins etc. TlenSend(proto, "<iq to='%s' type='%d' k='%d'/>", parent, type, page); } } } return 0; }
INT_PTR TlenProtocol::MUCContactMenuHandleMUC(WPARAM wParam, LPARAM lParam) { MCONTACT hContact; DBVARIANT dbv; TLEN_LIST_ITEM *item; if (!isOnline) return 1; if ((hContact=wParam) != NULL && isOnline) { if (!db_get(hContact, m_szModuleName, "jid", &dbv)) { char serialId[32]; mir_snprintf(serialId, SIZEOF(serialId), TLEN_IQID"%d", TlenSerialNext(this)); item = TlenListAdd(this, LIST_INVITATIONS, serialId); item->nick = mir_strdup(dbv.pszVal); TlenSend(this, "<p to='c' tp='c' id='%s'/>", serialId); db_free(&dbv); } } return 0; }
void TlenResultSetRoster(TlenProtocol *proto, XmlNode *queryNode) { DBVARIANT dbv; XmlNode *itemNode, *groupNode; TLEN_LIST_ITEM *item; MCONTACT hContact; char *jid, *name, *nick; int i; char *str; for (i=0; i<queryNode->numChild; i++) { itemNode = queryNode->child[i]; if (!strcmp(itemNode->name, "item")) { if ((jid=TlenXmlGetAttrValue(itemNode, "jid")) != NULL) { str = TlenXmlGetAttrValue(itemNode, "subscription"); if (!strcmp(str, "remove")) { if ((hContact = TlenHContactFromJID(proto, jid)) != NULL) { if (db_get_w(hContact, proto->m_szModuleName, "Status", ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE) db_set_w(hContact, proto->m_szModuleName, "Status", ID_STATUS_OFFLINE); } TlenListRemove(proto, LIST_ROSTER, jid); } else { item = TlenListAdd(proto, LIST_ROSTER, jid); if (item != NULL) { if (str == NULL) item->subscription = SUB_NONE; else if (!strcmp(str, "both")) item->subscription = SUB_BOTH; else if (!strcmp(str, "to")) item->subscription = SUB_TO; else if (!strcmp(str, "from")) item->subscription = SUB_FROM; else item->subscription = SUB_NONE; if ((name=TlenXmlGetAttrValue(itemNode, "name")) != NULL) { nick = TlenTextDecode(name); } else { nick = TlenLocalNickFromJID(jid); } if (nick != NULL) { if (item->nick) mir_free(item->nick); item->nick = nick; if ((hContact=TlenHContactFromJID(proto, jid)) == NULL) { // Received roster has a new JID. // Add the jid (with empty resource) to Miranda contact list. hContact = TlenDBCreateContact(proto, jid, nick, FALSE); } db_set_s(hContact, "CList", "MyHandle", nick); if (item->group) mir_free(item->group); if ((groupNode=TlenXmlGetChild(itemNode, "group")) != NULL && groupNode->text != NULL) { item->group = TlenGroupDecode(groupNode->text); Clist_CreateGroup(0, _A2T(item->group)); // Don't set group again if already correct, or Miranda may show wrong group count in some case if (!db_get(hContact, "CList", "Group", &dbv)) { if (strcmp(dbv.pszVal, item->group)) db_set_s(hContact, "CList", "Group", item->group); db_free(&dbv); } else db_set_s(hContact, "CList", "Group", item->group); } else { item->group = NULL; db_unset(hContact, "CList", "Group"); } } } } } } } }
void TlenIqResultRoster(TlenProtocol *proto, XmlNode *iqNode) { XmlNode *queryNode; char *type; char *str; // RECVED: roster information // ACTION: populate LIST_ROSTER and create contact for any new rosters if ((type=TlenXmlGetAttrValue(iqNode, "type")) == NULL) return; if ((queryNode=TlenXmlGetChild(iqNode, "query")) == NULL) return; if (!strcmp(type, "result")) { str = TlenXmlGetAttrValue(queryNode, "xmlns"); if (str != NULL && !strcmp(str, "jabber:iq:roster")) { DBVARIANT dbv; XmlNode *itemNode, *groupNode; TLEN_SUBSCRIPTION sub; TLEN_LIST_ITEM *item; char *jid, *name, *nick; int i, oldStatus; for (i=0; i<queryNode->numChild; i++) { itemNode = queryNode->child[i]; if (!strcmp(itemNode->name, "item")) { str = TlenXmlGetAttrValue(itemNode, "subscription"); if (str == NULL) sub = SUB_NONE; else if (!strcmp(str, "both")) sub = SUB_BOTH; else if (!strcmp(str, "to")) sub = SUB_TO; else if (!strcmp(str, "from")) sub = SUB_FROM; else sub = SUB_NONE; //if (str != NULL && (!strcmp(str, "to") || !strcmp(str, "both"))) { if ((jid=TlenXmlGetAttrValue(itemNode, "jid")) != NULL) { if ((name=TlenXmlGetAttrValue(itemNode, "name")) != NULL) nick = TlenTextDecode(name); else nick = TlenLocalNickFromJID(jid); if (nick != NULL) { MCONTACT hContact; item = TlenListAdd(proto, LIST_ROSTER, jid); if (item->nick) mir_free(item->nick); item->nick = nick; item->subscription = sub; if ((hContact=TlenHContactFromJID(proto, jid)) == NULL) { // Received roster has a new JID. // Add the jid (with empty resource) to Miranda contact list. hContact = TlenDBCreateContact(proto, jid, nick, FALSE); } db_set_s(hContact, "CList", "MyHandle", nick); if (item->group) mir_free(item->group); if ((groupNode=TlenXmlGetChild(itemNode, "group")) != NULL && groupNode->text != NULL) { item->group = TlenGroupDecode(groupNode->text); Clist_CreateGroup(0, _A2T(item->group)); // Don't set group again if already correct, or Miranda may show wrong group count in some case if (!db_get(hContact, "CList", "Group", &dbv)) { if (strcmp(dbv.pszVal, item->group)) db_set_s(hContact, "CList", "Group", item->group); db_free(&dbv); } else db_set_s(hContact, "CList", "Group", item->group); } else { item->group = NULL; db_unset(hContact, "CList", "Group"); } if (!db_get(hContact, proto->m_szModuleName, "AvatarHash", &dbv)) { if (item->avatarHash) mir_free(item->avatarHash); item->avatarHash = mir_strdup(dbv.pszVal); proto->debugLogA("Setting hash [%s] = %s", nick, item->avatarHash); db_free(&dbv); } item->avatarFormat = db_get_dw(hContact, proto->m_szModuleName, "AvatarFormat", PA_FORMAT_UNKNOWN); } } } } // Delete orphaned contacts (if roster sync is enabled) if (db_get_b(NULL, proto->m_szModuleName, "RosterSync", FALSE) == TRUE) { for (MCONTACT hContact = db_find_first(proto->m_szModuleName); hContact; ) { MCONTACT hNext = hContact = db_find_next(hContact, proto->m_szModuleName); ptrA jid( db_get_sa(hContact, proto->m_szModuleName, "jid")); if (jid != NULL) { if (!TlenListExist(proto, LIST_ROSTER, jid)) { proto->debugLogA("Syncing roster: deleting 0x%x", hContact); CallService(MS_DB_CONTACT_DELETE, hContact, 0); } } hContact = hNext; } } CLISTMENUITEM mi = { sizeof(mi) }; mi.flags = CMIM_FLAGS; Menu_ModifyItem(proto->hMenuMUC, &mi); if (proto->hMenuChats != NULL) Menu_ModifyItem(proto->hMenuChats, &mi); proto->isOnline = TRUE; proto->debugLogA("Status changed via THREADSTART"); oldStatus = proto->m_iStatus; TlenSendPresence(proto, proto->m_iDesiredStatus); ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, proto->m_iStatus); } } }
BOOL SendPicture(TlenProtocol *proto, MCONTACT hContact) { DBVARIANT dbv; if (!db_get(hContact, proto->m_szModuleName, "jid", &dbv)) { char *jid = dbv.pszVal; char szFilter[512]; char *szFileName = (char*) mir_alloc(_MAX_PATH); OPENFILENAMEA ofn = {0}; CallService(MS_UTILS_GETBITMAPFILTERSTRINGS, ( WPARAM ) sizeof( szFilter ), ( LPARAM )szFilter ); ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400; ofn.hwndOwner = NULL; ofn.lpstrFilter = szFilter; ofn.lpstrCustomFilter = NULL; ofn.lpstrFile = szFileName; ofn.nMaxFile = _MAX_PATH; ofn.Flags = OFN_FILEMUSTEXIST; szFileName[0] = '\0'; if ( GetOpenFileNameA( &ofn )) { long size; FILE* fp = fopen( szFileName, "rb" ); if (fp) { fseek(fp, 0, SEEK_END); size = ftell(fp); if (size > 0 && size < 256*1024) { TLEN_LIST_ITEM *item; mir_sha1_ctx sha; DWORD digest[5]; int i; char idStr[10]; char fileBuffer[2048]; int id = TlenSerialNext(proto); mir_snprintf(idStr, sizeof(idStr), "%d", id); item = TlenListAdd(proto, LIST_PICTURE, idStr); item->ft = TlenFileCreateFT(proto, jid); item->ft->files = (char **) mir_alloc(sizeof(char *)); item->ft->filesSize = (long *) mir_alloc(sizeof(long)); item->ft->files[0] = szFileName; item->ft->filesSize[0] = size; item->ft->fileTotalSize = size; fseek(fp, 0, SEEK_SET); mir_sha1_init( &sha ); for (i = item->ft->filesSize[0]; i > 0; ) { int toread = min(2048, i); int readcount = (int)fread(fileBuffer, (size_t)1, (size_t)toread, fp); i -= readcount; if (readcount > 0) { mir_sha1_append( &sha, (BYTE* )fileBuffer, readcount); } if (toread != readcount) { break; } } mir_sha1_finish( &sha, (BYTE* )digest ); TlenSend(proto, "<message type='pic' to='%s' crc='%08x%08x%08x%08x%08x' idt='%s' size='%d' ext='%s'/>", jid, (int)htonl(digest[0]), (int)htonl(digest[1]), (int)htonl(digest[2]), (int)htonl(digest[3]), (int)htonl(digest[4]), idStr, item->ft->filesSize[0], "jpg"); } else { /* file too big */ } fclose(fp); } } db_free(&dbv); } return FALSE; }
void TlenProcessPic(XmlNode *node, TlenProtocol *proto) { TLEN_LIST_ITEM *item = NULL; char *crc, *crc_c, *idt, *size, *from, *fromRaw, *rt; from = TlenXmlGetAttrValue(node, "from"); fromRaw = TlenLoginFromJID(from); idt = TlenXmlGetAttrValue(node, "idt"); size = TlenXmlGetAttrValue(node, "size"); crc_c = TlenXmlGetAttrValue(node, "crc_c"); crc = TlenXmlGetAttrValue(node, "crc"); rt = TlenXmlGetAttrValue(node, "rt"); if (idt != NULL) { item = TlenListGetItemPtr(proto, LIST_PICTURE, idt); } if (item != NULL) { if (!strcmp(from, "ps")) { char *st = TlenXmlGetAttrValue(node, "st"); if (st != NULL) { item->ft->iqId = mir_strdup(st); item->ft->id2 = mir_strdup(rt); if (item->ft->hFileEvent != NULL) { SetEvent(item->ft->hFileEvent); item->ft->hFileEvent = NULL; } } } else if (!strcmp(item->ft->jid, fromRaw)) { if (crc_c != NULL) { if (!strcmp(crc_c, "n")) { /* crc_c = n, picture transfer accepted */ TlenPsPost(proto, item); } else if (!strcmp(crc_c, "f")) { /* crc_c = f, picture cached, no need to transfer again */ LogPictureMessage(proto, item->ft->jid, item->ft->files[0], TRUE); TlenP2PFreeFileTransfer(item->ft); TlenListRemove(proto, LIST_PICTURE, idt); } } else if (rt != NULL) { item->ft->id2 = mir_strdup(rt); TlenPsGet(proto, item); } } } else if (crc != NULL) { BOOL bAccept = proto->tlenOptions.imagePolicy == TLEN_IMAGES_ACCEPT_ALL || (proto->tlenOptions.imagePolicy == TLEN_IMAGES_IGNORE_NIR && IsAuthorized(proto, from)); if (bAccept) { FILE* fp; char fileName[MAX_PATH]; char *ext = TlenXmlGetAttrValue(node, "ext"); char *tmpPath = Utils_ReplaceVars( "%miranda_userdata%" ); int tPathLen = mir_snprintf(fileName, MAX_PATH, "%s\\Images\\Tlen", tmpPath); long oldSize = 0, lSize = atol(size); DWORD dwAttributes = GetFileAttributesA( fileName ); if ( dwAttributes == 0xffffffff || ( dwAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 ) CreateDirectoryTree(fileName); mir_free(tmpPath); fileName[ tPathLen++ ] = '\\'; mir_snprintf( fileName + tPathLen, MAX_PATH - tPathLen, "%s.%s", crc, ext ); fp = fopen( fileName, "rb" ); if (fp) { fseek(fp, 0, SEEK_END); oldSize = ftell(fp); fclose(fp); } if (oldSize != lSize) { item = TlenListAdd(proto, LIST_PICTURE, idt); item->ft = TlenFileCreateFT(proto, from); item->ft->files = (char **) mir_alloc(sizeof(char *)); item->ft->filesSize = (long *) mir_alloc(sizeof(long)); item->ft->files[0] = mir_strdup(fileName); item->ft->filesSize[0] = lSize; item->ft->fileTotalSize = item->ft->filesSize[0]; TlenSend(proto, "<message type='pic' to='%s' crc_c='n' idt='%s'/>", from, idt); } else { TlenSend(proto, "<message type='pic' to='%s' crc_c='f' idt='%s'/>", from, idt); LogPictureMessage(proto, from, fileName, FALSE); } } } mir_free(fromRaw); }
void __cdecl TlenProcessP2P(XmlNode *node, ThreadData *info) { XmlNode *queryNode; TLEN_LIST_ITEM *item; char *from; if (info == NULL) return; queryNode = TlenXmlGetChild(node, "query"); if ((from=TlenXmlGetAttrValue(node, "from")) != NULL) { XmlNode *fs , *vs, *dcng, *dc; /* file send */ fs = TlenXmlGetChild(queryNode, "fs"); /* voice send */ vs = TlenXmlGetChild(queryNode, "vs"); dcng = TlenXmlGetChild(queryNode, "dcng"); dc = TlenXmlGetChild(queryNode, "dc"); if (fs != NULL) { char *e, *id; /* e - step in the process (starting with 1)*/ /* i - id of the file */ /* s - size of the file */ /* c - number of files */ /* v - ??? */ e = TlenXmlGetAttrValue(fs, "e"); id = TlenXmlGetAttrValue(fs, "i"); if (e != NULL) { if (!strcmp(e, "1")) { char *c, *s; TLEN_FILE_TRANSFER * ft = (TLEN_FILE_TRANSFER *) mir_alloc(sizeof(TLEN_FILE_TRANSFER)); memset(ft, 0, sizeof(TLEN_FILE_TRANSFER)); c = TlenXmlGetAttrValue(fs, "c"); s = TlenXmlGetAttrValue(fs, "s"); ft->jid = mir_strdup(from); ft->proto = info->proto; ft->hContact = TlenHContactFromJID(info->proto, from); ft->iqId = mir_strdup(id); ft->fileTotalSize = atoi(s); ft->newP2P = TRUE; if ((item=TlenListAdd(ft->proto, LIST_FILE, ft->iqId)) != NULL) { char fileInfo[128]; item->ft = ft; mir_snprintf(fileInfo, SIZEOF(fileInfo), "%s file(s), %s bytes", c, s); TCHAR* filenameT = mir_utf8decodeT((char*)fileInfo); PROTORECVFILET pre = {0}; pre.flags = PREF_TCHAR; pre.fileCount = 1; pre.timestamp = time(NULL); pre.tszDescription = filenameT; pre.ptszFiles = &filenameT; pre.lParam = (LPARAM)ft; ft->proto->debugLogA("sending chainrecv"); ProtoChainRecvFile(ft->hContact, &pre); mir_free(filenameT); } } else if (!strcmp(e, "3")) { /* transfer error */ } else if (!strcmp(e, "4")) { /* transfer denied */ } else if (!strcmp(e, "5")) { /* transfer accepted */ if ((item=TlenListGetItemPtr(info->proto, LIST_FILE, id)) != NULL) { item->id2 = mir_strdup("84273372"); item->ft->id2 = mir_strdup("84273372"); TlenSend(info->proto, "<iq to='%s'><query xmlns='p2p'><dcng n='file_send' k='5' v='2' s='1' i='%s' ck='o7a32V9n2UZYCWpBUhSbFw==' ks='16' iv='MhjWEj9WTsovrQc=o7a32V9n2UZYCWpBUhSbFw==' mi='%s'/></query></iq>", from, item->id2, id); } } } } else if (vs != NULL) { } else if (dcng != NULL) { char *s, *id, *id2; info->proto->debugLogA("DCNG"); s = TlenXmlGetAttrValue(dcng, "s"); id2 = TlenXmlGetAttrValue(dcng, "i"); id = TlenXmlGetAttrValue(dcng, "mi"); if (!strcmp(s, "1")) { /* Keys */ /* n - name (file_send) */ /* k - ??? */ /* v - ??? */ /* s - step */ /* i - id of the file */ /* ck - aes key */ /* ks - key size (in bytes) */ /* iv - aes initial vector */ /* mi - p2p connection id */ char *n, *k, *v, *ck, *iv; n = TlenXmlGetAttrValue(dcng, "n"); k = TlenXmlGetAttrValue(dcng, "k"); v = TlenXmlGetAttrValue(dcng, "v"); ck = TlenXmlGetAttrValue(dcng, "ck"); iv = TlenXmlGetAttrValue(dcng, "iv"); if (!strcmp(n, "file_send")) { if ((item=TlenListGetItemPtr(info->proto, LIST_FILE, id)) != NULL) { item->id2 = mir_strdup(id2); item->ft->id2 = mir_strdup(id2); TlenBindUDPSocket(item->ft); TlenSend(info->proto, "<iq to='%s'><query xmlns='p2p'><dcng la='%s' lp='%d' pa='%s' pp='%d' i='%s' v='2' k='5' s='2'/></query></iq>", item->ft->jid, item->ft->localName, item->ft->wLocalPort, item->ft->localName, item->ft->wLocalPort, item->ft->id2); } } } else if (!strcmp(s, "2")) { info->proto->debugLogA("step = 2"); info->proto->debugLogA("%s",from); info->proto->debugLogA("%s",id2); /* IP and port */ if ((item=TlenListFindItemPtrById2(info->proto, LIST_FILE, id2)) != NULL) { item->ft->hostName = mir_strdup(TlenXmlGetAttrValue(dcng, "pa")); item->ft->wPort = atoi(TlenXmlGetAttrValue(dcng, "pp")); TlenBindUDPSocket(item->ft); TlenSend(info->proto, "<iq to='%s'><query xmlns='p2p'><dcng la='%s' lp='%d' pa='%s' pp='%d' i='%s' k='5' s='4'/></query></iq>", item->ft->jid, item->ft->localName, item->ft->wLocalPort, item->ft->localName, item->ft->wLocalPort, item->ft->id2); forkthread((void (__cdecl *)(void*))TlenNewFileReceiveThread, 0, item->ft); forkthread((void (__cdecl *)(void*))TlenNewFileSendThread, 0, item->ft); } } else if (!strcmp(s, "4")) { /* IP and port */ if ((item=TlenListFindItemPtrById2(info->proto, LIST_FILE, id2)) != NULL) { info->proto->debugLogA("step = 4"); item->ft->hostName = mir_strdup(TlenXmlGetAttrValue(dcng, "pa")); item->ft->wPort = atoi(TlenXmlGetAttrValue(dcng, "pp")); forkthread((void (__cdecl *)(void*))TlenNewFileReceiveThread, 0, item->ft); } } } else if (dc != NULL) { } } }