aboutsummaryrefslogtreecommitdiffstats
path: root/skype/skype.c
blob: 978ceec829b795eddf0f46e79dae2a159d189e7f (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
/* 
 * 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 <bitlbee.h>

#define SKYPE_PORT_DEFAULT "2727"

struct skype_data
{
	struct im_connection *ic;
	int fd;
	char *txq;
	int tx_len;
	int r_inpa, w_inpa;
};

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;

	printf("write(): %s", buf);
	write( sd->fd, buf, len );

	// TODO: error handling

	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->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))
			{
				char *status = strrchr(line, ' ');
				char *user = strchr(line, ' ');
				ptr = strchr(++user, ' ');
				*ptr = '\0';
				ptr = g_strdup_printf("%s@skype.com", user);
				imcb_add_buddy(ic, ptr, NULL);
				// TODO online can be away
				if(strcmp(++status, "OFFLINE") != 0)
					imcb_buddy_status(ic, ptr, OPT_LOGGED_IN, NULL, NULL);
				g_free(ptr);
			}
			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->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);
	return st;
}

gboolean skype_connected( gpointer data, gint source, b_input_condition cond )
{
	struct im_connection *ic = data;
	struct skype_data *sd = ic->proto_data;

	imcb_connected(ic);
	if( sd->fd < 0 )
	{
		imcb_error( ic, "Could not connect to server" );
		imc_logout( ic, TRUE );
		return FALSE;
	}
	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);

	sd->ic = ic;
}

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

static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
{
}

static GList *skype_away_states( struct im_connection *ic )
{
	static GList *l = NULL;

	l = g_list_append( l, (void*)"Online" );
	return l;
}

static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
{
}

static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
{
}

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->away_states = skype_away_states;
	ret->add_buddy = skype_add_buddy;
	ret->remove_buddy = skype_remove_buddy;
	ret->away_states = skype_away_states;
	ret->set_away = skype_set_away;
	ret->handle_cmp = g_strcasecmp;
	register_protocol( ret );
}