[f06e3ac] | 1 | /* |
---|
| 2 | * This is the most simple possible BitlBee plugin. To use, compile it as |
---|
| 3 | * a shared library and place it in the plugin directory: |
---|
| 4 | * |
---|
| 5 | * gcc -o example.so -shared example.c `pkg-config --cflags bitlbee` |
---|
| 6 | * cp example.so /usr/local/lib/bitlbee |
---|
| 7 | */ |
---|
| 8 | #include <stdio.h> |
---|
[ed2e37f] | 9 | #include <poll.h> |
---|
[f06e3ac] | 10 | #include <bitlbee.h> |
---|
| 11 | |
---|
| 12 | #define SKYPE_PORT_DEFAULT "2727" |
---|
| 13 | |
---|
| 14 | struct skype_data |
---|
| 15 | { |
---|
| 16 | struct im_connection *ic; |
---|
[a3d6427] | 17 | char *username; |
---|
[f06e3ac] | 18 | int fd; |
---|
| 19 | char *txq; |
---|
| 20 | int tx_len; |
---|
| 21 | int r_inpa, w_inpa; |
---|
[77c1abe] | 22 | // when we receive a new message id, we query the handle, then the body |
---|
| 23 | // store the handle here |
---|
| 24 | // TODO: it would be nicer to use a hashmap for this or something |
---|
| 25 | char *handle; |
---|
[f06e3ac] | 26 | }; |
---|
| 27 | |
---|
[adce2de] | 28 | struct skype_away_state |
---|
| 29 | { |
---|
| 30 | char *code; |
---|
| 31 | char *full_name; |
---|
| 32 | }; |
---|
| 33 | |
---|
| 34 | const struct skype_away_state skype_away_state_list[] = |
---|
| 35 | { |
---|
| 36 | { "ONLINE", "Online" }, |
---|
| 37 | { "SKYPEME", "Skype Me" }, |
---|
| 38 | { "AWAY", "Away" }, |
---|
| 39 | { "NA", "Not available" }, |
---|
| 40 | { "DND", "Do Not Disturb" }, |
---|
| 41 | { "INVISIBLE", "Invisible" }, |
---|
[23411c6] | 42 | { "OFFLINE", "Offline" }, |
---|
| 43 | { NULL, NULL} |
---|
[adce2de] | 44 | }; |
---|
| 45 | |
---|
[f06e3ac] | 46 | static void skype_init( account_t *acc ) |
---|
| 47 | { |
---|
| 48 | set_t *s; |
---|
| 49 | |
---|
| 50 | s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc ); |
---|
| 51 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 52 | |
---|
| 53 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 54 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | int skype_write( struct im_connection *ic, char *buf, int len ) |
---|
| 58 | { |
---|
| 59 | struct skype_data *sd = ic->proto_data; |
---|
| 60 | |
---|
[9fd4241] | 61 | printf("write(): %s", buf); |
---|
| 62 | write( sd->fd, buf, len ); |
---|
[f06e3ac] | 63 | |
---|
[9fd4241] | 64 | return TRUE; |
---|
[f06e3ac] | 65 | } |
---|
| 66 | |
---|
[1323e36] | 67 | static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
| 68 | { |
---|
| 69 | struct im_connection *ic = data; |
---|
| 70 | struct skype_data *sd = ic->proto_data; |
---|
| 71 | char buf[1024]; |
---|
| 72 | int st; |
---|
[9fd4241] | 73 | char **lines, **lineptr, *line, *ptr; |
---|
[1323e36] | 74 | |
---|
| 75 | if( sd->fd == -1 ) |
---|
| 76 | return FALSE; |
---|
| 77 | st = read( sd->fd, buf, sizeof( buf ) ); |
---|
| 78 | if( st > 0 ) |
---|
| 79 | { |
---|
| 80 | buf[st] = '\0'; |
---|
| 81 | printf("read(): '%s'\n", buf); |
---|
[9fd4241] | 82 | lines = g_strsplit(buf, "\n", 0); |
---|
| 83 | lineptr = lines; |
---|
| 84 | while((line = *lineptr)) |
---|
| 85 | { |
---|
| 86 | if(!strlen(line)) |
---|
| 87 | break; |
---|
| 88 | printf("skype_read_callback() new line: '%s'\n", line); |
---|
| 89 | if(!strncmp(line, "USERS ", 6)) |
---|
| 90 | { |
---|
| 91 | char **i; |
---|
| 92 | char **nicks; |
---|
| 93 | |
---|
| 94 | nicks = g_strsplit(line + 6, ", ", 0); |
---|
| 95 | i = nicks; |
---|
| 96 | while(*i) |
---|
| 97 | { |
---|
| 98 | g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i); |
---|
| 99 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 100 | i++; |
---|
| 101 | } |
---|
| 102 | g_strfreev(nicks); |
---|
| 103 | } |
---|
| 104 | else if(!strncmp(line, "USER ", 5)) |
---|
| 105 | { |
---|
[adce2de] | 106 | int flags = 0; |
---|
[be975f8] | 107 | char *status = strrchr(line, ' '); |
---|
| 108 | char *user = strchr(line, ' '); |
---|
[adce2de] | 109 | status++; |
---|
[be975f8] | 110 | ptr = strchr(++user, ' '); |
---|
| 111 | *ptr = '\0'; |
---|
[6627d92] | 112 | ptr++; |
---|
| 113 | if(!strncmp(ptr, "ONLINESTATUS ", 13) && strcmp(user, sd->username) != 0 && strcmp(user, "echo123") != 0) |
---|
[a3d6427] | 114 | { |
---|
| 115 | ptr = g_strdup_printf("%s@skype.com", user); |
---|
| 116 | imcb_add_buddy(ic, ptr, NULL); |
---|
| 117 | if(strcmp(status, "OFFLINE") != 0) |
---|
| 118 | flags |= OPT_LOGGED_IN; |
---|
| 119 | if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0) |
---|
| 120 | flags |= OPT_AWAY; |
---|
| 121 | imcb_buddy_status(ic, ptr, flags, NULL, NULL); |
---|
| 122 | g_free(ptr); |
---|
| 123 | } |
---|
[9fd4241] | 124 | } |
---|
[77c1abe] | 125 | else if(!strncmp(line, "CHATMESSAGE ", 12)) |
---|
| 126 | { |
---|
| 127 | char *id = strchr(line, ' '); |
---|
| 128 | if(++id) |
---|
| 129 | { |
---|
| 130 | char *info = strchr(id, ' '); |
---|
| 131 | *info = '\0'; |
---|
| 132 | info++; |
---|
| 133 | if(!strcmp(info, "STATUS RECEIVED")) |
---|
| 134 | { |
---|
| 135 | // new message, request its body |
---|
| 136 | printf("new received message #%s\n", id); |
---|
| 137 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id); |
---|
| 138 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 139 | g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id); |
---|
| 140 | skype_write( ic, buf, strlen( buf ) ); |
---|
[62bb4e4] | 141 | g_snprintf(buf, 1024, "SET CHATMESSAGE %s SEEN\n", id); |
---|
| 142 | skype_write( ic, buf, strlen( buf ) ); |
---|
[77c1abe] | 143 | } |
---|
| 144 | else if(!strncmp(info, "FROM_HANDLE ", 12)) |
---|
| 145 | { |
---|
| 146 | info += 12; |
---|
| 147 | // new handle |
---|
| 148 | sd->handle = g_strdup_printf("%s@skype.com", info); |
---|
| 149 | printf("new handle: '%s'\n", info); |
---|
| 150 | } |
---|
| 151 | else if(!strncmp(info, "BODY ", 5)) |
---|
| 152 | { |
---|
| 153 | info += 5; |
---|
| 154 | // new body |
---|
| 155 | printf("<%s> %s\n", sd->handle, info); |
---|
[ed2e37f] | 156 | if(sd->handle) |
---|
| 157 | imcb_buddy_msg(ic, sd->handle, info, 0, 0); |
---|
[77c1abe] | 158 | g_free(sd->handle); |
---|
| 159 | sd->handle = NULL; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
[9fd4241] | 163 | lineptr++; |
---|
| 164 | } |
---|
| 165 | g_strfreev(lines); |
---|
[1323e36] | 166 | } |
---|
| 167 | else if( st == 0 || ( st < 0 && !sockerr_again() ) ) |
---|
| 168 | { |
---|
| 169 | closesocket( sd->fd ); |
---|
| 170 | sd->fd = -1; |
---|
| 171 | |
---|
| 172 | imcb_error( ic, "Error while reading from server" ); |
---|
| 173 | imc_logout( ic, TRUE ); |
---|
| 174 | return FALSE; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /* EAGAIN/etc or a successful read. */ |
---|
| 178 | return TRUE; |
---|
| 179 | } |
---|
| 180 | |
---|
[f06e3ac] | 181 | gboolean skype_start_stream( struct im_connection *ic ) |
---|
| 182 | { |
---|
[1323e36] | 183 | struct skype_data *sd = ic->proto_data; |
---|
[f06e3ac] | 184 | char *buf; |
---|
| 185 | int st; |
---|
| 186 | |
---|
[1323e36] | 187 | if( sd->r_inpa <= 0 ) |
---|
| 188 | sd->r_inpa = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic ); |
---|
| 189 | |
---|
[9fd4241] | 190 | // download buddies |
---|
| 191 | buf = g_strdup_printf("SEARCH FRIENDS\n"); |
---|
[f06e3ac] | 192 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
| 193 | g_free(buf); |
---|
| 194 | return st; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | gboolean skype_connected( gpointer data, gint source, b_input_condition cond ) |
---|
| 198 | { |
---|
| 199 | struct im_connection *ic = data; |
---|
[1323e36] | 200 | struct skype_data *sd = ic->proto_data; |
---|
[ed2e37f] | 201 | struct pollfd pfd[1]; |
---|
[f06e3ac] | 202 | |
---|
[ed2e37f] | 203 | pfd[0].fd = sd->fd; |
---|
| 204 | pfd[0].events = POLLOUT; |
---|
| 205 | |
---|
| 206 | poll(pfd, 1, 1000); |
---|
| 207 | if(pfd[0].revents & POLLHUP) |
---|
[1323e36] | 208 | { |
---|
| 209 | imcb_error( ic, "Could not connect to server" ); |
---|
| 210 | imc_logout( ic, TRUE ); |
---|
[9fd4241] | 211 | return FALSE; |
---|
[1323e36] | 212 | } |
---|
[ed2e37f] | 213 | imcb_connected(ic); |
---|
[f06e3ac] | 214 | return skype_start_stream(ic); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | static void skype_login( account_t *acc ) |
---|
| 218 | { |
---|
| 219 | struct im_connection *ic = imcb_new( acc ); |
---|
| 220 | struct skype_data *sd = g_new0( struct skype_data, 1 ); |
---|
| 221 | |
---|
| 222 | ic->proto_data = sd; |
---|
| 223 | |
---|
| 224 | imcb_log( ic, "Connecting" ); |
---|
| 225 | printf("%s:%d\n", acc->server, set_getint( &acc->set, "port")); |
---|
| 226 | sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic ); |
---|
| 227 | printf("sd->fd: %d\n", sd->fd); |
---|
[77c1abe] | 228 | /*imcb_add_buddy(ic, "test@skype.com", NULL); |
---|
| 229 | imcb_buddy_status(ic, "test@skype.com", OPT_LOGGED_IN, NULL, NULL); |
---|
| 230 | imcb_buddy_msg(ic, "test@skype.com", "test from skype plugin", 0, 0);*/ |
---|
[a3d6427] | 231 | sd->username = g_strdup( acc->user ); |
---|
[f06e3ac] | 232 | |
---|
| 233 | sd->ic = ic; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | static void skype_logout( struct im_connection *ic ) |
---|
| 237 | { |
---|
| 238 | struct skype_data *sd = ic->proto_data; |
---|
[a3d6427] | 239 | g_free(sd->username); |
---|
[f06e3ac] | 240 | g_free(sd); |
---|
| 241 | } |
---|
| 242 | |
---|
[93ece66] | 243 | static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
| 244 | { |
---|
[77c1abe] | 245 | char *buf, *ptr, *nick; |
---|
[93ece66] | 246 | int st; |
---|
| 247 | |
---|
[cbec0d6] | 248 | nick = g_strdup(who); |
---|
[77c1abe] | 249 | ptr = strchr(nick, '@'); |
---|
[0bb1b7f] | 250 | if(ptr) |
---|
| 251 | *ptr = '\0'; |
---|
[93ece66] | 252 | |
---|
[77c1abe] | 253 | buf = g_strdup_printf("MESSAGE %s %s\n", nick, message); |
---|
| 254 | g_free(nick); |
---|
[93ece66] | 255 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
| 256 | g_free(buf); |
---|
| 257 | |
---|
| 258 | return st; |
---|
| 259 | } |
---|
| 260 | |
---|
[23411c6] | 261 | const struct skype_away_state *skype_away_state_by_name( char *name ) |
---|
| 262 | { |
---|
| 263 | int i; |
---|
| 264 | |
---|
| 265 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
| 266 | if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 ) |
---|
| 267 | return( skype_away_state_list + i ); |
---|
| 268 | |
---|
| 269 | return NULL; |
---|
| 270 | } |
---|
| 271 | |
---|
[f06e3ac] | 272 | static void skype_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
| 273 | { |
---|
[23411c6] | 274 | const struct skype_away_state *state; |
---|
| 275 | char *buf; |
---|
| 276 | |
---|
| 277 | if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 ) |
---|
| 278 | state = skype_away_state_by_name( "Away" ); |
---|
| 279 | else |
---|
| 280 | state = skype_away_state_by_name( state_txt ); |
---|
| 281 | printf("would set to: '%s'\n", state->code); |
---|
| 282 | buf = g_strdup_printf("SET USERSTATUS %s\n", state->code); |
---|
| 283 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 284 | g_free(buf); |
---|
[f06e3ac] | 285 | } |
---|
| 286 | |
---|
| 287 | static GList *skype_away_states( struct im_connection *ic ) |
---|
| 288 | { |
---|
[23411c6] | 289 | GList *l = NULL; |
---|
[adce2de] | 290 | int i; |
---|
| 291 | |
---|
[23411c6] | 292 | for( i = 0; skype_away_state_list[i].full_name; i ++ ) |
---|
| 293 | l = g_list_append( l, (void*) skype_away_state_list[i].full_name ); |
---|
[adce2de] | 294 | |
---|
[f06e3ac] | 295 | return l; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | static void skype_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 299 | { |
---|
[6627d92] | 300 | char *buf, *nick, *ptr; |
---|
| 301 | |
---|
[cbec0d6] | 302 | nick = g_strdup(who); |
---|
[6627d92] | 303 | ptr = strchr(nick, '@'); |
---|
| 304 | if(ptr) |
---|
| 305 | *ptr = '\0'; |
---|
| 306 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick); |
---|
| 307 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 308 | printf("add '%s'\n", nick); |
---|
| 309 | g_free(nick); |
---|
[f06e3ac] | 310 | } |
---|
| 311 | |
---|
| 312 | static void skype_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 313 | { |
---|
[6627d92] | 314 | char *buf, *nick, *ptr; |
---|
| 315 | |
---|
[cbec0d6] | 316 | nick = g_strdup(who); |
---|
[6627d92] | 317 | ptr = strchr(nick, '@'); |
---|
| 318 | if(ptr) |
---|
| 319 | *ptr = '\0'; |
---|
| 320 | buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick); |
---|
| 321 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 322 | printf("remove '%s'\n", nick); |
---|
| 323 | g_free(nick); |
---|
[f06e3ac] | 324 | } |
---|
| 325 | |
---|
| 326 | void init_plugin(void) |
---|
| 327 | { |
---|
| 328 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
| 329 | |
---|
| 330 | ret->name = "skype"; |
---|
| 331 | ret->login = skype_login; |
---|
| 332 | ret->init = skype_init; |
---|
| 333 | ret->logout = skype_logout; |
---|
[93ece66] | 334 | ret->buddy_msg = skype_buddy_msg; |
---|
[d5a514a] | 335 | ret->set_away = skype_set_away; |
---|
[f06e3ac] | 336 | ret->away_states = skype_away_states; |
---|
| 337 | ret->add_buddy = skype_add_buddy; |
---|
| 338 | ret->remove_buddy = skype_remove_buddy; |
---|
| 339 | ret->handle_cmp = g_strcasecmp; |
---|
| 340 | register_protocol( ret ); |
---|
| 341 | } |
---|