aboutsummaryrefslogtreecommitdiffstats
path: root/skype/skype.c
blob: df8d767983994cc946223ebb5d3dc6677eb0b313 (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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* 
 * This is the most simple possible BitlBee plugin. To use, compile it as 
 * a shared library and place it in the plugin directory: 
 *
 * gcc -o example.so -shared example.c `pkg-config --cflags bitlbee`
 * cp example.so /usr/local/lib/bitlbee
 */
#include <stdio.h>
#include <poll.h>
#include <bitlbee.h>

#define SKYPE_PORT_DEFAULT "2727"

struct skype_data
{
	struct im_connection *ic;
	char *username;
	int fd;
	char *txq;
	int tx_len;
	int r_inpa, w_inpa;
	// when we receive a new message id, we query the handle, then the body
	// store the handle here
	// TODO: it would be nicer to use a hashmap for this or something
	char *handle;
};

struct skype_away_state
{
	char *code;
	char *full_name;
};

const struct skype_away_state skype_away_state_list[] =
{
	{ "ONLINE",  "Online" },
	{ "SKYPEME",  "Skype Me" },
	{ "AWAY",   "Away" },
	{ "NA",    "Not available" },
	{ "DND",      "Do Not Disturb" },
	{ "INVISIBLE",      "Invisible" },
	{ "OFFLINE",      "Offline" },
	{ NULL, NULL}
};

static void skype_init( account_t *acc )
{
	set_t *s;

	s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc );
	s->flags |= ACC_SET_OFFLINE_ONLY;

	s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
	s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
}

int skype_write( struct im_connection *ic, char *buf, int len )
{
	struct skype_data *sd = ic->proto_data;
	struct pollfd pfd[1];

	pfd[0].fd = sd->fd;
	pfd[0].events = POLLOUT;

	poll(pfd, 1, 1000);
	if(pfd[0].revents & POLLHUP)
	{
		imcb_error( ic, "Could not connect to server" );
		imc_logout( ic, TRUE );
		return FALSE;
	}
	printf("write(): %s", buf);
	write( sd->fd, buf, len );

	return TRUE;
}

static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond )
{
	struct im_connection *ic = data;
	struct skype_data *sd = ic->proto_data;
	char buf[1024];
	int st;
	char **lines, **lineptr, *line, *ptr;

	if( !sd || sd->fd == -1 )
		return FALSE;
	st = read( sd->fd, buf, sizeof( buf ) );
	if( st > 0 )
	{
		buf[st] = '\0';
		printf("read(): '%s'\n", buf);
		lines = g_strsplit(buf, "\n", 0);
		lineptr = lines;
		while((line = *lineptr))
		{
			if(!strlen(line))
				break;
			printf("skype_read_callback() new line: '%s'\n", line);
			if(!strncmp(line, "USERS ", 6))
			{
				char **i;
				char **nicks;

				nicks = g_strsplit(line + 6, ", ", 0);
				i = nicks;
				while(*i)
				{
					g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i);
					skype_write( ic, buf, strlen( buf ) );
					i++;
				}
				g_strfreev(nicks);
			}
			else if(!strncmp(line, "USER ", 5))
			{
				int flags = 0;
				char *status = strrchr(line, ' ');
				char *user = strchr(line, ' ');
				status++;
				ptr = strchr(++user, ' ');
				*ptr = '\0';
				ptr++;
				if(!strncmp(ptr, "ONLINESTATUS ", 13) && strcmp(user, sd->username) != 0 && strcmp(user, "echo123") != 0)
				{
					ptr = g_strdup_printf("%s@skype.com", user);
					imcb_add_buddy(ic, ptr, NULL);
					if(strcmp(status, "OFFLINE") != 0)
						flags |= OPT_LOGGED_IN;
					if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0)
						flags |= OPT_AWAY;
					imcb_buddy_status(ic, ptr, flags, NULL, NULL);
					g_free(ptr);
				}
			}
			else if(!strncmp(line, "CHATMESSAGE ", 12))
			{
				char *id = strchr(line, ' ');
				if(++id)
				{
					char *info = strchr(id, ' ');
					*info = '\0';
					info++;
					if(!strcmp(info, "STATUS RECEIVED"))
					{
						// new message, request its body
						printf("new received message  #%s\n", id);
						g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
						skype_write( ic, buf, strlen( buf ) );
						g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
						skype_write( ic, buf, strlen( buf ) );
						g_snprintf(buf, 1024, "SET CHATMESSAGE %s SEEN\n", id);
						skype_write( ic, buf, strlen( buf ) );
					}
					else if(!strncmp(info, "FROM_HANDLE ", 12))
					{
						info += 12;
						// new handle
						sd->handle = g_strdup_printf("%s@skype.com", info);
						printf("new handle: '%s'\n", info);
					}
					else if(!strncmp(info, "BODY ", 5))
					{
						info += 5;
						// new body
						printf("<%s> %s\n", sd->handle, info);
						if(sd->handle)
							imcb_buddy_msg(ic, sd->handle, info, 0, 0);
						g_free(sd->handle);
						sd->handle = NULL;
					}
				}
			}
			lineptr++;
		}
		g_strfreev(lines);
	}
	else if( st == 0 || ( st < 0 && !sockerr_again() ) )
	{
		closesocket( sd->fd );
		sd->fd = -1;

		imcb_error( ic, "Error while reading from server" );
		imc_logout( ic, TRUE );
		return FALSE;
	}

	/* EAGAIN/etc or a successful read. */
	return TRUE;
}

