forked from EionRobb/skype4pidgin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skype_messaging_network.c
226 lines (202 loc) · 5.09 KB
/
skype_messaging_network.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Skype plugin for libpurple/Pidgin/Adium
* Written by: Eion Robb <eionrobb@gmail.com>
*
* This plugin uses the Skype API to show your contacts in libpurple, and send/receive
* chat messages.
* It requires the Skype program to be running.
*
* 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 3 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/>.
*/
#ifndef INCLUDED_LIBSKYPE_C
# error "Don't compile this file directly. Just compile libskype.c instead."
#endif
#include <glib.h>
#include <errno.h>
#include <string.h>
//#include <sys/ioctl.h>
#ifdef _WIN32
#define fsync(fd) _commit(fd)
#endif
static guint input_timeout;
static gint source_sock = -1;
static gboolean connected = FALSE;
static gboolean in_progress = FALSE;
static gchar etb_string[2] = {23, 0};
void
read_function_cb(gpointer data, gint source, PurpleInputCondition cond)
{
int len;
gchar response[3096];
static GString *response_string = NULL;
gchar *reply;
gchar **reply_pieces;
int i;
len = read(source, response, sizeof(response)-1);
if (len < 0)
{
if (errno != EAGAIN && errno != EWOULDBLOCK)
{
skype_disconnect();
}
return;
}
if (len == sizeof(response)-1)
{
if (response_string == NULL)
response_string = g_string_new_len(response, len);
else
response_string = g_string_append_len(response_string, response, len);
}
else
{
if (response_string)
{
if (len > 0)
response_string = g_string_append_len(response_string, response, len);
reply = g_string_free(response_string, FALSE);
response_string = NULL;
}
else if (len)
reply = g_strndup(response, len);
else
return;
reply = g_strstrip(reply);
reply_pieces = g_strsplit(reply, etb_string, -1);
g_free(reply);
for (i=0; reply_pieces[i+1]; i++)
{
reply = reply_pieces[i];
if (g_str_equal(reply, "LOGIN"))
{
connected = TRUE;
in_progress = FALSE;
skype_debug_info("skype", "Received: LOGIN\n");
} else {
g_thread_create((GThreadFunc)skype_message_received, g_strdup(reply), FALSE, NULL);
}
}
//check that we received part of a message
if (strlen(reply_pieces[i]))
{
skype_debug_info("skype", "Last piece: '%s'\n", reply_pieces[i]);
response_string = g_string_new(reply_pieces[i]);
}
g_strfreev(reply_pieces);
}
}
gpointer
skype_read_thread(gpointer data)
{
while(connected || in_progress)
{
read_function_cb(data, source_sock, PURPLE_INPUT_READ);
g_thread_yield();
sleep(1);
}
return data;
}
void
connect_function(gpointer data, gint source, const gchar *error_message)
{
gchar *loginmsg;
PurpleAccount *acct = skype_get_account(NULL);
if (error_message)
{
in_progress = FALSE;
g_thread_create((GThreadFunc)skype_message_received, g_strdup("CONNSTATUS LOGGEDOUT"), FALSE, NULL);
return;
}
source_sock = source;
loginmsg = g_strdup_printf("LOGIN %s %s", acct->username, acct->password);
send_message(loginmsg);
skype_debug_info("skype", "Sending: 'LOGIN {username} {password}'\n");
g_free(loginmsg);
g_thread_create((GThreadFunc)skype_read_thread, NULL, FALSE, NULL);
}
static gboolean
skype_connect()
{
return connected;
}
static void
skype_disconnect()
{
if (!connected)
return;
send_message("QUIT");
connected = FALSE;
close(source_sock);
source_sock = -1;
purple_input_remove(input_timeout);
in_progress = FALSE;
}
static void
send_message(const char* message)
{
int message_num;
char *error_return;
char *temp;
int msglen = -1;
int len = 0;
if (message)
{
msglen = strlen(message) + 1;
temp = g_strdup_printf("%s%c", message, 23);
len = write(source_sock, temp, msglen);
fsync(source_sock);
g_free(temp);
}
if (len != msglen)
{
//There was an error
if (message[0] == '#')
{
//And we're expecting a response
sscanf(message, "#%d ", &message_num);
error_return = g_strdup_printf("#%d ERROR NETWORK", message_num);
g_thread_create((GThreadFunc)skype_message_received, (void *)error_return, FALSE, NULL);
}
}
}
static void
hide_skype()
{
//don't need to since SILENT_MODE ON works
return;
}
gboolean
connection_timeout(gpointer data)
{
in_progress = FALSE;
return FALSE;
}
static gboolean
exec_skype()
{
if (!connected && !in_progress)
{
in_progress = TRUE;
PurpleAccount *acct = skype_get_account(NULL);
purple_proxy_connect(acct->gc, acct, purple_account_get_string(acct, "host", "skype.robbmob.com"), purple_account_get_int(acct, "port", 5000), connect_function, acct);
g_thread_create((GThreadFunc)skype_read_thread, acct, FALSE, NULL);
purple_timeout_add_seconds(10, connection_timeout, acct);
}
return TRUE;
}
static gboolean
is_skype_running()
{
return connected || in_progress;
}