Exemple #1
0
void SendFriendMsgJob::run()
{
    bool has_img = false;
    foreach ( const QQChatItem &msg, msgs_ )
    {
        if ( msg.type() == QQChatItem::kFriendOffpic )
        {
            if ( !jobs_.contains(msg.content()) )
            {
                QFile img_file(msg.content());
                img_file.open(QIODevice::ReadOnly);
                QByteArray img_data = img_file.readAll();
                img_file.close();

                QByteArray data = Protocol::QQProtocol::instance()->imgSender()->createOffpicBody(msg.content(), img_data);

                jobs_.append(msg.content());
                SendImgJob *job = new SendImgJob(msg.content(), data, SendImgJob::kOffpic);
                connect(job, SIGNAL(sigJobDone(__JobBase *, bool)), this, SLOT(onImgSendDone(__JobBase *, bool)));
                job->run();

                has_img = true;
            }
        }
    }
/* http://toxygen.net/libgadu/protocol/#ch1.6 */
QString ImportFromGG::decode(const QByteArray& msg, const Contact user)      //uin potrzebny w razie pojawienia sie obrazka
{
  QString decoded_msg;
  QByteArray format;
  bool formatting=false;
  unsigned char key=0xff;
  int j=0, position=0, header=3;  //header - do pomijania nagłówka rich textu (0x02 + 2 bajty na długosc richa)

  while (j<msg.size())
  {
    quint8 c=msg.at(j);
    quint8 decoded_char=c^key;         //deszyfracja ;)
    if (formatting)
    {
      if (header>0)
      {
        format.append(decoded_char);
        position++;
      }
      else
        header--;
    }
    else
      if (!decoded_char)   //koniec, jeśli cos zostało jeszcze (i<msg.size()) to jest to formatowanie
        formatting=true;
      else
        decoded_msg.append(decoded_char);
    key=c;
    j++;
  }

  //sprawdź formatowanie w poszukiwaniu obrazków
  QByteArray nformat;
  position=0;

  unsigned char *array= (reinterpret_cast<unsigned char*>(format.data()));

  while (position<format.size())
  {
    struct gg_msg_richtext_format frm;
    struct gg_msg_richtext_image img;

    memcpy(&frm, array + position,sizeof(frm));
    for (unsigned int i=0;i<sizeof(frm);i++)
      nformat.append(format[position++]);

    if (frm.font & 0x08)   //kolor
      for (unsigned int i=0;i<sizeof(struct gg_msg_richtext_color);i++)
        nformat.append(format[position++]);

    if (frm.font & 0x80)   //obrazek
    {
      memcpy(&img, array + position,sizeof(img));
      for (unsigned int i=0;i<sizeof(img);i++)
        nformat.append(format[position++]);

      //sprawdź poprawność nagłówka - najwyraźniej kiedys był inny i powoduje to problemy
      //ewentualnie fake ;)
      if (img.unknown1!=0x109 || (img.size==20 && img.crc32==4567))
      {
        nformat="";                //calkowicie odrzuć formatowanie
        break;                     //zakoncz analize formatu
      }

      //wytnij dodatkowe informacje
      QByteArray file_name;
      position+=3;     //3 nieznane bajty
      while (array[position])      //nazwa
        file_name.append(array[position++]);
      position++;    //null
      position+=8;   //8 bajtów na crc i rozmiar (juz jest w nagłówku (img) )

      QFile img_file(ChatImageService::imagesPath() + QString("%1-%2-%3-%4").arg(user.id().toInt()).arg(img.size).arg(img.crc32).arg(file_name.data()));
      img_file.open(QIODevice::WriteOnly);
      img_file.write(reinterpret_cast<char*>(array+position),img.size);
      img_file.close();
      position+=img.size;
    }
  }

  FormattedMessage fmt_msg=GaduFormatter::createMessage(account, user, decoded_msg, (unsigned char*) nformat.data(), nformat.size(), true);
  return fmt_msg.toHtml();
}