gboolean skype_start_stream( struct im_connection *ic )
{
	struct skype_data *sd = ic->proto_data;
	char *buf;
	int st;

	if(!sd)
		return FALSE;

	if( sd->r_inpa <= 0 )
		sd->r_inpa = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );

	// download buddies
	buf = g_strdup_printf("SEARCH FRIENDS\n");
	st = skype_write( ic, buf, strlen( buf ) );
	g_free(buf);
	buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
	skype_write( ic, buf, strlen( buf ) );
	g_free(buf);
	return st;
}

gboolean skype_connected( gpointer data, gint source, b_input_condition cond )
{
	struct im_connection *ic = data;
	imcb_connected(ic);
	return skype_start_stream(ic);
}

static void skype_login( account_t *acc )
{
	struct im_connection *ic = imcb_new( acc );
	struct skype_data *sd = g_new0( struct skype_data, 1 );

	ic->proto_data = sd;

	imcb_log( ic, "Connecting" );
	printf("%s:%d\n", acc->server, set_getint( &acc->set, "port"));
	sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic );
	printf("sd->fd: %d\n", sd->fd);
	/*imcb_add_buddy(ic, "test@skype.com", NULL);
	imcb_buddy_status(ic, "test@skype.com", OPT_LOGGED_IN, NULL, NULL);
	imcb_buddy_msg(ic, "test@skype.com", "test from skype plugin", 0, 0);*/
	sd->username = g_strdup( acc->user );

	sd->ic = ic;
}

static void skype_logout( struct im_connection *ic )
{
	struct skype_data *sd = ic->proto_data;
	char *buf;

	buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
	skype_write( ic, buf, strlen( buf ) );
	g_free(buf);

	g_free(sd->username);
	g_free(sd);
	ic->proto_data = NULL;
}

static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
{
	char *buf, *ptr, *nick;
	int st;

	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if(ptr)
		*ptr = '\0';

	buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
	g_free(nick);
	st = skype_write( ic, buf, strlen( buf ) );
	g_free(buf);

	return st;
}

const struct skype_away_state *skype_away_state_by_name( char *name )
{
	int i;

	for( i = 0; skype_away_state_list[i].full_name; i ++ )
		if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 )
			return( skype_away_state_list + i );

	return NULL;
}

static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
{
	const struct skype_away_state *state;
	char *buf;

	if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 )
		state = skype_away_state_by_name( "Away" );
	else
		state = skype_away_state_by_name( state_txt );
	printf("would set to: '%s'\n", state->code);
	buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
	skype_write( ic, buf, strlen( buf ) );
	g_free(buf);
}

static GList *skype_away_states( struct im_connection *ic )
{
	GList *l = NULL;
	int i;
	
	for( i = 0; skype_away_state_list[i].full_name; i ++ )
		l = g_list_append( l, (void*) skype_away_state_list[i].full_name );
	
	return l;
}

static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
{
	char *buf, *nick, *ptr;

	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if(ptr)
		*ptr = '\0';
	buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
	skype_write( ic, buf, strlen( buf ) );
	printf("add '%s'\n", nick);
	g_free(nick);
}

static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
{
	char *buf, *nick, *ptr;

	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if(ptr)
		*ptr = '\0';
	buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
	skype_write( ic, buf, strlen( buf ) );
	printf("remove '%s'\n", nick);
	g_free(nick);
}

void init_plugin(void)
{
	struct prpl *ret = g_new0( struct prpl, 1 );

	ret->name = "skype";
	ret->login = skype_login;
	ret->init = skype_init;
	ret->logout = skype_logout;
	ret->buddy_msg = skype_buddy_msg;
	ret->set_away = skype_set_away;
	ret->away_states = skype_away_states;
	ret->add_buddy = skype_add_buddy;
	ret->remove_buddy = skype_remove_buddy;
	ret->handle_cmp = g_strcasecmp;
	register_protocol( ret );
}