[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> |
---|
| 9 | #include <bitlbee.h> |
---|
| 10 | |
---|
| 11 | #define SKYPE_PORT_DEFAULT "2727" |
---|
| 12 | |
---|
| 13 | struct skype_data |
---|
| 14 | { |
---|
| 15 | struct im_connection *ic; |
---|
| 16 | int fd; |
---|
| 17 | char *txq; |
---|
| 18 | int tx_len; |
---|
| 19 | int r_inpa, w_inpa; |
---|
| 20 | }; |
---|
| 21 | |
---|
| 22 | static void skype_init( account_t *acc ) |
---|
| 23 | { |
---|
| 24 | set_t *s; |
---|
| 25 | |
---|
| 26 | s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc ); |
---|
| 27 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 28 | |
---|
| 29 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 30 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | int skype_write( struct im_connection *ic, char *buf, int len ) |
---|
| 34 | { |
---|
| 35 | struct skype_data *sd = ic->proto_data; |
---|
| 36 | |
---|
[9fd4241] | 37 | printf("write(): %s", buf); |
---|
| 38 | write( sd->fd, buf, len ); |
---|
[f06e3ac] | 39 | |
---|
[9fd4241] | 40 | // TODO: error handling |
---|
[f06e3ac] | 41 | |
---|
[9fd4241] | 42 | return TRUE; |
---|
[f06e3ac] | 43 | } |
---|
| 44 | |
---|
[1323e36] | 45 | static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
| 46 | { |
---|
| 47 | struct im_connection *ic = data; |
---|
| 48 | struct skype_data *sd = ic->proto_data; |
---|
| 49 | char buf[1024]; |
---|
| 50 | int st; |
---|
[9fd4241] | 51 | char **lines, **lineptr, *line, *ptr; |
---|
[1323e36] | 52 | |
---|
| 53 | if( sd->fd == -1 ) |
---|
| 54 | return FALSE; |
---|
| 55 | st = read( sd->fd, buf, sizeof( buf ) ); |
---|
| 56 | if( st > 0 ) |
---|
| 57 | { |
---|
| 58 | buf[st] = '\0'; |
---|
| 59 | printf("read(): '%s'\n", buf); |
---|
[9fd4241] | 60 | lines = g_strsplit(buf, "\n", 0); |
---|
| 61 | lineptr = lines; |
---|
| 62 | while((line = *lineptr)) |
---|
| 63 | { |
---|
| 64 | if(!strlen(line)) |
---|
| 65 | break; |
---|
| 66 | printf("skype_read_callback() new line: '%s'\n", line); |
---|
| 67 | if(!strncmp(line, "USERS ", 6)) |
---|
| 68 | { |
---|
| 69 | char **i; |
---|
| 70 | char **nicks; |
---|
| 71 | |
---|
| 72 | nicks = g_strsplit(line + 6, ", ", 0); |
---|
| 73 | i = nicks; |
---|
| 74 | while(*i) |
---|
| 75 | { |
---|
| 76 | g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i); |
---|
| 77 | skype_write( ic, buf, strlen( buf ) ); |
---|
| 78 | i++; |
---|
| 79 | } |
---|
| 80 | g_strfreev(nicks); |
---|
| 81 | } |
---|
| 82 | else if(!strncmp(line, "USER ", 5)) |
---|
| 83 | { |
---|
| 84 | // TODO we should add the buddy even if the status is offline |
---|
| 85 | if((ptr = strrchr(line, ' ')) && strcmp(++ptr, "OFFLINE") != 0) |
---|
| 86 | { |
---|
| 87 | char *user = strchr(line, ' '); |
---|
| 88 | ptr = strchr(++user, ' '); |
---|
| 89 | *ptr = '\0'; |
---|
| 90 | ptr = g_strdup_printf("%s@skype.com", user); |
---|
| 91 | imcb_add_buddy(ic, ptr, NULL); |
---|
| 92 | imcb_buddy_status(ic, ptr, OPT_LOGGED_IN, NULL, NULL); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | lineptr++; |
---|
| 96 | } |
---|
| 97 | g_strfreev(lines); |
---|
[1323e36] | 98 | } |
---|
| 99 | else if( st == 0 || ( st < 0 && !sockerr_again() ) ) |
---|
| 100 | { |
---|
| 101 | closesocket( sd->fd ); |
---|
| 102 | sd->fd = -1; |
---|
| 103 | |
---|
| 104 | imcb_error( ic, "Error while reading from server" ); |
---|
| 105 | imc_logout( ic, TRUE ); |
---|
| 106 | return FALSE; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* EAGAIN/etc or a successful read. */ |
---|
| 110 | return TRUE; |
---|
| 111 | } |
---|
| 112 | |
---|
[f06e3ac] | 113 | gboolean skype_start_stream( struct im_connection *ic ) |
---|
| 114 | { |
---|
[1323e36] | 115 | struct skype_data *sd = ic->proto_data; |
---|
[f06e3ac] | 116 | char *buf; |
---|
| 117 | int st; |
---|
| 118 | |
---|
[1323e36] | 119 | if( sd->r_inpa <= 0 ) |
---|
| 120 | sd->r_inpa = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic ); |
---|
| 121 | |
---|
[9fd4241] | 122 | // download buddies |
---|
| 123 | buf = g_strdup_printf("SEARCH FRIENDS\n"); |
---|
[f06e3ac] | 124 | st = skype_write( ic, buf, strlen( buf ) ); |
---|
| 125 | g_free(buf); |
---|
| 126 | return st; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | gboolean skype_connected( gpointer data, gint source, b_input_condition cond ) |
---|
| 130 | { |
---|
| 131 | struct im_connection *ic = data; |
---|
[1323e36] | 132 | struct skype_data *sd = ic->proto_data; |
---|
[f06e3ac] | 133 | |
---|
| 134 | imcb_connected(ic); |
---|
[1323e36] | 135 | if( sd->fd < 0 ) |
---|
| 136 | { |
---|
| 137 | imcb_error( ic, "Could not connect to server" ); |
---|
| 138 | imc_logout( ic, TRUE ); |
---|
[9fd4241] | 139 | return FALSE; |
---|
[1323e36] | 140 | } |
---|
[f06e3ac] | 141 | return skype_start_stream(ic); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | static void skype_login( account_t *acc ) |
---|
| 145 | { |
---|
| 146 | struct im_connection *ic = imcb_new( acc ); |
---|
| 147 | struct skype_data *sd = g_new0( struct skype_data, 1 ); |
---|
| 148 | |
---|
| 149 | ic->proto_data = sd; |
---|
| 150 | |
---|
| 151 | imcb_log( ic, "Connecting" ); |
---|
| 152 | printf("%s:%d\n", acc->server, set_getint( &acc->set, "port")); |
---|
| 153 | sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic ); |
---|
| 154 | printf("sd->fd: %d\n", sd->fd); |
---|
| 155 | |
---|
| 156 | sd->ic = ic; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | static void skype_logout( struct im_connection *ic ) |
---|
| 160 | { |
---|
| 161 | struct skype_data *sd = ic->proto_data; |
---|
| 162 | g_free(sd); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | static void skype_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
| 166 | { |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | static GList *skype_away_states( struct im_connection *ic ) |
---|
| 170 | { |
---|
| 171 | static GList *l = NULL; |
---|
| 172 | |
---|
| 173 | l = g_list_append( l, (void*)"Online" ); |
---|
| 174 | return l; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | static void skype_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 178 | { |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | static void skype_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 182 | { |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void init_plugin(void) |
---|
| 186 | { |
---|
| 187 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
| 188 | |
---|
| 189 | ret->name = "skype"; |
---|
| 190 | ret->login = skype_login; |
---|
| 191 | ret->init = skype_init; |
---|
| 192 | ret->logout = skype_logout; |
---|
| 193 | ret->away_states = skype_away_states; |
---|
| 194 | ret->add_buddy = skype_add_buddy; |
---|
| 195 | ret->remove_buddy = skype_remove_buddy; |
---|
| 196 | ret->away_states = skype_away_states; |
---|
| 197 | ret->set_away = skype_set_away; |
---|
| 198 | ret->handle_cmp = g_strcasecmp; |
---|
| 199 | register_protocol( ret ); |
---|
| 200 | } |
---|