1 | #include "bitlbee.h" |
---|
2 | #include "lib/http_client.h" |
---|
3 | #include "msn.h" |
---|
4 | |
---|
5 | #define GATEWAY_HOST "geo.gateway.messenger.live.com" |
---|
6 | #define GATEWAY_PORT 443 |
---|
7 | |
---|
8 | #define REQUEST_TEMPLATE \ |
---|
9 | "POST /gateway/gateway.dll?SessionID=%s&%s HTTP/1.1\r\n" \ |
---|
10 | "Host: %s\r\n" \ |
---|
11 | "Content-Length: %zd\r\n" \ |
---|
12 | "\r\n" \ |
---|
13 | "%s" |
---|
14 | |
---|
15 | static gboolean msn_gw_poll_timeout(gpointer data, gint source, b_input_condition cond); |
---|
16 | |
---|
17 | struct msn_gw *msn_gw_new(struct msn_data *md) |
---|
18 | { |
---|
19 | struct msn_gw *gw = g_new0(struct msn_gw, 1); |
---|
20 | gw->last_host = g_strdup(GATEWAY_HOST); |
---|
21 | gw->port = GATEWAY_PORT; |
---|
22 | gw->ssl = (GATEWAY_PORT == 443); |
---|
23 | gw->poll_timeout = -1; |
---|
24 | gw->data = md; |
---|
25 | gw->in = g_byte_array_new(); |
---|
26 | gw->out = g_byte_array_new(); |
---|
27 | return gw; |
---|
28 | } |
---|
29 | |
---|
30 | void msn_gw_free(struct msn_gw *gw) |
---|
31 | { |
---|
32 | if (gw->poll_timeout != -1) { |
---|
33 | b_event_remove(gw->poll_timeout); |
---|
34 | } |
---|
35 | g_byte_array_free(gw->in, TRUE); |
---|
36 | g_byte_array_free(gw->out, TRUE); |
---|
37 | g_free(gw->session_id); |
---|
38 | g_free(gw->last_host); |
---|
39 | g_free(gw); |
---|
40 | } |
---|
41 | |
---|
42 | static gboolean msn_gw_parse_session_header(struct msn_gw *gw, char *value) |
---|
43 | { |
---|
44 | int i; |
---|
45 | char **subvalues; |
---|
46 | gboolean closed = FALSE; |
---|
47 | |
---|
48 | subvalues = g_strsplit(value, "; ", 0); |
---|
49 | |
---|
50 | for (i = 0; subvalues[i]; i++) { |
---|
51 | if (strcmp(subvalues[i], "Session=close") == 0) { |
---|
52 | /* gateway closed, signal the death of the socket */ |
---|
53 | closed = TRUE; |
---|
54 | } else if (g_str_has_prefix(subvalues[i], "SessionID=")) { |
---|
55 | /* copy the part after the = to session_id*/ |
---|
56 | g_free(gw->session_id); |
---|
57 | gw->session_id = g_strdup(subvalues[i] + 10); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | g_strfreev(subvalues); |
---|
62 | |
---|
63 | return !closed; |
---|
64 | } |
---|
65 | |
---|
66 | void msn_gw_callback(struct http_request *req) |
---|
67 | { |
---|
68 | char *value; |
---|
69 | struct msn_gw *gw = req->data; |
---|
70 | |
---|
71 | gw->waiting = FALSE; |
---|
72 | |
---|
73 | if (!gw->open) { |
---|
74 | /* the user tried to logout while the request was pending |
---|
75 | * see msn_ns_close() */ |
---|
76 | msn_gw_free(gw); |
---|
77 | return; |
---|
78 | } |
---|
79 | |
---|
80 | if (getenv("BITLBEE_DEBUG")) { |
---|
81 | fprintf(stderr, "\n\x1b[90mHTTP:%s\n", req->reply_body); |
---|
82 | fprintf(stderr, "\n\x1b[97m\n"); |
---|
83 | } |
---|
84 | |
---|
85 | if (req->status_code != 200) { |
---|
86 | gw->callback(gw->data, -1, B_EV_IO_READ); |
---|
87 | return; |
---|
88 | } |
---|
89 | |
---|
90 | if ((value = get_rfc822_header(req->reply_headers, "X-MSN-Messenger", 0))) { |
---|
91 | if (!msn_gw_parse_session_header(gw, value)) { |
---|
92 | gw->callback(gw->data, -1, B_EV_IO_READ); |
---|
93 | g_free(value); |
---|
94 | return; |
---|
95 | } |
---|
96 | g_free(value); |
---|
97 | } |
---|
98 | |
---|
99 | if ((value = get_rfc822_header(req->reply_headers, "X-MSN-Host", 0))) { |
---|
100 | g_free(gw->last_host); |
---|
101 | gw->last_host = value; /* transfer */ |
---|
102 | } |
---|
103 | |
---|
104 | if (req->body_size) { |
---|
105 | g_byte_array_append(gw->in, (const guint8 *) req->reply_body, req->body_size); |
---|
106 | gw->callback(gw->data, -1, B_EV_IO_READ); |
---|
107 | } |
---|
108 | |
---|
109 | if (gw->poll_timeout != -1) { |
---|
110 | b_event_remove(gw->poll_timeout); |
---|
111 | } |
---|
112 | gw->poll_timeout = b_timeout_add(5000, msn_gw_poll_timeout, gw); |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | void msn_gw_dorequest(struct msn_gw *gw, char *args) |
---|
117 | { |
---|
118 | char *request = NULL; |
---|
119 | char *body = NULL; |
---|
120 | size_t bodylen = 0; |
---|
121 | |
---|
122 | if (gw->out) { |
---|
123 | bodylen = gw->out->len; |
---|
124 | g_byte_array_append(gw->out, (guint8 *) "", 1); /* nullnullnull */ |
---|
125 | body = (char *) g_byte_array_free(gw->out, FALSE); |
---|
126 | gw->out = g_byte_array_new(); |
---|
127 | } |
---|
128 | |
---|
129 | request = g_strdup_printf(REQUEST_TEMPLATE, |
---|
130 | gw->session_id ? : "", args ? : "", gw->last_host, bodylen, body ? : ""); |
---|
131 | |
---|
132 | http_dorequest(gw->last_host, gw->port, gw->ssl, request, msn_gw_callback, gw); |
---|
133 | gw->open = TRUE; |
---|
134 | gw->waiting = TRUE; |
---|
135 | |
---|
136 | g_free(body); |
---|
137 | g_free(request); |
---|
138 | } |
---|
139 | |
---|
140 | void msn_gw_open(struct msn_gw *gw) |
---|
141 | { |
---|
142 | msn_gw_dorequest(gw, "Action=open&Server=NS"); |
---|
143 | } |
---|
144 | |
---|
145 | static gboolean msn_gw_poll_timeout(gpointer data, gint source, b_input_condition cond) |
---|
146 | { |
---|
147 | struct msn_gw *gw = data; |
---|
148 | gw->poll_timeout = -1; |
---|
149 | if (!gw->waiting) { |
---|
150 | msn_gw_dorequest(gw, NULL); |
---|
151 | } |
---|
152 | return FALSE; |
---|
153 | } |
---|
154 | |
---|
155 | ssize_t msn_gw_read(struct msn_gw *gw, char **buf) |
---|
156 | { |
---|
157 | size_t bodylen; |
---|
158 | if (!gw->open) { |
---|
159 | return 0; |
---|
160 | } |
---|
161 | |
---|
162 | bodylen = gw->in->len; |
---|
163 | g_byte_array_append(gw->in, (guint8 *) "", 1); /* nullnullnull */ |
---|
164 | *buf = (char *) g_byte_array_free(gw->in, FALSE); |
---|
165 | gw->in = g_byte_array_new(); |
---|
166 | return bodylen; |
---|
167 | } |
---|
168 | |
---|
169 | void msn_gw_write(struct msn_gw *gw, char *buf, size_t len) |
---|
170 | { |
---|
171 | g_byte_array_append(gw->out, (const guint8 *) buf, len); |
---|
172 | if (!gw->open) { |
---|
173 | msn_gw_open(gw); |
---|
174 | } else if (!gw->waiting) { |
---|
175 | msn_gw_dorequest(gw, NULL); |
---|
176 | } |
---|
177 | } |
---|