Fri, 27 Apr 2001 22:21:53 +0000
[gaim-migrate @ 1773]
la la la
| 1546 | 1 | /* |
| 2 | * libyay | |
| 3 | * | |
| 4 | * Copyright (C) 2001 Eric Warmenhoven <warmenhoven@yahoo.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | ||
| 22 | #include "internal.h" | |
| 23 | ||
| 24 | void (*yahoo_socket_notify)(struct yahoo_session *, int, int, gboolean) = NULL; | |
| 25 | void (*yahoo_print)(struct yahoo_session *, int, const char *) = NULL; | |
| 26 | ||
| 27 | struct yahoo_session *yahoo_new() | |
| 28 | { | |
| 29 | struct yahoo_session *sess; | |
| 30 | ||
| 31 | if (!(sess = g_new0(struct yahoo_session, 1))) | |
| 32 | return NULL; | |
| 33 | ||
| 34 | return sess; | |
| 35 | } | |
| 36 | ||
|
1565
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
37 | void yahoo_set_proxy(struct yahoo_session *session, int proxy_type, char *proxy_host, int proxy_port) |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
38 | { |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
39 | if (!session || !proxy_type || !proxy_host) |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
40 | return; |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
41 | |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
42 | session->proxy_type = proxy_type; |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
43 | session->proxy_host = g_strdup(proxy_host); |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
44 | session->proxy_port = proxy_port; |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
45 | } |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
46 | |
| 1546 | 47 | static int yahoo_connect_host(struct yahoo_session *sess, const char *host, int port, int *statusret) |
| 48 | { | |
| 49 | struct sockaddr_in sa; | |
| 50 | struct hostent *hp; | |
| 51 | int fd; | |
| 52 | ||
| 53 | if (!(hp = gethostbyname(host))) { | |
| 54 | YAHOO_PRINT(sess, YAHOO_LOG_WARNING, "Resolve error"); | |
| 55 | if (statusret) | |
| 56 | *statusret = (h_errno | YAHOO_CONN_STATUS_RESOLVERR); | |
| 57 | return -1; | |
| 58 | } | |
| 59 | ||
| 60 | memset(&sa, 0, sizeof(struct sockaddr_in)); | |
| 61 | sa.sin_port = htons(port); | |
| 62 | memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); | |
| 63 | sa.sin_family = hp->h_addrtype; | |
| 64 | ||
| 65 | fd = socket(hp->h_addrtype, SOCK_STREAM, 0); | |
| 66 | ||
| 67 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
| 68 | ||
| 69 | if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { | |
| 70 | if ((errno == EINPROGRESS) || (errno == EINTR)) { | |
| 71 | YAHOO_PRINT(sess, YAHOO_LOG_NOTICE, "Connect would block"); | |
| 72 | if (statusret) | |
| 73 | *statusret |= YAHOO_CONN_STATUS_INPROGRESS; | |
| 74 | return fd; | |
| 75 | } | |
| 76 | close(fd); | |
| 77 | fd = -1; | |
| 78 | } | |
| 79 | ||
| 80 | return fd; | |
| 81 | } | |
| 82 | ||
| 83 | struct yahoo_conn *yahoo_new_conn(struct yahoo_session *session, int type, const char *host, int port) | |
| 84 | { | |
| 85 | struct yahoo_conn *conn; | |
| 86 | int status; | |
| 87 | ||
| 88 | if (!session) | |
| 89 | return NULL; | |
| 90 | ||
| 91 | conn = g_new0(struct yahoo_conn, 1); | |
| 92 | conn->type = type; | |
| 93 | ||
| 94 | if (host) { | |
| 95 | conn->socket = yahoo_connect_host(session, host, port, &status); | |
|
1565
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
96 | } else if (session->proxy_type) { |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
97 | YAHOO_PRINT(session, YAHOO_LOG_DEBUG, "connecting to proxy"); |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
98 | conn->socket = yahoo_connect_host(session, session->proxy_host, |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
99 | session->proxy_port, &status); |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
100 | if (type == YAHOO_CONN_TYPE_MAIN) |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
101 | conn->type = YAHOO_CONN_TYPE_PROXY; |
| 1546 | 102 | } else { |
| 103 | switch (type) { | |
| 104 | case YAHOO_CONN_TYPE_AUTH: | |
| 105 | conn->socket = yahoo_connect_host(session, YAHOO_AUTH_HOST, | |
| 106 | YAHOO_AUTH_PORT, &status); | |
| 107 | break; | |
| 108 | case YAHOO_CONN_TYPE_MAIN: | |
| 109 | conn->socket = yahoo_connect_host(session, YAHOO_PAGER_HOST, | |
| 110 | YAHOO_PAGER_PORT, &status); | |
| 111 | break; | |
| 112 | case YAHOO_CONN_TYPE_DUMB: | |
| 113 | conn->socket = yahoo_connect_host(session, YAHOO_DATA_HOST, | |
| 114 | YAHOO_DATA_PORT, &status); | |
| 115 | break; | |
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | if (conn->socket < 0) { | |
| 120 | g_free(conn); | |
| 121 | return NULL; | |
| 122 | } | |
| 123 | ||
| 124 | if (yahoo_socket_notify) | |
| 125 | (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_WRITE, TRUE); | |
| 126 | else | |
| 127 | YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "yahoo_socket_notify not set up"); | |
| 128 | ||
| 129 | session->connlist = g_list_append(session->connlist, conn); | |
| 130 | ||
| 131 | return conn; | |
| 132 | } | |
| 133 | ||
| 134 | struct yahoo_conn *yahoo_getconn_type(struct yahoo_session *sess, int type) | |
| 135 | { | |
| 136 | GList *c; | |
| 137 | struct yahoo_conn *conn; | |
| 138 | ||
| 139 | if (!sess) | |
| 140 | return NULL; | |
| 141 | ||
| 142 | c = sess->connlist; | |
| 143 | while (c) { | |
| 144 | conn = c->data; | |
| 145 | if (conn->type == type) | |
| 146 | return conn; | |
| 147 | c = g_list_next(c); | |
| 148 | } | |
| 149 | ||
| 150 | return NULL; | |
| 151 | } | |
| 152 | ||
| 153 | struct yahoo_conn *yahoo_find_conn(struct yahoo_session *sess, int socket) | |
| 154 | { | |
| 155 | GList *c; | |
| 156 | struct yahoo_conn *conn; | |
| 157 | ||
| 158 | c = sess->connlist; | |
| 159 | while (c) { | |
| 160 | conn = c->data; | |
| 161 | if (conn->socket == socket) | |
| 162 | return conn; | |
| 163 | c = g_list_next(c); | |
| 164 | } | |
| 165 | ||
| 166 | return NULL; | |
| 167 | } | |
| 168 | ||
| 169 | int yahoo_connect(struct yahoo_session *session, const char *host, int port) | |
| 170 | { | |
| 171 | if (!session) | |
| 172 | return 0; | |
| 173 | ||
| 174 | if (!yahoo_new_conn(session, YAHOO_CONN_TYPE_AUTH, host, port)) | |
| 175 | return 0; | |
| 176 | ||
| 177 | return 1; | |
| 178 | } | |
| 179 | ||
| 180 | int yahoo_major_connect(struct yahoo_session *session, const char *host, int port) | |
| 181 | { | |
| 182 | if (!session) | |
| 183 | return 0; | |
| 184 | ||
| 185 | if (!yahoo_new_conn(session, YAHOO_CONN_TYPE_MAIN, host, port)) | |
| 186 | return 0; | |
| 187 | ||
| 188 | return 1; | |
| 189 | } | |
| 190 | ||
| 191 | void yahoo_close(struct yahoo_session *session, struct yahoo_conn *conn) | |
| 192 | { | |
| 193 | if (!session || !conn) | |
| 194 | return; | |
| 195 | ||
| 196 | if (!g_list_find(session->connlist, conn)) | |
| 197 | return; | |
| 198 | ||
|
1566
a83bfcaaa269
[gaim-migrate @ 1576]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1565
diff
changeset
|
199 | if (yahoo_socket_notify) { |
|
a83bfcaaa269
[gaim-migrate @ 1576]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1565
diff
changeset
|
200 | if (conn->connected) |
|
a83bfcaaa269
[gaim-migrate @ 1576]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1565
diff
changeset
|
201 | (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_READ, FALSE); |
|
a83bfcaaa269
[gaim-migrate @ 1576]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1565
diff
changeset
|
202 | else |
|
a83bfcaaa269
[gaim-migrate @ 1576]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1565
diff
changeset
|
203 | (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_WRITE, FALSE); |
|
a83bfcaaa269
[gaim-migrate @ 1576]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1565
diff
changeset
|
204 | } |
| 1546 | 205 | close(conn->socket); |
| 206 | ||
| 207 | session->connlist = g_list_remove(session->connlist, conn); | |
| 208 | if (conn->txqueue) | |
| 209 | g_free(conn->txqueue); | |
| 210 | g_free(conn); | |
| 211 | } | |
| 212 | ||
| 213 | int yahoo_disconnect(struct yahoo_session *session) | |
| 214 | { | |
| 215 | if (!session) | |
| 216 | return 0; | |
| 217 | if (session->name) | |
| 218 | g_free(session->name); | |
| 219 | yahoo_logoff(session); | |
| 220 | session->name = NULL; | |
| 221 | while (session->connlist) | |
| 222 | yahoo_close(session, session->connlist->data); | |
| 223 | if (session->cookie) | |
| 224 | g_free(session->cookie); | |
| 225 | session->cookie = NULL; | |
| 226 | if (session->login_cookie) | |
| 227 | g_free(session->login_cookie); | |
| 228 | session->login_cookie = NULL; | |
| 229 | while (session->ignored) { | |
| 230 | g_free(session->ignored->data); | |
| 231 | session->ignored = g_list_remove(session->ignored, session->ignored->data); | |
| 232 | } | |
| 233 | if (session->identities) | |
| 234 | g_strfreev(session->identities); | |
| 235 | session->identities = NULL; | |
| 236 | if (session->login) | |
| 237 | g_free(session->login); | |
| 238 | session->login = NULL; | |
| 239 | while (session->groups) { | |
| 240 | struct yahoo_group *grp = session->groups->data; | |
| 241 | g_strfreev(grp->buddies); | |
| 242 | g_free(grp->name); | |
| 243 | g_free(grp); | |
| 244 | session->groups = g_list_remove(session->groups, grp); | |
| 245 | } | |
| 246 | return 0; | |
| 247 | } | |
| 248 | ||
| 249 | int yahoo_delete(struct yahoo_session *session) | |
| 250 | { | |
| 251 | if (!session) | |
| 252 | return 0; | |
|
1565
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
253 | if (session->proxy_host) |
|
95f2c94c708d
[gaim-migrate @ 1575]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1546
diff
changeset
|
254 | g_free(session->proxy_host); |
| 1546 | 255 | g_free(session); |
| 256 | return 0; | |
| 257 | } |