summaryrefslogtreecommitdiff
path: root/protocols/Omegle/src/connection.cpp
blob: f0f40221f1b2afae696f5b8212187cd638b87161 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*

Omegle plugin for Miranda Instant Messenger
_____________________________________________

Copyright © 2011-17 Robert Pösel, 2017-23 Miranda NG team

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, see <http://www.gnu.org/licenses/>.

*/

#include "stdafx.h"

// Known server messages, only to inform lpgen
const char *server_messages[] = {
	LPGEN("Stranger is using Omegle's mobile Web site (omegle.com on a phone or tablet)"),
	LPGEN("You both speak the same language.")
};

void OmegleProto::SignOn(void*)
{
	SYSTEMTIME t;
	GetLocalTime(&t);
	debugLogA("[%d.%d.%d] Using Omegle Protocol %s", t.wDay, t.wMonth, t.wYear, __VERSION_STRING_DOTS);

	ScopedLock s(signon_lock_);

	int old_status = m_iStatus;
	m_iStatus = m_iDesiredStatus;
	ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus);

	setDword("LogonTS", (uint32_t)time(0));
	ClearChat();
	OnJoinChat(0, false);

	if (getByte(OMEGLE_KEY_AUTO_CONNECT, 0))
		NewChat();
}

void OmegleProto::SignOff(void*)
{
	ScopedLock s(signon_lock_);

	int old_status = m_iStatus;
	m_iStatus = ID_STATUS_OFFLINE;

	Netlib_Shutdown(facy.hEventsConnection);

	OnLeaveChat(NULL, NULL);

	delSetting("LogonTS");

	ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus);
}

void OmegleProto::StopChat(bool disconnect)
{
	if (facy.state_ == STATE_WAITING)
		UpdateChat(nullptr, TranslateT("Connecting canceled."), false);

	else if (facy.state_ == STATE_ACTIVE || facy.state_ == STATE_SPY) {
		bool spy = facy.state_ == STATE_SPY;

		if (disconnect) {
			facy.state_ = STATE_DISCONNECTING;
			UpdateChat(nullptr, TranslateT("Disconnecting..."), true);

			if (facy.stop())
				debugLogA("***** Disconnected from stranger %s", facy.chat_id_.c_str());
			else
				debugLogA("***** Error in disconnecting from stranger %s", facy.chat_id_.c_str());
		}

		if (spy) {
			DeleteChatContact(TranslateT("Stranger 1"));
			DeleteChatContact(TranslateT("Stranger 2"));
		}
		else DeleteChatContact(TranslateT("Stranger"));

		SetTopic(); // reset topic content

		Srmm_SetStatusText(GetChatHandle(), nullptr);
	}
	else return; // disconnecting or inactive

	facy.state_ = STATE_INACTIVE;
	facy.chat_id_.clear();
}

void OmegleProto::NewChat()
{
	if (facy.state_ == STATE_WAITING) {
		UpdateChat(nullptr, TranslateT("We are already waiting for new stranger..."), false);
		return;
	}
	
	if (facy.state_ == STATE_ACTIVE || facy.state_ == STATE_SPY) {
		UpdateChat(nullptr, TranslateT("Disconnecting..."), true);

		if (facy.stop())
			debugLogA("***** Disconnected from stranger %s", facy.chat_id_.c_str());
		else
			debugLogA("***** Error in disconnecting from stranger %s", facy.chat_id_.c_str());

		if (facy.state_ == STATE_SPY) {
			DeleteChatContact(TranslateT("Stranger 1"));
			DeleteChatContact(TranslateT("Stranger 2"));
		}
		else {
			DeleteChatContact(TranslateT("Stranger"));
		}

		SetTopic(); // reset topic content

		ClearChat();

		UpdateChat(nullptr, TranslateT("Connecting..."), true);

		facy.state_ = STATE_WAITING;

		if (facy.start()) {
			UpdateChat(nullptr, TranslateT("Waiting for Stranger..."), true);
			debugLogA("***** Waiting for stranger %s", facy.chat_id_.c_str());
		}
	}
	else if (facy.state_ == STATE_DISCONNECTING) {
		//UpdateChat(NULL, TranslateT("We are disconnecting now, wait..."), false);
		return;
	}
	else {
		ClearChat();
		UpdateChat(nullptr, TranslateT("Connecting..."), true);

		facy.state_ = STATE_WAITING;

		if (facy.start()) {
			UpdateChat(nullptr, TranslateT("Waiting for Stranger..."), true);
			debugLogA("***** Waiting for stranger %s", facy.chat_id_.c_str());

			ForkThread(&OmegleProto::EventsLoop, this);
		}
	}
}

void OmegleProto::EventsLoop(void *)
{
	ScopedLock s(events_loop_lock_);

	time_t tim = ::time(0);
	debugLogA(">>>>> Entering Omegle::EventsLoop[%d]", tim);

	while (facy.events()) {
		if (facy.state_ == STATE_INACTIVE || facy.state_ == STATE_DISCONNECTING || !isOnline())
			break;
		debugLogA("***** OmegleProto::EventsLoop[%d] refreshing...", tim);
	}

	ResetEvent(events_loop_lock_);

	Netlib_CloseHandle(facy.hConnection);
	facy.hConnection = nullptr;

	Netlib_CloseHandle(facy.hEventsConnection);
	facy.hEventsConnection = nullptr;

	debugLogA("<<<<< Exiting OmegleProto::EventsLoop[%d]", tim);
}