summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/FMessage.cpp
blob: 0eca3aeef760e3b25e2a450045e564c338b998d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * FMessage.cpp
 *
 *  Created on: 02/07/2012
 *      Author: Antonio
 */

#include <ctime>
#include <stdlib.h>
#include <algorithm>
#include "FMessage.h"
#include "utilities.h"

//SDL_mutex* FMessage::generating_lock = SDL_CreateMutex();
IMutex* FMessage::generating_lock = NULL;
int FMessage::generating_id = 0;
std::string FMessage::generating_header = Utilities::intToStr(static_cast<int> (time(NULL))).append("-");


FMessage::FMessage() {
	this->key = NULL;
	this->timestamp = 0;
	this->media_wa_type = 0;
	this->longitude = 0;
	this->latitude = 0;
	this->media_duration_seconds = 0;
	this->media_size = 0;
	this->media_name = "";
	this->media_url = "";
	this->data = "";
}

FMessage::FMessage(const std::string& remote_jid, bool from_me, const std::string& data) {
	Key* local_key;
	FMessage::generating_lock->lock();
	FMessage::generating_id++;
	local_key = new Key(remote_jid, from_me, generating_header + Utilities::intToStr(generating_id));
	this->key = local_key;
	FMessage::generating_lock->unlock();
	this->data = data;
	this->timestamp = time(NULL);
	this->media_wa_type = 0;
	this->longitude = 0;
	this->latitude = 0;
	this->media_duration_seconds = 0;
	this->media_size = 0;
	this->media_name = "";
	this->media_url = "";
}

std::string FMessage::nextKeyIdNumber() {
	int id = 0;
	FMessage::generating_lock->lock();
	id = (FMessage::generating_id++);
	FMessage::generating_lock->unlock();
	return generating_header + (Utilities::intToStr(id));
}

FMessage::FMessage(Key* key) {
	this->key = key;
	this->timestamp = 0;
	this->media_wa_type = 0;
	this->longitude = 0;
	this->latitude = 0;
	this->media_duration_seconds = 0;
	this->media_size = 0;
	this->media_name = "";
	this->media_url = "";
}

std::string FMessage::getMessage_WA_Type_StrValue(unsigned char type) {
	switch (type) {
	case FMessage::WA_TYPE_UNDEFINED:
		return "";
	case FMessage::WA_TYPE_SYSTEM:
		return "system";
	case FMessage::WA_TYPE_AUDIO:
		return "audio";
	case FMessage::WA_TYPE_CONTACT:
		return "vcard";
	case FMessage::WA_TYPE_IMAGE:
		return "image";
	case FMessage::WA_TYPE_LOCATION:
		return "location";
	case FMessage::WA_TYPE_VIDEO:
		return "video";
	}

	return "";
}

FMessage::~FMessage() {
	if (this->key != NULL)
		delete key;
}

Key::Key(const std::string& remote_jid, bool from_me, const std::string& id) {
	this->remote_jid = remote_jid;
	this->from_me = from_me;
	this->id = id;
}

std::string Key::toString() {
	return "Key[id=" + id + ", from_me=" + (from_me ? "true":"false") + ", remote_jid=" + remote_jid + "]";
}


unsigned char FMessage::getMessage_WA_Type(std::string* type) {
	if ((type == NULL) || (type->length() == 0))
		return WA_TYPE_UNDEFINED;
	std::string typeLower = *type;
	std::transform(typeLower.begin(), typeLower.end(), typeLower.begin(), ::tolower);
	if (typeLower.compare("system") == 0)
		return WA_TYPE_SYSTEM;
	if (typeLower.compare("image") == 0)
		return WA_TYPE_IMAGE;
	if (typeLower.compare("audio") == 0)
		return WA_TYPE_AUDIO;
	if (typeLower.compare("video") == 0)
		return WA_TYPE_VIDEO;
	if (typeLower.compare("vcard") == 0)
		return WA_TYPE_CONTACT;
	if (typeLower.compare("location") == 0)
		return WA_TYPE_LOCATION;

	return WA_TYPE_UNDEFINED;
}