commit 6f9948063b8ea39b102ed843dd442bf10044a1da Author: User Kibab Date: Fri Sep 22 15:57:47 2023 +0200 Checked in the latest version of IG11 source code The code doesn't currently compile, at least on FreeBSD 13.2. But the protocol used to talk to ICQ servers is not in use anyway. So this code is more like a historical artifact than anything that can be used right now. This code lived under svn.boba.su/Sect. diff --git a/ChangeLog.txt b/ChangeLog.txt new file mode 100644 index 0000000..753ab85 --- /dev/null +++ b/ChangeLog.txt @@ -0,0 +1,96 @@ +Ëîã ïðåäûäóùèõ ðåâèçèé (SVN-ñåðâåð ) + + +------------------------------------------------------------------------ +r50 | Kibab | 2008-10-15 18:01:49 +0400 (ñð, 15 îêò 2008) | 4 lines + +[+] Makefile.non-secta äëÿ ñáîðêè ïóáëè÷íûõ âåðñèé. +  ïóáëè÷íîé âåðñèè îòñóòñòâóåò îáðàùåíèå ê , íå âåä¸òñÿ îòëîâ . + + +------------------------------------------------------------------------ +r49 | Kibab | 2008-10-15 13:57:49 +0400 (ñð, 15 îêò 2008) | 4 lines + +[*] Ñáîðêà ïðîõîäèò íà GNU/Linux 32-bit gcc 3.4.3. +[+] Äîáàâëåíà strip â Makefile.release. + + +------------------------------------------------------------------------ +r48 | Kibab | 2008-10-15 12:54:30 +0400 (ñð, 15 îêò 2008) | 3 lines + +- Óñòàíîâêà SO_NOSIGPIPE íà ñåðâåðíîì ñîêåòå è íà êëèåíòñêèõ. À òàêæå íà ñîêåòå ê ñåðâåðó AOL. + + +------------------------------------------------------------------------ +r47 | Kibab | 2008-10-15 11:31:57 +0400 (ñð, 15 îêò 2008) | 6 lines + +[+] Óñòàíîâêà òàêæå è SO_REUSEPORT â äîïîëíåíèå ê SO_REUSEADDR; +[+] Èñïîëüçîâàíèå ìüþòåêñîâ âìåñòî SysV-ñåìàôîðîâ - íå áóäåò îøèáîê, ñâÿçàííûõ ñ îòñóòñòâèåì ñåìàôîðîâ â ñèñòåìå; +[+] Äîáàâëåí îáðàáîò÷èê SIGPIPE äëÿ îòëîâà ñèãíàëîâ îò ì¸ðòâûõ ñîêåòîâ, â êîòîðûå ñåðâåð ïûòàåòñÿ ïèñàòü; +[+] Äîáàâëåí Makefile.release äëÿ êîìïèëÿöèè áåç àññåðòîâ è äåáàã-ïðèíòîâ. Òóäà íàäî åù¸ äîáàâèòü strip, ÷òî ñèëüíî óìåíüøèò ðàçìåð ýëüôà. + + +------------------------------------------------------------------------ +r46 | Kibab | 2008-10-13 13:05:51 +0400 (ïí, 13 îêò 2008) | 4 lines + +[+] Çàáûë íàïèñàòü - SO_REUSEADDR òåïåðü ðàáîòàåò; +[*] â Windows-âåðñèè âåðíóë óäàëåíèå êðèòè÷åñêîé ñåêöèè. + + +------------------------------------------------------------------------ +r45 | Kibab | 2008-10-13 13:00:57 +0400 (ïí, 13 îêò 2008) | 4 lines + +[+] Âûõîä èç öèêëà îáñëóæèâàíèÿ êëèåíòîâ ïî SIGINT (Ctrl-C); +[+] Ñèíõðîíèçàöèÿ ïîòîêîâ ïðè îáíîâëåíèè ñòàòèñòèêè (÷åðåç ñèñòåìíûå ñåìàôîðû). + + +------------------------------------------------------------------------ +r44 | Kibab | 2008-10-12 22:29:13 +0400 (âñ, 12 îêò 2008) | 8 lines + +- Ïðîåêò ñòàë ñîáèðàòüñÿ ïîä FreeBSD. Ðàáîòàåò íîðìàëüíî, íî: + * Íåò ñèíõðîíèçàöèè ïîòîêîâ ïðè äîñòóïå ê îáíîâëåíèþ ñòàòèñòèêè ïî êëèåíòàì - íàäî äîáàâèòü àíàëîã êðèòè÷åñêîé ñåêöèè; + * Ïî÷åìó-òî äà¸ò ñáîé ïîïûòêà SO_REUSEADDR; + * Íåîáõîäèì ðåôàêòîðèíã êîäà â main.cpp, ÷òîáû ìîæíî áûëî áîëåå óäîáíî ñëèòü ñ main_freebsd.cpp; + * Êîìïèëÿöèÿ ïîä Linux íå ãàðàíòèðóåòñÿ - ïîêà íå ïðîâåðÿë. +Êàê áû òî íè áûëî, íà÷àëî ïîëîæåíî. + + +------------------------------------------------------------------------ +r41 | _Rst7 | 2008-10-03 14:20:03 +0400 (ïò, 03 îêò 2008) | 1 line + +! â îòäåëüíûé ôàéë . Íà äàííûé ìîìåíò +------------------------------------------------------------------------ +r40 | _Rst7 | 2008-10-03 14:18:19 +0400 (ïò, 03 îêò 2008) | 1 line + +!íåáîëüøàÿ îïòèìèçàöèè send, êàê-òî ïî 2 TCP-ïàêåòà íà îäèí SNAC áóäåò ìíîãîâàòî... +------------------------------------------------------------------------ +r39 | _Rst7 | 2008-09-30 14:35:30 +0400 (âò, 30 ñåí 2008) | 1 line + +!Íåìíîãî íåóäà÷íûé ïðåäûäóùèé êîììèò +------------------------------------------------------------------------ +r38 | _Rst7 | 2008-09-30 14:33:27 +0400 (âò, 30 ñåí 2008) | 1 line + ++Ïåðåäåëàë ïðèåì îôôëàéí-ñîîáùåíèé. Òåïåðü íå òóïèò ;) +------------------------------------------------------------------------ +r37 | Kibab | 2008-08-13 13:23:11 +0400 (ñð, 13 àâã 2008) | 3 lines + +-[+] Äîáàâèë ìàëåíüêîå ïðèëîæåíüèöå, â êîòîðîì áóäó òåñòèòü êîìïèëÿöèþ ïîä *nix ðàçëè÷íûõ ìîäóëåé ;-) + + +------------------------------------------------------------------------ +r36 | _Rst7 | 2008-08-13 12:13:51 +0400 (ñð, 13 àâã 2008) | 1 line + +!Íåìíîãî çàìåíèë çíà÷åíèÿ òàéìàóòîâ äëÿ ðàáîòû íà çàãðóæåííîé ìàøèíå. +------------------------------------------------------------------------ +r34 | _Rst7 | 2008-03-18 13:39:47 +0300 (âò, 18 ìàð 2008) | 1 line + +!Ïðàâèëüíàÿ êîììàíäà äëÿ äîáàâëåíèÿ â áàäèëèñò +------------------------------------------------------------------------ +r13 | Kibab | 2007-10-24 12:57:30 +0400 (ñð, 24 îêò 2007) | 1 line + +[+] Initial commit of IG11 files. +------------------------------------------------------------------------ +r12 | Kibab | 2007-10-24 12:54:03 +0400 (ñð, 24 îêò 2007) | 1 line + + +------------------------------------------------------------------------ diff --git a/Makefile.release b/Makefile.release new file mode 100644 index 0000000..897011b --- /dev/null +++ b/Makefile.release @@ -0,0 +1,16 @@ +APP=ig11 +SRCS=statgen.cpp md5.cpp snaccache.cpp main_freebsd.cpp flap.cpp icqkid2.cpp +OBJS=${SRCS:.cpp=.o} + +CC=g++ +CXXFLAGS=-O2 -DNDEBUG +LDFLAGS= -pthread + +link: writerevision ${OBJS} + @${CC} -o ${APP} ${CXXFLAGS} ${OBJS} ${LDFLAGS} + @strip ${APP} +clean: + @rm -rf *.o ${APP} + +writerevision: + @echo "#define __SVN_REVISION__ \"`svnversion`\"" > revision.h diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..64e4b9d --- /dev/null +++ b/README.txt @@ -0,0 +1,14 @@ +Сервер NatICQ +(c) Rst7, BoBa, Kibab +===================== +ИÑ�пользование: + ./ig11 5050 путь_к_файлу_Ñ�татиÑ�тики +Ð’ директории Ñ� Ñ�ервером лежит файл stat_template.txt, в который необходимо положить шаблон Ñ�траницы Ñ� информацией о количеÑ�тве подключённых клиентов. +Его Ñ�одержимое будет запиÑ�ыватьÑ�Ñ� в файл_Ñ�татиÑ�тики. Файл Ñ�татиÑ�тики -- текÑ�товый файл, в котором "%d" будет заменÑ�тьÑ�Ñ� на чиÑ�ло клиентов. + +Пример запуÑ�ка на "боевом" Ñ�ервере: +#!/bin/sh +cd igserver +/usr/sbin/daemon -u nobody ./ig11 5050 /path/to/www/clients_count.html > ig_log.txt + +Багрепорты по работе Ñ�ервера принимаютÑ�Ñ� только при указании точной верÑ�ии ОС и Ñ�ервера, а также иÑ�пользуемой верÑ�ии NatICQ. diff --git a/common.h b/common.h new file mode 100644 index 0000000..f7d01ec --- /dev/null +++ b/common.h @@ -0,0 +1,39 @@ +/*************************************************************************** + * Copyright (C) 2007 by Andrey "Ice" Sploshnov * + * ice.nightcrawler@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef ICQKID_COMMON_H +#define ICQKID_COMMON_H + +#ifdef HAVE_CONFIG_H +# include +#endif + +#ifdef _WIN32 + #include + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; +// typedef unsigned long uint32_t; +#elif defined STDC_HEADERS +# include +#else +# include +#endif + +#endif /* ICQKID_COMMON_H */ diff --git a/flap.cpp b/flap.cpp new file mode 100644 index 0000000..b89cd18 --- /dev/null +++ b/flap.cpp @@ -0,0 +1,1361 @@ +/*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifdef _WIN32 + #include + #define MSG_NOSIGNAL 0 + typedef int socklen_t; +#else + #include + #include + #include + #include + #include + #define SOCKET int + #define ioctlsocket ioctl +#endif + +#define TNETWORK_CLOSE 0 +#define TNETWORK_ERR -1 +#define TNETWORK_TIMEOUT -2 + +#include "flap.h" +#include + +#ifndef _WIN32 + #include + #include + #include + #include + #include +#endif + +/*#include +#include +#include */ + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +FlapPacket::FlapPacket(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +FlapPacket::~FlapPacket() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- + +bool FlapPacket::recv_from(int sock) +{ + vector tmp_buf(6); + unsigned long res; + t_network_error=TNETWORK_ERR; + fd_set rfds; + struct timeval tv; + + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return false; + if (res<6) + { + tv.tv_sec=0; + tv.tv_usec=300000; //Îæèäàíèå 300ìñ + FD_ZERO(&rfds); + FD_SET((SOCKET)sock, &rfds); + int sel_ret = select(sock+1, &rfds, NULL, NULL, &tv); + if (sel_ret==0) goto L_NTO; + if (sel_ret<0) return false; + if (!FD_ISSET(sock, &rfds)) return false; //TNETWORK_ERR; + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return false; + if (res<1) return false; //Îáðûâ ñâÿçè + } + if (res<6) + { + L_NTO: + t_network_error=TNETWORK_TIMEOUT; + return false; + } + if (recv(sock,(char*)&tmp_buf[0],6,0)!=6) return false; + + if (tmp_buf[0]!='*') return false; + frame_type = tmp_buf[1]; + memcpy(&seq_number, &tmp_buf[2], sizeof(seq_number)); + seq_number=ntohs(seq_number); + uint16_t payload_length; + memcpy(&payload_length, &tmp_buf[4], sizeof(payload_length)); + payload_length=ntohs(payload_length); + payload.resize(payload_length); + char *p=(char*)&payload[0]; + int l=payload_length; + int i; + while(l>0) + { + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return false; + if (!res) + { + tv.tv_sec=30; + tv.tv_usec=0; + FD_ZERO(&rfds); + FD_SET((SOCKET)sock, &rfds); + int sel_ret = select(sock+1, &rfds, NULL, NULL, &tv); + if (sel_ret==0) return false; + if (sel_ret<0) return false; + if (!FD_ISSET(sock, &rfds)) return false; //TNETWORK_ERR; + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return false; + } + if (res<1) return false; + if (res>(unsigned int)l) res=l; + i=recv(sock,p,res,0); + if (i<=0) return false; + p+=i; + l-=i; + } + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool FlapPacket::send_to(int sock) +{ + vector tmp_buf(6+payload.size()); + tmp_buf[0]='*'; + tmp_buf[1]=frame_type; + uint16_t tmp_seq_number = htons(seq_number); + memcpy(&tmp_buf[2], &tmp_seq_number, sizeof(tmp_seq_number)); + uint16_t payload_length = htons(payload.size()); + memcpy(&tmp_buf[4], &payload_length, sizeof(payload_length)); + if (payload.size()) + { + memcpy(&tmp_buf[6],&payload[0], payload.size()); + } + t_network_error=TNETWORK_ERR; + if ((unsigned int)send(sock, (char*)&tmp_buf[0], 6+payload.size(), 0)!=(6+payload.size())) return false; +// if(payload.size()) +// if (send(sock, (char*)&payload[0], payload.size(), 0)!=(int)payload.size()) return false; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +SNACData::SNACData(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +SNACData::~SNACData() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool SNACData::decode_from(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+10)) return false; + memcpy(&service_id, &v[start_ind], sizeof(service_id)); + service_id=ntohs(service_id); + memcpy(&subtype_id, &v[start_ind+2], sizeof(subtype_id)); + subtype_id=ntohs(subtype_id); + memcpy(&flags, &v[start_ind+4], sizeof(flags)); + flags=ntohs(flags); + memcpy(&req_id, &v[start_ind+6], sizeof(req_id)); + req_id=ntohl(req_id); + + data.assign(v.begin()+start_ind+10, v.end()); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void SNACData::encode_to(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+10+data.size())) v.resize(start_ind+10+data.size()); + uint16_t tmp_service_id = htons(service_id); + uint16_t tmp_subtype_id = htons(subtype_id); + uint16_t tmp_flags = htons(flags); + uint32_t tmp_req_id = htonl(req_id); + + memcpy(&v[start_ind], &tmp_service_id, sizeof(tmp_service_id)); + memcpy(&v[start_ind+2], &tmp_subtype_id, sizeof(tmp_subtype_id)); + memcpy(&v[start_ind+4], &tmp_flags, sizeof(tmp_flags)); + memcpy(&v[start_ind+6], &tmp_req_id, sizeof(tmp_req_id)); + if(data.size()) + memcpy(&v[start_ind+10], &data[0], data.size()); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +TLVField::TLVField(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +TLVField::~TLVField() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool TLVField::getAsInt16(uint16_t & i) +{ + uint16_t tmp_i16; + if (sizeof(tmp_i16)>data.size()) return false; + memcpy(&tmp_i16, &data[0], sizeof(tmp_i16)); + i = ntohs(tmp_i16); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void TLVField::putInt16(uint16_t i) +{ + uint16_t tmp_i16 = htons(i); + data.resize(sizeof(tmp_i16)); + memcpy(&data[0], &tmp_i16, sizeof(tmp_i16)); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool TLVField::getAsInt32(uint32_t & i) +{ + uint32_t tmp_i32; + if (sizeof(tmp_i32)>data.size()) return false; + memcpy(&tmp_i32, &data[0], sizeof(tmp_i32)); + i = ntohl(tmp_i32); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void TLVField::putInt32(uint32_t i) +{ + uint32_t tmp_i32 = htonl(i); + data.resize(sizeof(tmp_i32)); + memcpy(&data[0], &tmp_i32, sizeof(tmp_i32)); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool TLVField::getAsString(string & s) +{ + if(data.size()) + s.assign(reinterpret_cast(&data[0]), data.size()); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void TLVField::putString(string s) +{ + data.assign(s.begin(), s.end()); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool TLVField::decode_from(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+4)) return false; + uint16_t tmp_i, length; + memcpy(&tmp_i, &v[start_ind], sizeof(tmp_i)); + type=ntohs(tmp_i); + memcpy(&tmp_i, &v[start_ind+sizeof(tmp_i)], sizeof(tmp_i)); + length=ntohs(tmp_i); + if (v.size()<(start_ind+4+length)) return false; + data.assign(v.begin()+start_ind+4, v.begin()+start_ind+4+length); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void TLVField::encode_to(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+4+data.size())) v.resize(start_ind+4+data.size()); + uint16_t tmp_type = htons(type); + uint16_t tmp_length = htons(data.size()); + + memcpy(&v[start_ind], &tmp_type, sizeof(tmp_type)); + memcpy(&v[start_ind+2], &tmp_length, sizeof(tmp_length)); + if(data.size()) + memcpy(&v[start_ind+4], &data[0], data.size()); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +TLVPack::TLVPack(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +TLVPack::~TLVPack() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool TLVPack::decode_from(vector & v, size_t start_ind) +{ + data.clear(); + + while(true) + { + if (start_ind>=v.size()) break; + TLVField tf; + if (!tf.decode_from(v, start_ind)) return false; + data.push_back(tf); + start_ind+=tf.data.size()+4; // +2 for TLVtype +2 for TLV length + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void TLVPack::encode_to(vector & v, size_t start_ind) +{ + for (unsigned int i=0; i & v, size_t start_ind) +{ + if (v.size()<(start_ind+4)) return false; + uint32_t flap_version; + memcpy(&flap_version, &v[start_ind], sizeof(flap_version)); + flap_version = ntohl(flap_version); + if (flap_version!=1) return false; + start_ind+=sizeof(flap_version); + + return TLVPack::decode_from(v, start_ind); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void SignOnPacket::encode_to(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+4)) v.resize(start_ind+4); + uint32_t flap_version = 1; + flap_version = htonl(flap_version); + memcpy(&v[start_ind], &flap_version, sizeof(flap_version)); + start_ind+=sizeof(flap_version); + + TLVPack::encode_to(v, start_ind); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +SignOffPacket::SignOffPacket(void) : TLVPack() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +SignOffPacket::~SignOffPacket() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool SignOffPacket::decode_from(vector & v, size_t start_ind) +{ + return TLVPack::decode_from(v, start_ind); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void SignOffPacket::encode_to(vector & v, size_t start_ind) +{ + TLVPack::encode_to(v, start_ind); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +BaseMsg::BaseMsg(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +BaseMsg::~BaseMsg() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool BaseMsg::decode_header(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+11)) return false; + memcpy(msg_cookie, &v[start_ind], 8); + start_ind+=8; + memcpy(&msg_channel, &v[start_ind], sizeof(msg_channel)); + msg_channel=ntohs(msg_channel); + start_ind+=sizeof(msg_channel); + uint8_t uin_length = v[start_ind++]; + if (v.size()<(start_ind+uin_length)) return false; + uin.assign(v.begin()+start_ind, v.begin()+start_ind+uin_length); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void BaseMsg::encode_header(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+11+uin.length())) v.resize(start_ind+11+uin.length()); + memcpy(&v[start_ind], msg_cookie, 8); + start_ind+=8; + uint16_t tmp_msg_channel = htons(msg_channel); + memcpy(&v[start_ind], &tmp_msg_channel, sizeof(tmp_msg_channel)); + start_ind+=sizeof(tmp_msg_channel); + v[start_ind++]=uin.length(); + memcpy(&v[start_ind], uin.data(), uin.length()); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool BaseMsg::decode_data(vector & v, size_t start_ind) +{ + data.clear(); + while(true) + { + if (start_ind>=v.size()) break; + TLVField tmp_tlv; + if (!tmp_tlv.decode_from(v, start_ind)) return false; + data.push_back(tmp_tlv); + start_ind+=tmp_tlv.data.size()+4; + } + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void BaseMsg::encode_data(vector & v, size_t start_ind) +{ + for (unsigned int i=0; i & v, size_t start_ind) +{ + encode_header(v, start_ind); + start_ind+=11+uin.length(); + encode_data(v, start_ind); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +BaseIncomingMsg::BaseIncomingMsg(void) : BaseMsg() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +BaseIncomingMsg::~BaseIncomingMsg() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool BaseIncomingMsg::decode_from(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+15)) return false; + if (!decode_header(v, start_ind)) return false; + + start_ind+=11+uin.length(); + start_ind+=2; // uint16_t warn_lev + uint16_t fix_tlvs_num; + memcpy(&fix_tlvs_num, &v[start_ind], sizeof(fix_tlvs_num)); + fix_tlvs_num=ntohs(fix_tlvs_num); + start_ind+=sizeof(fix_tlvs_num); + for (int i=0; i & tlv_v) +{ + for (unsigned int i=0; i & v = tlv_v[i].data; + if (v.size()<12) return false; + size_t start_ind=2; // skip frag_id & frag_ver + uint16_t capa_len; + memcpy(&capa_len, &v[start_ind], sizeof(capa_len)); + capa_len=ntohs(capa_len); + if (v.size()<(unsigned int)(12+capa_len)) return false; + start_ind+=sizeof(capa_len)+capa_len+2; // also skip uint8_t capa_arr[capa_len] & frag_id2 & frag_ver2 + uint16_t text_len; + memcpy(&text_len, &v[start_ind], sizeof(text_len)); + text_len=ntohs(text_len)-4; // -4, text_len include charset len and sub_charset len + if (v.size()<(unsigned int)(12+capa_len+text_len)) return false; + start_ind+=sizeof(text_len); + memcpy(&charset, &v[start_ind], sizeof(charset)); + charset=ntohs(charset); + start_ind+=sizeof(charset); + memcpy(&sub_charset, &v[start_ind], sizeof(sub_charset)); + sub_charset=ntohs(sub_charset); + start_ind+=sizeof(sub_charset); + message_str.assign(v.begin()+start_ind, v.begin()+start_ind+text_len); + return true; + } + + return false; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void SimpleMsg::encode_to(vector & tlv_v) +{ + vector v(13+message_str.length()); + uint8_t start_buf[13]={0x05, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x1a, 0x00, 0x00, 0xff, 0xff}; // lazy hack + memcpy(&v[0], start_buf, 13); + uint16_t data_len = message_str.length()+4; // include charset len and sub_charset len + data_len=htons(data_len); + memcpy(&v[7], &data_len, sizeof(data_len)); + uint16_t tmp_charset=htons(charset); + memcpy(&v[9], &tmp_charset, sizeof(tmp_charset)); + uint16_t tmp_sub_charset=htons(sub_charset); + memcpy(&v[11], &tmp_sub_charset, sizeof(tmp_sub_charset)); + memcpy(&v[13], message_str.data(), message_str.length()); + + TLVField tf1(v, 0x0002); + TLVField tf2("", 0x0006); + + tlv_v.clear(); + tlv_v.push_back(tf2); + tlv_v.push_back(tf1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +RendezVousMsg::RendezVousMsg(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +RendezVousMsg::~RendezVousMsg() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool RendezVousMsg::decode_from(vector & data) +{ + size_t pos=0; + if (data.size()<(pos+sizeof(type))) return false; + memcpy(&type, &data[pos], sizeof(type)); + type=ntohs(type); + pos+=sizeof(type); + + if (data.size()<(pos+8)) return false; + memcpy(cookie, &data[pos], 8); + pos+=8; + + if (data.size()<(pos+16)) return false; + memcpy(capability, &data[pos], 16); + pos+=16; + + TLVPack tp; + if (!tp.decode_from(data, pos)) return false; + + TLVField * tlv = tp.findTLV(0x0004); + if (tlv!=NULL) + if (!tlv->getAsInt32(external_ip)) return false; + + tlv = tp.findTLV(0x0005); + if (tlv!=NULL) + if (!tlv->getAsInt16(external_port)) return false; + + tlv = tp.findTLV(0x2711); + if (tlv!=NULL) + { + if (!tlv2711.decode_from(tlv->data)) return false; + } + else + { + return false; + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void RendezVousMsg::encode_to(vector & data) +{ + vector cap_spec_data; + tlv2711.encode_to(cap_spec_data); + + data.resize(2+8+16); + size_t pos=0; + + uint16_t tmp_type=htons(type); + memcpy(&data[pos], &tmp_type, sizeof(tmp_type)); + pos+=sizeof(tmp_type); + + memcpy(&data[pos], cookie, 8); + pos+=8; + + memcpy(&data[pos], capability, 16); + pos+=16; + + TLVPack tp; + tp.data.push_back(TLVField(external_ip, 0x0003)); + tp.data.push_back(TLVField(external_port, 0x0005)); + tp.data.push_back(TLVField((uint16_t)0x0001, 0x000A)); + // Dont insert TLV 0x0B - meaning unknown + tp.data.push_back(TLVField(NULL, 0, 0x000F)); + tp.data.push_back(TLVField(cap_spec_data, 0x2711)); + + tp.encode_to(data, pos); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +TLV2711::TLV2711(void) +{ + memset(text_color, 0, 4); + memset(bg_color, 0xff, 4); + bg_color[3]=0; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +TLV2711::~TLV2711() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool TLV2711::decode_from(vector & data, size_t pos) +{ +#define UNPACK_LE(arg) \ + if (data.size()<(pos+sizeof(arg))) return false; \ + memcpy(&arg, &data[pos], sizeof(arg)); /* Little Endian */ \ + pos+=sizeof(arg); + + uint16_t w_id; + UNPACK_LE(w_id) + if (w_id!=0x001B) return false; // Static size for valid messages + UNPACK_LE(version) + + uint8_t puid_message[16]={0}; + if (data.size()<(pos+sizeof(puid_message))) return false; + if (memcmp(&data[pos], puid_message, sizeof(puid_message))!=0) return false; // It's not a message + pos+=sizeof(puid_message); + + pos+=11; // Skip unused bytes + + UNPACK_LE(cookie) + + pos+=12; // Zeroes chunk + + if (data.size()<(pos+2)) return false; + msg_type=data[pos++]; + flags=data[pos++]; + + UNPACK_LE(status) + UNPACK_LE(priority) + + uint16_t msg_len; + UNPACK_LE(msg_len) + + if (msg_type!=0x01 && msg_type!=0x1a && msg_type!=0xe8 && msg_type!=0xe9 && \ + msg_type!=0xea && msg_type!=0xeb && msg_type!=0xec) return false; // Plain message or Plugin message described by text string or AutoStatus requests + + if (msg_len<1) return false; // NTS can't be shorter then 1 + if (data.size()<(pos+msg_len)) return false; + text.assign((const char *)&data[pos], msg_len-1); + pos+=msg_len; + + if (msg_type==0x01) // Plain message optional data + { + if (data.size()<(pos+4)) return true; + memcpy(text_color, &data[pos], 4); + pos+=4; + + if (data.size()<(pos+4)) return true; + memcpy(bg_color, &data[pos], 4); + pos+=4; + + uint32_t text_guid_len; + if (data.size()<(pos+sizeof(text_guid_len))) return true; + memcpy(&text_guid_len, &data[pos], sizeof(text_guid_len)); + pos+=sizeof(text_guid_len); + + if (data.size()<(pos+text_guid_len)) return true; + if(pos")==xtraz_script_text.npos) return false; + if (xtraz_script_text.find("")==xtraz_script_text.npos) return false; + if (xtraz_script_text.find("")==xtraz_script_text.npos) return false; + if (xtraz_script_text.find("")==xtraz_script_text.npos) return false; + + string::size_type start_i = xtraz_script_text.find("<PluginID>"); + if (start_i==xtraz_script_text.npos) return false; + start_i+=16; + string::size_type end_i = xtraz_script_text.find("</PluginID>", start_i); + if (end_i==xtraz_script_text.npos) return false; + string str_plug_id=string(xtraz_script_text, start_i, end_i-start_i); + if (str_plug_id!="srvMng") return false; + + start_i = xtraz_script_text.find("<srv><id>", end_i+17); + if (start_i==xtraz_script_text.npos) return false; + start_i+=29; + end_i = xtraz_script_text.find("</id>", start_i); + if (end_i==xtraz_script_text.npos) return false; + string str_serv_id=string(xtraz_script_text, start_i, end_i-start_i); + if (str_serv_id!="cAwaySrv") return false; + + start_i = xtraz_script_text.find("<req><id>", end_i+11); + if (start_i==xtraz_script_text.npos) return false; + start_i+=21; + end_i = xtraz_script_text.find("</id>", start_i); + if (end_i==xtraz_script_text.npos) return false; + string str_req_id=string(xtraz_script_text, start_i, end_i-start_i); + if (str_req_id!="AwayStat") return false; + + start_i = xtraz_script_text.find("<senderId>", end_i+11); + if (start_i==xtraz_script_text.npos) return false; + start_i+=16; + end_i = xtraz_script_text.find("</senderId>", start_i); + if (end_i==xtraz_script_text.npos) return false; + xstat_sender_id=string(xtraz_script_text, start_i, end_i-start_i); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool TLV2711::parseXtrazResponse(void) +{ + if (xtraz_script_text.find("")==xtraz_script_text.npos) return false; + if (xtraz_script_text.find("")==xtraz_script_text.npos) return false; + if (xtraz_script_text.find("")==xtraz_script_text.npos) return false; + if (xtraz_script_text.find("")==xtraz_script_text.npos) return false; + + string::size_type start_i = xtraz_script_text.find("<uin>"); + if (start_i==xtraz_script_text.npos) return false; + start_i+=11; + string::size_type end_i = xtraz_script_text.find("</uin>", start_i); + if (end_i==xtraz_script_text.npos) return false; + xstat_sender_id=string(xtraz_script_text, start_i, end_i-start_i); + + start_i = xtraz_script_text.find("<index>", end_i+12); + if (start_i==xtraz_script_text.npos) return false; + start_i+=13; + end_i = xtraz_script_text.find("</index>", start_i); + if (end_i==xtraz_script_text.npos) return false; + string str_ind=string(xtraz_script_text, start_i, end_i-start_i); + x_status.insert(x_status.end(),atoi(str_ind.c_str())); + + start_i = xtraz_script_text.find("<title>", end_i+14); + if (start_i==xtraz_script_text.npos) return false; + start_i+=13; + end_i = xtraz_script_text.find("</title>", start_i); + if (end_i==xtraz_script_text.npos) return false; + x_title=string(xtraz_script_text, start_i, end_i-start_i); + + start_i = xtraz_script_text.find("<desc>", end_i+14); + if (start_i==xtraz_script_text.npos) return false; + start_i+=12; + end_i = xtraz_script_text.find("</desc>", start_i); + if (end_i==xtraz_script_text.npos) return false; + x_descr=string(xtraz_script_text, start_i, end_i-start_i); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void TLV2711::encode_to(vector & data, size_t pos) +{ +#define PACK_LE(arg) \ + if (data.size()<(pos+sizeof(arg))) data.resize(pos+sizeof(arg)); \ + memcpy(&data[pos], &arg, sizeof(arg)); /* Little Endian */ \ + pos+=sizeof(arg); + + uint16_t w_id=0x001B; + PACK_LE(w_id) + PACK_LE(version) + + data.resize(pos+16); + memset(&data[pos], 0, 16); // Plugin - none + pos+=16; + + data.resize(pos+25); + memset(&data[pos], 0, 25); + data[pos+2]=3; // client capabilities flags - LE + data[pos+6]=4; // Unknown + data[pos+9]=14; // Length2 of ex.header - LE + pos+=25; + + PACK_LE(msg_type) + PACK_LE(flags) + PACK_LE(status) + PACK_LE(priority) + + uint16_t msg_len=text.length()+1; // NTS + PACK_LE(msg_len) + + data.resize(pos+msg_len); + memcpy(&data[pos], text.c_str(), msg_len); // NTS + pos+=msg_len; + + if (msg_type==0x01) + { + if (data.size()<(pos+4)) data.resize(pos+4); + memcpy(&data[pos], text_color, 4); + pos+=4; + + if (data.size()<(pos+4)) data.resize(pos+4); + memcpy(&data[pos], bg_color, 4); + pos+=4; + + if (text_guid.empty()) return; + + uint32_t text_guid_len = text_guid.length(); + PACK_LE(text_guid_len) + + if (data.size()<(pos+text_guid_len)) data.resize(pos+text_guid_len); + memcpy(&data[pos], text_guid.data(), text_guid_len); +// pos+=text_guid_len; + + return; // Plain text, encoding finished + } + else if (msg_type==0x1a) // Plugin message described by text string + { + uint16_t plug_info_len=16+2+4+plug_name.length()+15; + PACK_LE(plug_info_len) + + data.resize(pos+16); + memcpy(&data[pos], plug_guid, 16); + pos+=16; + + PACK_LE(plug_func_id) + + uint32_t plug_name_len=plug_name.length(); + PACK_LE(plug_name_len); + + data.resize(pos+plug_name_len); + memcpy(&data[pos], plug_name.data(), plug_name_len); + pos+=plug_name_len; + + data.resize(pos+15); + memset(&data[pos], 0, 15); + data[pos+2]=1; // Mean unknown + pos+=15; + + uint32_t plug_msg_len=xtraz_script_text.length(); + uint32_t plug_data_len=plug_msg_len+sizeof(plug_msg_len); + + PACK_LE(plug_data_len) + PACK_LE(plug_msg_len) + + data.resize(pos+plug_msg_len); + memcpy(&data[pos], xtraz_script_text.data(), plug_msg_len); +// pos+=plug_msg_len; + } +#undef PACK_LE +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +ExtendedMsg::ExtendedMsg(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +ExtendedMsg::~ExtendedMsg() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ExtendedMsg::decode_from(vector & tlv_v) +{ + TLVPack tp; + tp.data=tlv_v; + + user_class=0; + TLVField * tlv = tp.findTLV(0x0001); + if (tlv!=NULL) + if (!tlv->getAsInt16(user_class)) return false; + + user_status=0; + tlv = tp.findTLV(0x0006); + if (tlv!=NULL) + if (!tlv->getAsInt32(user_status)) return false; + + online_time=0; + tlv = tp.findTLV(0x000f); + if (tlv!=NULL) + if (!tlv->getAsInt32(online_time)) return false; + + create_time=0; + tlv = tp.findTLV(0x0003); + if (tlv!=NULL) + if (!tlv->getAsInt32(create_time)) return false; + + tlv = tp.findTLV(0x0005); + if (tlv!=NULL) + { + if (!rv_msg.decode_from(tlv->data)) return false; + } + else + { + return false; + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ExtendedMsg::encode_to(vector & tlv_v) +{ + tlv_v.clear(); + vector rv_data; + rv_msg.encode_to(rv_data); + tlv_v.push_back(TLVField(rv_data, 0x0005)); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +OldStyleMsg::OldStyleMsg(void) +{ + msg_type=0x01; // MTYPE_PLAIN + msg_flags=0x01; // MFLAG_NORMAL +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +OldStyleMsg::~OldStyleMsg() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool OldStyleMsg::decode_from(vector & tlv_v) +{ + for (unsigned int i=0; i(&tlv_v[i].data[8]), text_len-1); + return true; + } + + return false; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void OldStyleMsg::encode_to(vector & tlv_v) +{ + TLVField tlv; + tlv.type=0x0005; + tlv.data.resize(8+message_str.length()+1); + memcpy(&tlv.data[0], &sender_uin, sizeof(sender_uin)); // LITTLE ENDIAN!!! + tlv.data[4]=msg_type; + tlv.data[5]=msg_flags; + uint16_t text_len=message_str.length()+1; + memcpy(&tlv.data[6], &text_len, sizeof(text_len)); // LITTLE ENDIAN!!! + memcpy(&tlv.data[8], message_str.c_str(), text_len); + + tlv_v.clear(); + tlv_v.push_back(tlv); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +SSIContactListItem::SSIContactListItem(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +SSIContactListItem::~SSIContactListItem() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool SSIContactListItem::decode_from(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+2)) return false; + memcpy(&name_length, &v[start_ind], sizeof(name_length)); + name_length=ntohs(name_length); + start_ind+=sizeof(name_length); + + if (v.size()<(start_ind+name_length)) return false; + name.assign(reinterpret_cast(&v[start_ind]), name_length); + start_ind+=name.length(); + + if (v.size()<(start_ind+2)) return false; + memcpy(&group_id, &v[start_ind], sizeof(group_id)); + group_id=ntohs(group_id); + start_ind+=sizeof(group_id); + + if (v.size()<(start_ind+2)) return false; + memcpy(&item_id, &v[start_ind], sizeof(item_id)); + item_id=ntohs(item_id); + start_ind+=sizeof(item_id); + + if (v.size()<(start_ind+2)) return false; + memcpy(&type, &v[start_ind], sizeof(type)); + type=ntohs(type); + start_ind+=sizeof(type); + + if (v.size()<(start_ind+2)) return false; + memcpy(&payload_length, &v[start_ind], sizeof(payload_length)); + payload_length=ntohs(payload_length); + start_ind+=sizeof(payload_length); + + if (v.size()<(start_ind+payload_length)) return false; + + size_t start_ind_fix = start_ind; + payload.clear(); + while(true) + { + if (start_ind>(start_ind_fix+payload_length)) return false; + if (start_ind==(start_ind_fix+payload_length)) break; + TLVField tlv; + if (!tlv.decode_from(v, start_ind)) return false; + payload.push_back(tlv); + start_ind+=tlv.data.size()+4; // +2 for TLVtype +2 for TLV length + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void SSIContactListItem::encode_to(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+2)) v.resize(start_ind+2); + name_length=name.length(); + uint16_t tmp_name_length = htons(name_length); + memcpy(&v[start_ind], &tmp_name_length, sizeof(tmp_name_length)); + start_ind += sizeof(tmp_name_length); + if(name_length) + { + if (v.size()<(start_ind+name_length)) v.resize(start_ind+name_length); + //printf("name_length=%d",name_length); + memcpy(&v[start_ind], name.data(), name_length); + start_ind += name_length; + } + if (v.size()<(start_ind+2)) v.resize(start_ind+2); + uint16_t tmp_group_id = htons(group_id); + memcpy(&v[start_ind], &tmp_group_id, sizeof(tmp_group_id)); + start_ind += sizeof(tmp_group_id); + + if (v.size()<(start_ind+2)) v.resize(start_ind+2); + uint16_t tmp_item_id = htons(item_id); + memcpy(&v[start_ind], &tmp_item_id, sizeof(tmp_item_id)); + start_ind += sizeof(tmp_item_id); + + if (v.size()<(start_ind+2)) v.resize(start_ind+2); + uint16_t tmp_type = htons(type); + memcpy(&v[start_ind], &tmp_type, sizeof(tmp_type)); + start_ind += sizeof(tmp_type); + + payload_length=0; + + for (unsigned int i=0; i & v, size_t start_ind) +{ + if (v.size()<(start_ind+1)) return false; + + ssi_version=v[start_ind]; + start_ind++; + + if (v.size()<(start_ind+2)) return false; + memcpy(&items_count, &v[start_ind], sizeof(items_count)); + items_count=ntohs(items_count); + start_ind+=sizeof(items_count); + + for (int i=0; i & v, size_t start_ind) +{ + if (v.size()<(start_ind+16)) return false; + + memcpy(&channel, &v[start_ind], sizeof(channel)); + channel=ntohs(channel); + start_ind+=sizeof(channel); + + memcpy(&msg_flags, &v[start_ind], sizeof(msg_flags)); + msg_flags=ntohl(msg_flags); + start_ind+=sizeof(msg_flags); + + memcpy(&max_snac_size, &v[start_ind], sizeof(max_snac_size)); + max_snac_size=ntohs(max_snac_size); + start_ind+=sizeof(max_snac_size); + + memcpy(&max_send_warn_lev, &v[start_ind], sizeof(max_send_warn_lev)); + max_send_warn_lev=ntohs(max_send_warn_lev); + start_ind+=sizeof(max_send_warn_lev); + + memcpy(&max_recv_warn_lev, &v[start_ind], sizeof(max_recv_warn_lev)); + max_recv_warn_lev=ntohs(max_recv_warn_lev); + start_ind+=sizeof(max_recv_warn_lev); + + memcpy(&min_msg_interval, &v[start_ind], sizeof(min_msg_interval)); + min_msg_interval=ntohs(min_msg_interval); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICBMParameters::encode_to(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+16)) v.resize(start_ind+16); + + uint16_t tmp_channel=htons(channel); + memcpy(&v[start_ind], &tmp_channel, sizeof(tmp_channel)); + start_ind+=sizeof(tmp_channel); + + uint32_t tmp_msg_flags=htonl(msg_flags); + memcpy(&v[start_ind], &tmp_msg_flags, sizeof(tmp_msg_flags)); + start_ind+=sizeof(tmp_msg_flags); + + uint16_t tmp_max_snac_size=htons(max_snac_size); + memcpy(&v[start_ind], &tmp_max_snac_size, sizeof(tmp_max_snac_size)); + start_ind+=sizeof(tmp_max_snac_size); + + uint16_t tmp_max_send_warn_lev=htons(max_send_warn_lev); + memcpy(&v[start_ind], &tmp_max_send_warn_lev, sizeof(tmp_max_send_warn_lev)); + start_ind+=sizeof(tmp_max_send_warn_lev); + + uint16_t tmp_max_recv_warn_lev=htons(max_recv_warn_lev); + memcpy(&v[start_ind], &tmp_max_recv_warn_lev, sizeof(tmp_max_recv_warn_lev)); + start_ind+=sizeof(tmp_max_recv_warn_lev); + + uint16_t tmp_min_msg_interval=htons(min_msg_interval); + memcpy(&v[start_ind], &tmp_min_msg_interval, sizeof(tmp_min_msg_interval)); + start_ind+=sizeof(tmp_min_msg_interval); + + v[start_ind++]=0x00; + v[start_ind++]=0x00; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +DownloadBuddyIconRequest::DownloadBuddyIconRequest(void) +{ + cmd=1; + hash_size=16; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +DownloadBuddyIconRequest::~DownloadBuddyIconRequest() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void DownloadBuddyIconRequest::encode_to(vector & v, size_t start_ind) +{ + size_t vec_size=1+uin.length()+4+1+hash_size; + if (v.size()<(start_ind+vec_size)) v.resize(start_ind+vec_size); + + v[start_ind]=uin.length(); + start_ind++; + memcpy(&v[start_ind], uin.data(), uin.length()); + start_ind+=uin.length(); + + v[start_ind]=cmd; + start_ind++; + + uint16_t tmp_icon_id=htons(icon_id); + memcpy(&v[start_ind], &tmp_icon_id, sizeof(tmp_icon_id)); + start_ind+=sizeof(tmp_icon_id); + + v[start_ind]=icon_flags; + start_ind++; + + v[start_ind]=hash_size; + start_ind++; + + memcpy(&v[start_ind], md5_hash, 16); +// start_ind+=16; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +DownloadBuddyIconReply::DownloadBuddyIconReply(void) +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +DownloadBuddyIconReply::~DownloadBuddyIconReply() +{ +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool DownloadBuddyIconReply::decode_from(vector & v, size_t start_ind) +{ + if (v.size()<(start_ind+1)) return false; + uint8_t uin_len = v[start_ind]; + start_ind++; + + if (v.size()<(start_ind+uin_len)) return false; + uin.assign(v.begin()+start_ind, v.begin()+start_ind+uin_len); + start_ind+=uin_len; + + if (v.size()<(start_ind+2)) return false; + memcpy(&icon_id, &v[start_ind], 2); + icon_id=ntohs(icon_id); + start_ind+=2; + + if (v.size()<(start_ind+1)) return false; + icon_flags=v[start_ind]; + start_ind++; + + if (v.size()<(start_ind+1)) return false; + uint8_t hash_size=v[start_ind]; + start_ind++; + + if (hash_size!=16) return false; + + if (v.size()<(start_ind+16)) return false; + memcpy(md5_hash, &v[start_ind], 16); + start_ind+=16; + + if (v.size()<(start_ind+1)) return false; + cmd=v[start_ind]; + start_ind++; + + // Just skip fields which aren't needed for us + start_ind+=20; + + uint16_t icon_size; + if (v.size()<(start_ind+sizeof(icon_size))) return false; + memcpy(&icon_size, &v[start_ind], sizeof(icon_size)); + icon_size=ntohs(icon_size); + start_ind+=sizeof(icon_size); + + if (v.size()<(start_ind+icon_size)) return false; + icon_data.assign(v.begin()+start_ind, v.begin()+start_ind+icon_size); + + return true; +} diff --git a/flap.h b/flap.h new file mode 100644 index 0000000..087634c --- /dev/null +++ b/flap.h @@ -0,0 +1,348 @@ +/*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _FLAP_H_ +#define _FLAP_H_ + +#include +#include +#include +#include "common.h" + +using namespace std; + +// Available frame types +#define FT_SIGNON 1 +#define FT_DATA 2 +#define FT_ERROR 3 +#define FT_SIGNOFF 4 +#define FT_KEEP_ALIVE 5 + +#define TLV_UID 0x01 +#define TLV_PWD 0x02 +#define TLV_CLI_STR_ID 0x03 +#define TLV_BOSS_ADDR 0x05 +#define TLV_AUTH_COOKIE 0x06 +#define TLV_CLI_NUM_DISTR 0x14 +#define TLV_CLI_NUM_ID 0x16 +#define TLV_CLI_VER_MAJOR 0x17 +#define TLV_CLI_VER_MINOR 0x18 +#define TLV_CLI_VER_LESSER 0x19 +#define TLV_CLI_NUM_BUILD 0x1a +#define TLV_CLI_LANG 0x0f +#define TLV_CLI_COUNTRY 0x0e + +class FlapPacket{ + public: + FlapPacket(void); + ~FlapPacket(); + + bool recv_from(int sock); + bool send_to(int sock); + + uint8_t frame_type; + uint16_t seq_number; + + vector payload; + + int t_network_error; +}; + +class SNACData{ + public: + SNACData(void); + ~SNACData(); + + bool decode_from(vector & v, size_t start_ind=0); + void encode_to(vector & v, size_t start_ind=0); + + uint16_t service_id; + uint16_t subtype_id; + uint16_t flags; + uint32_t req_id; + + vector data; +}; + +class TLVField{ + public: + TLVField(void); + TLVField(uint8_t i, uint16_t t) { data.resize(1); data[0]=i; type=t; } + TLVField(uint16_t i, uint16_t t) { putInt16(i); type=t; } + TLVField(uint32_t i, uint16_t t) { putInt32(i); type=t; } + TLVField(string s, uint16_t t) { putString(s); type=t; } + TLVField(vector & v, uint16_t t) { data=v; type=t; } + TLVField(const uint8_t * p, size_t p_len, uint16_t t) { if (p_len>0 && p!=NULL) data.assign(p, p+p_len); else data.clear(); type=t; } + ~TLVField(); + + bool getAsInt16(uint16_t & i); + void putInt16(uint16_t i); + + bool getAsInt32(uint32_t & i); + void putInt32(uint32_t i); + + bool getAsString(string & s); + void putString(string s); + + bool decode_from(vector & v, size_t start_ind); + void encode_to(vector & v, size_t start_ind); + + // For simplify can set type manually, without accessors + uint16_t type; + vector data; +}; + +class TLVPack{ + public: + TLVPack(void); + virtual ~TLVPack(); + + virtual bool decode_from(vector & v, size_t start_ind=0); + virtual void encode_to(vector & v, size_t start_ind=0); + + TLVField * findTLV(uint16_t t); + + vector data; +}; + +class SignOnPacket : public TLVPack { + public: + SignOnPacket(void); + virtual ~SignOnPacket(); + + virtual bool decode_from(vector & v, size_t start_ind=0); + virtual void encode_to(vector & v, size_t start_ind=0); +}; + +class SignOffPacket : public TLVPack { + public: + SignOffPacket(void); + virtual ~SignOffPacket(); + + virtual bool decode_from(vector & v, size_t start_ind=0); + virtual void encode_to(vector & v, size_t start_ind=0); +}; + +class BaseMsg{ + public: + BaseMsg(void); + virtual ~BaseMsg(); + + uint8_t msg_cookie[8]; + uint16_t msg_channel; + string uin; + vector data; + + protected: + bool decode_header(vector & v, size_t start_ind=0); + void encode_header(vector & v, size_t start_ind=0); + bool decode_data(vector & v, size_t start_ind); + void encode_data(vector & v, size_t start_ind); +}; + +class BaseOutgoingMsg : public BaseMsg{ + public: + BaseOutgoingMsg(void); + virtual ~BaseOutgoingMsg(); + + void encode_to(vector & v, size_t start_ind=0); +}; + +class BaseIncomingMsg : public BaseMsg{ + public: + BaseIncomingMsg(void); + virtual ~BaseIncomingMsg(); + + bool decode_from(vector & v, size_t start_ind=0); +}; + +class SimpleMsg{ + public: + SimpleMsg(void); + ~SimpleMsg(); + + bool decode_from(vector & tlv_v); + void encode_to(vector & tlv_v); + + uint16_t charset; + uint16_t sub_charset; + string message_str; +}; + +class TLV2711{ + public: + TLV2711(void); + ~TLV2711(); + + bool decode_from(vector & data, size_t pos=0); + void encode_to(vector & data, size_t pos=0); + + bool parseXtrazRequest(void); + bool parseXtrazResponse(void); + + uint16_t version; + uint16_t cookie; + uint8_t msg_type; + uint8_t flags; + uint16_t status; + uint16_t priority; + + string text; + + uint8_t text_color[4]; //RGBN + uint8_t bg_color[4]; //RGBN + string text_guid; + + uint8_t plug_guid[16]; + uint16_t plug_func_id; + string plug_name; + string xtraz_script_text; + string xstat_sender_id; + vector x_status; + string x_title; + string x_descr; +}; + +class RendezVousMsg{ + public: + RendezVousMsg(void); + ~RendezVousMsg(); + + bool decode_from(vector & data); + void encode_to(vector & data); + + uint16_t type; // 0 - request, 1 - cancel, 2 - accept + uint8_t cookie[8]; + uint8_t capability[16]; + uint32_t external_ip; // In host format + uint16_t external_port; + TLV2711 tlv2711; +}; + + +class ExtendedMsg{ + public: + ExtendedMsg(void); + ~ExtendedMsg(); + + bool decode_from(vector & tlv_v); + void encode_to(vector & tlv_v); + + uint16_t user_class; + uint32_t user_status; + uint32_t online_time; + uint32_t create_time; + + RendezVousMsg rv_msg; +}; + +class OldStyleMsg{ + public: + OldStyleMsg(void); + ~OldStyleMsg(); + + bool decode_from(vector & tlv_v); + void encode_to(vector & tlv_v); + + uint32_t sender_uin; + uint8_t msg_type; + uint8_t msg_flags; + string message_str; +}; + +class SSIContactListItem{ + public: + SSIContactListItem(void); + ~SSIContactListItem(); + + bool decode_from(vector & v, size_t start_ind); + void encode_to(vector & v, size_t start_ind); + + uint16_t name_length; + string name; + uint16_t group_id; + uint16_t item_id; + uint16_t type; + uint16_t payload_length; + + vector payload; +}; + +class SSIContactList{ + public: + SSIContactList(void); + ~SSIContactList(); + + bool decode_from(vector & v, size_t start_ind=0); + + uint8_t ssi_version; + uint16_t items_count; + uint32_t last_timestamp; + + vector items; +}; + +class ICBMParameters{ + public: + ICBMParameters(void); + ~ICBMParameters(); + + bool decode_from(vector & v, size_t start_ind=0); + void encode_to(vector & v, size_t start_ind=0); + + uint16_t channel; + uint32_t msg_flags; + uint16_t max_snac_size; + uint16_t max_send_warn_lev; + uint16_t max_recv_warn_lev; + uint16_t min_msg_interval; +}; + +class DownloadBuddyIconRequest{ + public: + DownloadBuddyIconRequest(void); + ~DownloadBuddyIconRequest(); + + void encode_to(vector & v, size_t start_ind=0); + + string uin; + uint8_t cmd; + uint16_t icon_id; + uint8_t icon_flags; + uint8_t hash_size; + uint8_t md5_hash[16]; +}; + +class DownloadBuddyIconReply{ + public: + DownloadBuddyIconReply(void); + ~DownloadBuddyIconReply(); + + bool decode_from(vector & v, size_t start_ind=0); + + string uin; + uint16_t icon_id; + uint8_t icon_flags; + uint8_t md5_hash[16]; + uint8_t cmd; + vector icon_data; +}; + +#endif diff --git a/icqkid2.cpp b/icqkid2.cpp new file mode 100644 index 0000000..10b1ec7 --- /dev/null +++ b/icqkid2.cpp @@ -0,0 +1,5086 @@ + /*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifdef _WIN32 + #include + #define MSG_NOSIGNAL 0 + typedef int socklen_t; +#else + #include + #include + #include + #include + #include +#endif + +#include +#include "icqkid2.h" +#include "flap.h" +#include "snaccache.h" +#include "md5.h" + +#ifndef _WIN32 + #include + #define CLOSE_SOCK(arg) close(arg); arg=-1; +#else + #include + #define CLOSE_SOCK(arg) closesocket(arg); arg=-1; +#endif + +#include + +#include +using namespace std; + +#define FSTATUS_DND (uint32_t)0x00000013 +#define FSTATUS_NA (uint32_t)0x00000005 +#define FSTATUS_OCCUPIED (uint32_t)0x00000011 + +// Clear old SNAC Cache entry after 5 min +#define SNAC_CACHE_LIFETIME 300 + +#define OFFLINE_RECIEVE (uint8_t)0x3c +#define OFFLINE_DELETE (uint8_t)0x3e + +#define SSI_ITEM_ADD (uint16_t)0x0008 +#define SSI_ITEM_CHANGE (uint16_t)0x0009 +#define SSI_ITEM_DELETE (uint16_t)0x000a + +#define AOL_SALT_STR "AOL Instant Messenger (SM)" + +// --PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC-- +// --PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC-- +// --PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC--PUBLIC-- + +static const uint8_t XStatus_arr[34][16] = { + {0x01, 0xD8, 0xD7, 0xEE, 0xAC, 0x3B, 0x49, 0x2A, 0xA5, 0x8D, 0xD3, 0xD8, 0x77, 0xE6, 0x6B, 0x92}, + {0x5A, 0x58, 0x1E, 0xA1, 0xE5, 0x80, 0x43, 0x0C, 0xA0, 0x6F, 0x61, 0x22, 0x98, 0xB7, 0xE4, 0xC7}, + {0x83, 0xC9, 0xB7, 0x8E, 0x77, 0xE7, 0x43, 0x78, 0xB2, 0xC5, 0xFB, 0x6C, 0xFC, 0xC3, 0x5B, 0xEC}, + {0xE6, 0x01, 0xE4, 0x1C, 0x33, 0x73, 0x4B, 0xD1, 0xBC, 0x06, 0x81, 0x1D, 0x6C, 0x32, 0x3D, 0x81}, + {0x8C, 0x50, 0xDB, 0xAE, 0x81, 0xED, 0x47, 0x86, 0xAC, 0xCA, 0x16, 0xCC, 0x32, 0x13, 0xC7, 0xB7}, + {0x3F, 0xB0, 0xBD, 0x36, 0xAF, 0x3B, 0x4A, 0x60, 0x9E, 0xEF, 0xCF, 0x19, 0x0F, 0x6A, 0x5A, 0x7F}, + {0xF8, 0xE8, 0xD7, 0xB2, 0x82, 0xC4, 0x41, 0x42, 0x90, 0xF8, 0x10, 0xC6, 0xCE, 0x0A, 0x89, 0xA6}, + {0x80, 0x53, 0x7D, 0xE2, 0xA4, 0x67, 0x4A, 0x76, 0xB3, 0x54, 0x6D, 0xFD, 0x07, 0x5F, 0x5E, 0xC6}, + {0xF1, 0x8A, 0xB5, 0x2E, 0xDC, 0x57, 0x49, 0x1D, 0x99, 0xDC, 0x64, 0x44, 0x50, 0x24, 0x57, 0xAF}, + {0x1B, 0x78, 0xAE, 0x31, 0xFA, 0x0B, 0x4D, 0x38, 0x93, 0xD1, 0x99, 0x7E, 0xEE, 0xAF, 0xB2, 0x18}, + {0x61, 0xBE, 0xE0, 0xDD, 0x8B, 0xDD, 0x47, 0x5D, 0x8D, 0xEE, 0x5F, 0x4B, 0xAA, 0xCF, 0x19, 0xA7}, + {0x48, 0x8E, 0x14, 0x89, 0x8A, 0xCA, 0x4A, 0x08, 0x82, 0xAA, 0x77, 0xCE, 0x7A, 0x16, 0x52, 0x08}, + {0x10, 0x7A, 0x9A, 0x18, 0x12, 0x32, 0x4D, 0xA4, 0xB6, 0xCD, 0x08, 0x79, 0xDB, 0x78, 0x0F, 0x09}, + {0x6F, 0x49, 0x30, 0x98, 0x4F, 0x7C, 0x4A, 0xFF, 0xA2, 0x76, 0x34, 0xA0, 0x3B, 0xCE, 0xAE, 0xA7}, + {0x12, 0x92, 0xE5, 0x50, 0x1B, 0x64, 0x4F, 0x66, 0xB2, 0x06, 0xB2, 0x9A, 0xF3, 0x78, 0xE4, 0x8D}, + {0xD4, 0xA6, 0x11, 0xD0, 0x8F, 0x01, 0x4E, 0xC0, 0x92, 0x23, 0xC5, 0xB6, 0xBE, 0xC6, 0xCC, 0xF0}, + {0x60, 0x9D, 0x52, 0xF8, 0xA2, 0x9A, 0x49, 0xA6, 0xB2, 0xA0, 0x25, 0x24, 0xC5, 0xE9, 0xD2, 0x60}, + {0x63, 0x62, 0x73, 0x37, 0xA0, 0x3F, 0x49, 0xFF, 0x80, 0xE5, 0xF7, 0x09, 0xCD, 0xE0, 0xA4, 0xEE}, + {0x1F, 0x7A, 0x40, 0x71, 0xBF, 0x3B, 0x4E, 0x60, 0xBC, 0x32, 0x4C, 0x57, 0x87, 0xB0, 0x4C, 0xF1}, + {0x78, 0x5E, 0x8C, 0x48, 0x40, 0xD3, 0x4C, 0x65, 0x88, 0x6F, 0x04, 0xCF, 0x3F, 0x3F, 0x43, 0xDF}, + {0xA6, 0xED, 0x55, 0x7E, 0x6B, 0xF7, 0x44, 0xD4, 0xA5, 0xD4, 0xD2, 0xE7, 0xD9, 0x5C, 0xE8, 0x1F}, + {0x12, 0xD0, 0x7E, 0x3E, 0xF8, 0x85, 0x48, 0x9E, 0x8E, 0x97, 0xA7, 0x2A, 0x65, 0x51, 0xE5, 0x8D}, + {0xBA, 0x74, 0xDB, 0x3E, 0x9E, 0x24, 0x43, 0x4B, 0x87, 0xB6, 0x2F, 0x6B, 0x8D, 0xFE, 0xE5, 0x0F}, + {0x63, 0x4F, 0x6B, 0xD8, 0xAD, 0xD2, 0x4A, 0xA1, 0xAA, 0xB9, 0x11, 0x5B, 0xC2, 0x6D, 0x05, 0xA1}, + {0x2C, 0xE0, 0xE4, 0xE5, 0x7C, 0x64, 0x43, 0x70, 0x9C, 0x3A, 0x7A, 0x1C, 0xE8, 0x78, 0xA7, 0xDC}, + {0x10, 0x11, 0x17, 0xC9, 0xA3, 0xB0, 0x40, 0xF9, 0x81, 0xAC, 0x49, 0xE1, 0x59, 0xFB, 0xD5, 0xD4}, + {0x16, 0x0C, 0x60, 0xBB, 0xDD, 0x44, 0x43, 0xF3, 0x91, 0x40, 0x05, 0x0F, 0x00, 0xE6, 0xC0, 0x09}, + {0x64, 0x43, 0xC6, 0xAF, 0x22, 0x60, 0x45, 0x17, 0xB5, 0x8C, 0xD7, 0xDF, 0x8E, 0x29, 0x03, 0x52}, + {0x16, 0xF5, 0xB7, 0x6F, 0xA9, 0xD2, 0x40, 0x35, 0x8C, 0xC5, 0xC0, 0x84, 0x70, 0x3C, 0x98, 0xFA}, + {0x63, 0x14, 0x36, 0xff, 0x3f, 0x8a, 0x40, 0xd0, 0xa5, 0xcb, 0x7b, 0x66, 0xe0, 0x51, 0xb3, 0x64}, + {0xb7, 0x08, 0x67, 0xf5, 0x38, 0x25, 0x43, 0x27, 0xa1, 0xff, 0xcf, 0x4c, 0xc1, 0x93, 0x97, 0x97}, + {0xdd, 0xcf, 0x0e, 0xa9, 0x71, 0x95, 0x40, 0x48, 0xa9, 0xc6, 0x41, 0x32, 0x06, 0xd6, 0xf2, 0x80}, + {0xd4, 0xe2, 0xb0, 0xba, 0x33, 0x4e, 0x4f, 0xa5, 0x98, 0xd0, 0x11, 0x7d, 0xbf, 0x4d, 0x3c, 0xc8}, + {0x00, 0x72, 0xd9, 0x08, 0x4a, 0xd1, 0x43, 0xdd, 0x91, 0x99, 0x6f, 0x02, 0x69, 0x66, 0x02, 0x6f} + }; + +static const uint8_t IGCAP[] ={0x77,0x8b,0x7d,0x16,0x76,0xd0,0x38,0xef,0x73,0x78}; +static const uint8_t NatICQCAP[]={'N', 'a', 't', 'I', 'C', 'Q'}; + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +ICQKid2::ICQKid2(void) + : sock(-1), flap_seq_number(1), snac_seq_number(1), network_timeout(-1), \ + network_break_flag(false), connect_phase_percentage(0), \ + icons_service(NULL), online_status(STATUS_OFFLINE), xStatus(X_STATUS_NONE), \ + myPrivSrvStatus(PRIV_ALL_CAN_SEE), myPrivSrvStatus_item_id(0) +{ + snac_cache = new SnacCache; + setProxy(); + setLoginHost(); + last_keepalive_timestamp=time(NULL); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +ICQKid2::~ICQKid2() +{ + doDisconnect(); + delete snac_cache; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::doConnect(uint32_t astat) +{ +#ifdef _WIN32 + WORD wVersionRequested = MAKEWORD(1, 1); + WSADATA wsaData; + if (WSAStartup(wVersionRequested, &wsaData)!=0) return false; +#endif + + connect_error_code=0; + connect_error_url=""; + + uint32_t snac_sync; + vector md5_salt; + string boss_host; + int boss_port; + vector boss_cookie; + + // PHASE I + connect_phase_percentage=0; +// HttpProxy http_proxy(proxy_host, proxy_port, proxy_uid, proxy_pwd); +// SOCKS4Proxy socks4_proxy(proxy_host, proxy_port, proxy_uid); +// SOCKS5Proxy socks5_proxy(proxy_host, proxy_port, proxy_uid, proxy_pwd); +// switch(proxy_type) +// { +// case NONE : + sock=directConnect(loginhost, loginport); +// break; +// case HTTP : +// sock=http_proxy.connectTo(loginhost, loginport); +// break; +// case SOCKS4 : +// sock=socks4_proxy.connectTo(loginhost, loginport); +// break; +// case SOCKS5 : +// sock=socks5_proxy.connectTo(loginhost, loginport); +// break; +// } + if (sock<0) return false; //0 + connect_phase_percentage += 2; + + if (!waitHello()) { CLOSE_SOCK(sock); return false; } //2 + connect_phase_percentage += 2; + + if (!sendSignOn0()) { CLOSE_SOCK(sock); return false; } //4 + connect_phase_percentage += 2; //8; //2; + + if (!sendMD5saltrequest(&snac_sync)) { CLOSE_SOCK(sock); return false; } //6 + connect_phase_percentage += 2; + + if (!getMD5salt(snac_sync, md5_salt)) { CLOSE_SOCK(sock); return false; } //8 + connect_phase_percentage += 2; + + if (!sendMD5authorize(&snac_sync, md5_salt)) { CLOSE_SOCK(sock); return false; } //10 + connect_phase_percentage += 2; + + if (!getBOSSparams(boss_host, boss_port, boss_cookie)) { CLOSE_SOCK(sock); return false; } //12 + connect_phase_percentage += 2; + + (void)sendSignOff(); + CLOSE_SOCK(sock); + + return doConnect_phase2(astat, boss_host, boss_port, boss_cookie); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::doConnect_phase2(uint32_t astat, string boss_host, int boss_port, vector & boss_cookie, bool short_phase) +{ + // PHASE II + connect_phase_percentage=14; + +// HttpProxy http_proxy(proxy_host, proxy_port, proxy_uid, proxy_pwd); +// SOCKS4Proxy socks4_proxy(proxy_host, proxy_port, proxy_uid); +// SOCKS5Proxy socks5_proxy(proxy_host, proxy_port, proxy_uid, proxy_pwd); +// switch(proxy_type) +// { +// case NONE : + sock=directConnect(boss_host, boss_port); +// break; +// case HTTP : +// sock=http_proxy.connectTo(boss_host, boss_port); +// break; +// case SOCKS4 : +// sock=socks4_proxy.connectTo(boss_host, boss_port); +// break; +// case SOCKS5 : +// sock=socks5_proxy.connectTo(boss_host, boss_port); +// break; +// } + if (sock<0) return false; //14 + connect_phase_percentage += 3; + if (!waitHello()) { CLOSE_SOCK(sock); return false; } //17 + connect_phase_percentage += 3; + if (!sendSignOn2(boss_cookie)) { CLOSE_SOCK(sock); return false; } //20 + connect_phase_percentage += 4; + + if (!getServiceList()) { CLOSE_SOCK(sock); return false; } // 01,03 //24 + connect_phase_percentage += 4; + if (!askServices()) { CLOSE_SOCK(sock); return false; } // 01,17 //28 + connect_phase_percentage += 3; + if (!getServices()) { CLOSE_SOCK(sock); return false; } // 01,18 //31 + connect_phase_percentage += 3; //34 + + // Skip message of the day // 01,13 + + uint32_t sync_id; + if (!askRateLimits(&sync_id)) { CLOSE_SOCK(sock); return false; } // 01,06 //34 + connect_phase_percentage += 4; + if (!getRateLimits(sync_id)) { CLOSE_SOCK(sock); return false; } // 01,07 //38 + connect_phase_percentage += 4; + if (!sendRateLimits()) { CLOSE_SOCK(sock); return false; } // 01,08 //42 + connect_phase_percentage += 3; + + if (!short_phase) + { + // PHASE III + uint32_t self_sync_id; + uint32_t ssi_sync_id, ssi_copy_id; + uint32_t loc_sync_id; + uint32_t blm_sync_id; + uint32_t icbm_sync_id; + uint32_t prm_sync_id; + + if (!askSelfInfo(&self_sync_id)) { CLOSE_SOCK(sock); return false; } // 01,0e //45 + connect_phase_percentage += 1; + if (!askSSILimit(&ssi_sync_id)) { CLOSE_SOCK(sock); return false; } // 13,02 //46 + connect_phase_percentage += 3; + if (!askSSICopy(&ssi_copy_id)) { CLOSE_SOCK(sock); return false; } // 13,04 Not 05 - we havent got local copy //49 + connect_phase_percentage += 3; + if (!askLocationLimit(&loc_sync_id)) { CLOSE_SOCK(sock); return false; } // 02,02 //52 + connect_phase_percentage += 3; + if (!askBLMLimit(&blm_sync_id)) { CLOSE_SOCK(sock); return false; } // 03,02 //55 + connect_phase_percentage += 3; + if (!askICBMParams(&icbm_sync_id)) { CLOSE_SOCK(sock); return false; } // 04,04 //58 + connect_phase_percentage += 3; + if (!askPRMLimit(&prm_sync_id)) { CLOSE_SOCK(sock); return false; } // 09,02 //61 + connect_phase_percentage += 3; + + if (!getLocationLimit(loc_sync_id)) { CLOSE_SOCK(sock); return false; } // 02,03 //64 + connect_phase_percentage += 3; + if (!getBLMLimit(blm_sync_id)) { CLOSE_SOCK(sock); return false; } // 03,03 //67 + connect_phase_percentage += 3; + if (!getICBMParams(icbm_sync_id)) { CLOSE_SOCK(sock); return false; } // 04,05 //70 + connect_phase_percentage += 4; + if (!getPRMLimit(prm_sync_id)) { CLOSE_SOCK(sock); return false; } // 09,03 //74 + connect_phase_percentage += 3; + if (!getSSILimit(ssi_sync_id)) { CLOSE_SOCK(sock); return false; } // 13,03 //77 + connect_phase_percentage += 4; + if (!getSSICopy(ssi_copy_id)) { CLOSE_SOCK(sock); return false; } // 13,06 Not 0F - our copy always out-to-date //81 + connect_phase_percentage += 3; + + if (!sendLocationInfo()) { CLOSE_SOCK(sock); return false; } // 02,04 //84 + connect_phase_percentage += 4; + + if (!getSelfInfo(self_sync_id)) { CLOSE_SOCK(sock); return false; } // 01,0f //88 + connect_phase_percentage += 1; + + if (!sendICBMParams()) { CLOSE_SOCK(sock); return false; } // 04,02 //89 + connect_phase_percentage += 4; + + if (!sendStatus(astat, true)) { CLOSE_SOCK(sock); return false; } // 01,1E //93 + online_status=astat; + // Skip 01,0f + } + + // PHASE IV + if (!sendReady()) { CLOSE_SOCK(sock); return false; } // 01,02 //93 + // Skip 0b,02 + connect_phase_percentage += 3; + + if (!short_phase) + { + if (!activateSSI()) { CLOSE_SOCK(sock); return false; } // 13,07 //96 + connect_phase_percentage += 4; + if (!sendStatus(astat)) { CLOSE_SOCK(sock); return false; } // 01,1E //100 + online_status=astat; + // Skip 01,0f + } + + connect_phase_percentage = 100; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::setStatus(uint32_t astat) +{ + if (!sendStatus(astat)) return false; + if (!getSelfInfo(0)) return false; + online_status=astat; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::setXStatus(size_t x_stat, string title, string descr) +{ + xStatus=x_stat; + xStatusTitle=title; + xStatusDescription=descr; + + return sendLocationInfo(); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::changePassword(string new_password) +{ + TLVField tlv(NULL, 0, 0x0001); + uint16_t chunk_size=10+2+new_password.length()+1; + tlv.data.resize(chunk_size+2); + + size_t curr_pos=0; + memcpy(&tlv.data[curr_pos], &chunk_size, sizeof(chunk_size)); // Little Endian!!! + curr_pos += sizeof(chunk_size); + + uint32_t int_uin = atol(myuin.c_str()); + memcpy(&tlv.data[curr_pos], &int_uin, sizeof(int_uin)); // Little Endian!!! + curr_pos += sizeof(int_uin); + + tlv.data[curr_pos]=0xd0; + tlv.data[curr_pos+1]=0x07; // 07D0 - META_DATA_REQ + tlv.data[curr_pos+2]=0x02; + tlv.data[curr_pos+3]=0x00; // 0002 - request sequence number + tlv.data[curr_pos+4]=0x2e; + tlv.data[curr_pos+5]=0x04; // 042e - META_SET_PASSWORD + curr_pos += 6; + + uint16_t pass_len = new_password.length()+1; // +1 for NULL-terminator + memcpy(&tlv.data[curr_pos], &pass_len, sizeof(pass_len)); // Little Endian!!! + curr_pos += sizeof(pass_len); + memcpy(&tlv.data[curr_pos], new_password.c_str(), pass_len); + + vector data_vec; + tlv.encode_to(data_vec, 0); + + uint32_t snac_sync; + if (sendSNAC(0x0015, 0x0002, &snac_sync, &data_vec)!=1) return false; + + SNACData snd; + snd.service_id=0x0015; + snd.subtype_id=0x0003; + snd.req_id=snac_sync; + if (waitSNAC(&snd)!=1) return false; + tlv.data.clear(); + if (!tlv.decode_from(snd.data, 0)) return false; + if (tlv.type!=0x0001) return false; + if (tlv.data.size()<13) return false; + if (memcmp(&tlv.data[2], &int_uin, sizeof(int_uin))!=0) return false; + if (tlv.data[6]!=0xda || tlv.data[7]!=0x07) return false; // META_DATA_REQ + if (tlv.data[8]!=0x02 || tlv.data[9]!=0x00) return false; // sequence number + if (tlv.data[10]!=0xaa || tlv.data[11]!=0x00) return false; // data subtype: META_SET_PASSWORD_ACK + if (tlv.data[12]!=0x0a) return false; // success byte + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::doDisconnect(void) +{ + if (icons_service && icons_service!=this) + { + icons_service->doDisconnect(); + delete icons_service; + icons_service=NULL; + } + + if (sock<0) return; + CLOSE_SOCK(sock); + sock=-1; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendMessage(ICQKid2Message msg, unsigned short cookie) +{ + int uen_ind=findCLUIN(msg.uin); + if (uen_ind<0 && msg.enc_type!=ICQKid2Message::LOCAL8BIT && msg.enc_type!=ICQKid2Message::USASCII) return false; // I cannot send unicode message to users who aren't in my contact list + if (uen_ind>=0 && ContactListUins[uen_ind].online_status==STATUS_OFFLINE && msg.enc_type!=ICQKid2Message::LOCAL8BIT && msg.enc_type!=ICQKid2Message::USASCII) return false; // I cannot send unicode message to offline users + +// if (uen_ind>=0 && ContactListUins[uen_ind].proto>8) goto L1; + + if (uen_ind>=0 && msg.enc_type==ICQKid2Message::UCS2BE && !ContactListUins[uen_ind].unicode_cap) return false; + if (uen_ind>=0 && msg.enc_type==ICQKid2Message::UTF8 && (/*!ContactListUins[uen_ind].unicode_cap || */!ContactListUins[uen_ind].srv_relay_cap)) return false; + +// L1: + + BaseOutgoingMsg bom; + bom.uin=msg.uin; + memset(bom.msg_cookie,0xAB,8); + + switch (msg.enc_type) + { + case ICQKid2Message::USASCII : + { + SimpleMsg out_sim; + out_sim.message_str=msg.text; + out_sim.charset=0; + out_sim.sub_charset=msg.codepage; + bom.msg_channel=0x0001; + out_sim.encode_to(bom.data); + } + break; + + case ICQKid2Message::LOCAL8BIT : + { + SimpleMsg out_sim; + out_sim.message_str=msg.text; + out_sim.charset=0; + out_sim.sub_charset=msg.codepage; + bom.msg_channel=0x0001; + out_sim.encode_to(bom.data); + } + break; + + case ICQKid2Message::UCS2BE : + { + SimpleMsg out_sim; + out_sim.message_str=msg.text; + out_sim.charset=2; + out_sim.sub_charset=msg.codepage; + bom.msg_channel=0x0001; + out_sim.encode_to(bom.data); + } + break; + + case ICQKid2Message::UTF8 : + { + ExtendedMsg out_exm; + out_exm.rv_msg.type=0; // Request + memset(out_exm.rv_msg.cookie, 'Y', 8); + uint8_t ser_rel[] = {0x09, 0x46, 0x13, 0x49, 0x4c, 0x7f, 0x11, 0xd1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}; // ICQ Server relaying + memcpy(out_exm.rv_msg.capability, ser_rel, sizeof(ser_rel)); + out_exm.rv_msg.external_ip=0x0100007f; // 127.0.0.1 + out_exm.rv_msg.external_port=0x220c; // 1234 + + out_exm.rv_msg.tlv2711.version=8; + out_exm.rv_msg.tlv2711.msg_type=1; // Plain text message + out_exm.rv_msg.tlv2711.flags=0; // Normal message + out_exm.rv_msg.tlv2711.status=0; + out_exm.rv_msg.tlv2711.priority=1; + out_exm.rv_msg.tlv2711.text=msg.text; + memcpy(out_exm.rv_msg.tlv2711.text_color, msg.text_color, 4); + memcpy(out_exm.rv_msg.tlv2711.bg_color, msg.bg_color, 4); + if (ContactListUins[uen_ind].unicode_cap) + { + out_exm.rv_msg.tlv2711.text_guid="{0946134E-4C7F-11D1-8222-444553540000}"; // UTF-8 + } + else + { + out_exm.rv_msg.tlv2711.text_guid="{09461349-4C7F-11D1-8222-444553540000}"; // Not UTF-8 + } + bom.msg_channel=0x0002; + out_exm.encode_to(bom.data); + } + break; + } + + memcpy(bom.msg_cookie+4,&cookie,2); + + vector vec; + bom.encode_to(vec); + + int i=vec.size(); + vec.resize(i+4); + vec[i+0]=0; + vec[i+1]=3; + vec[i+2]=0; + vec[i+3]=0; + + return (sendSNAC(0x0004, 0x0006, NULL, &vec)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendMTN(string to, uint16_t atype) +{ + vector data_vec(11+to.length()+2, 0); + data_vec[9]=1; + data_vec[10]=to.length(); + memcpy(&data_vec[11], to.data(), to.length()); + uint16_t tmp_type = htons(atype); + memcpy(&data_vec[11+to.length()], &tmp_type, sizeof(tmp_type)); + + return (sendSNAC(0x0004, 0x0014, NULL, &data_vec)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getUserInfo(string uin, ICQKidShortUserInfo & info, bool force_update) +{ + map::iterator mi = userinfo_map.find(uin); + if (mi!=userinfo_map.end() && !force_update) + { + info=mi->second; + return true; + } + + uint32_t sync_id; + if (!askShortInfo(uin, &sync_id)) return false; + if (!getShortInfo(info, sync_id)) return false; + + info.Uin=uin; + userinfo_map[uin]=info; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getFullUserInfo(string uin, ICQKidFullUserInfo & info, bool force_update) +{ + map::iterator mi = userfullinfo_map.find(uin); + if (mi!=userfullinfo_map.end() && !force_update) + { + info=mi->second; + return true; + } + + uint32_t sync_id; + if (!askFullInfo(uin, &sync_id)) return false; + if (!getFullInfo(info, sync_id)) return false; + + info.Uin=uin; + userfullinfo_map[uin]=info; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::publicMyInfo(ICQKidFullUserInfo & info) +{ +#define PACK_LE(arg) \ + if (tlv_vec.size()<(tlv_pos+sizeof(arg))) tlv_vec.resize(tlv_pos+sizeof(arg)); \ + memcpy(&tlv_vec[tlv_pos], &arg, sizeof(arg)); \ + tlv_pos+=sizeof(arg); + +#define PACK_ASCIIZ(arg) \ + { \ + uint16_t str_len=arg.length()+1; \ + PACK_LE(str_len) \ + if (tlv_vec.size()<(tlv_pos+str_len)) tlv_vec.resize(tlv_pos+str_len); \ + memcpy(&tlv_vec[tlv_pos], arg.c_str(), str_len); \ + tlv_pos+=str_len; \ + } + +#define PACK_TLV(arg) \ + { \ + uint16_t letlv_type=arg; \ + uint16_t letlv_size=tlv_vec.size(); \ + tlv_pack.insert(tlv_pack.end(), (uint8_t*)(&letlv_type), ((uint8_t*)(&letlv_type))+sizeof(letlv_type)); /* LE */ \ + tlv_pack.insert(tlv_pack.end(), (uint8_t*)(&letlv_size), ((uint8_t*)(&letlv_size))+sizeof(letlv_size)); /* LE */ \ + tlv_pack.insert(tlv_pack.end(), tlv_vec.begin(), tlv_vec.end()); \ + tlv_vec.clear(); \ + tlv_pos=0; \ + } + + vector tlv_pack; + vector tlv_vec; + size_t tlv_pos=0; + + PACK_ASCIIZ(info.Nickname) + PACK_TLV(0x0154) + + PACK_ASCIIZ(info.Firstname) + PACK_TLV(0x0140) + + PACK_ASCIIZ(info.Lastname) + PACK_TLV(0x014A) + + PACK_ASCIIZ(info.Email) + PACK_LE(info.Pubprimarymail) + PACK_TLV(0x015E) + + for (size_t i=0; i & result_vec) +{ +#define PACK_LE(arg) \ + if (tlv_vec.size()<(tlv_pos+sizeof(arg))) tlv_vec.resize(tlv_pos+sizeof(arg)); \ + memcpy(&tlv_vec[tlv_pos], &arg, sizeof(arg)); \ + tlv_pos+=sizeof(arg); + +#define PACK_ASCIIZ(arg) \ + { \ + uint16_t str_len=arg.length()+1; \ + PACK_LE(str_len) \ + if (tlv_vec.size()<(tlv_pos+str_len)) tlv_vec.resize(tlv_pos+str_len); \ + memcpy(&tlv_vec[tlv_pos], arg.c_str(), str_len); \ + tlv_pos+=str_len; \ + } + +#define PACK_TLV(arg) \ + { \ + uint16_t letlv_type=arg; \ + uint16_t letlv_size=tlv_vec.size(); \ + tlv_pack.insert(tlv_pack.end(), (uint8_t*)(&letlv_type), ((uint8_t*)(&letlv_type))+sizeof(letlv_type)); /* LE */ \ + tlv_pack.insert(tlv_pack.end(), (uint8_t*)(&letlv_size), ((uint8_t*)(&letlv_size))+sizeof(letlv_size)); /* LE */ \ + tlv_pack.insert(tlv_pack.end(), tlv_vec.begin(), tlv_vec.end()); \ + tlv_vec.clear(); \ + tlv_pos=0; \ + } + +#define CHECK_FOR_ASCIIZ(str, tlv_type) \ + if (str!="") \ + { \ + PACK_ASCIIZ(str) \ + PACK_TLV(tlv_type) \ + } + +#define CHECK_FOR_LE(arg, tlv_type) \ + if (arg!=0) \ + { \ + PACK_LE(arg) \ + PACK_TLV(tlv_type) \ + } + + vector tlv_pack; + vector tlv_vec; + size_t tlv_pos=0; + + CHECK_FOR_ASCIIZ(info.Firstname, 0x0140) + CHECK_FOR_ASCIIZ(info.Lastname, 0x014A) + CHECK_FOR_ASCIIZ(info.Nickname, 0x0154) + CHECK_FOR_ASCIIZ(info.Email, 0x015E) + CHECK_FOR_ASCIIZ(info.Homecity, 0x0190) + CHECK_FOR_ASCIIZ(info.Homestate, 0x019A) + CHECK_FOR_LE(info.Homecountry, 0x01A4) + CHECK_FOR_ASCIIZ(info.Workcompany, 0x01AE) + CHECK_FOR_ASCIIZ(info.Workdepartment, 0x01B8) + CHECK_FOR_ASCIIZ(info.Workposition, 0x01C2) + CHECK_FOR_LE(info.Workoccupation, 0x01CC) + CHECK_FOR_ASCIIZ(info.Searchkey, 0x0226) + + if (info.Minage>0 && info.Maxage>0 && info.Minage<=info.Maxage) + { + PACK_LE(info.Minage) + PACK_LE(info.Maxage) + PACK_TLV(0x0168) + } + + CHECK_FOR_LE(info.Gender, 0x017C) + CHECK_FOR_LE(info.Maritalstatus, 0x033E) + + uint16_t lang16_1=info.Language1; + CHECK_FOR_LE(lang16_1, 0x0186) + + if (info.Past1category!=0 || info.Past1!="") + { + PACK_LE(info.Past1category) + PACK_ASCIIZ(info.Past1) + PACK_TLV(0x01D6) + } + + if (info.Interest1category!=0 || info.Interest1!="") + { + PACK_LE(info.Interest1category) + PACK_ASCIIZ(info.Interest1) + PACK_TLV(0x01EA) + } + + if (info.Affiliation1category!=0 || info.Affiliation1!="") + { + PACK_LE(info.Affiliation1category) + PACK_ASCIIZ(info.Affiliation1) + PACK_TLV(0x01FE) + } + + if (info.Homepagecategory!=0 || info.Homepagekeywords!="") + { + PACK_LE(info.Homepagecategory) + PACK_ASCIIZ(info.Homepagekeywords) + PACK_TLV(0x0212) + } + + PACK_LE(info.Searchonline) + PACK_TLV(0x0230) + + uint16_t chunk_size=tlv_pack.size()+10; + uint32_t my_int_uin=atoi(myuin.c_str()); + uint8_t tail[] = { 0xd0, 0x07, /* data type: META_DATA_REQ */ \ + 0x02, 0x00, /* request sequence number */ \ + 0x5f, 0x05 }; /* data subtype: CLI_WHITE_PAGES_SEARCH2 */ + + // Reuse tlv_vec + tlv_vec.clear(); + PACK_LE(chunk_size) + PACK_LE(my_int_uin) + tlv_vec.insert(tlv_vec.end(), tail, tail+sizeof(tail)); + tlv_vec.insert(tlv_vec.end(), tlv_pack.begin(), tlv_pack.end()); + +#undef PACK_TLV +#undef PACK_ASCIIZ +#undef PACK_LE +#undef CHECK_FOR_ASCIIZ +#undef CHECK_FOR_LE + + TLVField tlv_f(tlv_vec, 0x0001); // encapsulated META_DATA + tlv_vec.clear(); + tlv_f.encode_to(tlv_vec, 0); + + uint32_t snac_sync; + if (sendSNAC(0x0015, 0x0002, &snac_sync, &tlv_vec)!=1) return false; + + return getSearchAnswer(result_vec, snac_sync); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::searchByMail(string email, vector & result_vec) +{ + TLVField tlv(NULL, 0, 0x0001); + uint16_t chunk_size=10+4+2+email.length()+1; + tlv.data.resize(chunk_size+2); + + size_t curr_pos=0; + memcpy(&tlv.data[curr_pos], &chunk_size, sizeof(chunk_size)); // Little Endian!!! + curr_pos += sizeof(chunk_size); + + uint32_t int_uin = atol(myuin.c_str()); + memcpy(&tlv.data[curr_pos], &int_uin, sizeof(int_uin)); // Little Endian!!! + curr_pos += sizeof(int_uin); + + tlv.data[curr_pos]=0xd0; + tlv.data[curr_pos+1]=0x07; // 07D0 - META_DATA_REQ + tlv.data[curr_pos+2]=0x02; + tlv.data[curr_pos+3]=0x00; // 0002 - request sequence number + tlv.data[curr_pos+4]=0x73; + tlv.data[curr_pos+5]=0x05; // 0573 - META_SEARCH_BY_EMAIL_TLV + curr_pos += 6; + + tlv.data[curr_pos++]=0x5e; + tlv.data[curr_pos++]=0x01; // TLV.Type(0x015E) - email to search + uint16_t subtlv_len = 2+email.length()+1; + memcpy(&tlv.data[curr_pos], &subtlv_len, sizeof(subtlv_len)); // Little Endian!!! + curr_pos += sizeof(subtlv_len); + + uint16_t email_len = email.length()+1; // +1 for NULL-terminator + memcpy(&tlv.data[curr_pos], &email_len, sizeof(email_len)); // Little Endian!!! + curr_pos += sizeof(email_len); + memcpy(&tlv.data[curr_pos], email.c_str(), email_len); + + vector data_vec; + tlv.encode_to(data_vec, 0); + + uint32_t snac_sync; + if (sendSNAC(0x0015, 0x0002, &snac_sync, &data_vec)!=1) return false; + + return getSearchAnswer(result_vec, snac_sync); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::searchByUIN(string uin, vector & result_vec) +{ + TLVField tlv(NULL, 0, 0x0001); + uint16_t chunk_size=10+4+4; + tlv.data.resize(chunk_size+2); + + size_t curr_pos=0; + memcpy(&tlv.data[curr_pos], &chunk_size, sizeof(chunk_size)); // Little Endian!!! + curr_pos += sizeof(chunk_size); + + uint32_t int_my_uin = atol(myuin.c_str()); + memcpy(&tlv.data[curr_pos], &int_my_uin, sizeof(int_my_uin)); // Little Endian!!! + curr_pos += sizeof(int_my_uin); + + tlv.data[curr_pos]=0xd0; + tlv.data[curr_pos+1]=0x07; // 07D0 - META_DATA_REQ + tlv.data[curr_pos+2]=0x02; + tlv.data[curr_pos+3]=0x00; // 0002 - request sequence number + tlv.data[curr_pos+4]=0x69; + tlv.data[curr_pos+5]=0x05; // 0569 - META_SEARCH_BY_UIN_TLV + curr_pos += 6; + + tlv.data[curr_pos++]=0x36; + tlv.data[curr_pos++]=0x01; // TLV.Type(0x0136) - uin to search + tlv.data[curr_pos++]=0x04; + tlv.data[curr_pos++]=0x00; // TLV length + + uint32_t int_uin = atol(uin.c_str()); + memcpy(&tlv.data[curr_pos], &int_uin, sizeof(int_uin)); // Little Endian!!! + curr_pos += sizeof(int_uin); + + vector data_vec; + tlv.encode_to(data_vec, 0); + + uint32_t snac_sync; + if (sendSNAC(0x0015, 0x0002, &snac_sync, &data_vec)!=1) return false; + + return getSearchAnswer(result_vec, snac_sync); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sub_ssi_addContact(string uin, string nick, string groupname, uint16_t * retflag) +{ + int clg_ind = findCLGroup(groupname); + if (clg_ind<0) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NOTFOUND; + return false; + } + + SSIUINEntry uen; + uen.uin=uin; + uen.nick=nick; + uen.groupid=ContactListGroups[clg_ind].id; + uen.groupname=ContactListGroups[clg_ind].name; + uen.itemid=getUnusedItemID(); + if (uen.itemid==0) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_LIMIT; + return false; + } + uen.waitauth=false; + + uint16_t my_retflag; + uint32_t sync_id; + + if (!addDeleSSIUIN(uen, SSI_ITEM_ADD, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + // If need auth, try add it as awaiting auth + if (my_retflag==SSI_EDIT_ERR_NEEDAUTH) + { + uen.waitauth=true; + if (!addDeleSSIUIN(uen, SSI_ITEM_ADD, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + } + if (retflag!=NULL) *retflag=my_retflag; + if (my_retflag==SSI_EDIT_OK) ContactListUins.push_back(uen); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::addContact(string uin, string nick, string groupname, uint16_t * retflag) +{ + if (!startSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (!sub_ssi_addContact(uin, nick, groupname, retflag)) + { + if (!endSSITransact() && retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + int uen_ind = findCLUIN(uin); + if (uen_ind>=0) + { + uint32_t sync_id; + if (!updateSSIGroupContent(ContactListUins[uen_ind].groupid, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + uint16_t my_retflag; + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=my_retflag; + return false; + } + } + + if (!endSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (retflag!=SSI_EDIT_OK) return false; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sub_ssi_removeContact(string uin, uint16_t * retflag) +{ + int uen_ind = findCLUIN(uin); + if (uen_ind<0) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NOTFOUND; + return false; + } + + uint16_t my_retflag; + uint32_t sync_id; + + if (!addDeleSSIUIN(ContactListUins[uen_ind], SSI_ITEM_DELETE, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (retflag!=NULL) *retflag=my_retflag; + if (my_retflag==SSI_EDIT_OK) ContactListUins.erase(ContactListUins.begin()+uen_ind); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::removeContact(string uin, uint16_t * retflag) +{ + if (!startSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + uint16_t gr_id=0; + int uen_ind = findCLUIN(uin); + if (uen_ind>=0) gr_id=ContactListUins[uen_ind].groupid; + + if (!sub_ssi_removeContact(uin, retflag)) + { + if (!endSSITransact() && retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (gr_id>0) + { + uint32_t sync_id; + if (!updateSSIGroupContent(gr_id, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + uint16_t my_retflag; + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=my_retflag; + return false; + } + } + + if (!endSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (retflag!=SSI_EDIT_OK) return false; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sub_ssi_renameContact(string uin, string nick, uint16_t * retflag) +{ + int uen_ind = findCLUIN(uin); + if (uen_ind<0) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NOTFOUND; + return false; + } + + // After my tests i think that we cant rename contacts + // which awaiting auth, so we delete it and add once again + if (ContactListUins[uen_ind].waitauth) + { + string groupname=ContactListUins[uen_ind].groupname; + if (!sub_ssi_removeContact(uin, retflag)) return false; + return sub_ssi_addContact(uin, nick, groupname, retflag); + } + + SSIUINEntry uen=ContactListUins[uen_ind]; + uen.nick=nick; + + uint16_t my_retflag; + uint32_t sync_id; + + if (!addDeleSSIUIN(uen, SSI_ITEM_CHANGE, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (retflag!=NULL) *retflag=my_retflag; + if (my_retflag!=SSI_EDIT_OK) ContactListUins[uen_ind]=uen; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sub_registerNewUIN_simply(ICQKid2 * regist_serv, string password, string & new_uin, string * control_str) +{ + vector data_vec; + srand(time(NULL)); + uint32_t reg_cookie = (uint32_t)(2000000.0 * (rand() / (RAND_MAX + 1.0))); + +#define INSERT_LE_WORD(arg) \ + { \ + uint16_t uw=arg; \ + data_vec.insert(data_vec.end(), (uint8_t*)(&uw), ((uint8_t*)(&uw))+sizeof(uw)); \ + } + +#define INSERT_LE_DWORD(arg) \ + { \ + uint32_t udw=arg; \ + data_vec.insert(data_vec.end(), (uint8_t*)(&udw), ((uint8_t*)(&udw))+sizeof(udw)); \ + } + +#define INSERT_LE_ASCIIZ(str) \ + { \ + INSERT_LE_WORD(str.length()+1) \ + uint8_t * p_start = (uint8_t*)str.c_str(); \ + uint8_t * p_end = ((uint8_t*)str.c_str())+str.length()+1; \ + data_vec.insert(data_vec.end(), p_start, p_end); \ + } + + INSERT_LE_DWORD(0x00000000) + INSERT_LE_WORD(0x0028) + INSERT_LE_WORD(0x0000) + INSERT_LE_DWORD(0x00000000) + INSERT_LE_DWORD(0x00000000) + INSERT_LE_DWORD(reg_cookie) + INSERT_LE_DWORD(reg_cookie) + INSERT_LE_DWORD(0x00000000) + INSERT_LE_DWORD(0x00000000) + INSERT_LE_DWORD(0x00000000) + INSERT_LE_DWORD(0x00000000) + INSERT_LE_ASCIIZ(password) + INSERT_LE_DWORD(reg_cookie) + INSERT_LE_DWORD(0x01d10000) + + TLVField tlv_f(data_vec, 0x0001); + data_vec.clear(); + tlv_f.encode_to(data_vec, 0); + + if (control_str!=NULL) + { + TLVField tlv_f2(*control_str, 0x0009); + tlv_f2.encode_to(data_vec, data_vec.size()); + } + + uint32_t snac_sync; + if (regist_serv->sendSNAC(0x0017, 0x0004, &snac_sync, &data_vec)!=1) return false; + + SNACData snd; + snd.service_id=0x0017; + snd.subtype_id=0; + snd.req_id=snac_sync; + if (regist_serv->waitSNAC(&snd)!=1) return false; + + if (snd.subtype_id!=0x0005) return false; + if (snd.data.size()<54) return false; + if (snd.data[0]!=0 || snd.data[1]!=1) return false; + if (snd.data[10]!=0x2d || snd.data[11]!=0) return false; + if (snd.data[12]!=0 || snd.data[13]!=0) return false; // Hardcoded sequence number ^ INSERT_LE_WORD(0x0000) +// if (memcmp(&snd.data[26], ®_cookie, sizeof(reg_cookie))!=0) return false; // Unfortunately, cookies didn't match + uint32_t new_int_uin; + memcpy(&new_int_uin, &snd.data[46], sizeof(new_int_uin)); +// if (memcmp(&snd.data[50], ®_cookie, sizeof(reg_cookie))!=0) return false; // Unfortunately, cookies didn't match + + ostringstream ss; + ss << new_int_uin; + new_uin=ss.str(); + +#undef INSERT_LE_WORD +#undef INSERT_LE_DWORD +#undef INSERT_LE_ASCIIZ + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sub_registerNewUIN_picture(ICQKid2 * regist_serv, string password, string & new_uin) +{ + uint32_t snac_sync; + if (regist_serv->sendSNAC(0x0017, 0x000c, &snac_sync, NULL)!=1) return false; + + SNACData snd; + snd.service_id=0x0017; + snd.subtype_id=0; + snd.req_id=snac_sync; + if (regist_serv->waitSNAC(&snd)!=1) return false; + + if (snd.subtype_id!=0x000d) return false; + TLVPack tlv_pack; + if (!tlv_pack.decode_from(snd.data)) return false; + TLVField * tlvp1 = tlv_pack.findTLV(0x0001); // Mime type + if (tlvp1==NULL) return false; + TLVField * tlvp2 = tlv_pack.findTLV(0x0002); // Image data + if (tlvp2==NULL) return false; + + string mime_type; + if (!tlvp1->getAsString(mime_type)) return false; + string pic_str; + onRegisterControlPicture(tlvp2->data, mime_type, pic_str); + + return sub_registerNewUIN_simply(regist_serv, password, new_uin, &pic_str); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::renameContact(string uin, string nick, uint16_t * retflag) +{ + if (!startSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + uint16_t it_id1=0; + int uen_ind = findCLUIN(uin); + if (uen_ind>=0) it_id1=ContactListUins[uen_ind].itemid; + + if (!sub_ssi_renameContact(uin, nick, retflag)) + { + if (!endSSITransact() && retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + uint16_t it_id2=0; + uen_ind = findCLUIN(uin); + if (uen_ind>=0) + { + it_id2=ContactListUins[uen_ind].itemid; + if (it_id1!=it_id2) + { + uint32_t sync_id; + if (!updateSSIGroupContent(ContactListUins[uen_ind].groupid, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + uint16_t my_retflag; + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=my_retflag; + return false; + } + } + } + + if (!endSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (retflag!=SSI_EDIT_OK) return false; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::addContacts(vector & cont_vec) +{ + if (!startSSITransact()) + { + for (size_t i=0; i group_ids; + for (size_t i=0; i=0) group_ids.insert(ContactListUins[uen_ind].groupid); + } + + for (set::iterator i=group_ids.begin(); i!=group_ids.end(); ++i) + { + uint32_t sync_id; + if (!updateSSIGroupContent(*i, &sync_id)) + { + for (size_t j=0; j & cont_vec) +{ + if (!startSSITransact()) + { + for (size_t i=0; i group_ids; + for (size_t i=0; i=0) group_ids.insert(ContactListUins[uen_ind].groupid); + } + + for (size_t i=0; i::iterator i=group_ids.begin(); i!=group_ids.end(); ++i) + { + uint32_t sync_id; + if (!updateSSIGroupContent(*i, &sync_id)) + { + for (size_t j=0; j & cont_vec) +{ + if (!startSSITransact()) + { + for (size_t i=0; i group_ids; + for (size_t i=0; i=0) it_id1=ContactListUins[uen_ind].itemid; + if (!sub_ssi_renameContact(cont_vec[i].uin, cont_vec[i].nick, &cont_vec[i].retflag)) continue; + uint16_t it_id2=0; + uen_ind = findCLUIN(cont_vec[i].uin); + if (uen_ind>=0) + { + it_id2=ContactListUins[uen_ind].itemid; + if (it_id1!=it_id2) group_ids.insert(ContactListUins[uen_ind].groupid); + } + } + + for (set::iterator i=group_ids.begin(); i!=group_ids.end(); ++i) + { + uint32_t sync_id; + if (!updateSSIGroupContent(*i, &sync_id)) + { + for (size_t j=0; j uin_vec(1, uin); + return addBLMContacts(uin_vec); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::removeBLMContact(string uin) +{ + vector uin_vec(1, uin); + return removeBLMContacts(uin_vec); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::addBLMContacts(vector & uins) +{ + vector data; + size_t data_size=0; + + for (size_t i=0; i=0) continue; + SSIUINEntry uen; + uen.uin=uins[i]; + uen.isBLM=true; + ContactListUins.push_back(uen); + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::removeBLMContacts(vector & uins) +{ + vector data; + size_t data_size=0; + + for (size_t i=0; i=0 && ContactListUins[uen_ind].isBLM) ContactListUins.erase(ContactListUins.begin()+uen_ind); + else ++i; + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::addGroup(string groupname, uint16_t * retflag) +{ + SSIGroupEntry gen; + gen.id=getUnusedGroupID(); + if (gen.id==0) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_LIMIT; + return false; + } + gen.name=groupname; + if (gen.name=="") // Name "" reserved for MasterGroup + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_CANTADD; + return false; + } + + uint16_t my_retflag; + uint32_t sync_id; + if (!startSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!addDeleSSIGroup(gen, SSI_ITEM_ADD, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!updateSSIGroupContent(0, &sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!getSSIEditAck(my_retflag, sync_id)) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + if (!endSSITransact()) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NETWORK; + return false; + } + + if (retflag!=NULL) *retflag=my_retflag; + if (my_retflag!=SSI_EDIT_OK) return false; + + ContactListGroups.push_back(gen); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::removeGroup(string groupname, uint16_t * retflag) +{ + int gr_ind = findCLGroup(groupname); + if (gr_ind<0) + { + if (retflag!=NULL) *retflag=SSI_EDIT_ERR_NOTFOUND; + return false; + } + + SSIGroupEntry gen=ContactListGroups[gr_ind]; + + // Remove uins from this group + vector cont_vec; + for (size_t i=0; i & icon_data) +{ + // It's not possible to upload file more than 6kb + if (icon_data.size()>6144) return false; + + uint8_t addr_req_template[] = {0x00, 0x01, 0x31, /* Item name = "1", is an avatar id number as text */ + 0x00, 0x00, /* Group id */ + 0x18, 0x13, /* Item id */ + 0x00, 0x14, /* Item type - Own icon (avatar) info. */ + 0x00, 0x1A, /* Length of the additional data */ + 0x00, 0xD5, 0x00, 0x12, /* TLV 0xD5 - TLV for buddy icon info, len 0x12 */ + 0x01, /* avatar id number or icon flags? */ + 0x10, /* MD5 hash len - always 16 */ + 0xEC, 0x7B, 0x60, 0x67, 0xCF, 0xD1, 0x01, 0x99, 0x0C, 0xBD, 0x33, 0xF9, 0x00, 0xFC, 0x4B, 0x9B, /* md5 hash */ + 0x01, 0x31, 0x00, 0x00}; /* TLV 0x0131 - predefined contact name - empty */ + + calculate_md5((const char *)&icon_data[0], icon_data.size(), (char *)addr_req_template+17); + + for (size_t i=0; i data_vec(addr_req_template, addr_req_template+sizeof(addr_req_template)); + if (sendSNAC(0x13, 0x08, &req_id, &data_vec)!=1) return false; + + SNACData snd; + snd.service_id=0x0013; + snd.subtype_id=0x000E; + snd.req_id=req_id; + if (waitSNAC(&snd)!=1) return false; + + if (snd.data.size()<2) return false; + if (snd.data[0]!=0 || snd.data[1]!=0) return false; // Success code==0 + + SSIUINEntry icon_uen; + icon_uen.uin=myuin; + icon_uen.nick="1"; + icon_uen.groupid=0; + icon_uen.groupname=""; + icon_uen.itemid=ntohs(item_id); + icon_uen.waitauth=false; + icon_uen.have_icon=true; + icon_uen.icon_id=1; + icon_uen.icon_flags=0x01; + memcpy(icon_uen.icon_md5_hash, addr_req_template+17, 16); + icon_uen.icon_data=icon_data; + ContactListIcons.push_back(icon_uen); // Icon entry was added in SSI-list + + snd.data.clear(); + snd.service_id=0x0001; + snd.subtype_id=0x0021; + snd.req_id=0; + if (waitSNAC(&snd)!=1) { if (!ContactListIcons.empty()) removeAllMyIcons(); return false; } + + bool need_upload=false; + size_t pos=0; + while(posgetNetworkTimeout(); + icons_service->setNetworkTimeout(getNetworkTimeout()); + +#define DEL_ICON_SERV \ +if (icons_service && icons_service!=this) \ + { \ + icons_service->doDisconnect(); \ + delete icons_service; \ + icons_service=NULL; \ + } \ +if (!ContactListIcons.empty()) removeAllMyIcons(); + + data_vec.resize(4); + data_vec[0]=0; + data_vec[1]=1; // Icon id + uint16_t icon_len = htons(icon_data.size()); + memcpy(&data_vec[2], &icon_len, sizeof(icon_len)); + data_vec.insert(data_vec.end(), icon_data.begin(), icon_data.end()); + if (icons_service->sendSNAC(0x10, 0x02, &req_id, &data_vec)!=1) + { + DEL_ICON_SERV + return false; + } + + snd.data.clear(); + snd.service_id=0x0010; + snd.subtype_id=0x0003; + snd.req_id=req_id; + if (icons_service->waitSNAC(&snd)!=1) + { + DEL_ICON_SERV + return false; + } + + icons_service->setNetworkTimeout(old_serv_tm); + + if (snd.data.size()<21) + { + DEL_ICON_SERV + return false; + } + if (snd.data[4]!=16) + { + DEL_ICON_SERV + return false; + } + if (memcmp(addr_req_template+17, &snd.data[5], 16)!=0) + { + DEL_ICON_SERV + return false; + } + + snd.data.clear(); + snd.service_id=0x0001; + snd.subtype_id=0x0021; + snd.req_id=0; + if (waitSNAC(&snd)!=1) { if (!ContactListIcons.empty()) removeAllMyIcons(); return false; } + + pos=0; + while(posPRIV_CONTACTLIST_CAN_SEE) return false; + + SSIContactListItem cli; + cli.name=""; + cli.group_id=0; + cli.type=0x0004; + cli.payload.push_back(TLVField(stat, 0x00ca)); + + uint16_t oper; + if (myPrivSrvStatus_item_id==0) { cli.item_id=getUnusedItemID(); oper=SSI_ITEM_ADD; } + else { cli.item_id=myPrivSrvStatus_item_id; oper=SSI_ITEM_CHANGE; } + + vector data; + cli.encode_to(data, 0); + uint32_t sync_id; + if (sendSNAC(0x0013, oper, &sync_id, &data)!=1) return false; + uint16_t retflag; + if (!getSSIEditAck(retflag, sync_id)) return false; + if (retflag!=SSI_EDIT_OK) return false; + + myPrivSrvStatus=stat; + myPrivSrvStatus_item_id=cli.item_id; + return true; +} + +// --PRIVATE--PRIVATE--PRIVATE--PRIVATE--PRIVATE--PRIVATE-- +// --PRIVATE--PRIVATE--PRIVATE--PRIVATE--PRIVATE--PRIVATE-- +// --PRIVATE--PRIVATE--PRIVATE--PRIVATE--PRIVATE--PRIVATE-- + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getServiceList(void) +{ + SNACData snd; + snd.service_id=0x0001; + snd.subtype_id=0x0003; + snd.req_id=0; + if (waitSNAC(&snd)!=1) return false; + + // Server send to me SNAC families, which it supports. + size_t curr_pos=0; + server_services_list.clear(); + while(true) + { + if (snd.data.size()(serv_fam, 1)); + curr_pos+=sizeof(serv_fam); + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askServices(void) +{ + vector ask_serv_vec; + +// Ask for service only if one presents in server service list +#define TRY_ASK_SERVICE(arg_fam, arg_ver) { \ + bool service_found=false; \ + for (size_t i=0; i(serv_fam, serv_ver)); + curr_pos+=sizeof(serv_ver); + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askRateLimits(uint32_t * sync_id) +{ + return (sendSNAC(0x0001, 0x0006, sync_id)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getRateLimits(uint32_t sync_id) +{ + SNACData snd; + snd.service_id=0x0001; + snd.subtype_id=0x0007; + snd.req_id=sync_id; + if (waitSNAC(&snd)!=1) return false; + + // Server sends to me rate limits. + // I need to parse and remember it, because i need send + // back rate groups later + size_t curr_pos=0; + + if (snd.data.size()<(curr_pos+2)) return false; + uint16_t class_count; + memcpy(&class_count, &snd.data[curr_pos], sizeof(class_count)); + class_count=ntohs(class_count); + curr_pos+=sizeof(class_count); + + // Skip rate classes, we dont need it + if (snd.data.size()<(curr_pos+class_count*35)) return false; + curr_pos+=class_count*35; + + server_rate_groups.clear(); + while(true) + { + if (snd.data.size() vec(server_rate_groups.size()*2); + size_t curr_pos=0; + for (unsigned int i=0; i vec(aim2icq_capa, aim2icq_capa+sizeof(aim2icq_capa)); + + if ((i=subClientId.size())>0) + { + memcpy(&vec[16*5+8],&subClientId[0],i>8?8:i); + } + else + { + memcpy(&vec[16*5+8],"Unknown!",8); + } + + if ((i=subIGId.size())>0){ + memcpy(&vec[16*6+10],&subIGId[0],i>6?6:i); + }else{ + memcpy(&vec[16*6+10],"Unknown!",6); + } + + if (xStatus!=X_STATUS_NONE && xStatus<=X_STATUS_MAX_BOUND) + vec.insert(vec.end(), XStatus_arr[xStatus-1], XStatus_arr[xStatus-1]+16); + + TLVField tlv(vec, 0x0005); // CLSID values + vec.clear(); + tlv.encode_to(vec, 0); + + return (sendSNAC(0x0002, 0x0004, NULL, &vec)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askBLMLimit(uint32_t * sync_id) +{ + return (sendSNAC(0x0003, 0x0002, sync_id)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getBLMLimit(uint32_t sync_id) +{ + /* + We have TLVPack in SNAC data, but dont need in it. + There are tlvs like [Max number of contact list entries], + [Max number of watcher list entries], [Max online notifications] and another, in this pack. + */ + SNACData snd; + snd.service_id=0x0003; + snd.subtype_id=0x0003; + snd.req_id=sync_id; + return (waitSNAC(&snd)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askICBMParams(uint32_t * sync_id) +{ + return (sendSNAC(0x0004, 0x0004, sync_id)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getICBMParams(uint32_t sync_id) +{ + // Server sends to me ICBM parameters, i parse it for example. + // Shot form will be simple return waitSNAC(0x0004, 0x0005); + SNACData snd; + snd.service_id=0x0004; + snd.subtype_id=0x0005; + snd.req_id=sync_id; + if (waitSNAC(&snd)!=1) return false; + + ICBMParameters icbm_par; + if (!icbm_par.decode_from(snd.data)) return false; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendICBMParams(void) +{ + // I set my own ICBM parameters for each of channels + ICBMParameters icbm_par; + icbm_par.channel=0x0001; + icbm_par.msg_flags=0x0000000b; // bit1: messages allowed for specified channel + // bit2: missed calls notifications enabled for specified channel + // bit4: client supports typing notifications + icbm_par.max_snac_size=0x1f40; // 8000 bytes + icbm_par.max_send_warn_lev=0x03e7; // 999 + icbm_par.max_recv_warn_lev=0x03e7; // 999 + icbm_par.min_msg_interval=0; // msec + + vector vec; + icbm_par.encode_to(vec); + + if (sendSNAC(0x0004, 0x0002, NULL, &vec)!=1) return false; + + vec.clear(); + icbm_par.channel=0x0002; + icbm_par.msg_flags=0x00000003; + icbm_par.max_snac_size=0x1f40; // 8000 bytes + icbm_par.max_send_warn_lev=0x03e7; // 999 + icbm_par.max_recv_warn_lev=0x03e7; // 999 + icbm_par.min_msg_interval=0; // msec + icbm_par.encode_to(vec); + if (sendSNAC(0x0004, 0x0002, NULL, &vec)!=1) return false; + + vec.clear(); + icbm_par.channel=0x0004; + icbm_par.msg_flags=0x00000003; + icbm_par.max_snac_size=0x1f40; // 8000 bytes + icbm_par.max_send_warn_lev=0x03e7; // 999 + icbm_par.max_recv_warn_lev=0x03e7; // 999 + icbm_par.min_msg_interval=0; // msec + icbm_par.encode_to(vec); + if (sendSNAC(0x0004, 0x0002, NULL, &vec)!=1) return false; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askPRMLimit(uint32_t * sync_id) +{ + return (sendSNAC(0x0009, 0x0002, sync_id)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getPRMLimit(uint32_t sync_id) +{ + /* + We have TLVPack in SNAC data, but dont need in it. + There are tlvs like [Max number of visible list entries] and + [Max number of invisible list entries], in this pack. + */ + SNACData snd; + snd.service_id=0x0009; + snd.subtype_id=0x0003; + snd.req_id=sync_id; + return (waitSNAC(&snd)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askSSILimit(uint32_t * sync_id) +{ + return (sendSNAC(0x0013, 0x0002, sync_id)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getSSILimit(uint32_t sync_id) +{ + /* + We have TLVPack in SNAC data, but dont need in it. + There are tlvs like some SSI contact list limitations in this pack. + */ + SNACData snd; + snd.service_id=0x0013; + snd.subtype_id=0x0003; + snd.req_id=sync_id; + return (waitSNAC(&snd)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askSSICopy(uint32_t * sync_id) +{ + // Not 13,05 - we havent got local copy for update it! + return (sendSNAC(0x0013, 0x0004, sync_id)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getSSICopy(uint32_t sync_id) +{ + SNACData snd; + snd.service_id=0x0013; + snd.subtype_id=0x0006; + snd.flags=0x0001; + snd.req_id=sync_id; + + SSIContactList cl; + + while (snd.flags&0x0001) // Incomplete SNAC, it has continue in next + { + if (waitSNAC(&snd)!=1) + return false; + if (!cl.decode_from(snd.data)) + return false; + } + +// Clear only SSI contacts from list + for (size_t i=0; i=0) ContactListUins[i].groupname=ContactListGroups[gr_ind].name; +// addBLMContact(ContactListUins[i].uin); + } + + { + vector l; + l.resize(ContactListUins.size()); + for (unsigned int i=0; i vec; + TLVPack tlvp; + + tlvp.data.push_back(TLVField(STATUS_DCDISABLED|astat, 0x0006)); // Online status + if (ext) tlvp.data.push_back(TLVField((uint16_t)0x0000, 0x0008)); // Unknown + + uint8_t dc_info[] = { + 0x00, 0x00, 0x00, 0x00, /* Internal IP address */ + 0x00, 0x00, 0x00, 0x00, /* Internal TCP port */ + 0x04, /* DC type - DC not possible */ + 0x00, 0x09, /* DC protocol version - ICQ Lite (9) */ + 0x0a, 0x0b, 0x33, 0x45, /* DC Auth cookie */ + 0x00, 0x00, 0x0b, 0xf5, /* Web Front Port */ + 0x00, 0x00, 0x00, 0x01, /* Client futures */ + 0x00, 0x00, 0x00, 0x00, /* Last Info Update */ + 0x00, 0x00, 0x00, 0x00, /* Last Extended Info Update (i.e. icqphone status) */ + 0x00, 0x00, 0x00, 0x00, /* Last Extended Status Update (i.e. phonebook) */ + 0x00, 0x00 }; /* Unknown */ + tlvp.data.push_back(TLVField(dc_info, sizeof(dc_info), 0x000c)); // DC Info + + if (ext) tlvp.data.push_back(TLVField((uint16_t)0x0000, 0x001f)); // Unknown + + tlvp.encode_to(vec); + if (sendSNAC(0x0001, 0x001e, NULL, &vec)!=1) return false; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendReady(void) +{ + // Send to server SNAC families and versions, and tool ids and versions + vector ready_serv_vec; + +#define TRY_READY_SERVICE(arg_fam) { \ + bool service_found=false; \ + uint16_t service_version; \ + for (size_t i=0; ih_addr==NULL) return -1; + + memset(&addr, 0, sizeof(addr)); + memcpy(&(addr.sin_addr.s_addr), he->h_addr, sizeof(addr.sin_addr.s_addr)); + addr.sin_port=htons(aport); + addr.sin_family=PF_INET; + + if ((tmp_sock=socket(PF_INET, SOCK_STREAM, 0))==-1) return -1; + + if (connect(tmp_sock,(const struct sockaddr *)&addr, (socklen_t)sizeof(addr))) + { + CLOSE_SOCK(tmp_sock); + return -1; + } +#ifdef __FreeBSD__ + int opt = 1; + if(-1 == setsockopt(tmp_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt))){ + printf("\ndirectConnect: setsockopt(SO_NOSIGPIPE) failed with error: %i", errno); + } +#endif + + return tmp_sock; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::waitHello(void) +{ + FlapPacket fp; + int i=300; + do + { + onIdle(); + if (fp.recv_from(sock)) break; + if (fp.t_network_error!=TNETWORK_TIMEOUT) return false; + } + while(--i); + if (!i) return false; + + if (fp.frame_type!=FT_SIGNON) return false; + SignOnPacket sp; + if (!sp.decode_from(fp.payload)) return false; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +static string CalcPass(string s) +{ + static const char pxor[]={0xF3,0x26,0x81,0xC4,0x39,0x86,0xDB,0x92, + 0x71, 0xA3, 0xB9, 0xE6, 0x53, 0x7A, 0x95, 0x7C}; + string r=""; + char c; + unsigned int i=0; + while(i data_vec; + tlv_pack.encode_to(data_vec); + i=fp.payload.size(); + i+=data_vec.size(); + fp.payload.resize(i); + memcpy(&fp.payload[4],&data_vec[0],data_vec.size());*/ + return fp.send_to(sock); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendMD5saltrequest(uint32_t * snac_sync) +{ + TLVPack tlv_pack; + tlv_pack.data.push_back(TLVField(myuin, TLV_UID)); + tlv_pack.data.push_back(TLVField(NULL, 0, 0x004b)); // Unknown, empty + tlv_pack.data.push_back(TLVField(NULL, 0, 0x005a)); // Unknown, empty + + vector data_vec; + tlv_pack.encode_to(data_vec); + + if (sendSNAC(0x0017, 0x0006, snac_sync, &data_vec)!=1) return false; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getMD5salt(uint32_t snac_sync, vector & md5_salt) +{ + SNACData snd; + snd.service_id=0x0017; + snd.subtype_id=0x0000; + snd.req_id=snac_sync; + if (waitSNAC(&snd)!=1) return false; + + if (snd.subtype_id==0x0003) // Error + { + TLVPack tlv_pack; + if (tlv_pack.decode_from(snd.data)) + { + TLVField * err_tlv = tlv_pack.findTLV(0x0008); + if (err_tlv!=NULL) err_tlv->getAsInt16(connect_error_code); + TLVField * err_url_tlv = tlv_pack.findTLV(0x0004); + if (err_url_tlv!=NULL) err_url_tlv->getAsString(connect_error_url); + } + return false; + } + + if (snd.subtype_id!=0x0007) return false; // WTF? + + if (snd.data.size()<5) return false; + uint16_t auth_key_len = ntohs(*((uint16_t*)(&snd.data[0]))); + if (snd.data.size()<(auth_key_len+sizeof(auth_key_len))) return false; + md5_salt.assign(snd.data.begin()+sizeof(auth_key_len), snd.data.begin()+sizeof(auth_key_len)+auth_key_len); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::getMD5hash(vector md5_salt, uint8_t * md5_hash){ + vector auth_sum=md5_salt; + if (mypassword.length()>8) auth_sum.insert(auth_sum.end(), (uint8_t*)mypassword.data(), ((uint8_t*)mypassword.data())+8); // All passwords were cutted to 8 chars + else auth_sum.insert(auth_sum.end(), (uint8_t*)mypassword.data(), ((uint8_t*)mypassword.data())+mypassword.length()); + auth_sum.insert(auth_sum.end(), (uint8_t*)AOL_SALT_STR, ((uint8_t*)AOL_SALT_STR)+sizeof(AOL_SALT_STR)-1); + + calculate_md5((const char*)(&auth_sum[0]), auth_sum.size(), (char *)md5_hash); +} + +bool ICQKid2::sendMD5authorize(uint32_t * snac_sync, vector & md5_salt) +{ + TLVPack tlv_pack; + tlv_pack.data.push_back(TLVField(myuin, TLV_UID)); + tlv_pack.data.push_back(TLVField("IcqKid2", TLV_CLI_STR_ID)); // Client ID string + + uint8_t md5_hash[16]={0}; + getMD5hash(md5_salt,md5_hash); + + tlv_pack.data.push_back(TLVField(md5_hash, 16, 0x0025)); // MD5 hash of salt+password+AOL_SALT_STR + tlv_pack.data.push_back(TLVField((uint16_t)0x010a, TLV_CLI_NUM_ID)); // Client ID number + tlv_pack.data.push_back(TLVField((uint16_t)0x0014, TLV_CLI_VER_MAJOR)); // Client major version + tlv_pack.data.push_back(TLVField((uint16_t)0x0034, TLV_CLI_VER_MINOR)); // Client minor version + tlv_pack.data.push_back(TLVField((uint16_t)0x0000, TLV_CLI_VER_LESSER)); // Client lesser version + tlv_pack.data.push_back(TLVField((uint16_t)0x0bb8, TLV_CLI_NUM_BUILD)); // Client build number + tlv_pack.data.push_back(TLVField((uint32_t)0x0000043d, TLV_CLI_NUM_DISTR)); // Client distribution number + tlv_pack.data.push_back(TLVField("en", TLV_CLI_LANG)); // Client language + tlv_pack.data.push_back(TLVField("us", TLV_CLI_COUNTRY)); // Client country +// skip TLV 0x4a - SSI flag: 1 - SSI only, 0 - family 0x03 +// Versions of componets made like icq 5.1, otherwise avatars and xStatus couldn't work + + vector data_vec; + tlv_pack.encode_to(data_vec); + if (sendSNAC(0x0017, 0x0002, snac_sync, &data_vec)!=1) return false; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getBOSSparams(string & boss_host, int & boss_port, vector & cookie) +{ + FlapPacket fp; + int t=network_timeout; + do + { + if (fp.recv_from(sock)) break; + if (fp.t_network_error!=TNETWORK_TIMEOUT) return fp.t_network_error; + } + while(--t); + if (!t) return false; + if (fp.frame_type!=FT_DATA) return false; + SNACData snd; + if (!snd.decode_from(fp.payload)) return false; + TLVPack tlv_pack; + if (!tlv_pack.decode_from(snd.data)) return false; + TLVField * err_tlv = tlv_pack.findTLV(0x0008); + if (err_tlv!=NULL) // Packet contains error code + { + err_tlv->getAsInt16(connect_error_code); + TLVField * err_url_tlv = tlv_pack.findTLV(0x0004); + if (err_url_tlv!=NULL) err_url_tlv->getAsString(connect_error_url); +// return false; + } + TLVField * tlv_ptr = tlv_pack.findTLV(TLV_BOSS_ADDR); + if (tlv_ptr==NULL) return false; + string boss; + if (!tlv_ptr->getAsString(boss)) return false; + string::size_type i = boss.find(":"); + if (i==boss.npos || i>=(boss.length()-1)) return false; + boss_host.assign(boss, 0, i); + boss_port=atoi(string(boss, i+1).c_str()); + tlv_ptr = tlv_pack.findTLV(TLV_AUTH_COOKIE); + if (tlv_ptr==NULL) return false; + cookie=tlv_ptr->data; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendSignOff(void) +{ + FlapPacket fp; + fp.frame_type=FT_SIGNOFF; + fp.seq_number=flap_seq_number++; + return fp.send_to(sock); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendSignOn2(vector & cookie) +{ + FlapPacket fp; + fp.frame_type=FT_SIGNON; + fp.seq_number=flap_seq_number++; + + SignOnPacket sp; + sp.data.push_back(TLVField(cookie, TLV_AUTH_COOKIE)); + sp.encode_to(fp.payload); + return fp.send_to(sock); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +ICQKid2 * ICQKid2::getInstanceForService(uint16_t family) +{ + for (size_t i=0; i data(sizeof(family)); + memcpy(&data[0], &family, sizeof(family)); + uint32_t req_id; + if (sendSNAC(0x0001, 0x0004, &req_id, &data)!=1) return NULL; + + SNACData snd; + snd.service_id=0x0001; + snd.subtype_id=0x0005; + snd.req_id=req_id; + if (waitSNAC(&snd)!=1) return NULL; + + TLVPack tp; + if (!tp.decode_from(snd.data)) return NULL; + TLVField * tf = tp.findTLV(0x0d); //Snac family + if (tf==NULL) return NULL; + if (memcmp(&tf->data[0], &family, sizeof(family))!=0) return NULL; + + tf = tp.findTLV(0x05); //BOS address + if (tf==NULL) return NULL; + string bos_addr; + tf->getAsString(bos_addr); + + tf = tp.findTLV(0x06); //Auth cookie + if (tf==NULL) return NULL; + + ICQKid2 * serv_inst = new ICQKid2; + serv_inst->setUIN(myuin); + serv_inst->setPassword(mypassword); + serv_inst->setLoginHost(loginhost, loginport); + serv_inst->setProxy(proxy_host, proxy_port, proxy_uid, proxy_pwd, proxy_type); + serv_inst->setNetworkTimeout(getNetworkTimeout()); + + if (!serv_inst->doConnect_phase2(STATUS_ONLINE, bos_addr, loginport, tf->data, true)) + { + serv_inst->doDisconnect(); + delete serv_inst; + return NULL; + } + + for (size_t i=0; iserver_services_list.size(); ++i) + if (serv_inst->server_services_list[i].first==ntohs(family)) + return serv_inst; + + serv_inst->doDisconnect(); + delete serv_inst; + return NULL; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sub_getBuddyIcon(SSIUINEntry & uen) +{ + if (!uen.have_icon) return false; + + if (icons_service==NULL) + { + icons_service=getInstanceForService(0x0010); + if (icons_service==NULL) return false; + } + + int old_serv_tm = icons_service->getNetworkTimeout(); + icons_service->setNetworkTimeout(getNetworkTimeout()); + + DownloadBuddyIconRequest req; + req.uin=uen.uin; + req.icon_id=uen.icon_id; + req.icon_flags=uen.icon_flags; + memcpy(req.md5_hash, uen.icon_md5_hash, 16); + + vector data_vec; + req.encode_to(data_vec); + +#define REMOVE_ICONS_SERV \ + if (icons_service && icons_service!=this) \ + { \ + icons_service->doDisconnect(); \ + delete icons_service; \ + icons_service=NULL; \ + } +// End of define REMOVE_ICONS_SERV + + uint32_t req_id; + if (icons_service->sendSNAC(0x0010, 0x0006, &req_id, &data_vec)!=1) + { + REMOVE_ICONS_SERV + return false; + } + + SNACData snd; + snd.service_id=0x0010; + snd.subtype_id=0x0007; + snd.req_id=req_id; + if (icons_service->waitSNAC(&snd)!=1) + { + REMOVE_ICONS_SERV + return false; + } + + icons_service->setNetworkTimeout(old_serv_tm); + + DownloadBuddyIconReply rep; + if (!rep.decode_from(snd.data)) return false; + + uen.icon_data = rep.icon_data; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::addVisInvisIgnor(vector & cont, uint16_t it_type, string uin, string nick) +{ + int cont_ind=-1; + for (size_t i=0; i=0 && cont[cont_ind].nick==nick) return true; + + SSIContactListItem cli; + cli.name=uin; + cli.group_id=0; + cli.type=it_type; + if (nick!="") cli.payload.push_back(TLVField(nick, 0x0131)); + + uint16_t oper; + if (cont_ind<0) { cli.item_id=getUnusedItemID(); oper=SSI_ITEM_ADD; } + else { cli.item_id=cont[cont_ind].itemid; oper=SSI_ITEM_CHANGE; } + + vector data; + cli.encode_to(data, 0); + uint32_t sync_id; + if (sendSNAC(0x0013, oper, &sync_id, &data)!=1) return false; + uint16_t retflag; + if (!getSSIEditAck(retflag, sync_id)) return false; + if (retflag!=SSI_EDIT_OK) return false; + + if (cont_ind<0) + { + SSIUINEntry uen; + uen.uin=uin; + uen.groupid=0; + uen.itemid=cli.item_id; + uen.nick=nick; + cont.push_back(uen); + } + else + { + cont[cont_ind].nick=nick; + } + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::removeVisInvisIgnor(vector & cont, uint16_t it_type, string uin) +{ + int cont_ind=-1; + for (size_t i=0; i data; + cli.encode_to(data, 0); + uint32_t sync_id; + if (sendSNAC(0x0013, SSI_ITEM_DELETE, &sync_id, &data)!=1) return false; + uint16_t retflag; + if (!getSSIEditAck(retflag, sync_id)) return false; + if (retflag!=SSI_EDIT_OK) return false; + + for (size_t i=0; iclearOlderThan(SNAC_CACHE_LIFETIME); + if (snac_cache->findEntry(*snd)) + { + onIdle(); + return 1; + } + while (true) + { + FlapPacket fp; + do + { + if (network_break_flag) + { + network_break_flag=false; + return TNETWORK_TIMEOUT; + } + if (fp.recv_from(sock)) break; + if (fp.t_network_error!=TNETWORK_TIMEOUT) return fp.t_network_error; + } + while(--i); + if (!i) return TNETWORK_TIMEOUT; + if (fp.frame_type==FT_SIGNOFF) + { + TLVPack tlvp; + if (tlvp.decode_from(fp.payload)) + { + uint16_t err_code=0; + TLVField * err_tlv = tlvp.findTLV(0x0009); + if (err_tlv!=NULL) err_tlv->getAsInt16(err_code); + + string err_url; + TLVField * url_tlv = tlvp.findTLV(0x000b); + if (url_tlv!=NULL) url_tlv->getAsString(err_url); + + onSingOff(err_code, err_url); + } + return TNETWORK_CLOSE; + } + if (fp.frame_type!=FT_DATA) continue; + SNACData sncd; + if (!sncd.decode_from(fp.payload)) continue; + if (sncd.flags&0x8000 && sncd.data.size()>=2) + { + uint16_t trash_len; + memcpy(&trash_len, &sncd.data[0], sizeof(trash_len)); + trash_len=ntohs(trash_len); + if (sncd.data.size()>=(unsigned int)(2+trash_len)) + sncd.data.erase(sncd.data.begin(), sncd.data.begin()+2+trash_len); + } + if ((sncd.service_id!=snd->service_id && snd->service_id!=0) || \ + (sncd.subtype_id!=snd->subtype_id && snd->subtype_id!=0) || \ + (sncd.req_id!=snd->req_id && snd->req_id!=0)) + { + snac_cache->addEntry(sncd); + continue; + } + *snd=sncd; + break; + } + return 1; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +int ICQKid2::sendSNAC(uint16_t service_id, uint16_t subtype_id, uint32_t * req_id, vector * data_ptr) +{ + FlapPacket fp; + fp.frame_type=FT_DATA; + fp.seq_number=flap_seq_number++; + + SNACData sncd; + sncd.service_id=service_id; + sncd.subtype_id=subtype_id; + sncd.flags=0; + sncd.req_id=snac_seq_number++; + + if (data_ptr!=NULL) sncd.data=*data_ptr; + if (req_id!=NULL) *req_id=sncd.req_id; + + sncd.encode_to(fp.payload); + onIdle(); + if (network_break_flag) + { + network_break_flag=false; + return TNETWORK_TIMEOUT; + } + if (fp.send_to(sock)) + { + last_keepalive_timestamp=time(NULL); + return 1; + } + return fp.t_network_error; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +int ICQKid2::sendKeepAlive(void) +{ + FlapPacket fp; + fp.frame_type=FT_KEEP_ALIVE; + fp.seq_number=flap_seq_number++; + onIdle(); + if (fp.send_to(sock)) + { + last_keepalive_timestamp=time(NULL); + return 1; + } + return fp.t_network_error; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendAuthResponse(string uin, string text, uint8_t aflag) +{ + vector data(1+uin.length()+1+2+text.length()+2); + + data[0]=uin.length(); + memcpy(&data[1], uin.data(), uin.length()); + data[1+uin.length()]=aflag; + uint16_t text_len=htons(text.length()); + memcpy(&data[1+uin.length()+1], &text_len, sizeof(text_len)); + memcpy(&data[1+uin.length()+1+2], text.data(), text.length()); + memset(&data[1+uin.length()+1+2+text.length()], 0, 2); + + return (sendSNAC(0x0013, 0x001a, NULL, &data)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendAuthRequest(string uin, string text) +{ + vector data(1+uin.length()+2+text.length()+2); + + data[0]=uin.length(); + memcpy(&data[1], uin.data(), uin.length()); + uint16_t text_len=htons(text.length()); + memcpy(&data[1+uin.length()], &text_len, sizeof(text_len)); + memcpy(&data[1+uin.length()+2], text.data(), text.length()); + memset(&data[1+uin.length()+2+text.length()], 0, 2); + + return (sendSNAC(0x0013, 0x0018, NULL, &data)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseIncomingMsg(ICQKid2Message & msg, vector & data) +{ + msg.timestamp=time(NULL); + msg.cookie_valid=false; + + BaseIncomingMsg bim; + if (!bim.decode_from(data)) return false; + + if (bim.msg_channel==0x0001) // Channel 1 message format (plain-text messages) + { + SimpleMsg sim; + if (!sim.decode_from(bim.data)) return false; + msg.uin=bim.uin; + msg.text=sim.message_str; + switch(sim.charset) + { + case 0 : msg.enc_type=ICQKid2Message::USASCII; break; + case 2 : msg.enc_type=ICQKid2Message::UCS2BE; break; + case 3 : msg.enc_type=ICQKid2Message::LOCAL8BIT; break; + default : return false; + } + msg.codepage=sim.sub_charset; + return true; + } + else if (bim.msg_channel==0x0002) // Channel 2 message format (extended messages format) + { + ExtendedMsg exm; + if (!exm.decode_from(bim.data)) return false; + msg.uin=bim.uin; + msg.text=exm.rv_msg.tlv2711.text; + if (exm.rv_msg.tlv2711.msg_type==MSG_TYPE_PLAINTEXT) + { + if (exm.rv_msg.tlv2711.text_guid=="{0946134E-4C7F-11D1-8222-444553540000}" || \ + exm.rv_msg.tlv2711.text_guid=="{0946134e-4c7f-11d1-8222-444553540000}") msg.enc_type=ICQKid2Message::UTF8; + else msg.enc_type=ICQKid2Message::LOCAL8BIT; + + memcpy(msg.text_color, exm.rv_msg.tlv2711.text_color, 4); + memcpy(msg.bg_color, exm.rv_msg.tlv2711.bg_color, 4); + memcpy(msg.cookie,bim.msg_cookie,8); + msg.cookie_valid=true; + + //(void)sendMsgAutoResponse(bim.uin, bim.msg_cookie, MSG_TYPE_PLAINTEXT); // Plain text + return true; + } + else if (exm.rv_msg.tlv2711.msg_type==MSG_TYPE_AUTOAWAY || exm.rv_msg.tlv2711.msg_type==MSG_TYPE_AUTOBUSY || \ + exm.rv_msg.tlv2711.msg_type==MSG_TYPE_AUTONA || exm.rv_msg.tlv2711.msg_type==MSG_TYPE_AUTODND || \ + exm.rv_msg.tlv2711.msg_type==MSG_TYPE_AUTOFFC) + { + (void)sendMsgAutoResponse(bim.uin, bim.msg_cookie, exm.rv_msg.tlv2711.msg_type); + return false; // Don't need to use callback onIncomingMsg + } + else if (exm.rv_msg.tlv2711.msg_type==MSG_TYPE_PLUGIN) // Plugin message described by text string + { + // Process XtraZ Script + if (!exm.rv_msg.tlv2711.parseXtrazRequest()) return false; + if (bim.uin!=exm.rv_msg.tlv2711.xstat_sender_id) return false; + sendXStatusNotifyAutoResponse(bim.uin, bim.msg_cookie); + return false; // Don't need to use callback onIncomingMsg + } + } + else if (bim.msg_channel==0x0004) // Channel 4 message format (typed old-style messages) + { + OldStyleMsg osm; + if (!osm.decode_from(bim.data)) return false; + msg.uin=bim.uin; + msg.text=osm.message_str; + + // Messages types where FE-format presents + if (osm.msg_type==0x04 || osm.msg_type==0x06 || osm.msg_type==0x07 || osm.msg_type==0x08 || \ + osm.msg_type==0x09 || osm.msg_type==0x0C || osm.msg_type==0x0D || osm.msg_type==0x0E) + { + string::size_type fe_pos = msg.text.find_last_of("\376"); // 0xfe in oct + if (fe_pos!=msg.text.npos) msg.text.erase(0, fe_pos); + } + + switch(osm.msg_type) + { + case 0x06 : // MTYPE_AUTHREQ + onAuthRequest(msg.uin, msg.text); + return false; // Don't need to use callback onIncomingMsg +// break; + case 0x07 : // MTYPE_AUTHDENY + onAuthReply(msg.uin, msg.text, AUTH_DECLINED); + return false; // Don't need to use callback onIncomingMsg +// break; + case 0x08 : // MTYPE_AUTHOK + onAuthReply(msg.uin, msg.text, AUTH_ACCEPTED); + return false; // Don't need to use callback onIncomingMsg +// break; + case 0x0C : // MTYPE_ADDED + onWasAdded(msg.uin); + return false; // Don't need to use callback onIncomingMsg +// break; + case 0x1A : // MTYPE_PLUGIN + { + TLV2711 tlv_2711; + tlv_2711.xtraz_script_text=msg.text; + if (!tlv_2711.parseXtrazRequest()) return false; + if (bim.uin!=tlv_2711.xstat_sender_id) return false; + sendXStatusNotifyAutoResponse(bim.uin, bim.msg_cookie); + return false; // Don't need to use callback onIncomingMsg + } +// break; + default : + break; + } + return true; + } + + return false; // Unknown message channel +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseIncomingMTN(string & from, uint16_t & type, vector & data) +{ + if (data.size()<13) return false; + int from_length = data[10]; + if (data.size()<(size_t)(13+from_length)) return false; + from.assign(reinterpret_cast(&data[11]), from_length); + memcpy(&type, &data[11+from_length], sizeof(type)); + type=ntohs(type); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseAuthRequest(string & from, string & text, vector & data) +{ + if (data.empty()) return false; + uint8_t from_length=data[0]; + + if (data.size()<(unsigned int)(1+from_length)) return false; + from.assign(reinterpret_cast(&data[1]), from_length); + + if (data.size()<(unsigned int)(1+from_length+2)) return false; + uint16_t reason_length; + memcpy(&reason_length, &data[1+from_length], sizeof(reason_length)); + reason_length=ntohs(reason_length); + + if (data.size()<(unsigned int)(1+from_length+2+reason_length)) return false; + text.assign(reinterpret_cast(&data[1+from_length+2]), reason_length); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseAuthReply(string & from, string & text, uint8_t & aflag, vector & data) +{ + if (data.empty()) return false; + uint8_t from_length=data[0]; + if (data.size()<(unsigned int)(1+from_length+1)) return false; + from.assign(reinterpret_cast(&data[1]), from_length); + aflag=data[1+from_length]; + if (data.size()<(unsigned int)(1+from_length+1+2)) return false; + uint16_t reason_length; + memcpy(&reason_length, &data[1+from_length+1], sizeof(reason_length)); + reason_length=ntohs(reason_length); + if (data.size()<(unsigned int)(1+from_length+1+2+reason_length)) return false; + text.assign(reinterpret_cast(&data[1+from_length+1+2]), reason_length); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseOnlineNotify(string & uin, uint32_t & stat, vector & data, vector & xs, uint8_t & clientid) { + size_t curr_pos=0; + + if (data.empty()) return false; + uint8_t uin_length=data[curr_pos++]; + + if (data.size()<(curr_pos+uin_length)) return false; + uin.assign(reinterpret_cast(&data[curr_pos]), uin_length); + curr_pos+=uin_length; + curr_pos+=2; // Skip uint16_t warn_level - unused in ICQ + + if (data.size()<(curr_pos+2)) return false; + uint16_t tlv_count; + memcpy(&tlv_count, &data[curr_pos], sizeof(tlv_count)); + tlv_count=ntohs(tlv_count); + curr_pos+=2; + + stat=STATUS_ONLINE; // default value; + uint32_t int_ip=0, int_port=0, ext_ip=0; // default value; + + int uen_ind=findCLUIN(uin); + if (uen_ind>=0) + { + ContactListUins[uen_ind].have_icon=false; + ContactListUins[uen_ind].srv_relay_cap=false; + ContactListUins[uen_ind].unicode_cap=false; + ContactListUins[uen_ind].proto=0; + } + + clientid=0; + for (int i=0; i=0) + { + uint16_t p; + memcpy(&p,&tlv.data[9],2); + ContactListUins[uen_ind].proto=ntohs(p); + } + int_port=ntohl(int_port); + } + else if (tlv.type==0x000a) // Ext IP + { + if (tlv.data.size()<4) return false; + memcpy(&ext_ip, &tlv.data[0], sizeof(ext_ip)); + } + else/* if (tlv.type==0x001d && uen_ind>=0) // UserIcon Info + { + if (tlv.data.size()<20) return false; + size_t curpos=0; + + memcpy(&ContactListUins[uen_ind].icon_id, &tlv.data[curpos], sizeof(ContactListUins[uen_ind].icon_id)); + ContactListUins[uen_ind].icon_id=ntohs(ContactListUins[uen_ind].icon_id); + curpos+=sizeof(ContactListUins[uen_ind].icon_id); + + ContactListUins[uen_ind].icon_flags=tlv.data[curpos]; + curpos++; + + uint8_t hash_len=tlv.data[curpos]; + if (hash_len!=16) return false; + curpos++; + + uint8_t tmp_md5_hash[16]; + memcpy(tmp_md5_hash, &tlv.data[curpos], 16); + + ContactListUins[uen_ind].have_icon=true; + + if (memcmp(tmp_md5_hash, ContactListUins[uen_ind].icon_md5_hash, 16)!=0) + { + memcpy(ContactListUins[uen_ind].icon_md5_hash, tmp_md5_hash, 16); + onIconChanged(uin); + } + } + else */if (tlv.type==0x0019 && uen_ind>=0) // New type capabilities list + { + uint8_t cap_templ[16]={0x09, 0x46, 0xFF, 0xFF, 0x4c, 0x7f, 0x11, 0xd1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}; + vector reconstruct_capas; + for (size_t j=0; j=0) // Classic capabilities list + { + if (!ContactListUins[uen_ind].srv_relay_cap) ContactListUins[uen_ind].srv_relay_cap=haveSrvRelayCapability(tlv.data); + if (!ContactListUins[uen_ind].unicode_cap) ContactListUins[uen_ind].unicode_cap=haveUnicodeCapability(tlv.data); + if (!clientid){ + clientid=getClientID(tlv.data); + } + if (!ContactListUins[uen_ind].ig_cap){ + ContactListUins[uen_ind].ig_cap=haveIGCapability(tlv.data); + } + if ((clientid==1)&&(!ContactListUins[uen_ind].ig_cap)){ + ContactListUins[uen_ind].pidor=true; + } + if (haveXtrazCapability(tlv.data, xs)) + { +// sendXtrazRequest(uin); + } + else + { + xs.clear(); + if (!ContactListUins[uen_ind].xStatus.empty() || ContactListUins[uen_ind].xStatusTitle!="" || ContactListUins[uen_ind].xStatusDescription!="") + { +// ContactListUins[uen_ind].xStatus=X_STATUS_NONE; + ContactListUins[uen_ind].xStatusTitle=""; + ContactListUins[uen_ind].xStatusDescription=""; +// onXstatusChanged(uin, X_STATUS_NONE, "", ""); + } + } + } + else if (tlv.type==0x0003 && uen_ind>=0 && tlv.data.size()>=4) // Online since + { + ContactListUins[uen_ind].online_since=ntohl(*((uint32_t*)&tlv.data[0])); + } + else if (tlv.type==0x0004 && uen_ind>=0 && tlv.data.size()>=2) // Idle since + { + ContactListUins[uen_ind].idle_since=time(NULL)-60*ntohs(*((uint16_t*)&tlv.data[0])); + } + } + + if ((int_ip!=0 || int_port!=0 || ext_ip!=0) && uen_ind>=0) + { + if (int_ip!=0) ContactListUins[uen_ind].last_internal_ip=int_ip; + if (int_port!=0) ContactListUins[uen_ind].last_internal_port=int_port; + if (ext_ip!=0) ContactListUins[uen_ind].last_external_ip=ext_ip; + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseOfflineNotify(string & uin, vector & data) +{ + size_t curr_pos=0; + + if (data.empty()) return false; + uint8_t uin_length=data[curr_pos++]; + + if (data.size()<(curr_pos+uin_length)) return false; + uin.assign(reinterpret_cast(&data[curr_pos]), uin_length); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseSSIAddNotify(vector & data) +{ + size_t curr_pos=0; + + while(true) + { + if (curr_pos>=data.size()) break; + SSIContactListItem clit; + if (!clit.decode_from(data, curr_pos)) return false; + curr_pos += 2+clit.name_length+2+2+2+2+clit.payload_length; + + if (clit.type==0x0000) // Buddy record + { + SSIUINEntry uen; + uen.uin=clit.name; + uen.groupid=clit.group_id; + uen.itemid=clit.item_id; + uen.nick=""; + uen.groupname=""; + uen.waitauth=false; + for (unsigned int j=0; j=0) + ContactListUins[uen_ind]=uen; + else + ContactListUins.push_back(uen); + } + + if (clit.type==0x0001 && clit.group_id!=0x0000) // We dont need to virtual MasterGroup with group_id=0 + { + SSIGroupEntry gen; + gen.id=clit.group_id; + gen.name=clit.name; + + int gen_ind=findCLGroup(gen.id); + if (gen_ind>=0) + ContactListGroups[gen_ind]=gen; + else + ContactListGroups.push_back(gen); + } + } + + for (unsigned int i=0; i=0) ContactListUins[i].groupname=ContactListGroups[gr_ind].name; + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseSSIUpdateNotify(vector & data) +{ + // parseSSIAddNotify is universal method for adding and update, will use it + return parseSSIAddNotify(data); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseSSIDeleteNotify(vector & data) +{ + size_t curr_pos=0; + + while(true) + { + if (curr_pos>=data.size()) break; + SSIContactListItem clit; + if (!clit.decode_from(data, curr_pos)) return false; + curr_pos += 2+clit.name_length+2+2+2+2+clit.payload_length; + + if (clit.type==0x0000) // Buddy record + { + int uen_ind; + while ((uen_ind=findCLUIN(clit.item_id))>=0) + ContactListUins.erase(ContactListUins.begin()+uen_ind); + } + + if (clit.type==0x0001 && clit.group_id!=0x0000) // We dont need to virtual MasterGroup with group_id=0 + { + // Remove uins from this group + for (size_t i=0; i=0) + ContactListGroups.erase(ContactListGroups.begin()+gen_ind); + } + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseWasAdded(string & from, vector & data) +{ + if (data.empty()) return false; + uint8_t from_length=data[0]; + if (data.size()<(size_t)(1+from_length)) return false; + from.assign(reinterpret_cast(&data[1]), from_length); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseXtrazResponse(string & from, vector & x_status, string & x_title, string & x_descr, vector & data) +{ + if (data.size()<11) return false; + if (data[9]!=2) return false; // msg channel + uint8_t uin_len=data[10]; + if (data.size()<(size_t)(14+uin_len)) return false; + from.assign((const char*)&data[11], uin_len); + if (data[12+uin_len]!=3) return false; // Reason: channel specific + TLV2711 tlv2711; + if (!tlv2711.decode_from(data, 13+uin_len)) return false; + if (!tlv2711.parseXtrazResponse()) return false; + +// if (tlv2711.x_status>X_STATUS_MAX_BOUND) return false; + + x_status=tlv2711.x_status; + x_title=tlv2711.x_title; + x_descr=tlv2711.x_descr; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::parseMsgAutoResponse(ICQKid2Message & msg, uint8_t & type, vector & data) +{ + msg.timestamp=time(NULL); + + msg.cookie_valid=false; + + if (data.size()<11) return false; + memcpy(msg.cookie,&data[0],8); + msg.cookie_valid=true; + + if (data[9]!=2) return false; // msg channel + uint8_t uin_len=data[10]; + if (data.size()<(size_t)(14+uin_len)) return false; + msg.uin.assign((const char*)&data[11], uin_len); + + onClientMsgAck(msg.uin,&data[0]); + + if (data[12+uin_len]!=3) return false; // Reason: channel specific + TLV2711 tlv2711; + if (!tlv2711.decode_from(data, 13+uin_len)) return false; + + type=tlv2711.msg_type; + if (typeMSG_TYPE_AUTOFFC) return false; + + msg.text=tlv2711.text; + + if (tlv2711.text_guid=="{0946134E-4C7F-11D1-8222-444553540000}" || \ + tlv2711.text_guid=="{0946134e-4c7f-11d1-8222-444553540000}") msg.enc_type=ICQKid2Message::UTF8; + else msg.enc_type=ICQKid2Message::LOCAL8BIT; + + memcpy(msg.text_color, tlv2711.text_color, 4); + memcpy(msg.bg_color, tlv2711.bg_color, 4); + + return true; +} + +bool ICQKid2::parseMsgServerResponse( vector & data) +{ + string uin; + if (data.size()<11) return false; +// memcpy(cookie,&data[0],8); + uint8_t uin_len=data[10]; + if (data.size()<(size_t)(11+uin_len)) return false; + uin.assign((const char*)&data[11], uin_len); + onServerMsgAck(uin,&data[0]); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sendMsgAutoResponse(string touin, uint8_t * msg_cookie, uint8_t type) +{ + vector data; + size_t pos=0; + + data.resize(pos+8); + memcpy(&data[pos], msg_cookie, 8); + pos+=8; + + data.resize(pos+3); + data[pos]=0; + data[pos+1]=2; // message channel + data[pos+2]=touin.length(); + pos+=3; + + data.resize(pos+touin.length()); + memcpy(&data[pos], touin.data(), touin.length()); + pos+=touin.length(); + + data.resize(pos+2); + data[pos]=0; + data[pos+1]=3; // reason code - channel specific + pos+=2; + + TLV2711 tlv_2711; + tlv_2711.version=9; + tlv_2711.cookie=0; + tlv_2711.msg_type=type; + tlv_2711.status=0; + tlv_2711.priority=0; + switch(type) + { + case MSG_TYPE_PLAINTEXT : + tlv_2711.flags=0; + tlv_2711.text=""; + break; + + case MSG_TYPE_AUTOAWAY : + tlv_2711.flags=0x03; // +Normal msg, +auto msg, -multi msg + tlv_2711.text=AutoAwayMessageText; + break; + + case MSG_TYPE_AUTOBUSY : + tlv_2711.flags=0x03; // +Normal msg, +auto msg, -multi msg + tlv_2711.text=AutoBusyMessageText; + break; + + case MSG_TYPE_AUTONA : + tlv_2711.flags=0x03; // +Normal msg, +auto msg, -multi msg + tlv_2711.text=AutoNotAvailableMessageText; + break; + + case MSG_TYPE_AUTODND : + tlv_2711.flags=0x03; // +Normal msg, +auto msg, -multi msg + tlv_2711.text=AutoDoNotDisturbMessageText; + break; + + case MSG_TYPE_AUTOFFC : + tlv_2711.flags=0x03; // +Normal msg, +auto msg, -multi msg + tlv_2711.text=AutoFreeForChatMessageText; + break; + } + tlv_2711.encode_to(data, pos); + + return (sendSNAC(0x0004, 0x000b, NULL, &data)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::sengMsgAutoRequest(string touin, uint8_t type) +{ + BaseOutgoingMsg bom; + bom.msg_channel=0x0002; + bom.uin=touin; + memset(bom.msg_cookie, 0xAB, 8); + + ExtendedMsg ex_msg; + ex_msg.user_class=0x0050; + ex_msg.user_status=(online_status|STATUS_DCDISABLED); + ex_msg.online_time=0; + ex_msg.create_time=0; + + uint8_t icq_serv_relay[] = {0x09, 0x46, 0x13, 0x49, 0x4c, 0x7f, 0x11, 0xd1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}; + ex_msg.rv_msg.type=0; // request + memcpy(ex_msg.rv_msg.capability, icq_serv_relay, 16); + ex_msg.rv_msg.external_ip=0; + ex_msg.rv_msg.external_port=0; + memcpy(ex_msg.rv_msg.cookie, bom.msg_cookie, 8); + + ex_msg.rv_msg.tlv2711.version=9; + ex_msg.rv_msg.tlv2711.cookie=0; + ex_msg.rv_msg.tlv2711.msg_type=type; + ex_msg.rv_msg.tlv2711.flags=0x03; // +Normal msg, +auto msg, -multi msg + ex_msg.rv_msg.tlv2711.status=0; + ex_msg.rv_msg.tlv2711.priority=0; + ex_msg.rv_msg.tlv2711.text=""; + + ex_msg.encode_to(bom.data); + vector data; + bom.encode_to(data); + + sendSNAC(0x0004, 0x0006, NULL, &data); + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askOfflMsgAction(uint8_t flag, uint32_t * sync_id) +{ + uint8_t templ[10] = {0x08, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF}; //0xFF - variable fields + vector vec(templ, &templ[10]); + uint32_t int_uin = atol(myuin.c_str()); + memcpy(&vec[2], &int_uin, sizeof(int_uin)); // LITTLE ENDIAN!!! + memcpy(&vec[8], &flap_seq_number, sizeof(flap_seq_number)); // LITTLE ENDIAN!!! + vec[6]=flag; + + TLVField tlv(vec, 0x01); + vec.clear(); + tlv.encode_to(vec, 0); + + return (sendSNAC(0x0015, 0x0002, sync_id, &vec)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +//Snac 15/03 +bool ICQKid2::getOfflMsg(SNACData & snd) +{ +// offl_vec.clear(); + +// while (true) + { + if (snd.req_id!=offl_msg_sync_id) return false; + //SNACData snd; + //snd.service_id=0x0015; + //snd.subtype_id=0x0003; + //snd.req_id=sync_id; + //if (waitSNAC(&snd)!=1) return false; + + TLVField tlv; + if (!tlv.decode_from(snd.data, 0)) return false; + if (tlv.type!=1) return false; //Ãóàíî êàêîå-òî ;) + if (tlv.data.size()<10) return false; + + uint16_t offl_type; + memcpy(&offl_type, &tlv.data[6], sizeof(offl_type)); //LITTLE ENDIAN + + if (offl_type==0x0042) + { + if (!askOfflMsgAction(OFFLINE_DELETE, &offl_msg_sync_id)) return false; + //break; // End of offline messages + return true; + } + if (offl_type!=0x0041) return false; // If not end, must be 0x0041 + + if (tlv.data.size()<25) return false; + + uint32_t sender_uin; + memcpy(&sender_uin, &tlv.data[10], sizeof(sender_uin)); //LITLE ENDIAN + + uint16_t year; + memcpy(&year, &tlv.data[14], sizeof(year)); //LITLE ENDIAN + + struct tm tm_time; + tm_time.tm_isdst=0; + tm_time.tm_year=year-1900; + tm_time.tm_mon=tlv.data[16]-1; + tm_time.tm_mday=tlv.data[17]; + tm_time.tm_hour=tlv.data[18]; + tm_time.tm_min=tlv.data[19]; + tm_time.tm_sec=0; + + char offtime[256]; + sprintf(offtime, "[Offline message %d:%d %02d.%02d.%d]\r\n", + tm_time.tm_hour, + tm_time.tm_min, + tm_time.tm_mday, + tm_time.tm_mon+1, + tm_time.tm_year+1900); + + uint16_t str_len; + memcpy(&str_len, &tlv.data[22], sizeof(str_len)); //LITLE ENDIAN + + if (tlv.data.size()<(unsigned int)(24+str_len) || str_len<1) return false; + + string text(reinterpret_cast(&tlv.data[24]), str_len-1); + string txt = offtime + text; + + ostringstream ss; + ss << sender_uin; + { + ICQKid2Message msg(ss.str(), txt, ICQKid2Message::LOCAL8BIT, 0, true); + msg.timestamp=mktime(&tm_time);//-timezone; + onIncomingMsg(msg); + } + } + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askShortInfo(string uin, uint32_t * sync_id) +{ + uint8_t templ[16] = {0x0e, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xd0, 0x07, 0xFF, 0xFF, 0xba, 0x04, 0xFF, 0xFF, 0xFF, 0xFF}; + vector vec(templ, &templ[16]); + uint32_t int_myuin = atol(myuin.c_str()); + memcpy(&vec[2], &int_myuin, sizeof(int_myuin)); // LITTLE ENDIAN!!! + uint32_t int_uin = atol(uin.c_str()); + memcpy(&vec[12], &int_uin, sizeof(int_uin)); // LITTLE ENDIAN!!! + memcpy(&vec[8], &flap_seq_number, sizeof(flap_seq_number)); // LITTLE ENDIAN!!! + + TLVField tlv(vec, 0x01); + vec.clear(); + tlv.encode_to(vec, 0); + + return (sendSNAC(0x0015, 0x0002, sync_id, &vec)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getShortInfo(ICQKidShortUserInfo & info, uint32_t sync_id) +{ + SNACData snd; + snd.service_id=0x0015; + snd.subtype_id=0x0003; + snd.req_id=sync_id; + if (waitSNAC(&snd)!=1) return false; + + TLVField tlv; + if (!tlv.decode_from(snd.data, 0)) return false; + if (tlv.type!=1) return false; + if (tlv.data.size()<15) return false; + if (tlv.data[12]!=0x0a) return false; + + size_t curr_pos=13; + + uint16_t nick_length; + memcpy(&nick_length, &tlv.data[curr_pos], sizeof(nick_length)); // LITTLE ENDIAN!!! + if (nick_length<1) return false; + curr_pos += 2; + if (tlv.data.size()<(curr_pos+nick_length)) return false; + info.Nickname.assign(reinterpret_cast(&tlv.data[curr_pos]), nick_length-1); + curr_pos += nick_length; + + if (tlv.data.size()<(curr_pos+2)) return false; + uint16_t fname_length; + memcpy(&fname_length, &tlv.data[curr_pos], sizeof(fname_length)); // LITTLE ENDIAN!!! + if (fname_length<1) return false; + curr_pos += 2; + if (tlv.data.size()<(curr_pos+fname_length)) return false; + info.Firstname.assign(reinterpret_cast(&tlv.data[curr_pos]), fname_length-1); + curr_pos += fname_length; + + if (tlv.data.size()<(curr_pos+2)) return false; + uint16_t lname_length; + memcpy(&lname_length, &tlv.data[curr_pos], sizeof(lname_length)); // LITTLE ENDIAN!!! + if (lname_length<1) return false; + curr_pos += 2; + if (tlv.data.size()<(curr_pos+lname_length)) return false; + info.Lastname.assign(reinterpret_cast(&tlv.data[curr_pos]), lname_length-1); + curr_pos += lname_length; + + if (tlv.data.size()<(curr_pos+2)) return false; + uint16_t email_length; + memcpy(&email_length, &tlv.data[curr_pos], sizeof(email_length)); // LITTLE ENDIAN!!! + if (email_length<1) return false; + curr_pos += 2; + if (tlv.data.size()<(curr_pos+email_length)) return false; + info.Email.assign(reinterpret_cast(&tlv.data[curr_pos]), email_length-1); + curr_pos += email_length; + + if (tlv.data.size()<(curr_pos+1)) return false; + if (tlv.data[curr_pos]!=0) info.Needauth=true; + else info.Needauth=false; + curr_pos += 2; // Skip unknown char 00 + + if (tlv.data.size()<(curr_pos+1)) return false; + info.Gender = tlv.data[curr_pos]; + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::askFullInfo(string uin, uint32_t * sync_id) +{ + uint8_t data_templ[] = {0x00, 0x01, /* TLV.Type(1) - encapsulated META_DATA */ + 0x00, 0x10, /* TLV Length */ + 0x0E, 0x00, /* data chunk size LE */ + 0xFF, 0xFF, 0xFF, 0xFF, /* request owner uin in LE integer form */ + 0xD0, 0x07, /* data type: META_DATA_REQ */ + 0x01, 0x00, /* request sequence number */ + 0xFF, 0xFF, /* data subtype */ + 0xFF, 0xFF, 0xFF, 0xFF}; /* uin to search in LE integer form */ + + uint32_t my_int_uin=atoi(myuin.c_str()); + memcpy(data_templ+6, &my_int_uin, sizeof(my_int_uin)); + if (uin==myuin) { data_templ[14]=0xb2; data_templ[15]=0x04; } // Selfinfo request + else { data_templ[14]=0xd0; data_templ[15]=0x04; } // Foreign info request + uint32_t int_uin=atoi(uin.c_str()); + memcpy(data_templ+16, &int_uin, sizeof(int_uin)); + + vector data(data_templ, data_templ+sizeof(data_templ)); + + return (sendSNAC(0x0015, 0x0002, sync_id, &data)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getFullInfo(ICQKidFullUserInfo & info, uint32_t sync_id) +{ + SNACData snd; + snd.service_id=0x0015; + snd.subtype_id=0x0003; + snd.req_id=sync_id; + snd.flags=0x0001; + + uint32_t my_int_uin=atoi(myuin.c_str()); + + while(snd.flags&0x0001) + { + if (waitSNAC(&snd)!=1) return false; + if (snd.data.size()<17) return false; + if (snd.data[0]!=0 || snd.data[1]!=1) return false; // TLV.Type(1) - encapsulated META_DATA + if (memcmp(&snd.data[6], &my_int_uin, sizeof(my_int_uin))!=0) return false; + if (snd.data[10]!=0xDA || snd.data[11]!=0x07) return false; // data type: META_DATA + + if (snd.data[14]==0xC8 && snd.data[15]==0x00) // data subtype: META_BASIC_USERINFO + { + parseBasicUserInfo(snd.data, info); + } + else if (snd.data[14]==0xDC && snd.data[15]==0x00) // data subtype: META_MORE_USERINFO + { + parseMoreUserInfo(snd.data, info); + } + else if (snd.data[14]==0xEB && snd.data[15]==0x00) // data subtype: META_EMAIL_USERINFO + { + parseEmailUserInfo(snd.data, info); + } + else if (snd.data[14]==0x0E && snd.data[15]==0x01) // data subtype: META_HPAGECAT_USERINFO + { + parseHpagecatUserInfo(snd.data, info); + } + else if (snd.data[14]==0xD2 && snd.data[15]==0x00) // data subtype: META_WORK_USERINFO + { + parseWorkUserInfo(snd.data, info); + } + else if (snd.data[14]==0xE6 && snd.data[15]==0x00) // data subtype: META_NOTES_USERINFO + { + parseNotesUserInfo(snd.data, info); + } + else if (snd.data[14]==0xF0 && snd.data[15]==0x00) // data subtype: META_INTERESTS_USERINFO + { + parseInterestsUserInfo(snd.data, info); + } + else if (snd.data[14]==0xFA && snd.data[15]==0x00) // data subtype: META_AFFILIATIONS_USERINFO + { + parseAffiliationsUserInfo(snd.data, info); + } + } + + return true; +} + +#define UNPACK_LE(arg) \ + if (data.size()<(pos+sizeof(arg))) return; \ + memcpy(&arg, &data[pos], sizeof(arg)); \ + pos+=sizeof(arg); + +#define UNPACK_ASCIIZ(str) \ + { \ + uint16_t strz_len; \ + UNPACK_LE(strz_len) \ + if (data.size()<(pos+strz_len)) return; \ + if (strz_len>1) str.assign(reinterpret_cast(&data[pos]), strz_len-1); \ + else str=""; \ + pos+=strz_len; \ + } + + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseBasicUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + size_t pos=16; + if (data[pos++]!=0x0a) return; + + UNPACK_ASCIIZ(info.Nickname) + UNPACK_ASCIIZ(info.Firstname) + UNPACK_ASCIIZ(info.Lastname) + UNPACK_ASCIIZ(info.Email) + UNPACK_ASCIIZ(info.Homecity) + UNPACK_ASCIIZ(info.Homestate) + UNPACK_ASCIIZ(info.Homephone) + UNPACK_ASCIIZ(info.Homefax) + UNPACK_ASCIIZ(info.Homeaddress) + UNPACK_ASCIIZ(info.Cellphone) + UNPACK_ASCIIZ(info.Homezip) + UNPACK_LE(info.Homecountry) + UNPACK_LE(info.GMToffset) + UNPACK_LE(info.Auth) + UNPACK_LE(info.Webaware) + UNPACK_LE(info.DCpermissions) + UNPACK_LE(info.Pubprimarymail) +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseMoreUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + size_t pos=16; + if (data[pos++]!=0x0a) return; + + UNPACK_LE(info.Age) + UNPACK_LE(info.Gender) + UNPACK_ASCIIZ(info.Homepage) + UNPACK_LE(info.Birthyear) + UNPACK_LE(info.Birthmonth) + UNPACK_LE(info.Birthday) + UNPACK_LE(info.Language1) + UNPACK_LE(info.Language2) + UNPACK_LE(info.Language3) + pos+=2; + UNPACK_ASCIIZ(info.Origcity) + UNPACK_ASCIIZ(info.Origstate) + UNPACK_LE(info.Origcountry) + UNPACK_LE(info.Maritalstatus) + UNPACK_LE(info.Allowspam) + UNPACK_LE(info.Codepage) +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseEmailUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + info.Emails.clear(); + + size_t pos=16; + if (data[pos++]!=0x0a) return; + + uint8_t emails_count; + UNPACK_LE(emails_count) + + string email; + uint8_t pub_flag; + + for (size_t i=0; i<(size_t)emails_count; ++i) + { + UNPACK_LE(pub_flag); + UNPACK_ASCIIZ(email); + info.Emails.push_back(pair(email, pub_flag)); + } +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseHpagecatUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + size_t pos=16; + if (data[pos++]!=0x0a) return; + + UNPACK_LE(info.Homepageenabled) + UNPACK_LE(info.Homepagecategory) + UNPACK_ASCIIZ(info.Homepagekeywords) +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseWorkUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + size_t pos=16; + if (data[pos++]!=0x0a) return; + + UNPACK_ASCIIZ(info.Workcity) + UNPACK_ASCIIZ(info.Workstate) + UNPACK_ASCIIZ(info.Workphone) + UNPACK_ASCIIZ(info.Workfax) + UNPACK_ASCIIZ(info.Workaddress) + UNPACK_ASCIIZ(info.Workzip) + UNPACK_LE(info.Workcountry) + UNPACK_ASCIIZ(info.Workcompany) + UNPACK_ASCIIZ(info.Workdepartment) + UNPACK_ASCIIZ(info.Workposition) + UNPACK_LE(info.Workoccupation) + UNPACK_ASCIIZ(info.Workwebpage) +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseNotesUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + size_t pos=16; + if (data[pos++]!=0x0a) return; + + UNPACK_ASCIIZ(info.Notes) +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseInterestsUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + size_t pos=16; + if (data[pos++]!=0x0a) return; + + pos++; + UNPACK_LE(info.Interest1category) + UNPACK_ASCIIZ(info.Interest1) + UNPACK_LE(info.Interest2category) + UNPACK_ASCIIZ(info.Interest2) + UNPACK_LE(info.Interest3category) + UNPACK_ASCIIZ(info.Interest3) + UNPACK_LE(info.Interest4category) + UNPACK_ASCIIZ(info.Interest4) +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::parseAffiliationsUserInfo(vector & data, ICQKidFullUserInfo & info) +{ + size_t pos=16; + if (data[pos++]!=0x0a) return; + + pos++; + UNPACK_LE(info.Past1category) + UNPACK_ASCIIZ(info.Past1) + UNPACK_LE(info.Past2category) + UNPACK_ASCIIZ(info.Past2) + UNPACK_LE(info.Past3category) + UNPACK_ASCIIZ(info.Past3) + pos++; + UNPACK_LE(info.Affiliation1category) + UNPACK_ASCIIZ(info.Affiliation1) + UNPACK_LE(info.Affiliation2category) + UNPACK_ASCIIZ(info.Affiliation2) + UNPACK_LE(info.Affiliation3category) + UNPACK_ASCIIZ(info.Affiliation3) +} + +#undef UNPACK_LE +#undef UNPACK_ASCIIZ + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getSearchAnswer(vector & result_vec, uint32_t snac_sync) +{ + TLVField tlv; + uint32_t int_uin; + + SNACData snd; + snd.service_id=0x0015; + snd.subtype_id=0x0003; + snd.req_id=snac_sync; + + result_vec.clear(); + while(true) + { + if (waitSNAC(&snd)!=1) return false; + if (!tlv.decode_from(snd.data, 0)) return false; + if (tlv.type!=0x01) return false; + if (tlv.data.size()<13) return false; + if (tlv.data[6]!=0xda || tlv.data[7]!=0x07 || tlv.data[11]!=0x01) return false; + if (tlv.data[12]==0x0a) // Success flag + { + ICQKidShortUserInfo suinfo; + size_t curr_pos=15; + if (tlv.data.size()<(curr_pos+4)) return false; + memcpy(&int_uin, &tlv.data[curr_pos], sizeof(int_uin)); // Little Endian!!! + curr_pos += sizeof(int_uin); + ostringstream ss; + ss << int_uin; + suinfo.Uin=ss.str(); + + uint16_t nick_length; + if (tlv.data.size()<(curr_pos+2)) return false; + memcpy(&nick_length, &tlv.data[curr_pos], sizeof(nick_length)); // Little Endian!!! + curr_pos += sizeof(nick_length); + if (tlv.data.size()<(curr_pos+nick_length)) return false; + if (nick_length>1) suinfo.Nickname.assign(reinterpret_cast(&tlv.data[curr_pos]), nick_length-1); + curr_pos += nick_length; + + uint16_t fname_length; + if (tlv.data.size()<(curr_pos+2)) return false; + memcpy(&fname_length, &tlv.data[curr_pos], sizeof(fname_length)); // Little Endian!!! + curr_pos += sizeof(fname_length); + if (tlv.data.size()<(curr_pos+fname_length)) return false; + if (fname_length>1) suinfo.Firstname.assign(reinterpret_cast(&tlv.data[curr_pos]), fname_length-1); + curr_pos += fname_length; + + uint16_t lname_length; + if (tlv.data.size()<(curr_pos+2)) return false; + memcpy(&lname_length, &tlv.data[curr_pos], sizeof(lname_length)); // Little Endian!!! + curr_pos += sizeof(lname_length); + if (tlv.data.size()<(curr_pos+lname_length)) return false; + if (lname_length>1) suinfo.Lastname.assign(reinterpret_cast(&tlv.data[curr_pos]), lname_length-1); + curr_pos += lname_length; + + uint16_t email_length; + if (tlv.data.size()<(curr_pos+2)) return false; + memcpy(&email_length, &tlv.data[curr_pos], sizeof(email_length)); // Little Endian!!! + curr_pos += sizeof(email_length); + if (tlv.data.size()<(curr_pos+email_length)) return false; + if (email_length>1) suinfo.Email.assign(reinterpret_cast(&tlv.data[curr_pos]), email_length-1); + curr_pos += email_length; + + if (tlv.data.size()<(curr_pos+1)) return false; + if (tlv.data[curr_pos]==1) suinfo.Needauth=true; + else suinfo.Needauth=false; + ++curr_pos; + + if (tlv.data.size()<(curr_pos+2)) return false; + if (tlv.data[curr_pos]==1) suinfo.Status=STATUS_ONLINE; + else suinfo.Status=STATUS_OFFLINE; + curr_pos += 2; + + if (tlv.data.size()<(curr_pos+1)) return false; + else suinfo.Gender=tlv.data[curr_pos]; + ++curr_pos; + + if (tlv.data.size()<(curr_pos+2)) return false; + else memcpy(&(suinfo.Age), &tlv.data[curr_pos], sizeof(suinfo.Age)); // Little Endian!!! + + result_vec.push_back(suinfo); + } + if (tlv.data[10]==0xae) break; // It was last packet + if (tlv.data[10]!=0xa4) return false; // If not last and not A4 - It's error + } + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +uint16_t ICQKid2::getUnusedItemID(void) +{ + for (uint16_t i=0x0001; i<0xfffe; ++i) + { + if (myPrivSrvStatus_item_id==i) continue; + + bool has_collis=false; + for (size_t j=0; j::iterator iter=unknown_item_ids.begin(); iter!=unknown_item_ids.end(); ++iter) + if (*iter==i) + { + has_collis=true; + break; + } + if (has_collis) continue; + + return i; + } + + return 0; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +uint16_t ICQKid2::getUnusedGroupID(void) +{ + for (uint16_t i=0x0001; i<0xfffe; ++i) + { + bool has_collis=false; + for (size_t j=0; j data; + cli.encode_to(data, 0); + + return (sendSNAC(0x0013, flag, sync_id, &data)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::addDeleSSIGroup(SSIGroupEntry gen, uint16_t flag, uint32_t * sync_id) +{ + SSIContactListItem cli; + cli.name=gen.name; + cli.group_id=gen.id; + cli.item_id=0x0000; + cli.type=0x0001; // Group + + vector data; + cli.encode_to(data, 0); + + return (sendSNAC(0x0013, flag, sync_id, &data)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::updateSSIGroupContent(uint16_t group_id, uint32_t * sync_id) +{ + SSIContactListItem cli; + cli.group_id=group_id; + cli.item_id=0x0000; + cli.type=0x0001; // Group + if (group_id!=0) + { + int gr_ind=findCLGroup(group_id); + if (gr_ind<0) return false; + cli.name=ContactListGroups[gr_ind].name; + } + + TLVField tlv(NULL, 0, 0x00c8); // Group members + +#define PACK_16(arg) \ + { \ + uint16_t tmp_arg=htons(arg); \ + tlv.data.insert(tlv.data.end(), (uint8_t*)(&tmp_arg), ((uint8_t*)(&tmp_arg))+sizeof(tmp_arg)); \ + } + + if (group_id!=0) + { + for (size_t i=0; i data; + cli.encode_to(data, 0); + return (sendSNAC(0x0013, SSI_ITEM_CHANGE, sync_id, &data)==1); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::getOfflineMessages(void) +{ + if (!askOfflMsgAction(OFFLINE_RECIEVE, &offl_msg_sync_id)) return false; + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +int ICQKid2::pollIncomingEvents(int tmout) +{ + int tmp_tm = getNetworkTimeout(); + setNetworkTimeout(tmout); + + // Send keepalive every 60 secs + if ((time(NULL)-last_keepalive_timestamp)>60) sendKeepAlive(); + + // Process incoming packets + SNACData snd; + snd.service_id=0; + snd.subtype_id=0; + snd.req_id=0; + + int net_ret=waitSNAC(&snd); + setNetworkTimeout(tmp_tm); + + if (net_ret!=1) return net_ret; + + if (snd.service_id==0x0015 && snd.subtype_id==0x0003) + { + getOfflMsg(snd); + } + else + if (snd.service_id==0x0004 && snd.subtype_id==0x0007) // Incoming message + { + ICQKid2Message msg; + if (!parseIncomingMsg(msg, snd.data)) return TNETWORK_TIMEOUT; + onIncomingMsg(msg); + } + else if (snd.service_id==0x0004 && snd.subtype_id==0x000c) // Server side ack + { + if (!parseMsgServerResponse(snd.data)) return TNETWORK_TIMEOUT; + } + else if (snd.service_id==0x0004 && snd.subtype_id==0x000b) // Incoming client autoresponse, may be autostatus message + { + ICQKid2Message msg; + uint8_t type; + if (!parseMsgAutoResponse(msg, type, snd.data)) + goto L_PARSE2_040B; +// return TNETWORK_TIMEOUT; + + int uen_ind=findCLUIN(msg.uin); + if (uen_ind>=0) + { + switch(type) + { + case MSG_TYPE_AUTOAWAY : + if (ContactListUins[uen_ind].AutoAwayMessageText!=msg.text) + { + ContactListUins[uen_ind].AutoAwayMessageText=msg.text; + } + break; + + case MSG_TYPE_AUTOBUSY : + if (ContactListUins[uen_ind].AutoBusyMessageText!=msg.text) + { + ContactListUins[uen_ind].AutoBusyMessageText=msg.text; + } + break; + + case MSG_TYPE_AUTONA : + if (ContactListUins[uen_ind].AutoNotAvailableMessageText!=msg.text) + { + ContactListUins[uen_ind].AutoNotAvailableMessageText=msg.text; + } + break; + + case MSG_TYPE_AUTODND : + if (ContactListUins[uen_ind].AutoDoNotDisturbMessageText!=msg.text) + { + ContactListUins[uen_ind].AutoDoNotDisturbMessageText=msg.text; + } + break; + + case MSG_TYPE_AUTOFFC : + if (ContactListUins[uen_ind].AutoFreeForChatMessageText!=msg.text) + { + ContactListUins[uen_ind].AutoFreeForChatMessageText=msg.text; + } + break; + } + } + onIncomingAutoStatusMsg(msg, type); //Âûçûâàåì â ëþáîì ñëó÷àå ;) + } + else if (snd.service_id==0x0013 && snd.subtype_id==0x0019) // Authorize request + { + string from, text; + if (!parseAuthRequest(from, text, snd.data)) return TNETWORK_TIMEOUT; + onAuthRequest(from, text); + } + else if (snd.service_id==0x0013 && snd.subtype_id==0x001b) // Authorize reply + { + string from, text; + uint8_t aflag; + if (!parseAuthReply(from, text, aflag, snd.data)) return TNETWORK_TIMEOUT; + onAuthReply(from, text, aflag); + } + else if (snd.service_id==0x0003 && snd.subtype_id==0x000b) // Online notification + { + string from; + uint32_t stat; + vector xs; + xs.clear(); + uint8_t clientid=0; + if (!parseOnlineNotify(from, stat, snd.data, xs, clientid)) return TNETWORK_TIMEOUT; + uint32_t stat1=stat&0xffff0000; + uint32_t stat2=stat&0x0000ffff; + bool invis_flag=false; + + if (stat2&STATUS_INVISIBLE) + { + invis_flag=true; + stat2&=0xfffff0ff; + } + if (stat2==FSTATUS_NA) stat2=STATUS_NA; + if (stat2==FSTATUS_OCCUPIED) stat2=STATUS_OCCUPIED; + if (stat2==FSTATUS_DND) stat2=STATUS_DND; + +/* switch(stat2) + { + case STATUS_AWAY : sengMsgAutoRequest(from, MSG_TYPE_AUTOAWAY); break; + case STATUS_OCCUPIED : sengMsgAutoRequest(from, MSG_TYPE_AUTOBUSY); break; + case STATUS_NA : sengMsgAutoRequest(from, MSG_TYPE_AUTONA); break; + case STATUS_DND : sengMsgAutoRequest(from, MSG_TYPE_AUTODND); break; + case STATUS_FREE4CHAT : sengMsgAutoRequest(from, MSG_TYPE_AUTOFFC); break; + }*/ + + int uen_ind=findCLUIN(from); + if (uen_ind<0 || ContactListUins[uen_ind].clientid!=clientid){ + if (uen_ind>=0){ + ContactListUins[uen_ind].clientid=clientid; + } + onClientIDNotify(from, clientid); + } + if (uen_ind<0 || ContactListUins[uen_ind].status_modifiers!=stat1 || ContactListUins[uen_ind].online_status!=stat2 || ContactListUins[uen_ind].invisible!=invis_flag || ContactListUins[uen_ind].xStatus!=xs) + { + if (uen_ind>=0) + { + ContactListUins[uen_ind].status_modifiers=stat1; + ContactListUins[uen_ind].online_status=stat2; + ContactListUins[uen_ind].invisible=invis_flag; + ContactListUins[uen_ind].xStatus=xs; + } + onUserNotify(from, stat1, stat2, invis_flag, xs); + } + } + else if (snd.service_id==0x0003 && snd.subtype_id==0x000c) // Offline notification + { + string from; + if (!parseOfflineNotify(from, snd.data)) return TNETWORK_TIMEOUT; + int uen_ind=findCLUIN(from); + if (uen_ind<0) + { + vector xs; + xs.clear(); + onUserNotify(from, 0, STATUS_OFFLINE, false, xs); + } + else if (ContactListUins[uen_ind].status_modifiers!=0 || ContactListUins[uen_ind].online_status!=STATUS_OFFLINE || ContactListUins[uen_ind].invisible!=false) + { + ContactListUins[uen_ind].status_modifiers=0; + ContactListUins[uen_ind].online_status=STATUS_OFFLINE; + ContactListUins[uen_ind].invisible=false; + ContactListUins[uen_ind].srv_relay_cap=false; + ContactListUins[uen_ind].unicode_cap=false; + vector xs; + xs.clear(); + onUserNotify(from, 0, STATUS_OFFLINE, false, xs); + ContactListUins[uen_ind].clientid=0; + } + } + else if (snd.service_id==0x0013 && snd.subtype_id==0x0008) // SSI add item notification + { + if (!parseSSIAddNotify(snd.data)) return TNETWORK_TIMEOUT; + onContactListChanged(); + } + else if (snd.service_id==0x0013 && snd.subtype_id==0x0009) // SSI update item notification + { + if (!parseSSIUpdateNotify(snd.data)) return TNETWORK_TIMEOUT; + onContactListChanged(); + } + else if (snd.service_id==0x0013 && snd.subtype_id==0x000a) // SSI delete item notification + { + if (!parseSSIDeleteNotify(snd.data)) return TNETWORK_TIMEOUT; + onContactListChanged(); + } + else if (snd.service_id==0x0013 && snd.subtype_id==0x001c) // You were added notification + { + string from; + if (!parseWasAdded(from, snd.data)) return TNETWORK_TIMEOUT; + onWasAdded(from); + } + else if (snd.service_id==0x0004 && snd.subtype_id==0x0014) // Incoming MTN + { + string from; + uint16_t type; + if (!parseIncomingMTN(from, type, snd.data)) return TNETWORK_TIMEOUT; + onIncomingMTN(from, type); + } + else if (snd.service_id==0x0004 && snd.subtype_id==0x000b) // May be xTraZ response + { + L_PARSE2_040B: + string from; + vector x_status; + string x_title; + string x_descr; + if (!parseXtrazResponse(from, x_status, x_title, x_descr, snd.data)) return TNETWORK_TIMEOUT; + int uen_ind=findCLUIN(from); + if (uen_ind<0) + { + onXstatusChanged(from, x_status, x_title, x_descr); + } + else /*if (ContactListUins[uen_ind].xStatus!=x_status || ContactListUins[uen_ind].xStatusTitle!=x_title || ContactListUins[uen_ind].xStatusDescription!=x_descr)*/ + { + ContactListUins[uen_ind].xStatus=x_status; + ContactListUins[uen_ind].xStatusTitle=x_title; + ContactListUins[uen_ind].xStatusDescription=x_descr; + onXstatusChanged(from, x_status, x_title, x_descr); + } + } + + if (icons_service && icons_service!=this) + { + int icons_service_ret = icons_service->pollIncomingEvents(tmout); + if (icons_service_ret!=1 && icons_service_ret!=TNETWORK_TIMEOUT) + { + icons_service->doDisconnect(); + delete icons_service; + icons_service=NULL; + } + } + return 1; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::removeAllMyIcons(void) +{ + if (!startSSITransact()) return false; + for (size_t i=0; i md5_data(2); + md5_data[0]=ContactListIcons[i].icon_flags; + md5_data[1]=16; + md5_data.insert(md5_data.end(), ContactListIcons[i].icon_md5_hash, ContactListIcons[i].icon_md5_hash+16); + TLVField tlv1(md5_data, 0x00d5); + cli.payload.push_back(tlv1); + + TLVField tlv2("", 0x0131); + cli.payload.push_back(tlv2); + + vector data; + cli.encode_to(data, 0); + + if (sendSNAC(0x0013, SSI_ITEM_DELETE, NULL, &data)!=1) return false; + } + if (!endSSITransact()) return false; + + ContactListIcons.clear(); + + return true; +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +void ICQKid2::sendXStatusNotifyAutoResponse(string touin, uint8_t * msg_cookie) +{ + if (xStatus==X_STATUS_NONE || xStatus>X_STATUS_MAX_BOUND) return; + vector data; + size_t pos=0; + + data.resize(pos+8); + memcpy(&data[pos], msg_cookie, 8); + pos+=8; + + data.resize(pos+3); + data[pos]=0; + data[pos+1]=2; // message channel + data[pos+2]=touin.length(); + pos+=3; + + data.resize(pos+touin.length()); + memcpy(&data[pos], touin.data(), touin.length()); + pos+=touin.length(); + + data.resize(pos+2); + data[pos]=0; + data[pos+1]=3; // reason code - channel specific + pos+=2; + + ostringstream plug_msg; + plug_msg << ""; + plug_msg << "<ret event='OnRemoteNotification'>"; + plug_msg << "<srv>"; + plug_msg << "<id></id>"; + plug_msg << "<val srv_id=''>undefined</val>"; + plug_msg << "</srv>"; + plug_msg << "<srv>"; + plug_msg << "<id>cAwaySrv</id>"; + plug_msg << "<val srv_id='cAwaySrv'>"; + plug_msg << "<Root>"; + plug_msg << "<CASXtraSetAwayMessage></CASXtraSetAwayMessage>"; + plug_msg << "<uin>" << myuin << "</uin>"; + plug_msg << "<index>" << xStatus << "</index>"; + plug_msg << "<title>" << xStatusTitle << "</title>"; + plug_msg << "<desc>" << xStatusDescription << "</desc>"; + plug_msg << "</Root>"; + plug_msg << "</val>"; + plug_msg << "</srv>"; + plug_msg << "<srv>"; + plug_msg << "<id>cRandomizerSrv</id>"; + plug_msg << "<val srv_id='cRandomizerSrv'>undefined</val>"; + plug_msg << "</srv>"; + plug_msg << "</ret>"; + plug_msg << "\r\n"; + + uint8_t xtraz_script_guid[] = {0x3b, 0x60, 0xb3, 0xef, 0xd8, 0x2a, 0x6c, 0x45, 0xa4, 0xe0, 0x9c, 0x5a, 0x5e, 0x67, 0xe8, 0x65}; + + TLV2711 tlv_2711; + tlv_2711.version=9; + tlv_2711.cookie=0; + tlv_2711.msg_type=0x1a; // Message type - Plugin message described by text string + tlv_2711.flags=0; + tlv_2711.status=0; + tlv_2711.priority=0; + tlv_2711.text=""; + memcpy(tlv_2711.plug_guid, xtraz_script_guid, 16); + tlv_2711.plug_func_id=8; // Script notify + tlv_2711.plug_name="Script Plug-in: Remote Notification Arrive"; + tlv_2711.xtraz_script_text=plug_msg.str(); + tlv_2711.encode_to(data, pos); + + (void)sendSNAC(0x0004, 0x000b, NULL, &data); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::haveSrvRelayCapability(vector & data) +{ + uint8_t srv_rel_arr[16]={ 0x09, 0x46, 0x13, 0x49, 0x4c, 0x7f, 0x11, 0xd1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 }; // ICQ Server relaying + + size_t caps_num=data.size()/16; + for (size_t i=0; i & data) +{ + uint8_t unicode_arr[16]={ 0x09, 0x46, 0x13, 0x4e, 0x4c, 0x7f, 0x11, 0xd1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 }; // ICQ UTF-8 +// uint8_t old_unicode_arr[16]={ 0x2e, 0x7a, 0x64, 0x75, 0xfa, 0xdf, 0x4d, 0xc8, 0x88, 0x6f, 0xea, 0x35, 0x95, 0xfd, 0xb6, 0xdf }; // Old ICQ UTF-8 + + size_t caps_num=data.size()/16; + for (size_t i=0; i & data, vector &xs) +{ + bool r=false; + size_t caps_num=data.size()/16; + xs.clear(); //Íåò èêñà + for (size_t i=0; i data; + bom.encode_to(data); + + sendSNAC(0x0004, 0x0006, NULL, &data); +} + +// ----------------=========ooooOOOOOOOOOoooo=========---------------- +bool ICQKid2::haveIGCapability(vector & data){ + size_t caps_num=data.size()/16; + for (size_t i=0; i & data){ + size_t caps_num=data.size()/16; + for (size_t i=0; i +#include +#include +#include +using namespace std; + +#include + +#ifdef _WIN32 + #include + typedef char int8_t; + typedef short int int16_t; +// typedef long int int32_t; + + typedef unsigned char uint8_t; + typedef unsigned short int uint16_t; +// typedef unsigned long int uint32_t; +#else + #include + #include + #include +#endif + +#include "icqkid2_constants.h" + +class SSIUINEntry{ + public: + SSIUINEntry(void) + { + groupid=0; + itemid=0; + waitauth=false; + last_internal_ip=0; + last_internal_port=0; + last_external_ip=0; + have_icon=false; + icon_id=0; + icon_flags=0; + memset(icon_md5_hash, 0, 16); + online_status=STATUS_OFFLINE; + status_modifiers=0; + invisible=false; + xStatus.clear(); + isBLM=false; + srv_relay_cap=false; + unicode_cap=false; + online_since=0; + idle_since=0; + ig_cap=false; + pidor=false; + clientid=0; + } + + string uin; + string nick; + uint16_t groupid; + string groupname; + uint16_t itemid; + bool waitauth; + + uint32_t last_internal_ip; + uint32_t last_internal_port; + uint32_t last_external_ip; + + bool have_icon; + uint16_t icon_id; + uint8_t icon_flags; + uint8_t icon_md5_hash[16]; + vector icon_data; + + uint32_t online_status; + uint32_t status_modifiers; + bool invisible; + + vector xStatus; + string xStatusTitle; + string xStatusDescription; + + bool isBLM; + + bool srv_relay_cap; + bool unicode_cap; + bool ig_cap; + bool pidor; + uint8_t clientid; + + string AutoAwayMessageText; + string AutoBusyMessageText; + string AutoNotAvailableMessageText; + string AutoDoNotDisturbMessageText; + string AutoFreeForChatMessageText; + + uint32_t online_since; + uint32_t idle_since; + uint32_t proto; +}; + +class SSIGroupEntry{ + public: + uint16_t id; + string name; +}; + +class ICQKidShortUserInfo{ + public: + string Uin; + string Nickname; + string Firstname; + string Lastname; + string Email; + int Gender; + bool Needauth; + uint32_t Status; + uint16_t Age; +}; + +class ICQKidFullUserInfo{ + public: + ICQKidFullUserInfo(void) : Homecountry(0), GMToffset(0), Auth(0), Webaware(0), DCpermissions(0), Pubprimarymail(0), \ + Age(0), Gender(0), Birthyear(0), Birthmonth(0), Birthday(0), Language1(0), Language2(0), Language3(0), Origcountry(0), \ + Maritalstatus(0), Allowspam(0), Codepage(0), Homepageenabled(0), Homepagecategory(0), Workcountry(0), Workoccupation(0), \ + Interest1category(0), Interest2category(0), Interest3category(0), Interest4category(0), \ + Past1category(0), Past2category(0), Past3category(0), \ + Affiliation1category(0), Affiliation2category(0), Affiliation3category(0), \ + Minage(0), Maxage(0), Searchonline(0) {} + + // Basic info + string Uin; + string Nickname; + string Firstname; + string Lastname; + string Email; + string Homecity; + string Homestate; + string Homephone; + string Homefax; + string Homeaddress; + string Cellphone; + string Homezip; + uint16_t Homecountry; + int8_t GMToffset; // Signed! Real value = GMT + (-1*GMToffset/2) + uint8_t Auth; + uint8_t Webaware; + uint8_t DCpermissions; + uint8_t Pubprimarymail; + + // More info + uint16_t Age; + uint8_t Gender; + string Homepage; + uint16_t Birthyear; + uint8_t Birthmonth; + uint8_t Birthday; + uint8_t Language1; + uint8_t Language2; + uint8_t Language3; + string Origcity; + string Origstate; + uint16_t Origcountry; + uint8_t Maritalstatus; + uint8_t Allowspam; + uint16_t Codepage; + + // Email info + vector > Emails; // email and public flag + + // Homepage category Info + uint8_t Homepageenabled; + uint16_t Homepagecategory; + string Homepagekeywords; + + // Work info + string Workcity; + string Workstate; + string Workphone; + string Workfax; + string Workaddress; + string Workzip; + uint16_t Workcountry; + string Workcompany; + string Workdepartment; + string Workposition; + uint16_t Workoccupation; + string Workwebpage; + + // Notes info + string Notes; + + // Interests info + uint16_t Interest1category; + string Interest1; + uint16_t Interest2category; + string Interest2; + uint16_t Interest3category; + string Interest3; + uint16_t Interest4category; + string Interest4; + + // Affiliations info + uint16_t Past1category; + string Past1; + uint16_t Past2category; + string Past2; + uint16_t Past3category; + string Past3; + uint16_t Affiliation1category; + string Affiliation1; + uint16_t Affiliation2category; + string Affiliation2; + uint16_t Affiliation3category; + string Affiliation3; + + // Additional for white pages search + uint16_t Minage; + uint16_t Maxage; + string Searchkey; + uint8_t Searchonline; +}; + +class SnacCache; +class SNACData; + +class ICQKid2{ + public: + string mypassword; + virtual void getMD5hash(vector md5_salt, uint8_t * md5_hash); + typedef enum + { + NONE, + HTTP, + SOCKS4, + SOCKS5 + } ProxyType; + + ICQKid2(void); + virtual ~ICQKid2(); + + void setUIN(string auin) { myuin=auin; } + void setPassword(string apwd) { mypassword=apwd; } + void setLoginHost(string ahost="login.icq.com", int aport=5190) { loginhost=ahost; loginport=aport; } + void setProxy(string ahost="", int aport=0, string auid="", string apwd="", ProxyType apr_type=NONE) + { + proxy_host=ahost; + proxy_port=aport; + proxy_uid=auid; + proxy_pwd=apwd; + proxy_type=apr_type; + } + + bool doConnect(uint32_t astat=STATUS_ONLINE); + void doDisconnect(void); + bool setStatus(uint32_t astat); + uint32_t getMyOnlineStatus(void) const { return online_status; } + bool setXStatus(size_t x_stat, string title, string descr); + + bool registerNewUIN(string password, string & new_uin); + bool changePassword(string new_password); + + bool getUserInfo(string uin, ICQKidShortUserInfo & info, bool force_update=false); + bool getFullUserInfo(string uin, ICQKidFullUserInfo & info, bool force_update=false); + bool publicMyInfo(ICQKidFullUserInfo & info); + + bool searchByMail(string email, vector & result_vec); + bool searchByUIN(string uin, vector & result_vec); + bool searchByWhitePages(ICQKidFullUserInfo & info, vector & result_vec); + + class SSITransactContact { + public: + string uin; + string nick; + string groupname; + uint16_t retflag; + }; + + bool addContact(string uin, string nick, string groupname, uint16_t * retflag=NULL); + bool removeContact(string uin, uint16_t * retflag=NULL); + bool renameContact(string uin, string nick, uint16_t * retflag=NULL); + + bool addContacts(vector & cont_vec); + bool removeContacts(vector & cont_vec); + bool renameContacts(vector & cont_vec); + + bool addBLMContact(string uin); + bool removeBLMContact(string uin); + + bool addBLMContacts(vector & uins); + bool removeBLMContacts(vector & uins); + + bool addGroup(string groupname, uint16_t * retflag=NULL); + bool removeGroup(string groupname, uint16_t * retflag=NULL); + bool renameGroup(string groupname, string newname, uint16_t * retflag=NULL); + + bool updateContactList(void); + + bool authRequest(string uin, string text); + bool authReply(string uin, string text, uint8_t aflag); + + bool getBuddyIcon(string uin); + bool downloadMyIcon(void); + bool uploadMyIcon(vector & icon_data); + bool deleteMyIcon(void) { return removeAllMyIcons(); } + + bool addVisible(string uin, string nick="") { return addVisInvisIgnor(VisibleList, 0x0002, uin, nick); } + bool removeVisible(string uin, string nick="") { return removeVisInvisIgnor(VisibleList, 0x0002, uin); } + + bool addInvisible(string uin, string nick="") { return addVisInvisIgnor(InvisibleList, 0x0003, uin, nick); } + bool removeInvisible(string uin, string nick="") { return removeVisInvisIgnor(InvisibleList, 0x0003, uin); } + +// inline bool addIgnore(string uin, string nick="") { if (!removeContact(uin)) return false; return addVisInvisIgnor(IgnoreList, 0x000E, uin, nick); } + bool addIgnore(string uin, string nick="") { return addVisInvisIgnor(IgnoreList, 0x000E, uin, nick); } + bool removeIgnore(string uin, string nick="") { return removeVisInvisIgnor(IgnoreList, 0x000E, uin); } + + uint8_t getMyPrivacyStatus(void) const { return myPrivSrvStatus; } + bool setMyPrivacyStatus(uint8_t stat); + + class ICQKid2Message { + public: + typedef enum + { + USASCII, + LOCAL8BIT, + UCS2BE, + UTF8 + } EncodeType; + + ICQKid2Message(void) + { + enc_type=LOCAL8BIT; + codepage=0; + memset(text_color, 0, 4); + memset(bg_color, 0xff, 4); + bg_color[3]=0; + is_offline=false; + } + + ICQKid2Message(string auin, string atext, \ + EncodeType aenc_type=LOCAL8BIT, uint16_t acodepage=0, \ + bool ais_offline=false, \ + uint8_t * atext_color=NULL, uint8_t * abg_color=NULL) + { + uin=auin; + text=atext; + enc_type=aenc_type; + codepage=acodepage; + if (atext_color) memcpy(text_color, atext_color, 4); + else memset(text_color, 0, 4); + if (abg_color) memcpy(bg_color, abg_color, 4); + else + { + memset(bg_color, 0xff, 4); + bg_color[3]=0; + } + is_offline=ais_offline; + } + + uint8_t cookie[8]; + bool cookie_valid; + + string uin; // from or to + string text; + uint8_t text_color[4]; //RGBN + uint8_t bg_color[4]; //RGBN + EncodeType enc_type; + uint16_t codepage; + bool is_offline; + time_t timestamp; + }; + + bool sendMessage(ICQKid2Message amsg, unsigned short cookie); + bool sendMTN(string to, uint16_t atype); + bool mainLoop(bool & break_flag); + bool sendMsgAutoResponse(string touin, uint8_t * msg_cookie, uint8_t type=MSG_TYPE_PLAINTEXT); + + virtual void onIdle(void) {} + virtual void onIncomingMTN(string from, uint16_t atype) {} + virtual void onIncomingMsg(ICQKid2Message msg) {} + virtual void onAuthRequest(string from, string text) {} + virtual void onAuthReply(string from, string text, uint8_t aflag) {} + virtual void onUserNotify(string uin, uint32_t stat1, uint32_t stat2, bool invis_flag, vector xs) {} + virtual void onClientIDNotify(string from, uint8_t clientid) {} + virtual void onContactListChanged(void) {} + virtual void onWasAdded(string from) {} + virtual void onIconChanged(string uin) {} + virtual void onXstatusChanged(string uin, vector x_status, string x_title, string x_descr) {} + virtual void onRegisterControlPicture(vector & pic_data, string mime_type, string & pic_str) {} + virtual void onIncomingAutoStatusMsg(ICQKid2Message msg, uint8_t type) {} + virtual void onSingOff(uint16_t err_code, string err_url) {} + virtual void onClientMsgAck(string uin, uint8_t *cookie) {} + virtual void onServerMsgAck(string uin, uint8_t *cookie) {} + + int findCLUIN(string uin); + int findCLUIN(uint16_t item_id); + int findCLGroup(string groupname); + int findCLGroup(uint16_t group_id); + + vector ContactListUins; + vector ContactListGroups; + vector ContactListIcons; + + vector VisibleList; + vector InvisibleList; + vector IgnoreList; + + string AutoAwayMessageText; + string AutoBusyMessageText; + string AutoNotAvailableMessageText; + string AutoDoNotDisturbMessageText; + string AutoFreeForChatMessageText; + + string subClientId; + string subIGId; + + int getConnectPercentage(void) const { return connect_phase_percentage; } + void setNetworkTimeout(int tm) { network_timeout=tm; } + int getNetworkTimeout(void) const { return network_timeout; } + void breakNetworkOperation(void) { network_break_flag=true; } + + bool getOfflineMessages(void); + int pollIncomingEvents(int tmout); + + uint16_t connect_error_code; + string connect_error_url; + + void sendXtrazRequest(string uin); + + size_t xStatus; + string xStatusTitle; + string xStatusDescription; + + int connect_phase_percentage; + string myuin; + + private: + string proxy_host; + int proxy_port; + string proxy_uid; + string proxy_pwd; + ProxyType proxy_type; + + string loginhost; + int loginport; + + uint32_t offl_msg_sync_id; + + int sock; + + uint16_t flap_seq_number; + uint32_t snac_seq_number; + + vector > server_services_list; + vector server_rate_groups; + + map userinfo_map; + map userfullinfo_map; + + ICQKid2 * icons_service; + + int directConnect(string ahost, int aport); + + ICQKid2 * getInstanceForService(uint16_t family); + + bool doConnect_phase2(uint32_t astat, string boss_host, int boss_port, vector & boss_cookie, bool short_phase=false); + + bool waitHello(void); + bool sendSignOn0(void); + bool sendMD5saltrequest(uint32_t * snac_sync); + bool getMD5salt(uint32_t snac_sync, vector & md5_salt); + bool sendMD5authorize(uint32_t * snac_sync, vector & md5_salt); + bool getBOSSparams(string & boss_host, int & boss_port, vector & cookie); + bool sendSignOff(void); + bool sendSignOn2(vector & cookie); + + bool getServiceList(void); + bool askServices(void); + bool getServices(void); + + bool askRateLimits(uint32_t * sync_id); + bool getRateLimits(uint32_t sync_id); + bool sendRateLimits(void); + + bool askSelfInfo(uint32_t * sync_id); + bool getSelfInfo(uint32_t sync_id); + + bool askLocationLimit(uint32_t * sync_id); + bool getLocationLimit(uint32_t sync_id); + bool sendLocationInfo(void); + + bool askBLMLimit(uint32_t * sync_id); + bool getBLMLimit(uint32_t sync_id); + + bool askICBMParams(uint32_t * sync_id); + bool getICBMParams(uint32_t sync_id); + bool sendICBMParams(void); + + bool askPRMLimit(uint32_t * sync_id); + bool getPRMLimit(uint32_t sync_id); + + bool askSSILimit(uint32_t * sync_id); + bool getSSILimit(uint32_t sync_id); + bool askSSICopy(uint32_t * sync_id); + bool getSSICopy(uint32_t sync_id); + bool activateSSI(void); + + bool sendStatus(uint32_t astat=STATUS_ONLINE, bool ext=false); + bool sendReady(void); + + bool sendAuthResponse(string uin, string text, uint8_t aflag); + bool sendAuthRequest(string uin, string text); + + bool askOfflMsgAction(uint8_t flag, uint32_t * sync_id); + bool getOfflMsg(SNACData & snd); + + bool askShortInfo(string uin, uint32_t * sync_id); + bool getShortInfo(ICQKidShortUserInfo & info, uint32_t sync_id); + + bool askFullInfo(string uin, uint32_t * sync_id); + bool getFullInfo(ICQKidFullUserInfo & info, uint32_t sync_id); + + void parseBasicUserInfo(vector & data, ICQKidFullUserInfo & info); + void parseMoreUserInfo(vector & data, ICQKidFullUserInfo & info); + void parseEmailUserInfo(vector & data, ICQKidFullUserInfo & info); + void parseHpagecatUserInfo(vector & data, ICQKidFullUserInfo & info); + void parseWorkUserInfo(vector & data, ICQKidFullUserInfo & info); + void parseNotesUserInfo(vector & data, ICQKidFullUserInfo & info); + void parseInterestsUserInfo(vector & data, ICQKidFullUserInfo & info); + void parseAffiliationsUserInfo(vector & data, ICQKidFullUserInfo & info); + + bool getSearchAnswer(vector & result_vec, uint32_t snac_sync); + + bool parseIncomingMsg(ICQKid2Message & msg, vector & data); + bool parseIncomingMTN(string & from, uint16_t & type, vector & data); + bool parseAuthRequest(string & from, string & text, vector & data); + bool parseAuthReply(string & from, string & text, uint8_t & aflag, vector & data); + bool parseOnlineNotify(string & uin, uint32_t & stat, vector & data, vector &xs, uint8_t & clientid); + bool parseOfflineNotify(string & uin, vector & data); + bool parseSSIAddNotify(vector & data); + bool parseSSIUpdateNotify(vector & data); + bool parseSSIDeleteNotify(vector & data); + bool parseWasAdded(string & from, vector & data); + bool parseXtrazResponse(string & from, vector & x_status, string & x_title, string & x_descr, vector & data); + bool parseMsgAutoResponse(ICQKid2Message & msg, uint8_t & type, vector & data); + bool parseMsgServerResponse(vector & data); + + bool sengMsgAutoRequest(string touin, uint8_t type); + + uint16_t getUnusedItemID(void); + uint16_t getUnusedGroupID(void); + bool startSSITransact(void); + bool endSSITransact(void); + bool getSSIEditAck(uint16_t & retflag, uint32_t sync_id); + bool addDeleSSIUIN(SSIUINEntry uen, uint16_t flag, uint32_t * sync_id); + bool addDeleSSIGroup(SSIGroupEntry gen, uint16_t flag, uint32_t * sync_id); + bool updateSSIGroupContent(uint16_t group_id, uint32_t * sync_id); + + bool removeAllMyIcons(void); + + bool sub_ssi_addContact(string uin, string nick, string groupname, uint16_t * retflag); + bool sub_ssi_removeContact(string uin, uint16_t * retflag); + bool sub_ssi_renameContact(string uin, string nick, uint16_t * retflag); + + bool sub_registerNewUIN_simply(ICQKid2 * regist_serv, string password, string & new_uin, string * control_str=NULL); + bool sub_registerNewUIN_picture(ICQKid2 * regist_serv, string password, string & new_uin); + + bool sub_getBuddyIcon(SSIUINEntry & uen); + + bool haveSrvRelayCapability(vector & data); + bool haveUnicodeCapability(vector & data); + bool haveXtrazCapability(vector & data, vector & xs); +// void sendXtrazRequest(string uin); + void sendXStatusNotifyAutoResponse(string touin, uint8_t * msg_cookie); + bool haveIGCapability(vector & data); + uint8_t getClientID(vector & data); + + bool addVisInvisIgnor(vector & cont, uint16_t it_type, string uin, string nick=""); + bool removeVisInvisIgnor(vector & cont, uint16_t it_type, string uin); + + SnacCache * snac_cache; + set unknown_item_ids; + + int waitSNAC(SNACData * snd); + int sendSNAC(uint16_t service_id, uint16_t subtype_id, uint32_t * req_id=NULL, vector * data_ptr=NULL); + int sendKeepAlive(void); + + int network_timeout; + bool network_break_flag; + + time_t last_keepalive_timestamp; + + uint32_t online_status; + + uint8_t myPrivSrvStatus; + uint16_t myPrivSrvStatus_item_id; +}; + +#endif diff --git a/icqkid2_constants.h b/icqkid2_constants.h new file mode 100644 index 0000000..3152c60 --- /dev/null +++ b/icqkid2_constants.h @@ -0,0 +1,581 @@ +/*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _ICQKID2_CONSTANTS_H_ +#define _ICQKID2_CONSTANTS_H_ + +#define TNETWORK_CLOSE 0 +#define TNETWORK_ERR -1 +#define TNETWORK_TIMEOUT -2 + +#define STATUS_WEBAWARE (uint32_t)0x00010000 +#define STATUS_SHOWIP (uint32_t)0x00020000 +#define STATUS_BIRTHDAY (uint32_t)0x00080000 +#define STATUS_WEBFRONT (uint32_t)0x00200000 +#define STATUS_DCDISABLED (uint32_t)0x01000000 +#define STATUS_DCAUTH (uint32_t)0x10000000 +#define STATUS_DCCONT (uint32_t)0x20000000 + +// This status added by me for comfort, +// there is not status for offline in protocol standard +#define STATUS_OFFLINE (uint32_t)0xFFFFFFFF +//00001000 + +#define STATUS_ONLINE (uint32_t)0x00000000 +#define STATUS_AWAY (uint32_t)0x00000001 +#define STATUS_DND (uint32_t)0x00000002 +#define STATUS_NA (uint32_t)0x00000004 +#define STATUS_OCCUPIED (uint32_t)0x00000010 +#define STATUS_FREE4CHAT (uint32_t)0x00000020 +#define STATUS_INVISIBLE (uint32_t)0x00000100 + +#define STATUS_EVIL (uint32_t)0x00003000 +#define STATUS_DEPRESSION (uint32_t)0x00004000 +#define STATUS_HOME (uint32_t)0x00005000 +#define STATUS_WORK (uint32_t)0x00006000 +#define STATUS_LUNCH (uint32_t)0x00002001 + +#define AUTH_DECLINED (uint8_t)0x00 +#define AUTH_ACCEPTED (uint8_t)0x01 + +#define SSI_EDIT_OK (uint16_t)0x0000 +#define SSI_EDIT_ERR_NOTFOUND (uint16_t)0x0002 +#define SSI_EDIT_ERR_EXIST (uint16_t)0x0003 + +// Added by me, there isnt in standard +#define SSI_EDIT_ERR_NETWORK (uint16_t)0x0004 + +#define SSI_EDIT_ERR_CANTADD (uint16_t)0x000a +#define SSI_EDIT_ERR_LIMIT (uint16_t)0x000c +#define SSI_EDIT_ERR_AIMLIST (uint16_t)0x000d +#define SSI_EDIT_ERR_NEEDAUTH (uint16_t)0x000e + +#define MTN_BEGIN (uint16_t)0x0002 +#define MTN_TYPED (uint16_t)0x0001 +#define MTN_FINISH (uint16_t)0x0000 + +#define X_STATUS_NONE (size_t)0 +#define X_STATUS_ANGRY (size_t)1 +#define X_STATUS_BATH (size_t)2 +#define X_STATUS_TIRED (size_t)3 +#define X_STATUS_PARTY (size_t)4 +#define X_STATUS_BEER (size_t)5 +#define X_STATUS_THINK (size_t)6 +#define X_STATUS_EAT (size_t)7 +#define X_STATUS_TV (size_t)8 +#define X_STATUS_MEET (size_t)9 +#define X_STATUS_COFFEE (size_t)10 +#define X_STATUS_MUSIC (size_t)11 +#define X_STATUS_BUSINESS (size_t)12 +#define X_STATUS_SHOOT (size_t)13 +#define X_STATUS_FUN (size_t)14 +#define X_STATUS_PHONE (size_t)15 +#define X_STATUS_GAME (size_t)16 +#define X_STATUS_STUDY (size_t)17 +#define X_STATUS_SHOP (size_t)18 +#define X_STATUS_SICK (size_t)19 +#define X_STATUS_SLEEP (size_t)20 +#define X_STATUS_SURFING (size_t)21 +#define X_STATUS_BROWSE (size_t)22 +#define X_STATUS_WORK (size_t)23 +#define X_STATUS_TYPE (size_t)24 +#define X_STATUS_PICNIC (size_t)25 +#define X_STATUS_COOK (size_t)26 +#define X_STATUS_SMOKE (size_t)27 +#define X_STATUS_IM_HIGH (size_t)28 +#define X_STATUS_WC (size_t)29 +#define X_STATUS_BE_NOT_BE (size_t)30 +#define X_STATUS_PRO7_TV (size_t)31 +#define X_STATUS_LOVE (size_t)32 +#define X_STATUS_GOOGLE (size_t)33 +#define X_STATUS_NOTE (size_t)34 + +#define X_STATUS_MAX_BOUND X_STATUS_NOTE + +// INFO CONSTANTS + +#define INFO_COUNTRY_UNSPECIFIED 0 +#define INFO_COUNTRY_OTHER 9999 +#define INFO_COUNTRY_AFGHANISTAN 93 +#define INFO_COUNTRY_ALBANIA 355 +#define INFO_COUNTRY_ALGERIA 213 +#define INFO_COUNTRY_AMERICAN_SAMOA 684 +#define INFO_COUNTRY_ANDORRA 376 +#define INFO_COUNTRY_ANGOLA 244 +#define INFO_COUNTRY_ANGUILLA 101 +#define INFO_COUNTRY_ANTIGUA_AND_BARBUDA 102 +#define INFO_COUNTRY_ANTILLES 5902 +#define INFO_COUNTRY_ARGENTINA 54 +#define INFO_COUNTRY_ARMENIA 374 +#define INFO_COUNTRY_ARUBA 297 +#define INFO_COUNTRY_ASCENSION_ISLAND 247 +#define INFO_COUNTRY_AUSTRALIA 61 +#define INFO_COUNTRY_AUSTRIA 43 +#define INFO_COUNTRY_AZERBAIJAN 994 +#define INFO_COUNTRY_BAHAMAS 103 +#define INFO_COUNTRY_BAHRAIN 973 +#define INFO_COUNTRY_BANGLADESH 880 +#define INFO_COUNTRY_BARBADOS 104 +#define INFO_COUNTRY_BARBUDA 120 +#define INFO_COUNTRY_BELARUS 375 +#define INFO_COUNTRY_BELGIUM 32 +#define INFO_COUNTRY_BELIZE 501 +#define INFO_COUNTRY_BENIN 229 +#define INFO_COUNTRY_BERMUDA 105 +#define INFO_COUNTRY_BHUTAN 975 +#define INFO_COUNTRY_BOLIVIA 591 +#define INFO_COUNTRY_BOSNIA_AND_HERZEGOVINA 387 +#define INFO_COUNTRY_BOTSWANA 267 +#define INFO_COUNTRY_BRAZIL 55 +#define INFO_COUNTRY_BRITISH_VIRGIN_ISLANDS 106 +#define INFO_COUNTRY_BRUNEI 673 +#define INFO_COUNTRY_BULGARIA 359 +#define INFO_COUNTRY_BURKINA_FASO 226 +#define INFO_COUNTRY_BURUNDI 257 +#define INFO_COUNTRY_CAMBODIA 855 +#define INFO_COUNTRY_CAMEROON 237 +#define INFO_COUNTRY_CANADA 107 +#define INFO_COUNTRY_CANARY_ISLANDS 178 +#define INFO_COUNTRY_CAPE_VERDE_ISLANDS 238 +#define INFO_COUNTRY_CAYMAN_ISLANDS 108 +#define INFO_COUNTRY_CENTRAL_AFRICAN_REPUBLIC 236 +#define INFO_COUNTRY_CHAD 235 +#define INFO_COUNTRY_CHILE 56 +#define INFO_COUNTRY_CHINA 86 +#define INFO_COUNTRY_CHRISTMAS_ISLAND 672 +#define INFO_COUNTRY_COCOS_ISLANDS 6102 +#define INFO_COUNTRY_COCOS_KEELING_ISLANDS 6101 +#define INFO_COUNTRY_COLOMBIA 57 +#define INFO_COUNTRY_COMOROS 2691 +#define INFO_COUNTRY_CONGO 242 +#define INFO_COUNTRY_CONGO_ZAIRE 243 +#define INFO_COUNTRY_COOK_ISLANDS 682 +#define INFO_COUNTRY_COSTA_RICA 506 +#define INFO_COUNTRY_CROATIA 385 +#define INFO_COUNTRY_CUBA 53 +#define INFO_COUNTRY_CYPRUS 357 +#define INFO_COUNTRY_CZECH_REPUBLIC 420 +#define INFO_COUNTRY_DENMARK 45 +#define INFO_COUNTRY_DIEGO_GARCIA 246 +#define INFO_COUNTRY_DJIBOUTI 253 +#define INFO_COUNTRY_DOMINICA 109 +#define INFO_COUNTRY_DOMINICAN_REPUBLIC 110 +#define INFO_COUNTRY_ECUADOR 593 +#define INFO_COUNTRY_EGYPT 20 +#define INFO_COUNTRY_EL_SALVADOR 503 +#define INFO_COUNTRY_EQUATORIAL_GUINEA 240 +#define INFO_COUNTRY_ERITREA 291 +#define INFO_COUNTRY_ESTONIA 372 +#define INFO_COUNTRY_ETHIOPIA 251 +#define INFO_COUNTRY_FAEROE_ISLANDS 298 +#define INFO_COUNTRY_FALKLAND_ISLANDS 500 +#define INFO_COUNTRY_FIJI 679 +#define INFO_COUNTRY_FINLAND 358 +#define INFO_COUNTRY_FRANCE 33 +#define INFO_COUNTRY_FRENCH_ANTILLES 5901 +#define INFO_COUNTRY_FRENCH_GUIANA 594 +#define INFO_COUNTRY_FRENCH_POLYNESIA 689 +#define INFO_COUNTRY_GABON 241 +#define INFO_COUNTRY_GAMBIA 220 +#define INFO_COUNTRY_GEORGIA 995 +#define INFO_COUNTRY_GERMANY 49 +#define INFO_COUNTRY_GHANA 233 +#define INFO_COUNTRY_GIBRALTAR 350 +#define INFO_COUNTRY_GREECE 30 +#define INFO_COUNTRY_GREENLAND 299 +#define INFO_COUNTRY_GRENADA 111 +#define INFO_COUNTRY_GUADELOUPE 590 +#define INFO_COUNTRY_GUAM_US 671 +#define INFO_COUNTRY_GUATEMALA 502 +#define INFO_COUNTRY_GUINEA 224 +#define INFO_COUNTRY_GUINEA_BISSAU 245 +#define INFO_COUNTRY_GUYANA 592 +#define INFO_COUNTRY_HAITI 509 +#define INFO_COUNTRY_HONDURAS 504 +#define INFO_COUNTRY_HONG_KONG 852 +#define INFO_COUNTRY_HUNGARY 36 +#define INFO_COUNTRY_ICELAND 354 +#define INFO_COUNTRY_INDIA 91 +#define INFO_COUNTRY_INDONESIA 62 +#define INFO_COUNTRY_IRAN 98 +#define INFO_COUNTRY_IRAQ 964 +#define INFO_COUNTRY_IRELAND 353 +#define INFO_COUNTRY_ISRAEL 972 +#define INFO_COUNTRY_ITALY 39 +#define INFO_COUNTRY_IVORY_COAST 225 +#define INFO_COUNTRY_JAMAICA 112 +#define INFO_COUNTRY_JAPAN 81 +#define INFO_COUNTRY_JORDAN 962 +#define INFO_COUNTRY_KAZAKHSTAN 705 +#define INFO_COUNTRY_KENYA 254 +#define INFO_COUNTRY_KIRIBATI 686 +#define INFO_COUNTRY_KOREA_NORTH 850 +#define INFO_COUNTRY_KOREA_SOUTH 82 +#define INFO_COUNTRY_KUWAIT 965 +#define INFO_COUNTRY_KYRGYZSTAN 706 +#define INFO_COUNTRY_LAOS 856 +#define INFO_COUNTRY_LATVIA 371 +#define INFO_COUNTRY_LEBANON 961 +#define INFO_COUNTRY_LESOTHO 266 +#define INFO_COUNTRY_LIBERIA 231 +#define INFO_COUNTRY_LIBYA 218 +#define INFO_COUNTRY_LIECHTENSTEIN 4101 +#define INFO_COUNTRY_LITHUANIA 370 +#define INFO_COUNTRY_LUXEMBOURG 352 +#define INFO_COUNTRY_MACAU 853 +#define INFO_COUNTRY_MACEDONIA 389 +#define INFO_COUNTRY_MADAGASCAR 261 +#define INFO_COUNTRY_MALAWI 265 +#define INFO_COUNTRY_MALAYSIA 60 +#define INFO_COUNTRY_MALDIVES 960 +#define INFO_COUNTRY_MALI 223 +#define INFO_COUNTRY_MALTA 356 +#define INFO_COUNTRY_MARSHALL_ISLANDS 692 +#define INFO_COUNTRY_MARTINIQUE 596 +#define INFO_COUNTRY_MAURITANIA 222 +#define INFO_COUNTRY_MAURITIUS 230 +#define INFO_COUNTRY_MAYOTTE_ISLAND 269 +#define INFO_COUNTRY_MEXICO 52 +#define INFO_COUNTRY_MICRONESIA 691 +#define INFO_COUNTRY_MOLDOVA 373 +#define INFO_COUNTRY_MONACO 377 +#define INFO_COUNTRY_MONGOLIA 976 +#define INFO_COUNTRY_MONTSERRAT 113 +#define INFO_COUNTRY_MOROCCO 212 +#define INFO_COUNTRY_MOZAMBIQUE 258 +#define INFO_COUNTRY_MYANMAR 95 +#define INFO_COUNTRY_NAMIBIA 264 +#define INFO_COUNTRY_NAURU 674 +#define INFO_COUNTRY_NEPAL 977 +#define INFO_COUNTRY_NETHERLANDS 31 +#define INFO_COUNTRY_NETHERLANDS_ANTILLES 599 +#define INFO_COUNTRY_NEVIS 114 +#define INFO_COUNTRY_NEW_CALEDONIA 687 +#define INFO_COUNTRY_NEW_ZEALAND 64 +#define INFO_COUNTRY_NICARAGUA 505 +#define INFO_COUNTRY_NIGER 227 +#define INFO_COUNTRY_NIGERIA 234 +#define INFO_COUNTRY_NIUE 683 +#define INFO_COUNTRY_NORFOLK_ISLAND 6722 +#define INFO_COUNTRY_NORWAY 47 +#define INFO_COUNTRY_OMAN 968 +#define INFO_COUNTRY_PAKISTAN 92 +#define INFO_COUNTRY_PALAU 680 +#define INFO_COUNTRY_PANAMA 507 +#define INFO_COUNTRY_PAPUA_NEW_GUINEA 675 +#define INFO_COUNTRY_PARAGUAY 595 +#define INFO_COUNTRY_PERU 51 +#define INFO_COUNTRY_PHILIPPINES 63 +#define INFO_COUNTRY_POLAND 48 +#define INFO_COUNTRY_PORTUGAL 351 +#define INFO_COUNTRY_PUERTO_RICO 121 +#define INFO_COUNTRY_QATAR 974 +#define INFO_COUNTRY_REUNION_ISLAND 262 +#define INFO_COUNTRY_ROMANIA 40 +#define INFO_COUNTRY_ROTA_ISLAND 6701 +#define INFO_COUNTRY_RUSSIA 7 +#define INFO_COUNTRY_RWANDA 250 +#define INFO_COUNTRY_SAINT_HELENA 290 +#define INFO_COUNTRY_SAINT_KITTS 115 +#define INFO_COUNTRY_SAINT_KITTS_AND_NEVIS 1141 +#define INFO_COUNTRY_SAINT_LUCIA 122 +#define INFO_COUNTRY_SAINT_PIERRE_AND_MIQUELON 508 +#define INFO_COUNTRY_SAINT_VINCENT_AND_THE_GRENADINES 116 +#define INFO_COUNTRY_SAIPAN_ISLAND 670 +#define INFO_COUNTRY_SAN_MARINO 378 +#define INFO_COUNTRY_SAO_TOME_AND_PRINCIPE 239 +#define INFO_COUNTRY_SAUDI_ARABIA 966 +#define INFO_COUNTRY_SCOTLAND 442 +#define INFO_COUNTRY_SENEGAL 221 +#define INFO_COUNTRY_SEYCHELLES 248 +#define INFO_COUNTRY_SIERRA_LEONE 232 +#define INFO_COUNTRY_SINGAPORE 65 +#define INFO_COUNTRY_SLOVAKIA 421 +#define INFO_COUNTRY_SLOVENIA 386 +#define INFO_COUNTRY_SOLOMON_ISLANDS 677 +#define INFO_COUNTRY_SOMALIA 252 +#define INFO_COUNTRY_SOUTH_AFRICA 27 +#define INFO_COUNTRY_SPAIN 34 +#define INFO_COUNTRY_SRI_LANKA 94 +#define INFO_COUNTRY_SUDAN 249 +#define INFO_COUNTRY_SURINAME 597 +#define INFO_COUNTRY_SWAZILAND 268 +#define INFO_COUNTRY_SWEDEN 46 +#define INFO_COUNTRY_SWITZERLAND 41 +#define INFO_COUNTRY_SYRIAN_ARAB_REPUBLIC 963 +#define INFO_COUNTRY_TAIWAN 886 +#define INFO_COUNTRY_TAJIKISTAN 708 +#define INFO_COUNTRY_TANZANIA 255 +#define INFO_COUNTRY_THAILAND 66 +#define INFO_COUNTRY_TINIAN_ISLAND 6702 +#define INFO_COUNTRY_TOGO 228 +#define INFO_COUNTRY_TOKELAU 690 +#define INFO_COUNTRY_TONGA 676 +#define INFO_COUNTRY_TRINIDAD_AND_TOBAGO 117 +#define INFO_COUNTRY_TUNISIA 216 +#define INFO_COUNTRY_TURKEY 90 +#define INFO_COUNTRY_TURKMENISTAN 709 +#define INFO_COUNTRY_TURKS_AND_CAICOS_ISLANDS 118 +#define INFO_COUNTRY_TUVALU 688 +#define INFO_COUNTRY_UGANDA 256 +#define INFO_COUNTRY_UKRAINE 380 +#define INFO_COUNTRY_UNITED_ARAB_EMIRATES 971 +#define INFO_COUNTRY_UNITED_KINGDOM 44 +#define INFO_COUNTRY_URUGUAY 598 +#define INFO_COUNTRY_USA 1 +#define INFO_COUNTRY_UZBEKISTAN 711 +#define INFO_COUNTRY_VANUATU 678 +#define INFO_COUNTRY_VATICAN_CITY 379 +#define INFO_COUNTRY_VENEZUELA 58 +#define INFO_COUNTRY_VIETNAM 84 +#define INFO_COUNTRY_VIRGIN_ISLANDS 123 +#define INFO_COUNTRY_WALES 441 +#define INFO_COUNTRY_WALLIS_AND_FUTUNA_ISLANDS 681 +#define INFO_COUNTRY_WESTERN_SAMOA 685 +#define INFO_COUNTRY_YEMEN 967 +#define INFO_COUNTRY_YUGOSLAVIA 381 +#define INFO_COUNTRY_YUGOSLAVIA_MONTENEGRO 382 +#define INFO_COUNTRY_YUGOSLAVIA_SERBIA 3811 +#define INFO_COUNTRY_ZAMBIA 260 +#define INFO_COUNTRY_ZIMBABWE 263 + +#define INFO_GENDER_UNKNOWN 0 +#define INFO_GENDER_FEMALE 1 +#define INFO_GENDER_MALE 2 + +#define INFO_LANG_NONE 0 +#define INFO_LANG_AFRIKAANS 55 +#define INFO_LANG_ALBANIAN 58 +#define INFO_LANG_ARABIC 1 +#define INFO_LANG_ARMENIAN 59 +#define INFO_LANG_AZERBAIJANI 68 +#define INFO_LANG_BELORUSSIAN 72 +#define INFO_LANG_BHOJPURI 2 +#define INFO_LANG_BOSNIAN 56 +#define INFO_LANG_BULGARIAN 3 +#define INFO_LANG_BURMESE 4 +#define INFO_LANG_CANTONESE 5 +#define INFO_LANG_CATALAN 6 +#define INFO_LANG_CHAMORRO 61 +#define INFO_LANG_CHINESE 7 +#define INFO_LANG_CROATIAN 8 +#define INFO_LANG_CZECH 9 +#define INFO_LANG_DANISH 10 +#define INFO_LANG_DUTCH 11 +#define INFO_LANG_ENGLISH 12 +#define INFO_LANG_ESPERANTO 13 +#define INFO_LANG_ESTONIAN 14 +#define INFO_LANG_FARCI 15 +#define INFO_LANG_FINNISH 16 +#define INFO_LANG_FRENCH 17 +#define INFO_LANG_GAELIC 18 +#define INFO_LANG_GERMAN 19 +#define INFO_LANG_GREEK 20 +#define INFO_LANG_GUJARATI 70 +#define INFO_LANG_HEBREW 21 +#define INFO_LANG_HINDI 22 +#define INFO_LANG_HUNGARIAN 23 +#define INFO_LANG_ICELANDIC 24 +#define INFO_LANG_INDONESIAN 25 +#define INFO_LANG_ITALIAN 26 +#define INFO_LANG_JAPANESE 27 +#define INFO_LANG_KHMER 28 +#define INFO_LANG_KOREAN 29 +#define INFO_LANG_KURDISH 69 +#define INFO_LANG_LAO 30 +#define INFO_LANG_LATVIAN 31 +#define INFO_LANG_LITHUANIAN 32 +#define INFO_LANG_MACEDONIAN 65 +#define INFO_LANG_MALAY 33 +#define INFO_LANG_MANDARIN 63 +#define INFO_LANG_MONGOLIAN 62 +#define INFO_LANG_NORWEGIAN 34 +#define INFO_LANG_PERSIAN 57 +#define INFO_LANG_POLISH 35 +#define INFO_LANG_PORTUGUESE 36 +#define INFO_LANG_PUNJABI 60 +#define INFO_LANG_ROMANIAN 37 +#define INFO_LANG_RUSSIAN 38 +#define INFO_LANG_SERBO_CROATIAN 39 +#define INFO_LANG_SINDHI 66 +#define INFO_LANG_SLOVAK 40 +#define INFO_LANG_SLOVENIAN 41 +#define INFO_LANG_SOMALI 42 +#define INFO_LANG_SPANISH 43 +#define INFO_LANG_SWAHILI 44 +#define INFO_LANG_SWEDISH 45 +#define INFO_LANG_TAGALOG 46 +#define INFO_LANG_TAIWANESS 64 +#define INFO_LANG_TAMIL 71 +#define INFO_LANG_TATAR 47 +#define INFO_LANG_THAI 48 +#define INFO_LANG_TURKISH 49 +#define INFO_LANG_UKRAINIAN 50 +#define INFO_LANG_URDU 51 +#define INFO_LANG_VIETNAMESE 52 +#define INFO_LANG_WELSH 67 +#define INFO_LANG_YIDDISH 53 +#define INFO_LANG_YORUBA 54 + +#define INFO_MARITAL_UNSPECIFIED 0 +#define INFO_MARITAL_SINGLE 10 +#define INFO_MARITAL_CLOSE_RELATIONSHIPS 11 +#define INFO_MARITAL_ENGAGED 12 +#define INFO_MARITAL_MARRIED 20 +#define INFO_MARITAL_DIVORCED 30 +#define INFO_MARITAL_SEPARATED 31 +#define INFO_MARITAL_WIDOWED 40 + +#define INFO_OCCUPATION_UNSPECIFIED 0 +#define INFO_OCCUPATION_ACADEMIC 1 +#define INFO_OCCUPATION_ADMINISTRATIVE 2 +#define INFO_OCCUPATION_ART_ENTERTAINMENT 3 +#define INFO_OCCUPATION_COLLEGE_STUDENT 4 +#define INFO_OCCUPATION_COMPUTERS 5 +#define INFO_OCCUPATION_COMMUNITY_SOCIAL 6 +#define INFO_OCCUPATION_EDUCATION 7 +#define INFO_OCCUPATION_ENGINEERING 8 +#define INFO_OCCUPATION_FINANCIAL_SERVICES 9 +#define INFO_OCCUPATION_GOVERNMENT 10 +#define INFO_OCCUPATION_HIGH_SCHOOL_STUDENT 11 +#define INFO_OCCUPATION_HOME 12 +#define INFO_OCCUPATION_ICQ_PROVIDING_HELP 13 +#define INFO_OCCUPATION_LAW 14 +#define INFO_OCCUPATION_MANAGERIAL 15 +#define INFO_OCCUPATION_MANUFACTURING 16 +#define INFO_OCCUPATION_MEDICAL_HEALTH 17 +#define INFO_OCCUPATION_MILITARY 18 +#define INFO_OCCUPATION_NON_GOVERNMENT_ORGANIZATION 19 +#define INFO_OCCUPATION_PROFESSIONAL 20 +#define INFO_OCCUPATION_RETAIL 21 +#define INFO_OCCUPATION_RETIRED 22 +#define INFO_OCCUPATION_SCIENCE_RESEARCH 23 +#define INFO_OCCUPATION_SPORTS 24 +#define INFO_OCCUPATION_TECHNICAL 25 +#define INFO_OCCUPATION_UNIVERSITY_STUDENT 26 +#define INFO_OCCUPATION_WEB_BUILDING 27 +#define INFO_OCCUPATION_OTHER_SERVICES 99 + +#define INFO_INTEREST_UNSPECIFIED 0 +#define INFO_INTEREST_ART 100 +#define INFO_INTEREST_CARS 101 +#define INFO_INTEREST_CELEBRITY_FANS 102 +#define INFO_INTEREST_COLLECTIONS 103 +#define INFO_INTEREST_COMPUTERS 104 +#define INFO_INTEREST_CULTURE_LITERATURE 105 +#define INFO_INTEREST_FITNESS 106 +#define INFO_INTEREST_GAMES 107 +#define INFO_INTEREST_HOBBIES 108 +#define INFO_INTEREST_ICQ_PROVIDING_HELP 109 +#define INFO_INTEREST_INTERNET 110 +#define INFO_INTEREST_LIFESTYLE 111 +#define INFO_INTEREST_MOVIES_TV 112 +#define INFO_INTEREST_MUSIC 113 +#define INFO_INTEREST_OUTDOOR_ACTIVITIES 114 +#define INFO_INTEREST_PARENTING 115 +#define INFO_INTEREST_PETS_ANIMALS 116 +#define INFO_INTEREST_RELIGION 117 +#define INFO_INTEREST_SCIENCE_TECHNOLOGY 118 +#define INFO_INTEREST_SKILLS 119 +#define INFO_INTEREST_SPORTS 120 +#define INFO_INTEREST_WEB_DESIGN 121 +#define INFO_INTEREST_NATURE_AND_ENVIRONMENT 122 +#define INFO_INTEREST_NEWS_MEDIA 123 +#define INFO_INTEREST_GOVERNMENT 124 +#define INFO_INTEREST_BUSINESS_ECONOMY 125 +#define INFO_INTEREST_MYSTICS 126 +#define INFO_INTEREST_TRAVEL 127 +#define INFO_INTEREST_ASTRONOMY 128 +#define INFO_INTEREST_SPACE 129 +#define INFO_INTEREST_CLOTHING 130 +#define INFO_INTEREST_PARTIES 131 +#define INFO_INTEREST_WOMEN 132 +#define INFO_INTEREST_SOCIAL_SCIENCE 133 +#define INFO_INTEREST_60S 134 +#define INFO_INTEREST_70S 135 +#define INFO_INTEREST_80S 136 +#define INFO_INTEREST_50S 137 +#define INFO_INTEREST_FINANCE_AND_CORPORATE 138 +#define INFO_INTEREST_ENTERTAINMENT 139 +#define INFO_INTEREST_CONSUMER_ELECTRONICS 140 +#define INFO_INTEREST_RETAIL_STORES 141 +#define INFO_INTEREST_HEALTH_AND_BEAUTY 142 +#define INFO_INTEREST_MEDIA 143 +#define INFO_INTEREST_HOUSEHOLD_PRODUCTS 144 +#define INFO_INTEREST_MAIL_ORDER_CATALOG 145 +#define INFO_INTEREST_BUSINESS_SERVICES 146 +#define INFO_INTEREST_AUDIO_AND_VISUAL 147 +#define INFO_INTEREST_SPORTING_AND_ATHLETIC 148 +#define INFO_INTEREST_PUBLISHING 149 +#define INFO_INTEREST_HOME_AUTOMATION 150 + +#define INFO_PAST_UNSPECIFIED 0 +#define INFO_PAST_ELEMENTARY_SCHOOL 300 +#define INFO_PAST_HIGH_SCHOOL 301 +#define INFO_PAST_COLLEGE 302 +#define INFO_PAST_UNIVERSITY 303 +#define INFO_PAST_MILITARY 304 +#define INFO_PAST_PAST_WORK_PLACE 305 +#define INFO_PAST_PAST_ORGANIZATION 306 +#define INFO_PAST_OTHER 399 + +#define INFO_AFFIL_UNSPECIFIED 0 +#define INFO_AFFIL_ALUMNI_ORG 200 +#define INFO_AFFIL_CHARITY_ORG 201 +#define INFO_AFFIL_CLUB_SOCIAL_ORG 202 +#define INFO_AFFIL_COMMUNITY_ORG 203 +#define INFO_AFFIL_CULTURAL_ORG 204 +#define INFO_AFFIL_FAN_CLUBS 205 +#define INFO_AFFIL_FRATERNITY_SORORITY 206 +#define INFO_AFFIL_HOBBYISTS_ORG 207 +#define INFO_AFFIL_INTERNATIONAL_ORG 208 +#define INFO_AFFIL_NATURE_AND_ENVIRONMENT_ORG 209 +#define INFO_AFFIL_PROFESSIONAL_ORG 210 +#define INFO_AFFIL_SCIENTIFIC_TECHNICAL_ORG 211 +#define INFO_AFFIL_SELF_IMPROVEMENT_GROUP 212 +#define INFO_AFFIL_SPIRITUAL_RELIGIOUS_ORG 213 +#define INFO_AFFIL_SPORTS_ORG 214 +#define INFO_AFFIL_SUPPORT_ORG 215 +#define INFO_AFFIL_TRADE_AND_BUSINESS_ORG 216 +#define INFO_AFFIL_UNION 217 +#define INFO_AFFIL_VOLUNTEER_ORG 218 +#define INFO_AFFIL_OTHER 299 + +#define PRIV_ALL_CAN_SEE (uint8_t)1 +#define PRIV_NOBODY_CAN_SEE (uint8_t)2 +#define PRIV_VISLIST_CAN_SEE (uint8_t)3 +#define PRIV_INVISLIST_CANNOT_SEE (uint8_t)4 +#define PRIV_CONTACTLIST_CAN_SEE (uint8_t)5 + +#define MSG_TYPE_PLAINTEXT (uint8_t)0x01 +#define MSG_TYPE_PLUGIN (uint8_t)0x1a +#define MSG_TYPE_AUTOAWAY (uint8_t)0xE8 +#define MSG_TYPE_AUTOBUSY (uint8_t)0xE9 +#define MSG_TYPE_AUTONA (uint8_t)0xEA +#define MSG_TYPE_AUTODND (uint8_t)0xEB +#define MSG_TYPE_AUTOFFC (uint8_t)0xEC + +#define CONN_ERR_BAD_LOGIN 0x0004 +#define CONN_ERR_BAD_PASSWORD 0x0005 +#define CONN_ERR_RATE_LIMIT 0x0018 + +#define SIGNOFF_OTHER_LOCATION 0x0001 + +#endif diff --git a/main_freebsd.cpp b/main_freebsd.cpp new file mode 100644 index 0000000..2c8936d --- /dev/null +++ b/main_freebsd.cpp @@ -0,0 +1,1446 @@ +#pragma hdrstop + +#pragma checkoption -tWM + +//--------------------------------------------------------------------------- +#include + +#ifdef _WIN32 + #include + #include + #include + #include +#else + /* POSIX-compatible includes */ + #include + #include + #include + #include + #include + #include + #include + + #include + #include + #include + #include + #include + + /* ПроÑ�тые переименованиÑ� и дополненьица */ + #define O_BINARY 0 + #define ioctlsocket ioctl + #define SOCKET int + + /*МакÑ�имальный размер Ñ�тека потока */ + #if defined(__LP64__) + #define THREAD_STACK_SIZE 65536 * 2 + #else + #define THREAD_STACK_SIZE 65536 + #endif +#endif /* POSIX-compatible includes */ + +#ifdef DEBUG +#define DPRINTF(...) printf(__VA_ARGS__) +#else +#define DPRINTF(...) +#endif + +#include +#include +#include + +#include "icqkid2.h" +#include "revision.h" +#include "statgen.h" +#include "md5.h" + +// КонÑ�танты операций (взаимодейÑ�твие Ñ� Ñ�ервером шлюза) +#define T_REQLOGIN 1 +#define T_SENDMSG 2 +#define T_RECVMSG 3 +#define T_LOGIN 4 +#define T_ERROR 6 +#define T_CLENTRY 7 +#define T_STATUSCHANGE 9 +#define T_AUTHREQ 10 +#define T_REQINFOSHORT 11 +#define T_ADDCONTACT 12 +#define T_SSLRESP 13 +#define T_AUTHGRANT 14 +#define T_MY_STATUS_CH 15 // Изменение моего Ñ�татуÑ�а +//Сервер (аол) получил Ñ�ообщение +#define T_SRV_ACK 16 +//Клиент получил Ñ�ообщение +#define T_CLIENT_ACK 17 + +#define T_ECHO 18 +#define T_ECHORET 19 + +#define T_GROUPID 20 +#define T_GROUPFOLLOW 21 + +#define T_MY_XSTATUS_CH 22 + +//Мой ответ Чемберлену (подтверждение доÑ�тавки) +#define T_MSGACK 23 + +#define T_XTEXT_REQ 24 +#define T_XTEXT_ACK 25 +#define T_XTEXT_SET 26 + +#define T_ADDCONTACT_WITH_GRP 27 + +#define T_ADDGROUP 28 +#define T_ADDIGNORE 29 +#define T_SETPRIVACY 30 +#define T_LASTPRIVACY 31 + +#define T_SETCLIENT_ID 32 + +#define T_REMOVECONTACT 33 + /* Удаление контакта (client->server) */ +#define T_CONTACTREMOVED 34 + /* Удаление контакта (server->client) */ +#define T_MD5AUTH 35 +#pragma pack(push) +#pragma pack(1) +typedef struct +{ + uint32_t uin; + uint16_t type; + uint16_t len; + char data[16384]; +}NatICQ_PKT; +#pragma pack(pop) + +typedef struct +{ + uint32_t SEQ; + uint32_t uin; + char cookies[8]; +}TACK; + +/*****************************************************/ + +class MyIcqInterface : public ICQKid2{ + virtual void getMD5hash(vector md5_salt, uint8_t * md5_hash); + virtual void onIncomingMsg(ICQKid2Message msg); + virtual void onXstatusChanged(string uin, vector x_status, string x_title, string x_descr); + virtual void onClientMsgAck(string uin, uint8_t *cookie); + virtual void onServerMsgAck(string uin, uint8_t *cookie); + virtual void onUserNotify(string uin, uint32_t stat1, uint32_t stat2, bool invis_flag, vector xs); + virtual void onClientIDNotify(string uin, uint8_t clientid); + virtual void onSingOff(uint16_t err_code, string err_url); + virtual void onAuthRequest(string from, string text); + virtual void onAuthReply(string from, string text, uint8_t aflag); +public: + int total_msg; + int total_sended; + int naticq_sock; + int ack_size; + TACK ack_cache[32]; + void NatICQTX(NatICQ_PKT *p); + bool md5auth; +}; + +/* Ð�ужные объекты Ñ�инхронизации */ +#define SYNC_NUM 2 /* МакÑ�имальное чиÑ�ло объектов Ñ�инхронизации */ + +#define CRIT_STAT 0 /* СтатиÑ�тика клиентов */ +#ifdef _WIN32 +CRITICAL_SECTION CritSection[SYNC_NUM]; +#else +pthread_mutex_t mtx[SYNC_NUM]; +#endif + +int NatICQRX(int sock, NatICQ_PKT *p, int tmr); + +inline void Mutex_Lock(unsigned short crit_index) +{ +#ifdef _WIN32 + EnterCriticalSection(&CritSection[crit_index]); +#else + pthread_mutex_lock(&mtx[crit_index]); + +#endif +} +inline void Mutex_Unlock(unsigned short crit_index) +{ +#ifdef _WIN32 + LeaveCriticalSection(&CritSection[crit_index]); +#else + pthread_mutex_unlock(&mtx[crit_index]); +#endif +} + +inline void Mutex_Init(unsigned short crit_index) +{ +#ifdef _WIN32 + InitializeCriticalSection(&CritSection[crit_index]); +#else + int rc; + rc = pthread_mutex_init(&mtx[crit_index], NULL); + assert(rc != -1); +#endif +} + +inline void Mutex_Destroy(unsigned short crit_index) +{ +#ifdef _WIN32 + DeleteCriticalSection(&CritSection[crit_index]); +#else + pthread_mutex_destroy(&mtx[crit_index]); +#endif +} + +unsigned short ClientsCount = 0; +char *StatOutput_Path = NULL; + +#pragma hdrstop +unsigned int char16to8(unsigned int c) +{ + typedef struct + { + unsigned short u; + char dos; + char win; + char koi8; + } TUNICODE2CHAR; + static const TUNICODE2CHAR unicode2char[]= + { + // CAPITAL Cyrillic letters (base) + 0x410,0x80,0xC0,0xE1, // Ð� + 0x411,0x81,0xC1,0xE2, // Б + 0x412,0x82,0xC2,0xF7, // Ð’ + 0x413,0x83,0xC3,0xE7, // Г + 0x414,0x84,0xC4,0xE4, // Д + 0x415,0x85,0xC5,0xE5, // Е + 0x416,0x86,0xC6,0xF6, // Ж + 0x417,0x87,0xC7,0xFA, // З + 0x418,0x88,0xC8,0xE9, // И + 0x419,0x89,0xC9,0xEA, // Й + 0x41A,0x8A,0xCA,0xEB, // К + 0x41B,0x8B,0xCB,0xEC, // Л + 0x41C,0x8C,0xCC,0xED, // М + 0x41D,0x8D,0xCD,0xEE, // Ð� + 0x41E,0x8E,0xCE,0xEF, // О + 0x41F,0x8F,0xCF,0xF0, // П + 0x420,0x90,0xD0,0xF2, // Р + 0x421,0x91,0xD1,0xF3, // С + 0x422,0x92,0xD2,0xF4, // Т + 0x423,0x93,0xD3,0xF5, // У + 0x424,0x94,0xD4,0xE6, // Ф + 0x425,0x95,0xD5,0xE8, // Ð¥ + 0x426,0x96,0xD6,0xE3, // Ц + 0x427,0x97,0xD7,0xFE, // Ч + 0x428,0x98,0xD8,0xFB, // Ш + 0x429,0x99,0xD9,0xFD, // Щ + 0x42A,0x9A,0xDA,0xFF, // Ъ + 0x42B,0x9B,0xDB,0xF9, // Ы + 0x42C,0x9C,0xDC,0xF8, // Ь + 0x42D,0x9D,0xDD,0xFC, // Э + 0x42E,0x9E,0xDE,0xE0, // Ю + 0x42F,0x9F,0xDF,0xF1, // Я + // CAPITAL Cyrillic letters (additional) + 0x402,'_',0x80,'_', // _ .*.* + 0x403,'_',0x81,'_', // _ .*.* + 0x409,'_',0x8A,'_', // _ .*.* + 0x40A,'_',0x8C,'_', // _ .*.* + 0x40C,'_',0x8D,'_', // _ .*.* + 0x40B,'_',0x8E,'_', // _ .*.* + 0x40F,'_',0x8F,'_', // _ .*.* + 0x40E,0xF6,0xA1,'_', // ÐŽ ...* + 0x408,0x4A,0xA3,0x4A, // _ .*.* + 0x409,0x83,0xA5,0xBD, // _ .*.. + 0x401,0xF0,0xA8,0xB3, // Ð� + 0x404,0xF2,0xAA,0xB4, // Є + 0x407,0xF4,0xAF,0xB7, // Ї + 0x406,0x49,0xB2,0xB6, // _ .*.. + 0x405,0x53,0xBD,0x53, // _ .*.* + // SMALL Cyrillic letters (base) + 0x430,0xA0,0xE0,0xC1, // а + 0x431,0xA1,0xE1,0xC2, // б + 0x432,0xA2,0xE2,0xD7, // в + 0x433,0xA3,0xE3,0xC7, // г + 0x434,0xA4,0xE4,0xC4, // д + 0x435,0xA5,0xE5,0xC5, // е + 0x436,0xA6,0xE6,0xD6, // ж + 0x437,0xA7,0xE7,0xDA, // з + 0x438,0xA8,0xE8,0xC9, // и + 0x439,0xA9,0xE9,0xCA, // й + 0x43A,0xAA,0xEA,0xCB, // к + 0x43B,0xAB,0xEB,0xCC, // л + 0x43C,0xAC,0xEC,0xCD, // м + 0x43D,0xAD,0xED,0xCE, // н + 0x43E,0xAE,0xEE,0xCF, // о + 0x43F,0xAF,0xEF,0xD0, // п + 0x440,0xE0,0xF0,0xD2, // Ñ€ + 0x441,0xE1,0xF1,0xD3, // Ñ� + 0x442,0xE2,0xF2,0xD4, // Ñ‚ + 0x443,0xE3,0xF3,0xD5, // у + 0x444,0xE4,0xF4,0xC6, // Ñ„ + 0x445,0xE5,0xF5,0xC8, // Ñ… + 0x446,0xE6,0xF6,0xC3, // ц + 0x447,0xE7,0xF7,0xDE, // ч + 0x448,0xE8,0xF8,0xDB, // ш + 0x449,0xE9,0xF9,0xDD, // щ + 0x44A,0xEA,0xFA,0xDF, // ÑŠ + 0x44B,0xEB,0xFB,0xD9, // Ñ‹ + 0x44C,0xEC,0xFC,0xD8, // ÑŒ + 0x44D,0xED,0xFD,0xDC, // Ñ� + 0x44E,0xEE,0xFE,0xC0, // ÑŽ + 0x44F,0xEF,0xFF,0xD1, // Ñ� + // SMALL Cyrillic letters (additional) + 0x452,'_',0x90,'_', // _ .*.* + 0x453,'_',0x83,'_', // _ .*.* + 0x459,'_',0x9A,'_', // _ .*.* + 0x45A,'_',0x9C,'_', // _ .*.* + 0x45C,'_',0x9D,'_', // _ .*.* + 0x45B,'_',0x9E,'_', // _ .*.* + 0x45F,'_',0x9F,'_', // _ .*.* + 0x45E,0xF7,0xA2,'_', // Ñž ...* + 0x458,0x6A,0xBC,0x6A, // _ .*.* + 0x491,0xA3,0xB4,0xAD, // _ .*.. + 0x451,0xF1,0xB8,0xA3, // Ñ‘ + 0x454,0xF3,0xBA,0xA4, // Ñ” + 0x457,0xF5,0xBF,0xA7, // Ñ— + 0x456,0x69,0xB3,0xA6, // _ .*.. + 0x455,0x73,0xBE,0x73, // _ .*.* + 0x0A0,'_',0xA0,0x20, // space .*.. + 0x0A4,'_',0xA4,0xFD, // ¤ .*.. + 0x0A6,'_',0xA6,'_', // ¦ .*.* + 0x0B0,0xF8,0xB0,0x9C, // ° + 0x0B7,0xFA,0xB7,0x9E, // · + // 0x2022,,0x95,0x95, //  .*.. + // 0x2116,0xFC,0xB9,0x23, // â„– ...* + // 0x2219,,0xF9,0x9E, //  .*.. + // 0x221A,0xFB,,0x96, // v ..*. + // 0x25A0,0xFE,,0x94, // ¦ + 0x0000,0,0,0 + }; + const TUNICODE2CHAR *p=unicode2char; + unsigned int i; + if (c<128) return(c); + while((i=p->u)!=0) + { + if (c==i) + { + return(p->win); + } + p++; + } + c&=0xFF; + if (c<32) return(' '); + return(c); +} + +int utf8_2_win1251(char *out, const char *in, int outsz) +{ + int c; + char *old=out; + while((c=*in++)!=0) + { + if ((c&0xE0)==0xC0) + { + if ((in[0]&0xC0)==0x80) + { + c&=0x1F; + c<<=6; + c|=(*in++)&0x3F; + } + else goto L_1251; + } + else + if ((c&0xF0)==0xE0) + { + if (((in[0]&0xC0)==0x80)&&((in[1]&0xC0)==0x80)) + { + c&=0x0F; + c<<=12; + c|=((*in++)&0x3F)<<6; + c|=((*in++)&0x3F)<<0; + } + else break; + } + L_1251: + if (outsz--) + { + *out++=char16to8(c); + } + } + return out-old; +// if (outsz) *out=0; +} + +unsigned int char8to16(int c) +{ + if (c==0xA8) c=0x401; + if (c==0xAA) c=0x404; + if (c==0xAF) c=0x407; + if (c==0xB8) c=0x451; + if (c==0xBA) c=0x454; + if (c==0xBF) c=0x457; + if (c==0xB2) c=0x406; + if (c==0xB3) c=0x456; + if ((c>=0xC0)&&(c<0x100)) c+=0x350; + return(c); +} + +void win1251_2_utf8(string & out, const unsigned char *in) +{ + int c; + while((c=char8to16(*in++))!=0) + { + if (c>=0x80) + { + out+=(c>>6)|0xC0; + out+=(c&0x3F)|0x80; + } + else + out+=c; + } +} + +//==================================================================== +void MyIcqInterface::getMD5hash(vector md5_salt, uint8_t * md5_hash){ + if (md5auth){ + NatICQ_PKT pkt; + pkt.type=T_MD5AUTH; + pkt.len=md5_salt.size(); + memcpy(pkt.data, &md5_salt[0], md5_salt.size()); + pkt.data[pkt.len]=0; + NatICQTX(&pkt); + do { + if (NatICQRX(naticq_sock,&pkt,60)<=0){ + break; + } + } while(pkt.type!=T_MD5AUTH); + memcpy(md5_hash,&pkt.data,16); + }else{ + static uint8_t AOL_SALT_STR[]="AOL Instant Messenger (SM)"; + vector auth_sum=md5_salt; + if (mypassword.length()>8) auth_sum.insert(auth_sum.end(), (uint8_t*)mypassword.data(), ((uint8_t*)mypassword.data())+8); + else auth_sum.insert(auth_sum.end(), (uint8_t*)mypassword.data(), ((uint8_t*)mypassword.data())+mypassword.length()); + auth_sum.insert(auth_sum.end(), (uint8_t*)AOL_SALT_STR, ((uint8_t*)AOL_SALT_STR)+sizeof(AOL_SALT_STR)-1); + calculate_md5((const char*)(&auth_sum[0]), auth_sum.size(), (char *)md5_hash); + } +} +void MyIcqInterface::onAuthRequest(string from, string text) +{ + if (findCLUIN(from.c_str())>=0){ + NatICQ_PKT pkt; + pkt.uin=atoi(from.c_str()); + pkt.type=T_RECVMSG; + strcpy(pkt.data,"Auth REQ: "); + pkt.len=strlen(pkt.data); +// pkt.len+=utf8_2_win1251(pkt.data+pkt.len,text.c_str(),16383-pkt.len); + NatICQTX(&pkt); + } +} + +void MyIcqInterface::onAuthReply(string from, string text, uint8_t aflag) +{ + NatICQ_PKT pkt; + pkt.uin=atoi(from.c_str()); + pkt.type=T_RECVMSG; + strcpy(pkt.data,aflag?"Auth Resp OK!":"Auth resp failed, reason: "); + pkt.len=strlen(pkt.data); +// pkt.len+=utf8_2_win1251(pkt.data+pkt.len,text.c_str(),16383-pkt.len); + NatICQTX(&pkt); +} + +void MyIcqInterface::NatICQTX(NatICQ_PKT *p){ + int sz=p->len+8; + send(naticq_sock,(char *)p,sz,0); + total_sended+=sz; + return; +} + +void MyIcqInterface::onUserNotify(string uin, uint32_t stat1, uint32_t stat2, bool invis_flag, vector xs) +{ + NatICQ_PKT pkt; + pkt.uin=atoi(uin.c_str()); + pkt.type=T_STATUSCHANGE; + if (invis_flag) + { + ((uint32_t *)(pkt.data))[0]=STATUS_INVISIBLE; + } + else + { + memcpy(pkt.data,&stat2,2); + } + if (pkt.len=min(xs.size(),X_STATUS_MAX_BOUND)){ + memcpy(&pkt.data[2],&xs[0],pkt.len); + }else{ + pkt.data[2]=0; + pkt.len=1; + } + pkt.len+=2; + NatICQTX(&pkt); + int i=findCLUIN(uin); + if ((i>=0)&&ContactListUins[i].clientid){ + pkt.len=utf8_2_win1251(pkt.data,ContactListUins[i].nick.c_str(),16383); + pkt.data[pkt.len]='@'; + pkt.len++; + pkt.type=T_CLENTRY; + NatICQTX(&pkt); + } +} + +void MyIcqInterface::onClientIDNotify(string uin, uint8_t clientid){ + NatICQ_PKT pkt; + int i=findCLUIN(uin); + if ((i>=0)&&clientid){ + pkt.uin=ContactListUins[i].groupid; + pkt.len=0; + pkt.type=T_GROUPFOLLOW; + NatICQTX(&pkt); + pkt.uin=atoi(uin.c_str()); + pkt.len=utf8_2_win1251(pkt.data,ContactListUins[i].nick.c_str(),16383); + pkt.data[pkt.len]='@'; + pkt.len++; + pkt.type=T_CLENTRY; + NatICQTX(&pkt); + } +} + +void MyIcqInterface::onClientMsgAck(string uin, uint8_t *cookie) +{ + NatICQ_PKT pkt; + pkt.uin=atoi(uin.c_str()); + pkt.type=T_CLIENT_ACK; + pkt.len=2; + memcpy(pkt.data, cookie+4, 2); + NatICQTX(&pkt); +} + +void MyIcqInterface::onServerMsgAck(string uin, uint8_t *cookie) +{ + NatICQ_PKT pkt; + pkt.uin=atoi(uin.c_str()); + pkt.type=T_SRV_ACK; + pkt.len=2; + memcpy(pkt.data, cookie+4, 2); + NatICQTX(&pkt); +} + +void MyIcqInterface::onXstatusChanged(string uin, vector x_status, string x_title, string x_descr) +{ + NatICQ_PKT pkt; + int i=utf8_2_win1251(pkt.data+1,x_title.c_str(),255); + pkt.data[0]=i; + pkt.len=i+utf8_2_win1251(pkt.data+1+i,x_descr.c_str(),16383-256)+1; + pkt.uin=atoi(uin.c_str()); + pkt.type=T_XTEXT_ACK; + NatICQTX(&pkt); +} + +void MyIcqInterface::onIncomingMsg(ICQKid2Message msg){ + NatICQ_PKT pkt; + int i; + size_t steps; + switch (msg.enc_type){ + case ICQKid2Message::USASCII : + i=msg.text.size(); + memcpy(pkt.data,&msg.text[0],i); + pkt.len=i; + break; + case ICQKid2Message::LOCAL8BIT : + i=msg.text.size(); + memcpy(pkt.data,&msg.text[0],i); + pkt.len=i; + break; + case ICQKid2Message::UCS2BE : + steps = msg.text.length()>>1; + for (size_t i=0; i 65535)) + { + printf("Incorrect port number. Value must be in range 2-65535\n"); + return -1; + } + printf("IG11 NatICQ server (rev %s)\n", __SVN_REVISION__); + tzset(); +#ifdef _WIN32 + printf("Win32 build\n"); + srand(GetTickCount()); +#else +#ifdef __FreeBSD__ + printf("FreeBSD build\n"); +#else + printf("Linux build\n"); //в жопу гну +#endif //* FreeBSD * / + printf("Thread stack size: %u\n", THREAD_STACK_SIZE); + srand(time(NULL)); /* ИнициализациÑ� RNG Ñ� помощью random(4) */ +#endif //* _WIN32 * / + + if(argc==3) + { + StatOutput_Path = (char*)malloc(strlen(argv[2])+1); + strcpy(StatOutput_Path, argv[2]); + DPRINTF("stat outfile is %s\n",StatOutput_Path ); + } + + // 1.1. ИнициализациÑ� WINSOCK + +//DEFAULT_PORT: + +#ifdef _WIN32 + WSADATA WSAData; + + if(WSAStartup(MAKEWORD(2,0), &WSAData) != 0) + { + printf("WSAStartup() failed with error: %i\n", WSAGetLastError()); + return -1; + } + + // 1.2. Создание "Ñ�лушающего" Ñ�окета + if((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) + { + printf("socket() failed with error: %i\n", WSAGetLastError()); + return -1; + } + // УÑ�тановка опции: REUSE ADDRESS. + // ЕÑ�ли Ñ�ервер "упадёт" при подÑ�оединённых клиентах + // он Ñ�может Ñ�нова Ñ�тартовать иÑ�пользуÑ� тот же порт. + bool opt = true; + if(SOCKET_ERROR == setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt))) + { + printf("setsockopt(SO_REUSEADDR) failed with error: %i\n", WSAGetLastError()); + return -1; + } + //* 1.3. ПривÑ�зка Ñ�окета к локальному адреÑ�у * / + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + server_addr.sin_port = htons(port); + if(bind(listen_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) == SOCKET_ERROR) + { + printf("bind() failed with error: %i\n", WSAGetLastError()); + return -1; + } + /* 1.4. Включение проÑ�лушиваниÑ� на Ñ�ерверном Ñ�окете */ + if(listen(listen_socket, 5) == SOCKET_ERROR) + { + printf("listen() failed with error: %i\n", WSAGetLastError()); + return -1; + } +#else + DPRINTF("Running FreeBSD sockets initialization\n"); + //* FreeBSD code * / + setvbuf (stdout, 0, _IONBF, 0); // Отключение буферизации stdout + // 1.2. Создание "Ñ�лушающего" Ñ�окета + if((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { + printf("socket() failed with error: %i\n", errno); + return -1; + } + assert(listen_socket!=0); + //* 1.3. ПривÑ�зка Ñ�окета к локальному адреÑ�у * / + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + server_addr.sin_port = htons(port); + + // УÑ�тановка опции: REUSE ADDRESS. + // ЕÑ�ли Ñ�ервер "упадёт" при подÑ�оединённых клиентах + // он Ñ�может Ñ�нова Ñ�тартовать иÑ�пользуÑ� тот же порт. + + int opt = 1; + if(-1 == setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))){ + printf("setsockopt(SO_REUSEADDR) failed with error: %i\n", errno); + } + + /* Биндим Ñ�окет */ + if(bind(listen_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) { + printf("bind() failed with error: %i\n", errno); + return -1; + } + + //* 1.4. Включение проÑ�лушиваниÑ� на Ñ�ерверном Ñ�окете * / + if(listen(listen_socket, 5) == -1) + { + printf("listen() failed with error: %i\n", errno ); + return -1; + } + + // УÑ�тановка обработчиков Ñ�игналов + signal(SIGINT, SIGINT_Catcher); + signal(SIGPIPE, SIGPIPE_Catcher); +#endif //* FreeBSD/Linux socket init * / + + ClientsCount = 0; + Mutex_Init(CRIT_STAT); + + /* 1.5. Цикл принÑ�тиÑ� Ñ�оединений от клиентов */ + THREAD_PARAM *t_param; // = NULL; +#ifdef _WIN32 + int peerlen = sizeof(t_param->peer); +#else + socklen_t peerlen = sizeof(t_param->peer); +#endif + +#ifdef _WIN32 + while((server_socket = accept(listen_socket, (struct sockaddr *) &temp_addr, (int *)&accepted_len)) != INVALID_SOCKET) + { +#else + int thr_error = 0; + pthread_t hThread; + pthread_attr_t attr; + + while(!Must_stop) + { + server_socket = accept(listen_socket, (struct sockaddr *) &temp_addr, (socklen_t *)&accepted_len); + if(server_socket==-1){ + printf("Socket error. Exit!!!, errno=%d\n",errno); + exit(1); + } + +#ifdef __FreeBSD__ + if(-1 == setsockopt(listen_socket, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt))){ + printf("setsockopt(SO_NOSIGPIPE) failed with error: %i\n", errno); + } +#endif +#endif + t_param = new THREAD_PARAM; + + if(NULL == t_param) + { + printf("memory allocation error!\n"); + break; + } + t_param->uin=0; + // Какой хоÑ�Ñ‚ Ñ�оединилÑ�Ñ� Ñ� Ñ�ервером ? + + if(0 == getpeername(server_socket, &(t_param->peer), &peerlen)) + { + printf("Connect from %s:%i\n", inet_ntoa(((sockaddr_in *)&(t_param->peer))->sin_addr), ntohs(((sockaddr_in *)&(t_param->peer))->sin_port)); + } + + // ЗапуÑ�к потока длÑ� работы Ñ� Ñ�окетом и передача параметров + // (динамичеÑ�каÑ� памÑ�ть будет оÑ�вобождена при выходе из потока) + + t_param->s = server_socket; +// strcpy(t_param->exe, argv[2]); + +#ifdef _WIN32 + HANDLE hThread; + // Ñ�оздаём поток Ñ� помощью _beginthreadex() + if(NULL == (hThread = (HANDLE)_beginthreadex(NULL, 0, ServerThread, t_param, 0, NULL))) + { + printf("CreateThread() failed with error: %i\n", GetLastError()); + } +#else + int thr_error = 0; + pthread_t hThread; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + if(thr_error = pthread_create(&hThread, &attr, ServerThread, t_param) != 0) + { + printf("pthread_create() failed with error: %i\n", thr_error); + } +#endif + else + { + DPRINTF("New thread created, id=%X\n", hThread); + Mutex_Lock(CRIT_STAT); + ClientsCount++; + UpdateStatPage(); + Mutex_Unlock(CRIT_STAT); + } +#ifndef _WIN32 + pthread_attr_destroy(&attr); +#endif + + } + printf("Finished serving. Closing sockets and cleaning up...\n"); +#ifdef _WIN32 + DWORD e=GetLastError(); + shutdown(listen_socket, 2); + char buf[16]; + while(recv(listen_socket,buf,16,0)>0); +#else + shutdown(listen_socket, SHUT_RDWR); +#endif + + Mutex_Destroy(CRIT_STAT); + if(StatOutput_Path) free(StatOutput_Path); + return 0; +} + +int NatICQRX(int sock, NatICQ_PKT *p, int tmr) +{ + unsigned long res; + fd_set rfds; + struct timeval tv; + + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return -1; + if (res<8) + { + tv.tv_sec=tmr; + tv.tv_usec=10000; //Ожидание 10мÑ� + FD_ZERO(&rfds); + FD_SET(sock, &rfds); +// if (!tmr) return 0; + int sel_ret = select(sock+1, &rfds, NULL, NULL, &tv); + if (sel_ret==0) return 0; + if (sel_ret<0) return -1; + if (!FD_ISSET(sock, &rfds)) return -1; //TNETWORK_ERR; + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return -1; + if (res<1) return -1; //Обрыв Ñ�вÑ�зи + } + if (res<8) return 0; + if (recv(sock,(char *)p,8,0)!=8) return -1; + if (p->len>16383) return -1; + if (!p->len) return 1; + char *cp=p->data; + int l=p->len; + int i; + while(l>0) + { + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return -1; + if (!res) + { + tv.tv_sec=120; + tv.tv_usec=0; + FD_ZERO(&rfds); + FD_SET(sock, &rfds); + int sel_ret = select(sock+1, &rfds, NULL, NULL, &tv); + if (sel_ret==0) return -1; + if (sel_ret<0) return -1; + if (!FD_ISSET(sock, &rfds)) return -1; //TNETWORK_ERR; + res=0; + if (ioctlsocket(sock,FIONREAD,&res)) return -1; + } + if (res<1) return -1; + if (res>(unsigned int)l) res=l; + i=recv(sock,cp,res,0); + if (i<=0) return -1; + cp+=i; + l-=i; + } + return 1; +} + +void print_ssl_answer(char *s, unsigned short r) +{ + switch(r) + { + case SSI_EDIT_OK: + strcpy(s,"OK"); + break; + case SSI_EDIT_ERR_NOTFOUND: + strcpy(s,"Not found"); + break; + case SSI_EDIT_ERR_EXIST: + strcpy(s,"Allready exist"); + break; + case SSI_EDIT_ERR_NETWORK: + strcpy(s,"Network error"); + break; + case SSI_EDIT_ERR_CANTADD: + strcpy(s,"Can't add"); + break; + case SSI_EDIT_ERR_LIMIT: + strcpy(s,"Limit reached"); + break; + case SSI_EDIT_ERR_AIMLIST: + strcpy(s,"AIM<->ICQ not alowed"); + break; + case SSI_EDIT_ERR_NEEDAUTH: + strcpy(s,"Contact need auth"); + break; + default: + sprintf(s,"Unknown code %u",r); + break; + } +} + static const unsigned long pr_statuses[5]= + { + PRIV_ALL_CAN_SEE, + PRIV_NOBODY_CAN_SEE, + PRIV_VISLIST_CAN_SEE, + PRIV_INVISLIST_CANNOT_SEE, + PRIV_CONTACTLIST_CAN_SEE, + }; + + +#ifdef _WIN32 +unsigned int __stdcall ServerThread(LPVOID tParam) +#else +void *ServerThread(void *tParam) +#endif +{ + THREAD_PARAM *param = (THREAD_PARAM *)tParam; +#ifdef _WIN32 + unsigned int s=param->s; +#else + int s=param->s; +#endif + int i; + int j; + char uin[32]; + unsigned short r=0; + + time_t naticq_timeout; + MyIcqInterface miif; + miif.naticq_sock=s; + miif.subIGId=__SVN_REVISION__; + NatICQ_PKT rpkt; + DPRINTF("ServerThread started on socket %d\n", param->s); + do{ + if (NatICQRX(s,&rpkt,60)<=0) goto L_ERR; + if (rpkt.type==T_SETCLIENT_ID) + { + rpkt.data[rpkt.len]=0; + rpkt.data[8]=0; + miif.subClientId=rpkt.data; + } + } + while(rpkt.type!=T_REQLOGIN); + + miif.total_sended=0; + miif.total_msg=0; + miif.ack_size=0; + miif.setNetworkTimeout(1200/3); + + if (rpkt.len){ + rpkt.data[rpkt.len]=0; + miif.setPassword(rpkt.data); + miif.md5auth=false; + strcpy(rpkt.data,"Download new version!"); + rpkt.len=strlen(rpkt.data); + rpkt.type=T_ERROR; + miif.NatICQTX(&rpkt); + }else{ + miif.md5auth=true; + } + + sprintf(rpkt.data,"%u",rpkt.uin); + + miif.setUIN(rpkt.data); + param->uin = (uint32_t) rpkt.uin; + printf("ICQ UIN is %u\n", param->uin); + + if (!miif.doConnect()){ + printf("Connect failed at %d%% for %u\n",miif.connect_phase_percentage,param->uin); + sprintf(rpkt.data,"Connect failed at %d%%",miif.connect_phase_percentage); + L_ERRTX: + rpkt.len=strlen(rpkt.data); + rpkt.uin=0; + rpkt.type=T_ERROR; + miif.NatICQTX(&rpkt); +#ifdef _WIN32 + Sleep(1000); +#else + sleep(1); +#endif + goto L_ERR; + } + + rpkt.len=0; + rpkt.type=T_LOGIN; + miif.NatICQTX(&rpkt); + i=0; + j=miif.getMyPrivacyStatus(); + do + { + if (j==(int)pr_statuses[i]) break; + i++; + } + while(i<5); + rpkt.data[0]=i; + rpkt.len=1; + rpkt.type=T_LASTPRIVACY; + miif.NatICQTX(&rpkt); + for(i=0;i<(int)miif.ContactListGroups.size();i++) + { + rpkt.uin=miif.ContactListGroups[i].id; + //memcpy(rpkt.data,&miif.ContactListGroups[i].name[0],rpkt.len=miif.ContactListGroups[i].name.size()); + rpkt.len=utf8_2_win1251(rpkt.data,miif.ContactListGroups[i].name.c_str(),16383); + rpkt.type=T_GROUPID; + miif.NatICQTX(&rpkt); + } + j=-1; + for(i=0;i<(int)miif.ContactListUins.size();i++) + { + if (miif.ContactListUins[i].groupid!=j) + { + j=miif.ContactListUins[i].groupid; + rpkt.uin=j; + rpkt.len=0; + rpkt.type=T_GROUPFOLLOW; + miif.NatICQTX(&rpkt); + } + rpkt.uin=atoi(miif.ContactListUins[i].uin.c_str()); + //memcpy(rpkt.data,&miif.ContactListUins[i].nick[0],rpkt.len=miif.ContactListUins[i].nick.size()); + rpkt.len=utf8_2_win1251(rpkt.data,miif.ContactListUins[i].nick.c_str(),16383); + if (miif.ContactListUins[i].clientid){ + rpkt.data[rpkt.len]='@'; + rpkt.len++; + } + rpkt.type=T_CLENTRY; + miif.NatICQTX(&rpkt); + } + rpkt.uin=0; + rpkt.len=0; + rpkt.type=T_CLENTRY; + miif.NatICQTX(&rpkt); + if (!miif.getOfflineMessages()) goto L_ERR; + naticq_timeout=time(NULL)+300; + while(true) + { + int net_ret=miif.pollIncomingEvents(1); + if (net_ret!=TNETWORK_TIMEOUT && net_ret!=1) goto L_ERR; + if ((i=NatICQRX(s,&rpkt,0))<0) break; + if (!i) + { + if (time(NULL)=0) && + (miif.ContactListUins[uen_ind].srv_relay_cap) && + (miif.ContactListUins[uen_ind].online_status!=STATUS_OFFLINE) + ) + { + if (miif.ContactListUins[uen_ind].unicode_cap) + { + string utf; + win1251_2_utf8(utf, (unsigned char*)rpkt.data); + if (!miif.sendMessage(MyIcqInterface::ICQKid2Message(s,utf,MyIcqInterface::ICQKid2Message::UTF8),miif.total_msg)) goto L_OLDMSG; + } + else + { + if (!miif.sendMessage(MyIcqInterface::ICQKid2Message(s,rpkt.data,MyIcqInterface::ICQKid2Message::UTF8),miif.total_msg)) goto L_OLDMSG; + } + } + else + { + L_OLDMSG: + miif.sendMessage(MyIcqInterface::ICQKid2Message(s,rpkt.data,MyIcqInterface::ICQKid2Message::LOCAL8BIT),miif.total_msg); + } + } + break; + case T_MSGACK: + for(i=0; i0) + grp=miif.ContactListGroups[0].name; + else + grp=""; + win1251_2_utf8(nick,(unsigned char *)rpkt.data); + if (miif.findCLUIN(uin)<0) + { + miif.addContact(uin,nick,grp,&r); + } + else + { + miif.renameContact(uin,nick,&r); + } + if (r==SSI_EDIT_OK) + { + int i=miif.findCLGroup(grp); + if (i>=0) + { + rpkt.uin=miif.ContactListGroups[i].id; + rpkt.len=0; + rpkt.type=T_GROUPFOLLOW; + miif.NatICQTX(&rpkt); + } + rpkt.uin=atoi(uin); + rpkt.len=utf8_2_win1251(rpkt.data,nick.c_str(),16383); + rpkt.type=T_CLENTRY; + miif.NatICQTX(&rpkt); + rpkt.uin=0; + rpkt.len=0; + rpkt.type=T_CLENTRY; + miif.NatICQTX(&rpkt); + } + print_ssl_answer(rpkt.data,r); + rpkt.uin=0; + rpkt.type=T_SSLRESP; + rpkt.len=strlen(rpkt.data); + miif.NatICQTX(&rpkt); + break; + } + case T_ADDCONTACT_WITH_GRP: + { + int gi; + string grp; + char uin[32]; + string nick; + unsigned short r=0; + rpkt.data[rpkt.len]=0; + sprintf(uin,"%u",rpkt.uin); + gi=miif.findCLGroup(*((unsigned int *)(rpkt.data))); + if (gi>=0) + { + grp=miif.ContactListGroups[gi].name; + } + else + { + if (miif.ContactListGroups.size()>0) + grp=miif.ContactListGroups[0].name; + else + grp=""; + } + win1251_2_utf8(nick,(unsigned char *)rpkt.data+4); + if (miif.findCLUIN(uin)<0) + { + miif.addContact(uin,nick,grp,&r); + } + else + { + miif.removeContact(uin,&r); + miif.addContact(uin,nick,grp,&r); + } + if (r==SSI_EDIT_OK) + { + int i=miif.findCLGroup(grp); + if (i>=0) + { + rpkt.uin=miif.ContactListGroups[i].id; + rpkt.len=0; + rpkt.type=T_GROUPFOLLOW; + miif.NatICQTX(&rpkt); + } + rpkt.uin=atoi(uin); + rpkt.len=utf8_2_win1251(rpkt.data,nick.c_str(),16383); + rpkt.type=T_CLENTRY; + miif.NatICQTX(&rpkt); + rpkt.uin=0; + rpkt.len=0; + rpkt.type=T_CLENTRY; + miif.NatICQTX(&rpkt); + } + print_ssl_answer(rpkt.data,r); + rpkt.uin=0; + rpkt.type=T_SSLRESP; + rpkt.len=strlen(rpkt.data); + miif.NatICQTX(&rpkt); + break; + } + + case T_REMOVECONTACT: + /* Fuck up contact */ + + rpkt.data[rpkt.len]=0; + sprintf(uin,"%u",rpkt.uin); + + miif.removeContact(uin,&r); + + if (r==SSI_EDIT_OK) { + rpkt.type=T_CONTACTREMOVED; + rpkt.len=0; + miif.NatICQTX(&rpkt); + + }else{ + rpkt.len=sprintf(rpkt.data,"Error! Expected %d, got %d", SSI_EDIT_OK, r); + rpkt.type=T_ERROR; + miif.NatICQTX(&rpkt); + } + print_ssl_answer(rpkt.data,r); + rpkt.uin=0; + rpkt.type=T_SSLRESP; + rpkt.len=strlen(rpkt.data); + miif.NatICQTX(&rpkt); + break; + + case T_SETPRIVACY: + if ((i=rpkt.data[0])<5) + { + miif.setMyPrivacyStatus(pr_statuses[i]); + } + break; + + default: + /* Unknown opcode */ + + DPRINTF("%u: unknown opcode \"%u\" received!\n", param->uin, rpkt.type); + rpkt.len=sprintf(rpkt.data,"Unknown opcode %u", rpkt.type); + rpkt.type=T_ERROR; + miif.NatICQTX(&rpkt); + break; + } +// + } + miif.doDisconnect(); + // Сообщить кто отключилÑ�Ñ� +L_ERR: + + printf("Disconnect from %s:%i\n", inet_ntoa(((sockaddr_in *)&(param->peer))->sin_addr), ntohs(((sockaddr_in *)&(param->peer))->sin_port)); + +#ifdef _WIN32 + int me=0; +#else + pthread_t me; + me = pthread_self(); +#endif + printf("Thread %X (uin %u) received disconnect!\n", me, param->uin); + // Закрыть Ñ�окет и оÑ�вободить памÑ�ть переданной Ñ�труктуры +#ifdef _WIN32 + shutdown(s, 2); //SD_BOTH); + closesocket(s); +#else + shutdown(s, SHUT_RDWR); /* Shutdown socket, disallow further reads and writes */ + close(s); /* Close handle */ +#endif + delete param; + param = NULL; + + Mutex_Lock(CRIT_STAT); + ClientsCount--; + UpdateStatPage(); + Mutex_Unlock(CRIT_STAT); + return 0; /* Эквивалентно pthread_exit(), но при Ñ�том вызываютÑ�Ñ� нужные деÑ�трукторы */ +} +//--------------------------------------------------------------------------- + +/* + * Local Variables: * + * c-file-style: "bsd" * + * End: * + */ diff --git a/md5.cpp b/md5.cpp new file mode 100644 index 0000000..a22d30d --- /dev/null +++ b/md5.cpp @@ -0,0 +1,258 @@ +/*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include "md5.h" + +/* typedef a 32 bit type */ +typedef unsigned long int UINT4; + +/* Data structure for MD5 (Message Digest) computation */ +typedef struct { + UINT4 i[2]; /* number of _bits_ handled mod 2^64 */ + UINT4 buf[4]; /* scratch buffer */ + unsigned char in[64]; /* input buffer */ + unsigned char digest[16]; /* actual digest after MD5Final call */ +} MD5_CTX; + +static unsigned char PADDING[64] = { + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +/* F, G and H are basic MD5 functions: selection, majority, parity */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + +/* ROTATE_LEFT rotates x left n bits */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */ +/* Rotation is separate from addition to prevent recomputation */ + +#define FF(a, b, c, d, x, s, ac) {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } +#define GG(a, b, c, d, x, s, ac) {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } +#define HH(a, b, c, d, x, s, ac) {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } +#define II(a, b, c, d, x, s, ac) {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } + + +/* Basic MD5 step. Transform buf based on in. + */ +static void Transform (UINT4 *buf, UINT4 *in) +{ + UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3]; + + /* Round 1 */ +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 + FF ( a, b, c, d, in[ 0], S11, (UINT4) 3614090360u); /* 1 */ + FF ( d, a, b, c, in[ 1], S12, (UINT4) 3905402710u); /* 2 */ + FF ( c, d, a, b, in[ 2], S13, (UINT4) 606105819u); /* 3 */ + FF ( b, c, d, a, in[ 3], S14, (UINT4) 3250441966u); /* 4 */ + FF ( a, b, c, d, in[ 4], S11, (UINT4) 4118548399u); /* 5 */ + FF ( d, a, b, c, in[ 5], S12, (UINT4) 1200080426u); /* 6 */ + FF ( c, d, a, b, in[ 6], S13, (UINT4) 2821735955u); /* 7 */ + FF ( b, c, d, a, in[ 7], S14, (UINT4) 4249261313u); /* 8 */ + FF ( a, b, c, d, in[ 8], S11, (UINT4) 1770035416u); /* 9 */ + FF ( d, a, b, c, in[ 9], S12, (UINT4) 2336552879u); /* 10 */ + FF ( c, d, a, b, in[10], S13, (UINT4) 4294925233u); /* 11 */ + FF ( b, c, d, a, in[11], S14, (UINT4) 2304563134u); /* 12 */ + FF ( a, b, c, d, in[12], S11, (UINT4) 1804603682u); /* 13 */ + FF ( d, a, b, c, in[13], S12, (UINT4) 4254626195u); /* 14 */ + FF ( c, d, a, b, in[14], S13, (UINT4) 2792965006u); /* 15 */ + FF ( b, c, d, a, in[15], S14, (UINT4) 1236535329u); /* 16 */ + + /* Round 2 */ +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 + GG ( a, b, c, d, in[ 1], S21, (UINT4) 4129170786u); /* 17 */ + GG ( d, a, b, c, in[ 6], S22, (UINT4) 3225465664u); /* 18 */ + GG ( c, d, a, b, in[11], S23, (UINT4) 643717713u); /* 19 */ + GG ( b, c, d, a, in[ 0], S24, (UINT4) 3921069994u); /* 20 */ + GG ( a, b, c, d, in[ 5], S21, (UINT4) 3593408605u); /* 21 */ + GG ( d, a, b, c, in[10], S22, (UINT4) 38016083u); /* 22 */ + GG ( c, d, a, b, in[15], S23, (UINT4) 3634488961u); /* 23 */ + GG ( b, c, d, a, in[ 4], S24, (UINT4) 3889429448u); /* 24 */ + GG ( a, b, c, d, in[ 9], S21, (UINT4) 568446438u); /* 25 */ + GG ( d, a, b, c, in[14], S22, (UINT4) 3275163606u); /* 26 */ + GG ( c, d, a, b, in[ 3], S23, (UINT4) 4107603335u); /* 27 */ + GG ( b, c, d, a, in[ 8], S24, (UINT4) 1163531501u); /* 28 */ + GG ( a, b, c, d, in[13], S21, (UINT4) 2850285829u); /* 29 */ + GG ( d, a, b, c, in[ 2], S22, (UINT4) 4243563512u); /* 30 */ + GG ( c, d, a, b, in[ 7], S23, (UINT4) 1735328473u); /* 31 */ + GG ( b, c, d, a, in[12], S24, (UINT4) 2368359562u); /* 32 */ + + /* Round 3 */ +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 + HH ( a, b, c, d, in[ 5], S31, (UINT4) 4294588738u); /* 33 */ + HH ( d, a, b, c, in[ 8], S32, (UINT4) 2272392833u); /* 34 */ + HH ( c, d, a, b, in[11], S33, (UINT4) 1839030562u); /* 35 */ + HH ( b, c, d, a, in[14], S34, (UINT4) 4259657740u); /* 36 */ + HH ( a, b, c, d, in[ 1], S31, (UINT4) 2763975236u); /* 37 */ + HH ( d, a, b, c, in[ 4], S32, (UINT4) 1272893353u); /* 38 */ + HH ( c, d, a, b, in[ 7], S33, (UINT4) 4139469664u); /* 39 */ + HH ( b, c, d, a, in[10], S34, (UINT4) 3200236656u); /* 40 */ + HH ( a, b, c, d, in[13], S31, (UINT4) 681279174u); /* 41 */ + HH ( d, a, b, c, in[ 0], S32, (UINT4) 3936430074u); /* 42 */ + HH ( c, d, a, b, in[ 3], S33, (UINT4) 3572445317u); /* 43 */ + HH ( b, c, d, a, in[ 6], S34, (UINT4) 76029189u); /* 44 */ + HH ( a, b, c, d, in[ 9], S31, (UINT4) 3654602809u); /* 45 */ + HH ( d, a, b, c, in[12], S32, (UINT4) 3873151461u); /* 46 */ + HH ( c, d, a, b, in[15], S33, (UINT4) 530742520u); /* 47 */ + HH ( b, c, d, a, in[ 2], S34, (UINT4) 3299628645u); /* 48 */ + + /* Round 4 */ +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + II ( a, b, c, d, in[ 0], S41, (UINT4) 4096336452u); /* 49 */ + II ( d, a, b, c, in[ 7], S42, (UINT4) 1126891415u); /* 50 */ + II ( c, d, a, b, in[14], S43, (UINT4) 2878612391u); /* 51 */ + II ( b, c, d, a, in[ 5], S44, (UINT4) 4237533241u); /* 52 */ + II ( a, b, c, d, in[12], S41, (UINT4) 1700485571u); /* 53 */ + II ( d, a, b, c, in[ 3], S42, (UINT4) 2399980690u); /* 54 */ + II ( c, d, a, b, in[10], S43, (UINT4) 4293915773u); /* 55 */ + II ( b, c, d, a, in[ 1], S44, (UINT4) 2240044497u); /* 56 */ + II ( a, b, c, d, in[ 8], S41, (UINT4) 1873313359u); /* 57 */ + II ( d, a, b, c, in[15], S42, (UINT4) 4264355552u); /* 58 */ + II ( c, d, a, b, in[ 6], S43, (UINT4) 2734768916u); /* 59 */ + II ( b, c, d, a, in[13], S44, (UINT4) 1309151649u); /* 60 */ + II ( a, b, c, d, in[ 4], S41, (UINT4) 4149444226u); /* 61 */ + II ( d, a, b, c, in[11], S42, (UINT4) 3174756917u); /* 62 */ + II ( c, d, a, b, in[ 2], S43, (UINT4) 718787259u); /* 63 */ + II ( b, c, d, a, in[ 9], S44, (UINT4) 3951481745u); /* 64 */ + + buf[0] += a; + buf[1] += b; + buf[2] += c; + buf[3] += d; +} + +static void MD5Init (MD5_CTX *mdContext) +{ + mdContext->i[0] = mdContext->i[1] = (UINT4)0; + + /* Load magic initialization constants. + */ + mdContext->buf[0] = (UINT4)0x67452301; + mdContext->buf[1] = (UINT4)0xefcdab89; + mdContext->buf[2] = (UINT4)0x98badcfe; + mdContext->buf[3] = (UINT4)0x10325476; +} + +static void MD5Update (MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen) +{ + UINT4 in[16]; + int mdi; + unsigned int i, ii; + + /* compute number of bytes mod 64 */ + mdi = (int)((mdContext->i[0] >> 3) & 0x3F); + + /* update number of bits */ + if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0]) + mdContext->i[1]++; + mdContext->i[0] += ((UINT4)inLen << 3); + mdContext->i[1] += ((UINT4)inLen >> 29); + + while (inLen--) { + /* add new character to buffer, increment mdi */ + mdContext->in[mdi++] = *inBuf++; + + /* transform if necessary */ + if (mdi == 0x40) { + for (i = 0, ii = 0; i < 16; i++, ii += 4) + in[i] = (((UINT4)mdContext->in[ii+3]) << 24) | + (((UINT4)mdContext->in[ii+2]) << 16) | + (((UINT4)mdContext->in[ii+1]) << 8) | + ((UINT4)mdContext->in[ii]); + Transform (mdContext->buf, in); + mdi = 0; + } + } +} + +static void MD5Final (MD5_CTX *mdContext) +{ + UINT4 in[16]; + int mdi; + unsigned int i, ii; + unsigned int padLen; + + /* save number of bits */ + in[14] = mdContext->i[0]; + in[15] = mdContext->i[1]; + + /* compute number of bytes mod 64 */ + mdi = (int)((mdContext->i[0] >> 3) & 0x3F); + + /* pad out to 56 mod 64 */ + padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi); + MD5Update (mdContext, PADDING, padLen); + + /* append length in bits and transform */ + for (i = 0, ii = 0; i < 14; i++, ii += 4) + in[i] = (((UINT4)mdContext->in[ii+3]) << 24) | + (((UINT4)mdContext->in[ii+2]) << 16) | + (((UINT4)mdContext->in[ii+1]) << 8) | + ((UINT4)mdContext->in[ii]); + Transform (mdContext->buf, in); + + /* store buffer in digest */ + for (i = 0, ii = 0; i < 4; i++, ii += 4) { + mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF); + mdContext->digest[ii+1] = + (unsigned char)((mdContext->buf[i] >> 8) & 0xFF); + mdContext->digest[ii+2] = + (unsigned char)((mdContext->buf[i] >> 16) & 0xFF); + mdContext->digest[ii+3] = + (unsigned char)((mdContext->buf[i] >> 24) & 0xFF); + } +} + +/*********************************************************************** + ** End of md5.c ** + **********************************************************************/ + + +void calculate_md5(const char * src, unsigned int src_size, char * md5buf) +{ + MD5_CTX mdContext; + MD5Init(&mdContext); + MD5Update(&mdContext, (unsigned char *)src, src_size); + MD5Final (&mdContext); + memcpy(md5buf, mdContext.digest, 16); +} diff --git a/md5.h b/md5.h new file mode 100644 index 0000000..72e33dc --- /dev/null +++ b/md5.h @@ -0,0 +1,26 @@ +/*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _ICQKID2_MD5_H_ +#define _ICQKID2_MD5_H_ + +void calculate_md5(const char * src, unsigned int src_size, char * md5buf); + +#endif /* _MD5_H_ */ diff --git a/revision.h b/revision.h new file mode 100644 index 0000000..6b64881 --- /dev/null +++ b/revision.h @@ -0,0 +1 @@ +#define __SVN_REVISION__ "" diff --git a/snaccache.cpp b/snaccache.cpp new file mode 100644 index 0000000..7fc0c4b --- /dev/null +++ b/snaccache.cpp @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "snaccache.h" + +void SnacCache::addEntry(SNACData & snd) +{ + cache.push_back(pair(snd, time(NULL))); +} + +bool SnacCache::findEntry(SNACData & snd) +{ + for (size_t i=0; i=tmdelta) cache.erase(cache.begin()+i); + else ++i; + } +} diff --git a/snaccache.h b/snaccache.h new file mode 100644 index 0000000..50c561f --- /dev/null +++ b/snaccache.h @@ -0,0 +1,40 @@ +/*************************************************************************** + * Copyright (C) 2007 by Alexander S. Salieff * + * salieff@mail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _SNAC_CACHE_H_ +#define _SNAC_CACHE_H_ + +#include "flap.h" +#include +#include + +using namespace std; + +class SnacCache { + public: + void addEntry(SNACData & snd); + bool findEntry(SNACData & snd); + void clearOlderThan(time_t tmdelta); + + private: + vector > cache; +}; + +#endif diff --git a/stat_template.txt b/stat_template.txt new file mode 100644 index 0000000..b333877 --- /dev/null +++ b/stat_template.txt @@ -0,0 +1 @@ +Now clients: %d items. diff --git a/statgen.cpp b/statgen.cpp new file mode 100644 index 0000000..bdb6606 --- /dev/null +++ b/statgen.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include "statgen.h" + +extern unsigned short ClientsCount; +extern char *StatOutput_Path; + +void UpdateStatPage() +{ + if(!StatOutput_Path)return; + + FILE *html = fopen("stat_template.txt","r"); + if(!html) + { + printf("\nCannot open template, exiting...\n"); + return; + } + fseek(html,0,SEEK_END); + int fs = ftell(html); + fseek(html,0,SEEK_SET); + char *htm_unparsed = (char*) malloc(fs+1); + memset(htm_unparsed, 0, fs+1); + fread(htm_unparsed, fs, 1, html); + fclose(html); + int bc; + char *htm_parsed = (char*) malloc(bc = fs+1+16); + memset(htm_parsed, 0, bc); + sprintf(htm_parsed, htm_unparsed, ClientsCount); + //printf(htm_parsed); + int parsed_len = strlen(htm_parsed); + + FILE *html_out = fopen(StatOutput_Path, "w"); + fwrite(htm_parsed, parsed_len, 1, html_out); + fclose(html_out); + free(htm_parsed); + free(htm_unparsed); +} diff --git a/statgen.h b/statgen.h new file mode 100644 index 0000000..9466a2b --- /dev/null +++ b/statgen.h @@ -0,0 +1,6 @@ +#ifndef _STATGEN_H_ +#define _STATGEN_H_ + +void UpdateStatPage(); + +#endif