| 33 |
34 |
| 34 #define PURPLE_PLUGIN |
35 #define PURPLE_PLUGIN |
| 35 |
36 |
| 36 #include "myspace.h" |
37 #include "myspace.h" |
| 37 |
38 |
| 38 /* Internal functions */ |
39 static void msim_set_status(PurpleAccount *account, PurpleStatus *status); |
| 39 |
40 static void msim_set_idle(PurpleConnection *gc, int time); |
| 40 #ifdef MSIM_DEBUG_MSG |
41 |
| 41 static void print_hash_item(gpointer key, gpointer value, gpointer user_data); |
42 /** |
| 42 #endif |
43 * Perform actual postprocessing on a message, adding userid as specified. |
| 43 |
44 * |
| 44 static int msim_send_really_raw(PurpleConnection *gc, const char *buf, int total_bytes); |
45 * @param msg The message to postprocess. |
| 45 static gboolean msim_login_challenge(MsimSession *session, MsimMessage *msg); |
46 * @param uid_before Name of field where to insert new field before, or NULL for end. |
| 46 static gchar *msim_compute_login_response(const gchar nonce[2 * NONCE_SIZE], const gchar *email, const gchar *password, guint *response_len); |
47 * @param uid_field_name Name of field to add uid to. |
| 47 |
48 * @param uid The userid to insert. |
| 48 static gboolean msim_incoming_bm_record_cv(MsimSession *session, MsimMessage *msg); |
49 * |
| 49 static gboolean msim_incoming_bm(MsimSession *session, MsimMessage *msg); |
50 * If the field named by uid_field_name already exists, then its string contents will |
| 50 static gboolean msim_incoming_status(MsimSession *session, MsimMessage *msg); |
51 * be used for the field, except "<uid>" will be replaced by the userid. |
| 51 static gboolean msim_incoming_im(MsimSession *session, MsimMessage *msg); |
52 * |
| 52 /* static gboolean msim_incoming_zap(MsimSession *session, MsimMessage *msg); - in zap.c */ |
53 * If the field named by uid_field_name does not exist, it will be added before the |
| 53 static gboolean msim_incoming_action(MsimSession *session, MsimMessage *msg); |
54 * field named by uid_before, as an integer, with the userid. |
| 54 static gboolean msim_incoming_media(MsimSession *session, MsimMessage *msg); |
55 * |
| 55 static gboolean msim_incoming_unofficial_client(MsimSession *session, |
56 * Does not handle sending, or scheduling userid lookup. For that, see msim_postprocess_outgoing(). |
| 56 MsimMessage *msg); |
57 */ |
| 57 |
58 static MsimMessage * |
| 58 #ifdef MSIM_SEND_CLIENT_VERSION |
59 msim_do_postprocessing(MsimMessage *msg, const gchar *uid_before, |
| 59 static gboolean msim_send_unofficial_client(MsimSession *session, gchar *username); |
60 const gchar *uid_field_name, guint uid) |
| 60 #endif |
61 { |
| 61 |
62 MsimMessageElement *elem; |
| 62 static void msim_get_info_cb(MsimSession *session, MsimMessage *userinfo, gpointer data); |
63 msim_msg_dump("msim_do_postprocessing msg: %s\n", msg); |
| 63 |
64 |
| 64 static void msim_set_status_code(MsimSession *session, guint code, gchar *statstring); |
65 /* First, check - if the field already exists, replace <uid> within it */ |
| 65 |
66 if ((elem = msim_msg_get(msg, uid_field_name)) != NULL) { |
| 66 static gboolean msim_process_server_info(MsimSession *session, MsimMessage *msg); |
67 gchar *fmt_string; |
| 67 static gboolean msim_web_challenge(MsimSession *session, MsimMessage *msg); |
68 gchar *uid_str, *new_str; |
| 68 static gboolean msim_process_reply(MsimSession *session, MsimMessage *msg); |
69 |
| 69 |
70 /* Get the packed element, flattening it. This allows <uid> to be |
| 70 static gboolean msim_preprocess_incoming(MsimSession *session, MsimMessage *msg); |
71 * replaced within nested data structures, since the replacement is done |
| 71 |
72 * on the linear, packed data, not on a complicated data structure. |
| 72 #ifdef MSIM_USE_KEEPALIVE |
73 * |
| 73 static gboolean msim_check_alive(gpointer data); |
74 * For example, if the field was originally a dictionary or a list, you |
| 74 #endif |
75 * would have to iterate over all the items in it to see what needs to |
| 75 |
76 * be replaced. But by packing it first, the <uid> marker is easily replaced |
| 76 static gboolean msim_is_username_set(MsimSession *session, MsimMessage *msg); |
77 * just by a string replacement. |
| 77 |
78 */ |
| 78 static gboolean msim_process(MsimSession *session, MsimMessage *msg); |
79 fmt_string = msim_msg_pack_element_data(elem); |
| 79 |
80 |
| 80 static MsimMessage *msim_do_postprocessing(MsimMessage *msg, const gchar *uid_field_name, const gchar *uid_before, guint uid); |
81 uid_str = g_strdup_printf("%d", uid); |
| 81 static void msim_postprocess_outgoing_cb(MsimSession *session, MsimMessage *userinfo, gpointer data); |
82 new_str = purple_strreplace(fmt_string, "<uid>", uid_str); |
| 82 static gboolean msim_postprocess_outgoing(MsimSession *session, MsimMessage *msg, const gchar *username, const gchar *uid_field_name, const gchar *uid_before); |
83 g_free(uid_str); |
| 83 |
84 g_free(fmt_string); |
| 84 static gboolean msim_error(MsimSession *session, MsimMessage *msg); |
85 |
| 85 |
86 /* Free the old element data */ |
| 86 static void msim_check_inbox_cb(MsimSession *session, MsimMessage *userinfo, gpointer data); |
87 msim_msg_free_element_data(elem->data); |
| 87 static gboolean msim_check_inbox(gpointer data); |
88 |
| 88 |
89 /* Replace it with our new data */ |
| 89 static void msim_input_cb(gpointer gc_uncasted, gint source, PurpleInputCondition cond); |
90 elem->data = new_str; |
| 90 |
91 elem->type = MSIM_TYPE_RAW; |
| 91 |
92 |
| 92 static void msim_connect_cb(gpointer data, gint source, const gchar *error_message); |
93 } else { |
| 93 |
94 /* Otherwise, insert new field into outgoing message. */ |
| 94 static void msim_import_friends(PurplePluginAction *action); |
95 msg = msim_msg_insert_before(msg, uid_before, uid_field_name, MSIM_TYPE_INTEGER, GUINT_TO_POINTER(uid)); |
| 95 static void msim_import_friends_cb(MsimSession *session, MsimMessage *reply, gpointer user_data); |
96 } |
| 96 static gboolean msim_get_contact_list(MsimSession *session, int what_to_do_after); |
97 |
| 97 |
98 msim_msg_dump("msim_postprocess_outgoing_cb: postprocessed msg=%s\n", msg); |
| 98 static gboolean msim_uri_handler(const gchar *proto, const gchar *cmd, GHashTable *params); |
99 |
| 99 static void msim_uri_handler_addContact_cb(MsimSession *session, MsimMessage *userinfo, gpointer data); |
100 return msg; |
| 100 static void msim_uri_handler_sendIM_cb(MsimSession *session, MsimMessage *userinfo, gpointer data); |
101 } |
| 101 |
102 |
| 102 /** |
103 /** |
| 103 * Load the plugin. |
104 * Callback for msim_postprocess_outgoing() to add a userid to a message, and send it (once receiving userid). |
| 104 */ |
105 * |
| 105 gboolean |
106 * @param session |
| 106 msim_load(PurplePlugin *plugin) |
107 * @param userinfo The user information reply message, containing the user ID |
| 107 { |
108 * @param data The message to postprocess and send. |
| 108 /* If compiled to use RC4 from libpurple, check if it is really there. */ |
109 * |
| 109 if (!purple_ciphers_find_cipher("rc4")) { |
110 * The data message should contain these fields: |
| 110 purple_debug_error("msim", "rc4 not in libpurple, but it is required - not loading MySpaceIM plugin!\n"); |
111 * |
| 111 purple_notify_error(plugin, _("Missing Cipher"), |
112 * _uid_field_name: string, name of field to add with userid from userinfo message |
| 112 _("The RC4 cipher could not be found"), |
113 * _uid_before: string, name of field before field to insert, or NULL for end |
| 113 _("Upgrade " |
114 */ |
| 114 "to a libpurple with RC4 support (>= 2.0.1). MySpaceIM " |
115 static void |
| 115 "plugin will not be loaded.")); |
116 msim_postprocess_outgoing_cb(MsimSession *session, MsimMessage *userinfo, |
| 116 return FALSE; |
117 gpointer data) |
| 117 } |
118 { |
| 118 return TRUE; |
119 gchar *uid_field_name, *uid_before, *username; |
| |
120 guint uid; |
| |
121 MsimMessage *msg, *body; |
| |
122 |
| |
123 msg = (MsimMessage *)data; |
| |
124 |
| |
125 msim_msg_dump("msim_postprocess_outgoing_cb() got msg=%s\n", msg); |
| |
126 |
| |
127 /* Obtain userid from userinfo message. */ |
| |
128 body = msim_msg_get_dictionary(userinfo, "body"); |
| |
129 g_return_if_fail(body != NULL); |
| |
130 |
| |
131 uid = msim_msg_get_integer(body, "UserID"); |
| |
132 msim_msg_free(body); |
| |
133 |
| |
134 username = msim_msg_get_string(msg, "_username"); |
| |
135 |
| |
136 if (!uid) { |
| |
137 gchar *msg; |
| |
138 |
| |
139 msg = g_strdup_printf(_("No such user: %s"), username); |
| |
140 if (!purple_conv_present_error(username, session->account, msg)) { |
| |
141 purple_notify_error(NULL, NULL, _("User lookup"), msg); |
| |
142 } |
| |
143 |
| |
144 g_free(msg); |
| |
145 g_free(username); |
| |
146 /* TODO: free |
| |
147 * msim_msg_free(msg); |
| |
148 */ |
| |
149 return; |
| |
150 } |
| |
151 |
| |
152 uid_field_name = msim_msg_get_string(msg, "_uid_field_name"); |
| |
153 uid_before = msim_msg_get_string(msg, "_uid_before"); |
| |
154 |
| |
155 msg = msim_do_postprocessing(msg, uid_before, uid_field_name, uid); |
| |
156 |
| |
157 /* Send */ |
| |
158 if (!msim_msg_send(session, msg)) { |
| |
159 msim_msg_dump("msim_postprocess_outgoing_cb: sending failed for message: %s\n", msg); |
| |
160 } |
| |
161 |
| |
162 |
| |
163 /* Free field names AFTER sending message, because MsimMessage does NOT copy |
| |
164 * field names - instead, treats them as static strings (which they usually are). |
| |
165 */ |
| |
166 g_free(uid_field_name); |
| |
167 g_free(uid_before); |
| |
168 g_free(username); |
| |
169 /* TODO: free |
| |
170 * msim_msg_free(msg); |
| |
171 */ |
| |
172 } |
| |
173 |
| |
174 /** |
| |
175 * Postprocess and send a message. |
| |
176 * |
| |
177 * @param session |
| |
178 * @param msg Message to postprocess. Will NOT be freed. |
| |
179 * @param username Username to resolve. Assumed to be a static string (will not be freed or copied). |
| |
180 * @param uid_field_name Name of new field to add, containing uid of username. Static string. |
| |
181 * @param uid_before Name of existing field to insert username field before. Static string. |
| |
182 * |
| |
183 * @return TRUE if successful. |
| |
184 */ |
| |
185 static gboolean |
| |
186 msim_postprocess_outgoing(MsimSession *session, MsimMessage *msg, |
| |
187 const gchar *username, const gchar *uid_field_name, |
| |
188 const gchar *uid_before) |
| |
189 { |
| |
190 PurpleBuddy *buddy; |
| |
191 guint uid; |
| |
192 gboolean rc; |
| |
193 |
| |
194 g_return_val_if_fail(msg != NULL, FALSE); |
| |
195 |
| |
196 /* Store information for msim_postprocess_outgoing_cb(). */ |
| |
197 msim_msg_dump("msim_postprocess_outgoing: msg before=%s\n", msg); |
| |
198 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username)); |
| |
199 msg = msim_msg_append(msg, "_uid_field_name", MSIM_TYPE_STRING, g_strdup(uid_field_name)); |
| |
200 msg = msim_msg_append(msg, "_uid_before", MSIM_TYPE_STRING, g_strdup(uid_before)); |
| |
201 |
| |
202 /* First, try the most obvious. If numeric userid is given, use that directly. */ |
| |
203 if (msim_is_userid(username)) { |
| |
204 uid = atol(username); |
| |
205 } else { |
| |
206 /* Next, see if on buddy list and know uid. */ |
| |
207 buddy = purple_find_buddy(session->account, username); |
| |
208 if (buddy) { |
| |
209 uid = purple_blist_node_get_int(&buddy->node, "UserID"); |
| |
210 } else { |
| |
211 uid = 0; |
| |
212 } |
| |
213 |
| |
214 if (!buddy || !uid) { |
| |
215 /* Don't have uid offhand - need to ask for it, and wait until hear back before sending. */ |
| |
216 purple_debug_info("msim", ">>> msim_postprocess_outgoing: couldn't find username %s in blist\n", |
| |
217 username ? username : "(NULL)"); |
| |
218 msim_msg_dump("msim_postprocess_outgoing - scheduling lookup, msg=%s\n", msg); |
| |
219 /* TODO: where is cloned message freed? Should be in _cb. */ |
| |
220 msim_lookup_user(session, username, msim_postprocess_outgoing_cb, msim_msg_clone(msg)); |
| |
221 return TRUE; /* not sure of status yet - haven't sent! */ |
| |
222 } |
| |
223 } |
| |
224 |
| |
225 /* Already have uid, postprocess and send msg immediately. */ |
| |
226 purple_debug_info("msim", "msim_postprocess_outgoing: found username %s has uid %d\n", |
| |
227 username ? username : "(NULL)", uid); |
| |
228 |
| |
229 msg = msim_do_postprocessing(msg, uid_before, uid_field_name, uid); |
| |
230 |
| |
231 msim_msg_dump("msim_postprocess_outgoing: msg after (uid immediate)=%s\n", msg); |
| |
232 |
| |
233 rc = msim_msg_send(session, msg); |
| |
234 |
| |
235 /* TODO: free |
| |
236 * msim_msg_free(msg); |
| |
237 */ |
| |
238 |
| |
239 return rc; |
| |
240 } |
| |
241 |
| |
242 /** |
| |
243 * Send a buddy message of a given type. |
| |
244 * |
| |
245 * @param session |
| |
246 * @param who Username to send message to. |
| |
247 * @param text Message text to send. Not freed; will be copied. |
| |
248 * @param type A MSIM_BM_* constant. |
| |
249 * |
| |
250 * @return TRUE if success, FALSE if fail. |
| |
251 * |
| |
252 * Buddy messages ('bm') include instant messages, action messages, status messages, etc. |
| |
253 */ |
| |
254 gboolean |
| |
255 msim_send_bm(MsimSession *session, const gchar *who, const gchar *text, |
| |
256 int type) |
| |
257 { |
| |
258 gboolean rc; |
| |
259 MsimMessage *msg; |
| |
260 const gchar *from_username; |
| |
261 |
| |
262 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| |
263 g_return_val_if_fail(who != NULL, FALSE); |
| |
264 g_return_val_if_fail(text != NULL, FALSE); |
| |
265 |
| |
266 from_username = session->account->username; |
| |
267 |
| |
268 g_return_val_if_fail(from_username != NULL, FALSE); |
| |
269 |
| |
270 purple_debug_info("msim", "sending %d message from %s to %s: %s\n", |
| |
271 type, from_username, who, text); |
| |
272 |
| |
273 msg = msim_msg_new( |
| |
274 "bm", MSIM_TYPE_INTEGER, GUINT_TO_POINTER(type), |
| |
275 "sesskey", MSIM_TYPE_INTEGER, GUINT_TO_POINTER(session->sesskey), |
| |
276 /* 't' will be inserted here */ |
| |
277 "cv", MSIM_TYPE_INTEGER, GUINT_TO_POINTER(MSIM_CLIENT_VERSION), |
| |
278 "msg", MSIM_TYPE_STRING, g_strdup(text), |
| |
279 NULL); |
| |
280 |
| |
281 rc = msim_postprocess_outgoing(session, msg, who, "t", "cv"); |
| |
282 |
| |
283 msim_msg_free(msg); |
| |
284 |
| |
285 return rc; |
| |
286 } |
| |
287 |
| |
288 /** |
| |
289 * Lookup a username by userid, from buddy list. |
| |
290 * |
| |
291 * @param wanted_uid |
| |
292 * |
| |
293 * @return Username of wanted_uid, if on blist, or NULL. |
| |
294 * This is a static string, so don't free it. Copy it if needed. |
| |
295 * |
| |
296 */ |
| |
297 static const gchar * |
| |
298 msim_uid2username_from_blist(PurpleAccount *account, guint wanted_uid) |
| |
299 { |
| |
300 GSList *buddies, *cur; |
| |
301 const gchar *ret; |
| |
302 |
| |
303 buddies = purple_find_buddies(account, NULL); |
| |
304 |
| |
305 if (!buddies) |
| |
306 { |
| |
307 purple_debug_info("msim", "msim_uid2username_from_blist: no buddies?\n"); |
| |
308 return NULL; |
| |
309 } |
| |
310 |
| |
311 ret = NULL; |
| |
312 |
| |
313 for (cur = buddies; cur != NULL; cur = g_slist_next(cur)) |
| |
314 { |
| |
315 PurpleBuddy *buddy; |
| |
316 guint uid; |
| |
317 const gchar *name; |
| |
318 |
| |
319 /* See finch/gnthistory.c */ |
| |
320 buddy = cur->data; |
| |
321 |
| |
322 uid = purple_blist_node_get_int(&buddy->node, "UserID"); |
| |
323 name = purple_buddy_get_name(buddy); |
| |
324 |
| |
325 if (uid == wanted_uid) |
| |
326 { |
| |
327 ret = name; |
| |
328 break; |
| |
329 } |
| |
330 } |
| |
331 |
| |
332 g_slist_free(buddies); |
| |
333 return ret; |
| |
334 } |
| |
335 |
| |
336 /** |
| |
337 * Setup a callback, to be called when a reply is received with the returned rid. |
| |
338 * |
| |
339 * @param cb The callback, an MSIM_USER_LOOKUP_CB. |
| |
340 * @param data Arbitrary user data to be passed to callback (probably an MsimMessage *). |
| |
341 * |
| |
342 * @return The request/reply ID, used to link replies with requests, or -1. |
| |
343 * Put the rid in your request, 'rid' field. |
| |
344 * |
| |
345 * TODO: Make more generic and more specific: |
| |
346 * 1) MSIM_USER_LOOKUP_CB - make it for PERSIST_REPLY, not just user lookup |
| |
347 * 2) data - make it an MsimMessage? |
| |
348 */ |
| |
349 guint |
| |
350 msim_new_reply_callback(MsimSession *session, MSIM_USER_LOOKUP_CB cb, |
| |
351 gpointer data) |
| |
352 { |
| |
353 guint rid; |
| |
354 |
| |
355 g_return_val_if_fail(MSIM_SESSION_VALID(session), -1); |
| |
356 |
| |
357 rid = session->next_rid++; |
| |
358 |
| |
359 g_hash_table_insert(session->user_lookup_cb, GUINT_TO_POINTER(rid), cb); |
| |
360 g_hash_table_insert(session->user_lookup_cb_data, GUINT_TO_POINTER(rid), data); |
| |
361 |
| |
362 return rid; |
| |
363 } |
| |
364 |
| |
365 /** |
| |
366 * Return the icon name for a buddy and account. |
| |
367 * |
| |
368 * @param acct The account to find the icon for, or NULL for protocol icon. |
| |
369 * @param buddy The buddy to find the icon for, or NULL for the account icon. |
| |
370 * |
| |
371 * @return The base icon name string. |
| |
372 */ |
| |
373 static const gchar * |
| |
374 msim_list_icon(PurpleAccount *acct, PurpleBuddy *buddy) |
| |
375 { |
| |
376 /* Use a MySpace icon submitted by hbons at |
| |
377 * http://developer.pidgin.im/wiki/MySpaceIM. */ |
| |
378 return "myspace"; |
| |
379 } |
| |
380 |
| |
381 /** |
| |
382 * Obtain the status text for a buddy. |
| |
383 * |
| |
384 * @param buddy The buddy to obtain status text for. |
| |
385 * |
| |
386 * @return Status text, or NULL if error. Caller g_free()'s. |
| |
387 */ |
| |
388 static char * |
| |
389 msim_status_text(PurpleBuddy *buddy) |
| |
390 { |
| |
391 MsimSession *session; |
| |
392 MsimUser *user; |
| |
393 const gchar *display_name, *headline; |
| |
394 |
| |
395 g_return_val_if_fail(buddy != NULL, NULL); |
| |
396 |
| |
397 user = msim_get_user_from_buddy(buddy); |
| |
398 |
| |
399 session = (MsimSession *)buddy->account->gc->proto_data; |
| |
400 g_return_val_if_fail(MSIM_SESSION_VALID(session), NULL); |
| |
401 |
| |
402 display_name = headline = NULL; |
| |
403 |
| |
404 /* Retrieve display name and/or headline, depending on user preference. */ |
| |
405 if (purple_account_get_bool(session->account, "show_headline", TRUE)) { |
| |
406 headline = user->headline; |
| |
407 } |
| |
408 |
| |
409 if (purple_account_get_bool(session->account, "show_display_name", FALSE)) { |
| |
410 display_name = user->display_name; |
| |
411 } |
| |
412 |
| |
413 /* Return appropriate combination of display name and/or headline, or neither. */ |
| |
414 |
| |
415 if (display_name && headline) { |
| |
416 return g_strconcat(display_name, " ", headline, NULL); |
| |
417 } else if (display_name) { |
| |
418 return g_strdup(display_name); |
| |
419 } else if (headline) { |
| |
420 return g_strdup(headline); |
| |
421 } |
| |
422 |
| |
423 return NULL; |
| |
424 } |
| |
425 |
| |
426 /** |
| |
427 * Obtain the tooltip text for a buddy. |
| |
428 * |
| |
429 * @param buddy Buddy to obtain tooltip text on. |
| |
430 * @param user_info Variable modified to have the tooltip text. |
| |
431 * @param full TRUE if should obtain full tooltip text. |
| |
432 */ |
| |
433 static void |
| |
434 msim_tooltip_text(PurpleBuddy *buddy, PurpleNotifyUserInfo *user_info, |
| |
435 gboolean full) |
| |
436 { |
| |
437 MsimUser *user; |
| |
438 |
| |
439 g_return_if_fail(buddy != NULL); |
| |
440 g_return_if_fail(user_info != NULL); |
| |
441 |
| |
442 user = msim_get_user_from_buddy(buddy); |
| |
443 |
| |
444 if (PURPLE_BUDDY_IS_ONLINE(buddy)) { |
| |
445 MsimSession *session; |
| |
446 |
| |
447 session = (MsimSession *)buddy->account->gc->proto_data; |
| |
448 |
| |
449 g_return_if_fail(MSIM_SESSION_VALID(session)); |
| |
450 |
| |
451 /* TODO: if (full), do something different? */ |
| |
452 |
| |
453 /* TODO: request information? have to figure out how to do |
| |
454 * the asynchronous lookup like oscar does (tooltip shows |
| |
455 * 'retrieving...' if not yet available, then changes when it is). |
| |
456 * |
| |
457 * Right now, only show what we have on hand. |
| |
458 */ |
| |
459 |
| |
460 /* Show abbreviated user info. */ |
| |
461 msim_append_user_info(session, user_info, user, FALSE); |
| |
462 } |
| 119 } |
463 } |
| 120 |
464 |
| 121 /** |
465 /** |
| 122 * Get possible user status types. Based on mockprpl. |
466 * Get possible user status types. Based on mockprpl. |
| 123 * |
467 * |
| 124 * @return GList of status types. |
468 * @return GList of status types. |
| 125 */ |
469 */ |
| 126 GList * |
470 static GList * |
| 127 msim_status_types(PurpleAccount *acct) |
471 msim_status_types(PurpleAccount *acct) |
| 128 { |
472 { |
| 129 GList *types; |
473 GList *types; |
| 130 PurpleStatusType *status; |
474 PurpleStatusType *status; |
| 131 |
475 |
| 175 |
519 |
| 176 return types; |
520 return types; |
| 177 } |
521 } |
| 178 |
522 |
| 179 /** |
523 /** |
| 180 * Return the icon name for a buddy and account. |
524 * Compute the base64'd login challenge response based on username, password, nonce, and IPs. |
| 181 * |
525 * |
| 182 * @param acct The account to find the icon for, or NULL for protocol icon. |
526 * @param nonce The base64 encoded nonce ('nc') field from the server. |
| 183 * @param buddy The buddy to find the icon for, or NULL for the account icon. |
527 * @param email User's email address (used as login name). |
| 184 * |
528 * @param password User's cleartext password. |
| 185 * @return The base icon name string. |
529 * @param response_len Will be written with response length. |
| 186 */ |
530 * |
| 187 const gchar * |
531 * @return Binary login challenge response, ready to send to the server. |
| 188 msim_list_icon(PurpleAccount *acct, PurpleBuddy *buddy) |
532 * Must be g_free()'d when finished. NULL if error. |
| 189 { |
533 */ |
| 190 /* Use a MySpace icon submitted by hbons at |
534 static gchar * |
| 191 * http://developer.pidgin.im/wiki/MySpaceIM. */ |
535 msim_compute_login_response(const gchar nonce[2 * NONCE_SIZE], |
| 192 return "myspace"; |
536 const gchar *email, const gchar *password, guint *response_len) |
| 193 } |
537 { |
| 194 |
538 PurpleCipherContext *key_context; |
| 195 #ifdef MSIM_DEBUG_MSG |
539 PurpleCipher *sha1; |
| 196 static void |
540 PurpleCipherContext *rc4; |
| 197 print_hash_item(gpointer key, gpointer value, gpointer user_data) |
541 |
| 198 { |
542 guchar hash_pw[HASH_SIZE]; |
| 199 purple_debug_info("msim", "%s=%s\n", |
543 guchar key[HASH_SIZE]; |
| 200 key ? (gchar *)key : "(NULL)", |
544 gchar *password_utf16le, *password_utf8_lc; |
| 201 value ? (gchar *)value : "(NULL)"); |
545 guchar *data; |
| 202 } |
546 guchar *data_out; |
| |
547 size_t data_len, data_out_len; |
| |
548 gsize conv_bytes_read, conv_bytes_written; |
| |
549 GError *conv_error; |
| |
550 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
| |
551 int i; |
| 203 #endif |
552 #endif |
| 204 |
553 |
| 205 /** |
554 g_return_val_if_fail(nonce != NULL, NULL); |
| 206 * Send raw data (given as a NUL-terminated string) to the server. |
555 g_return_val_if_fail(email != NULL, NULL); |
| 207 * |
556 g_return_val_if_fail(password != NULL, NULL); |
| 208 * @param session |
557 g_return_val_if_fail(response_len != NULL, NULL); |
| 209 * @param msg The raw data to send, in a NUL-terminated string. |
558 |
| 210 * |
559 /* Convert password to lowercase (required for passwords containing |
| 211 * @return TRUE if succeeded, FALSE if not. |
560 * uppercase characters). MySpace passwords are lowercase, |
| 212 * |
561 * see ticket #2066. */ |
| 213 */ |
562 password_utf8_lc = g_utf8_strdown(password, -1); |
| 214 gboolean |
563 |
| 215 msim_send_raw(MsimSession *session, const gchar *msg) |
564 /* Convert ASCII password to UTF16 little endian */ |
| 216 { |
565 purple_debug_info("msim", "converting password to UTF-16LE\n"); |
| 217 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
566 conv_error = NULL; |
| 218 g_return_val_if_fail(msg != NULL, FALSE); |
567 password_utf16le = g_convert(password_utf8_lc, -1, "UTF-16LE", "UTF-8", |
| 219 |
568 &conv_bytes_read, &conv_bytes_written, &conv_error); |
| 220 purple_debug_info("msim", "msim_send_raw: writing <%s>\n", msg); |
569 g_free(password_utf8_lc); |
| 221 |
570 |
| 222 return msim_send_really_raw(session->gc, msg, strlen(msg)) == |
571 g_return_val_if_fail(conv_bytes_read == strlen(password), NULL); |
| 223 strlen(msg); |
572 |
| 224 } |
573 if (conv_error != NULL) { |
| 225 |
574 purple_debug_error("msim", |
| 226 /** Send raw data to the server, possibly with embedded NULs. |
575 "g_convert password UTF8->UTF16LE failed: %s", |
| 227 * |
576 conv_error->message); |
| 228 * Used in prpl_info struct, so that plugins can have the most possible |
577 g_error_free(conv_error); |
| 229 * control of what is sent over the connection. Inside this prpl, |
578 return NULL; |
| 230 * msim_send_raw() is used, since it sends NUL-terminated strings (easier). |
579 } |
| 231 * |
580 |
| 232 * @param gc PurpleConnection |
581 /* Compute password hash */ |
| 233 * @param buf Buffer to send |
582 purple_cipher_digest_region("sha1", (guchar *)password_utf16le, |
| 234 * @param total_bytes Size of buffer to send |
583 conv_bytes_written, sizeof(hash_pw), hash_pw, NULL); |
| 235 * |
584 g_free(password_utf16le); |
| 236 * @return Bytes successfully sent, or -1 on error. |
585 |
| 237 */ |
586 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
| 238 static int |
587 purple_debug_info("msim", "pwhash = "); |
| 239 msim_send_really_raw(PurpleConnection *gc, const char *buf, int total_bytes) |
588 for (i = 0; i < sizeof(hash_pw); i++) |
| 240 { |
589 purple_debug_info("msim", "%.2x ", hash_pw[i]); |
| 241 int total_bytes_sent; |
590 purple_debug_info("msim", "\n"); |
| 242 MsimSession *session; |
591 #endif |
| 243 |
592 |
| 244 g_return_val_if_fail(gc != NULL, -1); |
593 /* key = sha1(sha1(pw) + nonce2) */ |
| 245 g_return_val_if_fail(buf != NULL, -1); |
594 sha1 = purple_ciphers_find_cipher("sha1"); |
| 246 g_return_val_if_fail(total_bytes >= 0, -1); |
595 key_context = purple_cipher_context_new(sha1, NULL); |
| 247 |
596 purple_cipher_context_append(key_context, hash_pw, HASH_SIZE); |
| 248 session = (MsimSession *)gc->proto_data; |
597 purple_cipher_context_append(key_context, (guchar *)(nonce + NONCE_SIZE), NONCE_SIZE); |
| 249 |
598 purple_cipher_context_digest(key_context, sizeof(key), key, NULL); |
| 250 g_return_val_if_fail(MSIM_SESSION_VALID(session), -1); |
599 purple_cipher_context_destroy(key_context); |
| 251 |
600 |
| 252 /* Loop until all data is sent, or a failure occurs. */ |
601 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
| 253 total_bytes_sent = 0; |
602 purple_debug_info("msim", "key = "); |
| 254 do { |
603 for (i = 0; i < sizeof(key); i++) { |
| 255 int bytes_sent; |
604 purple_debug_info("msim", "%.2x ", key[i]); |
| 256 |
605 } |
| 257 bytes_sent = send(session->fd, buf + total_bytes_sent, |
606 purple_debug_info("msim", "\n"); |
| 258 total_bytes - total_bytes_sent, 0); |
607 #endif |
| 259 |
608 |
| 260 if (bytes_sent < 0) { |
609 rc4 = purple_cipher_context_new_by_name("rc4", NULL); |
| 261 purple_debug_info("msim", "msim_send_raw(%s): send() failed: %s\n", |
610 |
| 262 buf, g_strerror(errno)); |
611 /* Note: 'key' variable is 0x14 bytes (from SHA-1 hash), |
| 263 return total_bytes_sent; |
612 * but only first 0x10 used for the RC4 key. */ |
| 264 } |
613 purple_cipher_context_set_option(rc4, "key_len", (gpointer)0x10); |
| 265 total_bytes_sent += bytes_sent; |
614 purple_cipher_context_set_key(rc4, key); |
| 266 |
615 |
| 267 } while(total_bytes_sent < total_bytes); |
616 /* TODO: obtain IPs of network interfaces */ |
| 268 |
617 |
| 269 return total_bytes_sent; |
618 /* rc4 encrypt: |
| 270 } |
619 * nonce1+email+IP list */ |
| 271 |
620 |
| 272 |
621 data_len = NONCE_SIZE + strlen(email) + MSIM_LOGIN_IP_LIST_LEN; |
| 273 /** |
622 data = g_new0(guchar, data_len); |
| 274 * Start logging in to the MSIM servers. |
623 memcpy(data, nonce, NONCE_SIZE); |
| 275 * |
624 memcpy(data + NONCE_SIZE, email, strlen(email)); |
| 276 * @param acct Account information to use to login. |
625 memcpy(data + NONCE_SIZE + strlen(email), MSIM_LOGIN_IP_LIST, MSIM_LOGIN_IP_LIST_LEN); |
| 277 */ |
626 |
| 278 void |
627 data_out = g_new0(guchar, data_len); |
| 279 msim_login(PurpleAccount *acct) |
628 |
| 280 { |
629 purple_cipher_context_encrypt(rc4, (const guchar *)data, |
| 281 PurpleConnection *gc; |
630 data_len, data_out, &data_out_len); |
| 282 const gchar *host; |
631 purple_cipher_context_destroy(rc4); |
| 283 int port; |
632 g_free(data); |
| 284 |
633 |
| 285 g_return_if_fail(acct != NULL); |
634 if (data_out_len != data_len) { |
| 286 g_return_if_fail(acct->username != NULL); |
635 purple_debug_info("msim", "msim_compute_login_response: " |
| 287 |
636 "data length mismatch: %" G_GSIZE_FORMAT " != %" |
| 288 purple_debug_info("msim", "logging in %s\n", acct->username); |
637 G_GSIZE_FORMAT "\n", data_out_len, data_len); |
| 289 |
638 } |
| 290 gc = purple_account_get_connection(acct); |
639 |
| 291 gc->proto_data = msim_session_new(acct); |
640 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
| 292 gc->flags |= PURPLE_CONNECTION_HTML | PURPLE_CONNECTION_NO_URLDESC; |
641 purple_debug_info("msim", "response=<%s>\n", data_out); |
| 293 |
642 #endif |
| 294 /* 1. connect to server */ |
643 |
| 295 purple_connection_update_progress(gc, _("Connecting"), |
644 *response_len = data_out_len; |
| 296 0, /* which connection step this is */ |
645 |
| 297 4); /* total number of steps */ |
646 return (gchar *)data_out; |
| 298 |
647 } |
| 299 host = purple_account_get_string(acct, "server", MSIM_SERVER); |
648 |
| 300 port = purple_account_get_int(acct, "port", MSIM_PORT); |
649 /** |
| 301 |
650 * Process a login challenge, sending a response. |
| 302 /* From purple.sf.net/api: |
651 * |
| 303 * """Note that this function name can be misleading--although it is called |
652 * @param session |
| 304 * "proxy connect," it is used for establishing any outgoing TCP connection, |
|
| 305 * whether through a proxy or not.""" */ |
|
| 306 |
|
| 307 /* Calls msim_connect_cb when connected. */ |
|
| 308 if (!purple_proxy_connect(gc, acct, host, port, msim_connect_cb, gc)) { |
|
| 309 /* TODO: try other ports if in auto mode, then save |
|
| 310 * working port and try that first next time. */ |
|
| 311 purple_connection_error_reason (gc, |
|
| 312 PURPLE_CONNECTION_ERROR_NETWORK_ERROR, |
|
| 313 _("Couldn't create socket")); |
|
| 314 return; |
|
| 315 } |
|
| 316 } |
|
| 317 |
|
| 318 /** |
|
| 319 * Process a login challenge, sending a response. |
|
| 320 * |
|
| 321 * @param session |
|
| 322 * @param msg Login challenge message. |
653 * @param msg Login challenge message. |
| 323 * |
654 * |
| 324 * @return TRUE if successful, FALSE if not |
655 * @return TRUE if successful, FALSE if not |
| 325 */ |
656 */ |
| 326 static gboolean |
657 static gboolean |
| 327 msim_login_challenge(MsimSession *session, MsimMessage *msg) |
658 msim_login_challenge(MsimSession *session, MsimMessage *msg) |
| 328 { |
659 { |
| 329 PurpleAccount *account; |
660 PurpleAccount *account; |
| 330 gchar *response; |
661 gchar *response; |
| 331 guint response_len; |
662 guint response_len; |
| 332 gchar *nc; |
663 gchar *nc; |
| 381 |
712 |
| 382 return ret; |
713 return ret; |
| 383 } |
714 } |
| 384 |
715 |
| 385 /** |
716 /** |
| 386 * Compute the base64'd login challenge response based on username, password, nonce, and IPs. |
717 * Process unrecognized information. |
| 387 * |
718 * |
| 388 * @param nonce The base64 encoded nonce ('nc') field from the server. |
719 * @param session |
| 389 * @param email User's email address (used as login name). |
720 * @param msg An MsimMessage that was unrecognized, or NULL. |
| 390 * @param password User's cleartext password. |
721 * @param note Information on what was unrecognized, or NULL. |
| 391 * @param response_len Will be written with response length. |
722 */ |
| 392 * |
723 void |
| 393 * @return Binary login challenge response, ready to send to the server. |
724 msim_unrecognized(MsimSession *session, MsimMessage *msg, gchar *note) |
| 394 * Must be g_free()'d when finished. NULL if error. |
725 { |
| 395 */ |
726 /* TODO: Some more context, outwardly equivalent to a backtrace, |
| 396 static gchar * |
727 * for helping figure out what this msg is for. What was going on? |
| 397 msim_compute_login_response(const gchar nonce[2 * NONCE_SIZE], |
728 * But not too much information so that a user |
| 398 const gchar *email, const gchar *password, guint *response_len) |
729 * posting this dump reveals confidential information. |
| 399 { |
730 */ |
| 400 PurpleCipherContext *key_context; |
731 |
| 401 PurpleCipher *sha1; |
732 /* TODO: dump unknown msgs to file, so user can send them to me |
| 402 PurpleCipherContext *rc4; |
733 * if they wish, to help add support for new messages (inspired |
| 403 |
734 * by Alexandr Shutko, who maintains OSCAR protocol documentation). |
| 404 guchar hash_pw[HASH_SIZE]; |
735 * |
| 405 guchar key[HASH_SIZE]; |
736 * Filed enhancement ticket for libpurple as #4688. |
| 406 gchar *password_utf16le, *password_utf8_lc; |
737 */ |
| 407 guchar *data; |
738 |
| 408 guchar *data_out; |
739 purple_debug_info("msim", "Unrecognized data on account for %s\n", |
| 409 size_t data_len, data_out_len; |
740 (session && session->account && session->account->username) ? |
| 410 gsize conv_bytes_read, conv_bytes_written; |
741 session->account->username : "(NULL)"); |
| 411 GError *conv_error; |
742 if (note) { |
| 412 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
743 purple_debug_info("msim", "(Note: %s)\n", note); |
| 413 int i; |
744 } |
| |
745 |
| |
746 if (msg) { |
| |
747 msim_msg_dump("Unrecognized message dump: %s\n", msg); |
| |
748 } |
| |
749 } |
| |
750 |
| |
751 /** Called when the session key arrives to check whether the user |
| |
752 * has a username, and set one if desired. */ |
| |
753 static gboolean |
| |
754 msim_is_username_set(MsimSession *session, MsimMessage *msg) |
| |
755 { |
| |
756 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| |
757 g_return_val_if_fail(msg != NULL, FALSE); |
| |
758 g_return_val_if_fail(session->gc != NULL, FALSE); |
| |
759 |
| |
760 session->sesskey = msim_msg_get_integer(msg, "sesskey"); |
| |
761 purple_debug_info("msim", "SESSKEY=<%d>\n", session->sesskey); |
| |
762 |
| |
763 /* What is proof? Used to be uid, but now is 52 base64'd bytes... */ |
| |
764 |
| |
765 /* Comes with: proof,profileid,userid,uniquenick -- all same values |
| |
766 * some of the time, but can vary. This is our own user ID. */ |
| |
767 session->userid = msim_msg_get_integer(msg, "userid"); |
| |
768 |
| |
769 /* Save uid to account so this account can be looked up by uid. */ |
| |
770 purple_account_set_int(session->account, "uid", session->userid); |
| |
771 |
| |
772 /* Not sure what profileid is used for. */ |
| |
773 if (msim_msg_get_integer(msg, "profileid") != session->userid) { |
| |
774 msim_unrecognized(session, msg, |
| |
775 "Profile ID didn't match user ID, don't know why"); |
| |
776 } |
| |
777 |
| |
778 /* We now know are our own username, only after we're logged in.. |
| |
779 * which is weird, but happens because you login with your email |
| |
780 * address and not username. Will be freed in msim_session_destroy(). */ |
| |
781 session->username = msim_msg_get_string(msg, "uniquenick"); |
| |
782 |
| |
783 /* If user lacks a username, help them get one. */ |
| |
784 if (msim_msg_get_integer(msg, "uniquenick") == session->userid) { |
| |
785 purple_debug_info("msim_is_username_set", "no username is set\n"); |
| |
786 purple_request_yes_no(session->gc, |
| |
787 _("MySpaceIM - No Username Set"), |
| |
788 _("You appear to have no MySpace username."), |
| |
789 _("Would you like to set one now? (Note: THIS CANNOT BE CHANGED!)"), |
| |
790 0, |
| |
791 session->account, |
| |
792 NULL, |
| |
793 NULL, |
| |
794 session->gc, |
| |
795 G_CALLBACK(msim_set_username_cb), |
| |
796 G_CALLBACK(msim_do_not_set_username_cb)); |
| |
797 purple_debug_info("msim_is_username_set","'username not set' alert prompted\n"); |
| |
798 return FALSE; |
| |
799 } |
| |
800 return TRUE; |
| |
801 } |
| |
802 |
| |
803 #ifdef MSIM_USE_KEEPALIVE |
| |
804 /** |
| |
805 * Check if the connection is still alive, based on last communication. |
| |
806 */ |
| |
807 static gboolean |
| |
808 msim_check_alive(gpointer data) |
| |
809 { |
| |
810 MsimSession *session; |
| |
811 time_t delta; |
| |
812 |
| |
813 session = (MsimSession *)data; |
| |
814 |
| |
815 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| |
816 |
| |
817 delta = time(NULL) - session->last_comm; |
| |
818 |
| |
819 /* purple_debug_info("msim", "msim_check_alive: delta=%d\n", delta); */ |
| |
820 if (delta >= MSIM_KEEPALIVE_INTERVAL) { |
| |
821 purple_debug_info("msim", |
| |
822 "msim_check_alive: %zu > interval of %d, presumed dead\n", |
| |
823 delta, MSIM_KEEPALIVE_INTERVAL); |
| |
824 purple_connection_error_reason(session->gc, |
| |
825 PURPLE_CONNECTION_ERROR_NETWORK_ERROR, |
| |
826 _("Lost connection with server")); |
| |
827 |
| |
828 return FALSE; |
| |
829 } |
| |
830 |
| |
831 return TRUE; |
| |
832 } |
| 414 #endif |
833 #endif |
| 415 |
834 |
| 416 g_return_val_if_fail(nonce != NULL, NULL); |
835 /** |
| 417 g_return_val_if_fail(email != NULL, NULL); |
836 * Handle mail reply checks. |
| 418 g_return_val_if_fail(password != NULL, NULL); |
837 */ |
| 419 g_return_val_if_fail(response_len != NULL, NULL); |
838 static void |
| 420 |
839 msim_check_inbox_cb(MsimSession *session, MsimMessage *reply, gpointer data) |
| 421 /* Convert password to lowercase (required for passwords containing |
840 { |
| 422 * uppercase characters). MySpace passwords are lowercase, |
841 MsimMessage *body; |
| 423 * see ticket #2066. */ |
842 guint old_inbox_status; |
| 424 password_utf8_lc = g_utf8_strdown(password, -1); |
843 guint i, n; |
| 425 |
844 const gchar *froms[5], *tos[5], *urls[5], *subjects[5]; |
| 426 /* Convert ASCII password to UTF16 little endian */ |
845 |
| 427 purple_debug_info("msim", "converting password to UTF-16LE\n"); |
846 /* Information for each new inbox message type. */ |
| 428 conv_error = NULL; |
847 static struct |
| 429 password_utf16le = g_convert(password_utf8_lc, -1, "UTF-16LE", "UTF-8", |
848 { |
| 430 &conv_bytes_read, &conv_bytes_written, &conv_error); |
849 const gchar *key; |
| 431 g_free(password_utf8_lc); |
850 guint bit; |
| 432 |
851 const gchar *url; |
| 433 g_return_val_if_fail(conv_bytes_read == strlen(password), NULL); |
852 const gchar *text; |
| 434 |
853 } message_types[] = { |
| 435 if (conv_error != NULL) { |
854 { "Mail", MSIM_INBOX_MAIL, "http://messaging.myspace.com/index.cfm?fuseaction=mail.inbox", NULL }, |
| 436 purple_debug_error("msim", |
855 { "BlogComment", MSIM_INBOX_BLOG_COMMENT, "http://blog.myspace.com/index.cfm?fuseaction=blog", NULL }, |
| 437 "g_convert password UTF8->UTF16LE failed: %s", |
856 { "ProfileComment", MSIM_INBOX_PROFILE_COMMENT, "http://home.myspace.com/index.cfm?fuseaction=user", NULL }, |
| 438 conv_error->message); |
857 { "FriendRequest", MSIM_INBOX_FRIEND_REQUEST, "http://messaging.myspace.com/index.cfm?fuseaction=mail.friendRequests", NULL }, |
| 439 g_error_free(conv_error); |
858 { "PictureComment", MSIM_INBOX_PICTURE_COMMENT, "http://home.myspace.com/index.cfm?fuseaction=user", NULL } |
| 440 return NULL; |
859 }; |
| 441 } |
860 |
| 442 |
861 /* Can't write _()'d strings in array initializers. Workaround. */ |
| 443 /* Compute password hash */ |
862 message_types[0].text = _("New mail messages"); |
| 444 purple_cipher_digest_region("sha1", (guchar *)password_utf16le, |
863 message_types[1].text = _("New blog comments"); |
| 445 conv_bytes_written, sizeof(hash_pw), hash_pw, NULL); |
864 message_types[2].text = _("New profile comments"); |
| 446 g_free(password_utf16le); |
865 message_types[3].text = _("New friend requests!"); |
| 447 |
866 message_types[4].text = _("New picture comments"); |
| 448 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
867 |
| 449 purple_debug_info("msim", "pwhash = "); |
868 g_return_if_fail(reply != NULL); |
| 450 for (i = 0; i < sizeof(hash_pw); i++) |
869 |
| 451 purple_debug_info("msim", "%.2x ", hash_pw[i]); |
870 msim_msg_dump("msim_check_inbox_cb: reply=%s\n", reply); |
| 452 purple_debug_info("msim", "\n"); |
871 |
| |
872 body = msim_msg_get_dictionary(reply, "body"); |
| |
873 |
| |
874 if (body == NULL) |
| |
875 return; |
| |
876 |
| |
877 old_inbox_status = session->inbox_status; |
| |
878 |
| |
879 n = 0; |
| |
880 |
| |
881 for (i = 0; i < sizeof(message_types) / sizeof(message_types[0]); ++i) { |
| |
882 const gchar *key; |
| |
883 guint bit; |
| |
884 |
| |
885 key = message_types[i].key; |
| |
886 bit = message_types[i].bit; |
| |
887 |
| |
888 if (msim_msg_get(body, key)) { |
| |
889 /* Notify only on when _changes_ from no mail -> has mail |
| |
890 * (edge triggered) */ |
| |
891 if (!(session->inbox_status & bit)) { |
| |
892 purple_debug_info("msim", "msim_check_inbox_cb: got %s, at %d\n", |
| |
893 key ? key : "(NULL)", n); |
| |
894 |
| |
895 subjects[n] = message_types[i].text; |
| |
896 froms[n] = _("MySpace"); |
| |
897 tos[n] = session->username; |
| |
898 /* TODO: append token, web challenge, so automatically logs in. |
| |
899 * Would also need to free strings because they won't be static |
| |
900 */ |
| |
901 urls[n] = message_types[i].url; |
| |
902 |
| |
903 ++n; |
| |
904 } else { |
| |
905 purple_debug_info("msim", |
| |
906 "msim_check_inbox_cb: already notified of %s\n", |
| |
907 key ? key : "(NULL)"); |
| |
908 } |
| |
909 |
| |
910 session->inbox_status |= bit; |
| |
911 } |
| |
912 } |
| |
913 |
| |
914 if (n) { |
| |
915 purple_debug_info("msim", |
| |
916 "msim_check_inbox_cb: notifying of %d\n", n); |
| |
917 |
| |
918 /* TODO: free strings with callback _if_ change to dynamic (w/ token) */ |
| |
919 purple_notify_emails(session->gc, /* handle */ |
| |
920 n, /* count */ |
| |
921 TRUE, /* detailed */ |
| |
922 subjects, froms, tos, urls, |
| |
923 NULL, /* PurpleNotifyCloseCallback cb */ |
| |
924 NULL); /* gpointer user_data */ |
| |
925 |
| |
926 } |
| |
927 |
| |
928 msim_msg_free(body); |
| |
929 } |
| |
930 |
| |
931 /** |
| |
932 * Send request to check if there is new mail. |
| |
933 */ |
| |
934 static gboolean |
| |
935 msim_check_inbox(gpointer data) |
| |
936 { |
| |
937 MsimSession *session; |
| |
938 |
| |
939 session = (MsimSession *)data; |
| |
940 |
| |
941 if (!MSIM_SESSION_VALID(session)) { |
| |
942 purple_debug_info("msim", "msim_check_inbox: session invalid, stopping the mail check.\n"); |
| |
943 return FALSE; |
| |
944 } |
| |
945 |
| |
946 purple_debug_info("msim", "msim_check_inbox: checking mail\n"); |
| |
947 g_return_val_if_fail(msim_send(session, |
| |
948 "persist", MSIM_TYPE_INTEGER, 1, |
| |
949 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| |
950 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_GET, |
| |
951 "dsn", MSIM_TYPE_INTEGER, MG_CHECK_MAIL_DSN, |
| |
952 "lid", MSIM_TYPE_INTEGER, MG_CHECK_MAIL_LID, |
| |
953 "uid", MSIM_TYPE_INTEGER, session->userid, |
| |
954 "rid", MSIM_TYPE_INTEGER, |
| |
955 msim_new_reply_callback(session, msim_check_inbox_cb, NULL), |
| |
956 "body", MSIM_TYPE_STRING, g_strdup(""), |
| |
957 NULL), TRUE); |
| |
958 |
| |
959 /* Always return true, so that we keep checking for mail. */ |
| |
960 return TRUE; |
| |
961 } |
| |
962 |
| |
963 /** |
| |
964 * Add contact from server to buddy list, after looking up username. |
| |
965 * Callback from msim_add_contact_from_server(). |
| |
966 * |
| |
967 * @param data An MsimMessage * of the contact information. Will be freed. |
| |
968 */ |
| |
969 static void |
| |
970 msim_add_contact_from_server_cb(MsimSession *session, MsimMessage *user_lookup_info, gpointer data) |
| |
971 { |
| |
972 MsimMessage *contact_info, *user_lookup_info_body; |
| |
973 PurpleGroup *group; |
| |
974 PurpleBuddy *buddy; |
| |
975 MsimUser *user; |
| |
976 gchar *username, *group_name; |
| |
977 guint uid; |
| |
978 |
| |
979 contact_info = (MsimMessage *)data; |
| |
980 purple_debug_info("msim_add_contact_from_server_cb", "contact_info addr=%p\n", contact_info); |
| |
981 uid = msim_msg_get_integer(contact_info, "ContactID"); |
| |
982 |
| |
983 if (!user_lookup_info) { |
| |
984 username = g_strdup(msim_uid2username_from_blist(session->account, uid)); |
| |
985 g_return_if_fail(username != NULL); |
| |
986 } else { |
| |
987 user_lookup_info_body = msim_msg_get_dictionary(user_lookup_info, "body"); |
| |
988 username = msim_msg_get_string(user_lookup_info_body, "UserName"); |
| |
989 msim_msg_free(user_lookup_info_body); |
| |
990 g_return_if_fail(username != NULL); |
| |
991 } |
| |
992 |
| |
993 purple_debug_info("msim_add_contact_from_server_cb", |
| |
994 "*** about to add/update username=%s\n", username); |
| |
995 |
| |
996 /* 1. Creates a new group, or gets existing group if it exists (or so |
| |
997 * the documentation claims). */ |
| |
998 group_name = msim_msg_get_string(contact_info, "GroupName"); |
| |
999 if (!group_name || (*group_name == '\0')) { |
| |
1000 g_free(group_name); |
| |
1001 group_name = g_strdup(_("IM Friends")); |
| |
1002 purple_debug_info("myspace", "No GroupName specified, defaulting to '%s'.\n", group_name); |
| |
1003 } |
| |
1004 group = purple_find_group(group_name); |
| |
1005 if (!group) { |
| |
1006 group = purple_group_new(group_name); |
| |
1007 /* Add group to beginning. See #2752. */ |
| |
1008 purple_blist_add_group(group, NULL); |
| |
1009 } |
| |
1010 g_free(group_name); |
| |
1011 |
| |
1012 /* 2. Get or create buddy */ |
| |
1013 buddy = purple_find_buddy(session->account, username); |
| |
1014 if (!buddy) { |
| |
1015 purple_debug_info("msim_add_contact_from_server_cb", |
| |
1016 "creating new buddy: %s\n", username); |
| |
1017 buddy = purple_buddy_new(session->account, username, NULL); |
| |
1018 } |
| |
1019 |
| |
1020 /* TODO: use 'Position' in contact_info to take into account where buddy is */ |
| |
1021 purple_blist_add_buddy(buddy, NULL, group, NULL /* insertion point */); |
| |
1022 |
| |
1023 /* 3. Update buddy information */ |
| |
1024 user = msim_get_user_from_buddy(buddy); |
| |
1025 |
| |
1026 user->id = uid; |
| |
1027 /* Keep track of the user ID across sessions */ |
| |
1028 purple_blist_node_set_int(&buddy->node, "UserID", uid); |
| |
1029 |
| |
1030 /* Stores a few fields in the MsimUser, relevant to the buddy itself. |
| |
1031 * AvatarURL, Headline, ContactID. */ |
| |
1032 msim_store_user_info(session, contact_info, NULL); |
| |
1033 |
| |
1034 /* TODO: other fields, store in 'user' */ |
| |
1035 msim_msg_free(contact_info); |
| |
1036 |
| |
1037 g_free(username); |
| |
1038 } |
| |
1039 |
| |
1040 /** |
| |
1041 * Add first ContactID in contact_info to buddy's list. Used to add |
| |
1042 * server-side buddies to client-side list. |
| |
1043 * |
| |
1044 * @return TRUE if added. |
| |
1045 */ |
| |
1046 static gboolean |
| |
1047 msim_add_contact_from_server(MsimSession *session, MsimMessage *contact_info) |
| |
1048 { |
| |
1049 guint uid; |
| |
1050 const gchar *username; |
| |
1051 |
| |
1052 uid = msim_msg_get_integer(contact_info, "ContactID"); |
| |
1053 g_return_val_if_fail(uid != 0, FALSE); |
| |
1054 |
| |
1055 /* Lookup the username, since NickName and IMName is unreliable */ |
| |
1056 username = msim_uid2username_from_blist(session->account, uid); |
| |
1057 if (!username) { |
| |
1058 gchar *uid_str; |
| |
1059 |
| |
1060 uid_str = g_strdup_printf("%d", uid); |
| |
1061 purple_debug_info("msim_add_contact_from_server", |
| |
1062 "contact_info addr=%p\n", contact_info); |
| |
1063 msim_lookup_user(session, uid_str, msim_add_contact_from_server_cb, (gpointer)msim_msg_clone(contact_info)); |
| |
1064 g_free(uid_str); |
| |
1065 } else { |
| |
1066 msim_add_contact_from_server_cb(session, NULL, (gpointer)msim_msg_clone(contact_info)); |
| |
1067 } |
| |
1068 |
| |
1069 /* Say that the contact was added, even if we're still looking up |
| |
1070 * their username. */ |
| |
1071 return TRUE; |
| |
1072 } |
| |
1073 |
| |
1074 /** |
| |
1075 * Called when contact list is received from server. |
| |
1076 */ |
| |
1077 static void |
| |
1078 msim_got_contact_list(MsimSession *session, MsimMessage *reply, gpointer user_data) |
| |
1079 { |
| |
1080 MsimMessage *body, *body_node; |
| |
1081 gchar *msg; |
| |
1082 guint buddy_count; |
| |
1083 |
| |
1084 msim_msg_dump("msim_got_contact_list: reply=%s", reply); |
| |
1085 |
| |
1086 body = msim_msg_get_dictionary(reply, "body"); |
| |
1087 if (!body) { |
| |
1088 /* No friends. Not an error. */ |
| |
1089 return; |
| |
1090 } |
| |
1091 |
| |
1092 buddy_count = 0; |
| |
1093 |
| |
1094 for (body_node = body; |
| |
1095 body_node != NULL; |
| |
1096 body_node = msim_msg_get_next_element_node(body_node)) |
| |
1097 { |
| |
1098 MsimMessageElement *elem; |
| |
1099 |
| |
1100 elem = (MsimMessageElement *)body_node->data; |
| |
1101 |
| |
1102 if (g_str_equal(elem->name, "ContactID")) |
| |
1103 { |
| |
1104 /* Will look for first contact in body_node */ |
| |
1105 if (msim_add_contact_from_server(session, body_node)) { |
| |
1106 ++buddy_count; |
| |
1107 } |
| |
1108 } |
| |
1109 } |
| |
1110 |
| |
1111 switch (GPOINTER_TO_UINT(user_data)) { |
| |
1112 case MSIM_CONTACT_LIST_IMPORT_ALL_FRIENDS: |
| |
1113 msg = g_strdup_printf(ngettext("%d buddy was added or updated from the server (including buddies already on the server-side list)", |
| |
1114 "%d buddies were added or updated from the server (including buddies already on the server-side list)", |
| |
1115 buddy_count), |
| |
1116 buddy_count); |
| |
1117 purple_notify_info(session->account, _("Add contacts from server"), msg, NULL); |
| |
1118 g_free(msg); |
| |
1119 break; |
| |
1120 |
| |
1121 case MSIM_CONTACT_LIST_IMPORT_TOP_FRIENDS: |
| |
1122 /* TODO */ |
| |
1123 break; |
| |
1124 |
| |
1125 case MSIM_CONTACT_LIST_INITIAL_FRIENDS: |
| |
1126 /* Nothing */ |
| |
1127 break; |
| |
1128 } |
| |
1129 |
| |
1130 msim_msg_free(body); |
| |
1131 } |
| |
1132 |
| |
1133 /** |
| |
1134 * Get contact list, calling msim_got_contact_list() with |
| |
1135 * what_to_do_after as user_data gpointer. |
| |
1136 */ |
| |
1137 static gboolean |
| |
1138 msim_get_contact_list(MsimSession *session, int what_to_do_after) |
| |
1139 { |
| |
1140 return msim_send(session, |
| |
1141 "persist", MSIM_TYPE_INTEGER, 1, |
| |
1142 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| |
1143 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_GET, |
| |
1144 "dsn", MSIM_TYPE_INTEGER, MG_LIST_ALL_CONTACTS_DSN, |
| |
1145 "lid", MSIM_TYPE_INTEGER, MG_LIST_ALL_CONTACTS_LID, |
| |
1146 "uid", MSIM_TYPE_INTEGER, session->userid, |
| |
1147 "rid", MSIM_TYPE_INTEGER, |
| |
1148 msim_new_reply_callback(session, msim_got_contact_list, GUINT_TO_POINTER(what_to_do_after)), |
| |
1149 "body", MSIM_TYPE_STRING, g_strdup(""), |
| |
1150 NULL); |
| |
1151 } |
| |
1152 |
| |
1153 /** Called after username is set, if necessary and we're open for business. */ |
| |
1154 gboolean msim_we_are_logged_on(MsimSession *session) |
| |
1155 { |
| |
1156 MsimMessage *body; |
| |
1157 |
| |
1158 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| |
1159 |
| |
1160 /* The session is now set up, ready to be connected. This emits the |
| |
1161 * signedOn signal, so clients can now do anything with msimprpl, and |
| |
1162 * we're ready for it (session key, userid, username all setup). */ |
| |
1163 purple_connection_update_progress(session->gc, _("Connected"), 3, 4); |
| |
1164 purple_connection_set_state(session->gc, PURPLE_CONNECTED); |
| |
1165 |
| |
1166 /* Set display name to username (otherwise will show email address) */ |
| |
1167 purple_connection_set_display_name(session->gc, session->username); |
| |
1168 |
| |
1169 body = msim_msg_new( |
| |
1170 "UserID", MSIM_TYPE_INTEGER, session->userid, |
| |
1171 NULL); |
| |
1172 |
| |
1173 /* Request IM info about ourself. */ |
| |
1174 msim_send(session, |
| |
1175 "persist", MSIM_TYPE_STRING, g_strdup("persist"), |
| |
1176 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| |
1177 "dsn", MSIM_TYPE_INTEGER, MG_OWN_MYSPACE_INFO_DSN, |
| |
1178 "uid", MSIM_TYPE_INTEGER, session->userid, |
| |
1179 "lid", MSIM_TYPE_INTEGER, MG_OWN_MYSPACE_INFO_LID, |
| |
1180 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
| |
1181 "body", MSIM_TYPE_DICTIONARY, body, |
| |
1182 NULL); |
| |
1183 |
| |
1184 /* Request MySpace info about ourself. */ |
| |
1185 msim_send(session, |
| |
1186 "persist", MSIM_TYPE_STRING, g_strdup("persist"), |
| |
1187 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| |
1188 "dsn", MSIM_TYPE_INTEGER, MG_OWN_IM_INFO_DSN, |
| |
1189 "uid", MSIM_TYPE_INTEGER, session->userid, |
| |
1190 "lid", MSIM_TYPE_INTEGER, MG_OWN_IM_INFO_LID, |
| |
1191 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
| |
1192 "body", MSIM_TYPE_STRING, g_strdup(""), |
| |
1193 NULL); |
| |
1194 |
| |
1195 /* TODO: set options (persist cmd=514,dsn=1,lid=10) */ |
| |
1196 /* TODO: set blocklist */ |
| |
1197 |
| |
1198 /* Notify servers of our current status. */ |
| |
1199 purple_debug_info("msim", "msim_we_are_logged_on: notifying servers of status\n"); |
| |
1200 msim_set_status(session->account, |
| |
1201 purple_account_get_active_status(session->account)); |
| |
1202 |
| |
1203 /* TODO: setinfo */ |
| |
1204 /* |
| |
1205 body = msim_msg_new( |
| |
1206 "TotalFriends", MSIM_TYPE_INTEGER, 666, |
| |
1207 NULL); |
| |
1208 msim_send(session, |
| |
1209 "setinfo", MSIM_TYPE_BOOLEAN, TRUE, |
| |
1210 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| |
1211 "info", MSIM_TYPE_DICTIONARY, body, |
| |
1212 NULL); |
| |
1213 */ |
| |
1214 |
| |
1215 /* Disable due to problems with timeouts. TODO: fix. */ |
| |
1216 #ifdef MSIM_USE_KEEPALIVE |
| |
1217 purple_timeout_add(MSIM_KEEPALIVE_INTERVAL_CHECK, |
| |
1218 (GSourceFunc)msim_check_alive, session); |
| 453 #endif |
1219 #endif |
| 454 |
1220 |
| 455 /* key = sha1(sha1(pw) + nonce2) */ |
1221 /* Check mail if they want to. */ |
| 456 sha1 = purple_ciphers_find_cipher("sha1"); |
1222 if (purple_account_get_check_mail(session->account)) { |
| 457 key_context = purple_cipher_context_new(sha1, NULL); |
1223 session->inbox_handle = purple_timeout_add(MSIM_MAIL_INTERVAL_CHECK, |
| 458 purple_cipher_context_append(key_context, hash_pw, HASH_SIZE); |
1224 (GSourceFunc)msim_check_inbox, session); |
| 459 purple_cipher_context_append(key_context, (guchar *)(nonce + NONCE_SIZE), NONCE_SIZE); |
1225 msim_check_inbox(session); |
| 460 purple_cipher_context_digest(key_context, sizeof(key), key, NULL); |
1226 } |
| 461 purple_cipher_context_destroy(key_context); |
1227 |
| 462 |
1228 msim_get_contact_list(session, MSIM_CONTACT_LIST_INITIAL_FRIENDS); |
| 463 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
1229 |
| 464 purple_debug_info("msim", "key = "); |
1230 return TRUE; |
| 465 for (i = 0; i < sizeof(key); i++) { |
1231 } |
| 466 purple_debug_info("msim", "%.2x ", key[i]); |
1232 |
| 467 } |
1233 /** |
| 468 purple_debug_info("msim", "\n"); |
1234 * Record the client version in the buddy list, from an incoming message. |
| 469 #endif |
1235 */ |
| 470 |
|
| 471 rc4 = purple_cipher_context_new_by_name("rc4", NULL); |
|
| 472 |
|
| 473 /* Note: 'key' variable is 0x14 bytes (from SHA-1 hash), |
|
| 474 * but only first 0x10 used for the RC4 key. */ |
|
| 475 purple_cipher_context_set_option(rc4, "key_len", (gpointer)0x10); |
|
| 476 purple_cipher_context_set_key(rc4, key); |
|
| 477 |
|
| 478 /* TODO: obtain IPs of network interfaces */ |
|
| 479 |
|
| 480 /* rc4 encrypt: |
|
| 481 * nonce1+email+IP list */ |
|
| 482 |
|
| 483 data_len = NONCE_SIZE + strlen(email) + MSIM_LOGIN_IP_LIST_LEN; |
|
| 484 data = g_new0(guchar, data_len); |
|
| 485 memcpy(data, nonce, NONCE_SIZE); |
|
| 486 memcpy(data + NONCE_SIZE, email, strlen(email)); |
|
| 487 memcpy(data + NONCE_SIZE + strlen(email), MSIM_LOGIN_IP_LIST, MSIM_LOGIN_IP_LIST_LEN); |
|
| 488 |
|
| 489 data_out = g_new0(guchar, data_len); |
|
| 490 |
|
| 491 purple_cipher_context_encrypt(rc4, (const guchar *)data, |
|
| 492 data_len, data_out, &data_out_len); |
|
| 493 purple_cipher_context_destroy(rc4); |
|
| 494 g_free(data); |
|
| 495 |
|
| 496 if (data_out_len != data_len) { |
|
| 497 purple_debug_info("msim", "msim_compute_login_response: " |
|
| 498 "data length mismatch: %" G_GSIZE_FORMAT " != %" |
|
| 499 G_GSIZE_FORMAT "\n", data_out_len, data_len); |
|
| 500 } |
|
| 501 |
|
| 502 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE |
|
| 503 purple_debug_info("msim", "response=<%s>\n", data_out); |
|
| 504 #endif |
|
| 505 |
|
| 506 *response_len = data_out_len; |
|
| 507 |
|
| 508 return (gchar *)data_out; |
|
| 509 } |
|
| 510 |
|
| 511 /** |
|
| 512 * Schedule an IM to be sent once the user ID is looked up. |
|
| 513 * |
|
| 514 * @param gc Connection. |
|
| 515 * @param who A user id, email, or username to send the message to. |
|
| 516 * @param message Instant message text to send. |
|
| 517 * @param flags Flags. |
|
| 518 * |
|
| 519 * @return 1 if successful or postponed, -1 if failed |
|
| 520 * |
|
| 521 * Allows sending to a user by username, email address, or userid. If |
|
| 522 * a username or email address is given, the userid must be looked up. |
|
| 523 * This function does that by calling msim_postprocess_outgoing(). |
|
| 524 */ |
|
| 525 int |
|
| 526 msim_send_im(PurpleConnection *gc, const gchar *who, const gchar *message, |
|
| 527 PurpleMessageFlags flags) |
|
| 528 { |
|
| 529 MsimSession *session; |
|
| 530 gchar *message_msim; |
|
| 531 int rc; |
|
| 532 |
|
| 533 g_return_val_if_fail(gc != NULL, -1); |
|
| 534 g_return_val_if_fail(who != NULL, -1); |
|
| 535 g_return_val_if_fail(message != NULL, -1); |
|
| 536 |
|
| 537 /* 'flags' has many options, not used here. */ |
|
| 538 |
|
| 539 session = (MsimSession *)gc->proto_data; |
|
| 540 |
|
| 541 g_return_val_if_fail(MSIM_SESSION_VALID(session), -1); |
|
| 542 |
|
| 543 message_msim = html_to_msim_markup(session, message); |
|
| 544 |
|
| 545 if (msim_send_bm(session, who, message_msim, MSIM_BM_INSTANT)) { |
|
| 546 /* Return 1 to have Purple show this IM as being sent, 0 to not. I always |
|
| 547 * return 1 even if the message could not be sent, since I don't know if |
|
| 548 * it has failed yet--because the IM is only sent after the userid is |
|
| 549 * retrieved from the server (which happens after this function returns). |
|
| 550 * If an error does occur, it should be logged to the IM window. |
|
| 551 */ |
|
| 552 rc = 1; |
|
| 553 } else { |
|
| 554 rc = -1; |
|
| 555 } |
|
| 556 |
|
| 557 g_free(message_msim); |
|
| 558 |
|
| 559 return rc; |
|
| 560 } |
|
| 561 |
|
| 562 /** Send a buddy message of a given type. |
|
| 563 * |
|
| 564 * @param session |
|
| 565 * @param who Username to send message to. |
|
| 566 * @param text Message text to send. Not freed; will be copied. |
|
| 567 * @param type A MSIM_BM_* constant. |
|
| 568 * |
|
| 569 * @return TRUE if success, FALSE if fail. |
|
| 570 * |
|
| 571 * Buddy messages ('bm') include instant messages, action messages, status messages, etc. |
|
| 572 * |
|
| 573 */ |
|
| 574 gboolean |
|
| 575 msim_send_bm(MsimSession *session, const gchar *who, const gchar *text, |
|
| 576 int type) |
|
| 577 { |
|
| 578 gboolean rc; |
|
| 579 MsimMessage *msg; |
|
| 580 const gchar *from_username; |
|
| 581 |
|
| 582 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
| 583 g_return_val_if_fail(who != NULL, FALSE); |
|
| 584 g_return_val_if_fail(text != NULL, FALSE); |
|
| 585 |
|
| 586 from_username = session->account->username; |
|
| 587 |
|
| 588 g_return_val_if_fail(from_username != NULL, FALSE); |
|
| 589 |
|
| 590 purple_debug_info("msim", "sending %d message from %s to %s: %s\n", |
|
| 591 type, from_username, who, text); |
|
| 592 |
|
| 593 msg = msim_msg_new( |
|
| 594 "bm", MSIM_TYPE_INTEGER, GUINT_TO_POINTER(type), |
|
| 595 "sesskey", MSIM_TYPE_INTEGER, GUINT_TO_POINTER(session->sesskey), |
|
| 596 /* 't' will be inserted here */ |
|
| 597 "cv", MSIM_TYPE_INTEGER, GUINT_TO_POINTER(MSIM_CLIENT_VERSION), |
|
| 598 "msg", MSIM_TYPE_STRING, g_strdup(text), |
|
| 599 NULL); |
|
| 600 |
|
| 601 rc = msim_postprocess_outgoing(session, msg, who, "t", "cv"); |
|
| 602 |
|
| 603 msim_msg_free(msg); |
|
| 604 |
|
| 605 return rc; |
|
| 606 } |
|
| 607 |
|
| 608 |
|
| 609 /** Record the client version in the buddy list, from an incoming message. */ |
|
| 610 static gboolean |
1236 static gboolean |
| 611 msim_incoming_bm_record_cv(MsimSession *session, MsimMessage *msg) |
1237 msim_incoming_bm_record_cv(MsimSession *session, MsimMessage *msg) |
| 612 { |
1238 { |
| 613 gchar *username, *cv; |
1239 gchar *username, *cv; |
| 614 gboolean ret; |
1240 gboolean ret; |
| 637 g_free(cv); |
1263 g_free(cv); |
| 638 |
1264 |
| 639 return ret; |
1265 return ret; |
| 640 } |
1266 } |
| 641 |
1267 |
| 642 /** Handle an incoming buddy message. */ |
1268 #ifdef MSIM_SEND_CLIENT_VERSION |
| |
1269 /** |
| |
1270 * Send our client version to another unofficial client that understands it. |
| |
1271 */ |
| |
1272 static gboolean |
| |
1273 msim_send_unofficial_client(MsimSession *session, gchar *username) |
| |
1274 { |
| |
1275 gchar *our_info; |
| |
1276 gboolean ret; |
| |
1277 |
| |
1278 our_info = g_strdup_printf("Libpurple %d.%d.%d - msimprpl %s", |
| |
1279 PURPLE_MAJOR_VERSION, |
| |
1280 PURPLE_MINOR_VERSION, |
| |
1281 PURPLE_MICRO_VERSION, |
| |
1282 MSIM_PRPL_VERSION_STRING); |
| |
1283 |
| |
1284 ret = msim_send_bm(session, username, our_info, MSIM_BM_UNOFFICIAL_CLIENT); |
| |
1285 |
| |
1286 return ret; |
| |
1287 } |
| |
1288 #endif |
| |
1289 |
| |
1290 /** |
| |
1291 * Process incoming status messages. |
| |
1292 * |
| |
1293 * @param session |
| |
1294 * @param msg Status update message. Caller frees. |
| |
1295 * |
| |
1296 * @return TRUE if successful. |
| |
1297 */ |
| |
1298 static gboolean |
| |
1299 msim_incoming_status(MsimSession *session, MsimMessage *msg) |
| |
1300 { |
| |
1301 PurpleBuddyList *blist; |
| |
1302 MsimUser *user; |
| |
1303 GList *list; |
| |
1304 gchar *status_headline, *status_headline_escaped; |
| |
1305 gint status_code, purple_status_code; |
| |
1306 gchar *username; |
| |
1307 gchar *unrecognized_msg; |
| |
1308 |
| |
1309 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| |
1310 g_return_val_if_fail(msg != NULL, FALSE); |
| |
1311 |
| |
1312 msim_msg_dump("msim_status msg=%s\n", msg); |
| |
1313 |
| |
1314 /* Helpfully looked up by msim_incoming_resolve() for us. */ |
| |
1315 username = msim_msg_get_string(msg, "_username"); |
| |
1316 g_return_val_if_fail(username != NULL, FALSE); |
| |
1317 |
| |
1318 { |
| |
1319 gchar *ss; |
| |
1320 |
| |
1321 ss = msim_msg_get_string(msg, "msg"); |
| |
1322 purple_debug_info("msim", |
| |
1323 "msim_status: updating status for <%s> to <%s>\n", |
| |
1324 username, ss ? ss : "(NULL)"); |
| |
1325 g_free(ss); |
| |
1326 } |
| |
1327 |
| |
1328 /* Example fields: |
| |
1329 * |s|0|ss|Offline |
| |
1330 * |s|1|ss|:-)|ls||ip|0|p|0 |
| |
1331 */ |
| |
1332 list = msim_msg_get_list(msg, "msg"); |
| |
1333 |
| |
1334 status_code = msim_msg_get_integer_from_element(g_list_nth_data(list, MSIM_STATUS_ORDINAL_ONLINE)); |
| |
1335 purple_debug_info("msim", "msim_status: %s's status code = %d\n", username, status_code); |
| |
1336 status_headline = msim_msg_get_string_from_element(g_list_nth_data(list, MSIM_STATUS_ORDINAL_HEADLINE)); |
| |
1337 |
| |
1338 blist = purple_get_blist(); |
| |
1339 |
| |
1340 /* Add buddy if not found. |
| |
1341 * TODO: Could this be responsible for #3444? */ |
| |
1342 user = msim_find_user(session, username); |
| |
1343 if (!user) { |
| |
1344 PurpleBuddy *buddy; |
| |
1345 |
| |
1346 purple_debug_info("msim", |
| |
1347 "msim_status: making new buddy for %s\n", username); |
| |
1348 buddy = purple_buddy_new(session->account, username, NULL); |
| |
1349 purple_blist_add_buddy(buddy, NULL, NULL, NULL); |
| |
1350 |
| |
1351 user = msim_get_user_from_buddy(buddy); |
| |
1352 user->id = msim_msg_get_integer(msg, "f"); |
| |
1353 |
| |
1354 /* Keep track of the user ID across sessions */ |
| |
1355 purple_blist_node_set_int(&buddy->node, "UserID", user->id); |
| |
1356 |
| |
1357 msim_store_user_info(session, msg, NULL); |
| |
1358 } else { |
| |
1359 purple_debug_info("msim", "msim_status: found buddy %s\n", username); |
| |
1360 } |
| |
1361 |
| |
1362 if (status_headline && strcmp(status_headline, "") != 0) { |
| |
1363 /* The status headline is plaintext, but libpurple treats it as HTML, |
| |
1364 * so escape any HTML characters to their entity equivalents. */ |
| |
1365 status_headline_escaped = g_markup_escape_text(status_headline, strlen(status_headline)); |
| |
1366 } else { |
| |
1367 status_headline_escaped = NULL; |
| |
1368 } |
| |
1369 |
| |
1370 g_free(status_headline); |
| |
1371 |
| |
1372 if (user->headline) |
| |
1373 g_free(user->headline); |
| |
1374 |
| |
1375 /* don't copy; let the MsimUser own the headline, memory-wise */ |
| |
1376 user->headline = status_headline_escaped; |
| |
1377 |
| |
1378 /* Set user status */ |
| |
1379 switch (status_code) { |
| |
1380 case MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN: |
| |
1381 purple_status_code = PURPLE_STATUS_OFFLINE; |
| |
1382 break; |
| |
1383 |
| |
1384 case MSIM_STATUS_CODE_ONLINE: |
| |
1385 purple_status_code = PURPLE_STATUS_AVAILABLE; |
| |
1386 break; |
| |
1387 |
| |
1388 case MSIM_STATUS_CODE_AWAY: |
| |
1389 purple_status_code = PURPLE_STATUS_AWAY; |
| |
1390 break; |
| |
1391 |
| |
1392 case MSIM_STATUS_CODE_IDLE: |
| |
1393 /* Treat idle as an available status. */ |
| |
1394 purple_status_code = PURPLE_STATUS_AVAILABLE; |
| |
1395 break; |
| |
1396 |
| |
1397 default: |
| |
1398 purple_debug_info("msim", "msim_incoming_status for %s, unknown status code %d, treating as available\n", |
| |
1399 username, status_code); |
| |
1400 purple_status_code = PURPLE_STATUS_AVAILABLE; |
| |
1401 |
| |
1402 unrecognized_msg = g_strdup_printf("msim_incoming_status, unrecognized status code: %d\n", |
| |
1403 status_code); |
| |
1404 msim_unrecognized(session, NULL, unrecognized_msg); |
| |
1405 g_free(unrecognized_msg); |
| |
1406 |
| |
1407 } |
| |
1408 |
| |
1409 purple_prpl_got_user_status(session->account, username, purple_primitive_get_id_from_type(purple_status_code), NULL); |
| |
1410 |
| |
1411 if (status_code == MSIM_STATUS_CODE_IDLE) { |
| |
1412 purple_debug_info("msim", "msim_status: got idle: %s\n", username); |
| |
1413 purple_prpl_got_user_idle(session->account, username, TRUE, time(NULL)); |
| |
1414 } else { |
| |
1415 /* All other statuses indicate going back to non-idle. */ |
| |
1416 purple_prpl_got_user_idle(session->account, username, FALSE, time(NULL)); |
| |
1417 } |
| |
1418 |
| |
1419 #ifdef MSIM_SEND_CLIENT_VERSION |
| |
1420 if (status_code == MSIM_STATUS_CODE_ONLINE) { |
| |
1421 /* Secretly whisper to unofficial clients our own version as they come online */ |
| |
1422 msim_send_unofficial_client(session, username); |
| |
1423 } |
| |
1424 #endif |
| |
1425 |
| |
1426 if (status_code != MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN) { |
| |
1427 /* Get information when they come online. |
| |
1428 * TODO: periodically refresh? |
| |
1429 */ |
| |
1430 purple_debug_info("msim_incoming_status", "%s came online, looking up\n", username); |
| |
1431 msim_lookup_user(session, username, NULL, NULL); |
| |
1432 } |
| |
1433 |
| |
1434 g_free(username); |
| |
1435 msim_msg_list_free(list); |
| |
1436 |
| |
1437 return TRUE; |
| |
1438 } |
| |
1439 |
| |
1440 /** |
| |
1441 * Handle an incoming instant message. |
| |
1442 * |
| |
1443 * @param session The session |
| |
1444 * @param msg Message from the server, containing 'f' (userid from) and 'msg'. |
| |
1445 * Should also contain username in _username from preprocessing. |
| |
1446 * |
| |
1447 * @return TRUE if successful. |
| |
1448 */ |
| |
1449 static gboolean |
| |
1450 msim_incoming_im(MsimSession *session, MsimMessage *msg) |
| |
1451 { |
| |
1452 gchar *username, *msg_msim_markup, *msg_purple_markup; |
| |
1453 gchar *userid; |
| |
1454 time_t time_received; |
| |
1455 PurpleConversation *conv; |
| |
1456 |
| |
1457 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| |
1458 g_return_val_if_fail(msg != NULL, FALSE); |
| |
1459 |
| |
1460 username = msim_msg_get_string(msg, "_username"); |
| |
1461 /* I know this isn't really a string... but we need it to be one for |
| |
1462 * purple_find_conversation_with_account(). */ |
| |
1463 userid = msim_msg_get_string(msg, "f"); |
| |
1464 g_return_val_if_fail(username != NULL, FALSE); |
| |
1465 |
| |
1466 purple_debug_info("msim_incoming_im", "UserID is %s", userid); |
| |
1467 |
| |
1468 if (msim_is_userid(username)) { |
| |
1469 purple_debug_info("msim", "Ignoring message from spambot (%s) on account %s\n", |
| |
1470 username, purple_account_get_username(session->account)); |
| |
1471 g_free(username); |
| |
1472 return FALSE; |
| |
1473 } |
| |
1474 |
| |
1475 /* See if a conversation with their UID already exists...*/ |
| |
1476 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, userid, session->account); |
| |
1477 if (conv) { |
| |
1478 /* Since the conversation exists... We need to normalize it */ |
| |
1479 purple_conversation_set_name(conv, username); |
| |
1480 } |
| |
1481 |
| |
1482 msg_msim_markup = msim_msg_get_string(msg, "msg"); |
| |
1483 g_return_val_if_fail(msg_msim_markup != NULL, FALSE); |
| |
1484 |
| |
1485 msg_purple_markup = msim_markup_to_html(session, msg_msim_markup); |
| |
1486 g_free(msg_msim_markup); |
| |
1487 |
| |
1488 time_received = msim_msg_get_integer(msg, "date"); |
| |
1489 if (!time_received) { |
| |
1490 purple_debug_info("msim_incoming_im", "date in message not set.\n"); |
| |
1491 time_received = time(NULL); |
| |
1492 } |
| |
1493 |
| |
1494 serv_got_im(session->gc, username, msg_purple_markup, PURPLE_MESSAGE_RECV, time_received); |
| |
1495 |
| |
1496 g_free(username); |
| |
1497 g_free(msg_purple_markup); |
| |
1498 |
| |
1499 return TRUE; |
| |
1500 } |
| |
1501 |
| |
1502 /** |
| |
1503 * Handle an incoming action message. |
| |
1504 * |
| |
1505 * @param session |
| |
1506 * @param msg |
| |
1507 * |
| |
1508 * @return TRUE if successful. |
| |
1509 */ |
| |
1510 static gboolean |
| |
1511 msim_incoming_action(MsimSession *session, MsimMessage *msg) |
| |
1512 { |
| |
1513 gchar *msg_text, *username; |
| |
1514 gboolean rc; |
| |
1515 |
| |
1516 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| |
1517 g_return_val_if_fail(msg != NULL, FALSE); |
| |
1518 |
| |
1519 msg_text = msim_msg_get_string(msg, "msg"); |
| |
1520 g_return_val_if_fail(msg_text != NULL, FALSE); |
| |
1521 |
| |
1522 username = msim_msg_get_string(msg, "_username"); |
| |
1523 g_return_val_if_fail(username != NULL, FALSE); |
| |
1524 |
| |
1525 purple_debug_info("msim", "msim_incoming_action: action <%s> from <%s>\n", |
| |
1526 msg_text, username); |
| |
1527 |
| |
1528 if (g_str_equal(msg_text, "%typing%")) { |
| |
1529 serv_got_typing(session->gc, username, 0, PURPLE_TYPING); |
| |
1530 rc = TRUE; |
| |
1531 } else if (g_str_equal(msg_text, "%stoptyping%")) { |
| |
1532 serv_got_typing_stopped(session->gc, username); |
| |
1533 rc = TRUE; |
| |
1534 } else if (strstr(msg_text, "!!!ZAP_SEND!!!=RTE_BTN_ZAPS_")) { |
| |
1535 rc = msim_incoming_zap(session, msg); |
| |
1536 } else if (strstr(msg_text, "!!!GroupCount=")) { |
| |
1537 /* TODO: support group chats. I think the number in msg_text has |
| |
1538 * something to do with the 'gid' field. */ |
| |
1539 purple_debug_info("msim", "msim_incoming_action: TODO: implement #4691, group chats: %s\n", msg_text); |
| |
1540 |
| |
1541 rc = TRUE; |
| |
1542 } else if (strstr(msg_text, "!!!Offline=")) { |
| |
1543 /* TODO: support group chats. This one might mean a user |
| |
1544 * went offline or exited the chat. */ |
| |
1545 purple_debug_info("msim", "msim_incoming_action: TODO: implement #4691, group chats: %s\n", msg_text); |
| |
1546 |
| |
1547 rc = TRUE; |
| |
1548 } else if (msim_msg_get_integer(msg, "aid") != 0) { |
| |
1549 purple_debug_info("msim", "TODO: implement #4691, group chat from %d on %d: %s\n", |
| |
1550 msim_msg_get_integer(msg, "aid"), |
| |
1551 msim_msg_get_integer(msg, "f"), |
| |
1552 msg_text); |
| |
1553 |
| |
1554 rc = TRUE; |
| |
1555 } else { |
| |
1556 msim_unrecognized(session, msg, |
| |
1557 "got to msim_incoming_action but unrecognized value for 'msg'"); |
| |
1558 rc = FALSE; |
| |
1559 } |
| |
1560 |
| |
1561 g_free(msg_text); |
| |
1562 g_free(username); |
| |
1563 |
| |
1564 return rc; |
| |
1565 } |
| |
1566 |
| |
1567 /** |
| |
1568 * Process an incoming media (message background?) message. |
| |
1569 */ |
| |
1570 static gboolean |
| |
1571 msim_incoming_media(MsimSession *session, MsimMessage *msg) |
| |
1572 { |
| |
1573 gchar *username, *text; |
| |
1574 |
| |
1575 username = msim_msg_get_string(msg, "_username"); |
| |
1576 text = msim_msg_get_string(msg, "msg"); |
| |
1577 |
| |
1578 g_return_val_if_fail(username != NULL, FALSE); |
| |
1579 g_return_val_if_fail(text != NULL, FALSE); |
| |
1580 |
| |
1581 purple_debug_info("msim", "msim_incoming_media: from %s, got msg=%s\n", username, text); |
| |
1582 |
| |
1583 /* Media messages are sent when the user opens a window to someone. |
| |
1584 * Tell libpurple they started typing and stopped typing, to inform the Psychic |
| |
1585 * Mode plugin so it too can open a window to the user. */ |
| |
1586 serv_got_typing(session->gc, username, 0, PURPLE_TYPING); |
| |
1587 serv_got_typing_stopped(session->gc, username); |
| |
1588 |
| |
1589 g_free(username); |
| |
1590 |
| |
1591 return TRUE; |
| |
1592 } |
| |
1593 |
| |
1594 /** |
| |
1595 * Process an incoming "unofficial client" message. The plugin for |
| |
1596 * Miranda IM sends this message with the plugin information. |
| |
1597 */ |
| |
1598 static gboolean |
| |
1599 msim_incoming_unofficial_client(MsimSession *session, MsimMessage *msg) |
| |
1600 { |
| |
1601 MsimUser *user; |
| |
1602 gchar *username, *client_info; |
| |
1603 |
| |
1604 username = msim_msg_get_string(msg, "_username"); |
| |
1605 client_info = msim_msg_get_string(msg, "msg"); |
| |
1606 |
| |
1607 g_return_val_if_fail(username != NULL, FALSE); |
| |
1608 g_return_val_if_fail(client_info != NULL, FALSE); |
| |
1609 |
| |
1610 purple_debug_info("msim", "msim_incoming_unofficial_client: %s is using client %s\n", |
| |
1611 username, client_info); |
| |
1612 |
| |
1613 user = msim_find_user(session, username); |
| |
1614 |
| |
1615 g_return_val_if_fail(user != NULL, FALSE); |
| |
1616 |
| |
1617 if (user->client_info) { |
| |
1618 g_free(user->client_info); |
| |
1619 } |
| |
1620 user->client_info = client_info; |
| |
1621 |
| |
1622 g_free(username); |
| |
1623 /* Do not free client_info - the MsimUser now owns it. */ |
| |
1624 |
| |
1625 return TRUE; |
| |
1626 } |
| |
1627 |
| |
1628 /** |
| |
1629 * Handle an incoming buddy message. |
| |
1630 */ |
| 643 static gboolean |
1631 static gboolean |
| 644 msim_incoming_bm(MsimSession *session, MsimMessage *msg) |
1632 msim_incoming_bm(MsimSession *session, MsimMessage *msg) |
| 645 { |
1633 { |
| 646 guint bm; |
1634 guint bm; |
| 647 |
1635 |
| 659 case MSIM_BM_MEDIA: |
1647 case MSIM_BM_MEDIA: |
| 660 return msim_incoming_media(session, msg); |
1648 return msim_incoming_media(session, msg); |
| 661 case MSIM_BM_UNOFFICIAL_CLIENT: |
1649 case MSIM_BM_UNOFFICIAL_CLIENT: |
| 662 return msim_incoming_unofficial_client(session, msg); |
1650 return msim_incoming_unofficial_client(session, msg); |
| 663 default: |
1651 default: |
| 664 /* Not really an IM, but show it for informational |
1652 /* Not really an IM, but show it for informational |
| 665 * purposes during development. */ |
1653 * purposes during development. */ |
| 666 return msim_incoming_im(session, msg); |
1654 return msim_incoming_im(session, msg); |
| 667 } |
1655 } |
| 668 } |
1656 } |
| 669 |
1657 |
| 670 /** |
1658 /** |
| 671 * Handle an incoming instant message. |
1659 * Process the initial server information from the server. |
| 672 * |
1660 */ |
| 673 * @param session The session |
1661 static gboolean |
| 674 * @param msg Message from the server, containing 'f' (userid from) and 'msg'. |
1662 msim_process_server_info(MsimSession *session, MsimMessage *msg) |
| 675 * Should also contain username in _username from preprocessing. |
1663 { |
| |
1664 MsimMessage *body; |
| |
1665 |
| |
1666 body = msim_msg_get_dictionary(msg, "body"); |
| |
1667 g_return_val_if_fail(body != NULL, FALSE); |
| |
1668 |
| |
1669 /* Example body: |
| |
1670 AdUnitRefreshInterval=10. |
| |
1671 AlertPollInterval=360. |
| |
1672 AllowChatRoomEmoticonSharing=False. |
| |
1673 ChatRoomUserIDs=78744676;163733130;1300326231;123521495;142663391. |
| |
1674 CurClientVersion=673. |
| |
1675 EnableIMBrowse=True. |
| |
1676 EnableIMStuffAvatars=False. |
| |
1677 EnableIMStuffZaps=False. |
| |
1678 MaxAddAllFriends=100. |
| |
1679 MaxContacts=1000. |
| |
1680 MinClientVersion=594. |
| |
1681 MySpaceIM_ENGLISH=78744676. |
| |
1682 MySpaceNowTimer=720. |
| |
1683 PersistenceDataTimeout=900. |
| |
1684 UseWebChallenge=1. |
| |
1685 WebTicketGoHome=False |
| |
1686 |
| |
1687 Anything useful? TODO: use what is useful, and use it. |
| |
1688 */ |
| |
1689 purple_debug_info("msim_process_server_info", |
| |
1690 "maximum contacts: %d\n", |
| |
1691 msim_msg_get_integer(body, "MaxContacts")); |
| |
1692 |
| |
1693 session->server_info = body; |
| |
1694 /* session->server_info freed in msim_session_destroy */ |
| |
1695 |
| |
1696 return TRUE; |
| |
1697 } |
| |
1698 |
| |
1699 /** |
| |
1700 * Process a web challenge, used to login to the web site. |
| |
1701 */ |
| |
1702 static gboolean |
| |
1703 msim_web_challenge(MsimSession *session, MsimMessage *msg) |
| |
1704 { |
| |
1705 /* TODO: web challenge, store token. #2659. */ |
| |
1706 return FALSE; |
| |
1707 } |
| |
1708 |
| |
1709 /** |
| |
1710 * Process a persistance message reply from the server. |
| |
1711 * |
| |
1712 * @param session |
| |
1713 * @param msg Message reply from server. |
| 676 * |
1714 * |
| 677 * @return TRUE if successful. |
1715 * @return TRUE if successful. |
| 678 */ |
1716 * |
| 679 static gboolean |
1717 * msim_lookup_user sets callback for here |
| 680 msim_incoming_im(MsimSession *session, MsimMessage *msg) |
1718 */ |
| 681 { |
1719 static gboolean |
| 682 gchar *username, *msg_msim_markup, *msg_purple_markup; |
1720 msim_process_reply(MsimSession *session, MsimMessage *msg) |
| 683 gchar *userid; |
1721 { |
| 684 time_t time_received; |
1722 MSIM_USER_LOOKUP_CB cb; |
| 685 PurpleConversation *conv; |
1723 gpointer data; |
| |
1724 guint rid, cmd, dsn, lid; |
| 686 |
1725 |
| 687 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
1726 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| 688 g_return_val_if_fail(msg != NULL, FALSE); |
1727 g_return_val_if_fail(msg != NULL, FALSE); |
| 689 |
1728 |
| 690 username = msim_msg_get_string(msg, "_username"); |
1729 msim_store_user_info(session, msg, NULL); |
| 691 /* I know this isn't really a string... but we need it to be one for |
1730 |
| 692 * purple_find_conversation_with_account(). */ |
1731 rid = msim_msg_get_integer(msg, "rid"); |
| 693 userid = msim_msg_get_string(msg, "f"); |
1732 cmd = msim_msg_get_integer(msg, "cmd"); |
| 694 g_return_val_if_fail(username != NULL, FALSE); |
1733 dsn = msim_msg_get_integer(msg, "dsn"); |
| 695 |
1734 lid = msim_msg_get_integer(msg, "lid"); |
| 696 purple_debug_info("msim_incoming_im", "UserID is %s", userid); |
1735 |
| 697 |
1736 /* Unsolicited messages */ |
| 698 if (msim_is_userid(username)) { |
1737 if (cmd == (MSIM_CMD_BIT_REPLY | MSIM_CMD_GET)) { |
| 699 purple_debug_info("msim", "Ignoring message from spambot (%s) on account %s\n", |
1738 if (dsn == MG_SERVER_INFO_DSN && lid == MG_SERVER_INFO_LID) { |
| 700 username, purple_account_get_username(session->account)); |
1739 return msim_process_server_info(session, msg); |
| 701 g_free(username); |
1740 } else if (dsn == MG_WEB_CHALLENGE_DSN && lid == MG_WEB_CHALLENGE_LID) { |
| 702 return FALSE; |
1741 return msim_web_challenge(session, msg); |
| 703 } |
1742 } |
| 704 |
1743 } |
| 705 /* See if a conversation with their UID already exists...*/ |
1744 |
| 706 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, userid, session->account); |
1745 /* If a callback is registered for this userid lookup, call it. */ |
| 707 if (conv) { |
1746 cb = g_hash_table_lookup(session->user_lookup_cb, GUINT_TO_POINTER(rid)); |
| 708 /* Since the conversation exists... We need to normalize it */ |
1747 data = g_hash_table_lookup(session->user_lookup_cb_data, GUINT_TO_POINTER(rid)); |
| 709 purple_conversation_set_name(conv, username); |
1748 |
| 710 } |
1749 if (cb) { |
| 711 |
1750 purple_debug_info("msim", "msim_process_reply: calling callback now\n"); |
| 712 msg_msim_markup = msim_msg_get_string(msg, "msg"); |
1751 msim_msg_dump("for msg=%s\n", msg); |
| 713 g_return_val_if_fail(msg_msim_markup != NULL, FALSE); |
1752 /* Clone message, so that the callback 'cb' can use it (needs to free it also). */ |
| 714 |
1753 cb(session, msim_msg_clone(msg), data); |
| 715 msg_purple_markup = msim_markup_to_html(session, msg_msim_markup); |
1754 g_hash_table_remove(session->user_lookup_cb, GUINT_TO_POINTER(rid)); |
| 716 g_free(msg_msim_markup); |
1755 g_hash_table_remove(session->user_lookup_cb_data, GUINT_TO_POINTER(rid)); |
| 717 |
1756 } else { |
| 718 time_received = msim_msg_get_integer(msg, "date"); |
1757 purple_debug_info("msim", |
| 719 if (!time_received) { |
1758 "msim_process_reply: no callback for rid %d\n", rid); |
| 720 purple_debug_info("msim_incoming_im", "date in message not set.\n"); |
1759 } |
| 721 time_received = time(NULL); |
|
| 722 } |
|
| 723 |
|
| 724 serv_got_im(session->gc, username, msg_purple_markup, PURPLE_MESSAGE_RECV, time_received); |
|
| 725 |
|
| 726 g_free(username); |
|
| 727 g_free(msg_purple_markup); |
|
| 728 |
1760 |
| 729 return TRUE; |
1761 return TRUE; |
| 730 } |
1762 } |
| 731 |
1763 |
| 732 /** |
1764 /** |
| 733 * Process unrecognized information. |
1765 * Handle an error from the server. |
| 734 * |
1766 * |
| 735 * @param session |
1767 * @param session |
| 736 * @param msg An MsimMessage that was unrecognized, or NULL. |
1768 * @param msg The message. |
| 737 * @param note Information on what was unrecognized, or NULL. |
1769 * |
| 738 */ |
1770 * @return TRUE if successfully reported error. |
| 739 void |
1771 */ |
| 740 msim_unrecognized(MsimSession *session, MsimMessage *msg, gchar *note) |
1772 static gboolean |
| 741 { |
1773 msim_error(MsimSession *session, MsimMessage *msg) |
| 742 /* TODO: Some more context, outwardly equivalent to a backtrace, |
1774 { |
| 743 * for helping figure out what this msg is for. What was going on? |
1775 gchar *errmsg, *full_errmsg; |
| 744 * But not too much information so that a user |
1776 guint err; |
| 745 * posting this dump reveals confidential information. |
|
| 746 */ |
|
| 747 |
|
| 748 /* TODO: dump unknown msgs to file, so user can send them to me |
|
| 749 * if they wish, to help add support for new messages (inspired |
|
| 750 * by Alexandr Shutko, who maintains OSCAR protocol documentation). |
|
| 751 * |
|
| 752 * Filed enhancement ticket for libpurple as #4688. |
|
| 753 */ |
|
| 754 |
|
| 755 purple_debug_info("msim", "Unrecognized data on account for %s\n", |
|
| 756 (session && session->account && session->account->username) ? |
|
| 757 session->account->username : "(NULL)"); |
|
| 758 if (note) { |
|
| 759 purple_debug_info("msim", "(Note: %s)\n", note); |
|
| 760 } |
|
| 761 |
|
| 762 if (msg) { |
|
| 763 msim_msg_dump("Unrecognized message dump: %s\n", msg); |
|
| 764 } |
|
| 765 } |
|
| 766 |
|
| 767 /** |
|
| 768 * Handle an incoming action message. |
|
| 769 * |
|
| 770 * @param session |
|
| 771 * @param msg |
|
| 772 * |
|
| 773 * @return TRUE if successful. |
|
| 774 * |
|
| 775 */ |
|
| 776 static gboolean |
|
| 777 msim_incoming_action(MsimSession *session, MsimMessage *msg) |
|
| 778 { |
|
| 779 gchar *msg_text, *username; |
|
| 780 gboolean rc; |
|
| 781 |
1777 |
| 782 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
1778 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| 783 g_return_val_if_fail(msg != NULL, FALSE); |
1779 g_return_val_if_fail(msg != NULL, FALSE); |
| 784 |
1780 |
| 785 msg_text = msim_msg_get_string(msg, "msg"); |
1781 err = msim_msg_get_integer(msg, "err"); |
| 786 g_return_val_if_fail(msg_text != NULL, FALSE); |
1782 errmsg = msim_msg_get_string(msg, "errmsg"); |
| 787 |
1783 |
| 788 username = msim_msg_get_string(msg, "_username"); |
1784 full_errmsg = g_strdup_printf(_("Protocol error, code %d: %s"), err, |
| 789 g_return_val_if_fail(username != NULL, FALSE); |
1785 errmsg ? errmsg : "no 'errmsg' given"); |
| 790 |
1786 |
| 791 purple_debug_info("msim", "msim_incoming_action: action <%s> from <%s>\n", |
1787 g_free(errmsg); |
| 792 msg_text, username); |
1788 |
| 793 |
1789 purple_debug_info("msim", "msim_error (sesskey=%d): %s\n", |
| 794 if (g_str_equal(msg_text, "%typing%")) { |
1790 session->sesskey, full_errmsg); |
| 795 serv_got_typing(session->gc, username, 0, PURPLE_TYPING); |
1791 |
| 796 rc = TRUE; |
1792 /* Destroy session if fatal. */ |
| 797 } else if (g_str_equal(msg_text, "%stoptyping%")) { |
1793 if (msim_msg_get(msg, "fatal")) { |
| 798 serv_got_typing_stopped(session->gc, username); |
1794 PurpleConnectionError reason = PURPLE_CONNECTION_ERROR_NETWORK_ERROR; |
| 799 rc = TRUE; |
1795 purple_debug_info("msim", "fatal error, closing\n"); |
| 800 } else if (strstr(msg_text, "!!!ZAP_SEND!!!=RTE_BTN_ZAPS_")) { |
1796 |
| 801 rc = msim_incoming_zap(session, msg); |
1797 switch (err) { |
| 802 } else if (strstr(msg_text, "!!!GroupCount=")) { |
1798 case MSIM_ERROR_INCORRECT_PASSWORD: /* Incorrect password */ |
| 803 /* TODO: support group chats. I think the number in msg_text has |
1799 reason = PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED; |
| 804 * something to do with the 'gid' field. */ |
1800 if (!purple_account_get_remember_password(session->account)) |
| 805 purple_debug_info("msim", "msim_incoming_action: TODO: implement #4691, group chats: %s\n", msg_text); |
1801 purple_account_set_password(session->account, NULL); |
| 806 |
1802 #ifdef MSIM_MAX_PASSWORD_LENGTH |
| 807 rc = TRUE; |
1803 if (session->account->password && (strlen(session->account->password) > MSIM_MAX_PASSWORD_LENGTH)) { |
| 808 } else if (strstr(msg_text, "!!!Offline=")) { |
1804 gchar *suggestion; |
| 809 /* TODO: support group chats. This one might mean a user |
1805 |
| 810 * went offline or exited the chat. */ |
1806 suggestion = g_strdup_printf(_("%s Your password is " |
| 811 purple_debug_info("msim", "msim_incoming_action: TODO: implement #4691, group chats: %s\n", msg_text); |
1807 "%d characters, greater than the " |
| 812 |
1808 "expected maximum length of %d for " |
| 813 rc = TRUE; |
1809 "MySpaceIM. Please shorten your " |
| 814 } else if (msim_msg_get_integer(msg, "aid") != 0) { |
1810 "password at http://profileedit.myspace.com/index.cfm?fuseaction=accountSettings.changePassword and try again."), |
| 815 purple_debug_info("msim", "TODO: implement #4691, group chat from %d on %d: %s\n", |
1811 full_errmsg, (int) |
| 816 msim_msg_get_integer(msg, "aid"), |
1812 strlen(session->account->password), |
| 817 msim_msg_get_integer(msg, "f"), |
1813 MSIM_MAX_PASSWORD_LENGTH); |
| 818 msg_text); |
1814 |
| 819 |
1815 /* Replace full_errmsg. */ |
| 820 rc = TRUE; |
1816 g_free(full_errmsg); |
| |
1817 full_errmsg = suggestion; |
| |
1818 } |
| |
1819 #endif |
| |
1820 break; |
| |
1821 case MSIM_ERROR_LOGGED_IN_ELSEWHERE: /* Logged in elsewhere */ |
| |
1822 reason = PURPLE_CONNECTION_ERROR_NAME_IN_USE; |
| |
1823 if (!purple_account_get_remember_password(session->account)) |
| |
1824 purple_account_set_password(session->account, NULL); |
| |
1825 break; |
| |
1826 } |
| |
1827 purple_connection_error_reason (session->gc, reason, full_errmsg); |
| 821 } else { |
1828 } else { |
| 822 msim_unrecognized(session, msg, |
1829 purple_notify_error(session->account, _("MySpaceIM Error"), full_errmsg, NULL); |
| 823 "got to msim_incoming_action but unrecognized value for 'msg'"); |
1830 } |
| 824 rc = FALSE; |
1831 |
| 825 } |
1832 g_free(full_errmsg); |
| 826 |
|
| 827 g_free(msg_text); |
|
| 828 g_free(username); |
|
| 829 |
|
| 830 return rc; |
|
| 831 } |
|
| 832 |
|
| 833 /* Process an incoming media (message background?) message. */ |
|
| 834 static gboolean |
|
| 835 msim_incoming_media(MsimSession *session, MsimMessage *msg) |
|
| 836 { |
|
| 837 gchar *username, *text; |
|
| 838 |
|
| 839 username = msim_msg_get_string(msg, "_username"); |
|
| 840 text = msim_msg_get_string(msg, "msg"); |
|
| 841 |
|
| 842 g_return_val_if_fail(username != NULL, FALSE); |
|
| 843 g_return_val_if_fail(text != NULL, FALSE); |
|
| 844 |
|
| 845 purple_debug_info("msim", "msim_incoming_media: from %s, got msg=%s\n", username, text); |
|
| 846 |
|
| 847 /* Media messages are sent when the user opens a window to someone. |
|
| 848 * Tell libpurple they started typing and stopped typing, to inform the Psychic |
|
| 849 * Mode plugin so it too can open a window to the user. */ |
|
| 850 serv_got_typing(session->gc, username, 0, PURPLE_TYPING); |
|
| 851 serv_got_typing_stopped(session->gc, username); |
|
| 852 |
|
| 853 g_free(username); |
|
| 854 |
1833 |
| 855 return TRUE; |
1834 return TRUE; |
| 856 } |
1835 } |
| 857 |
1836 |
| 858 /* Process an incoming "unofficial client" message. The plugin for |
1837 /** |
| 859 * Miranda IM sends this message with the plugin information. */ |
1838 * Process a message. |
| 860 static gboolean |
|
| 861 msim_incoming_unofficial_client(MsimSession *session, MsimMessage *msg) |
|
| 862 { |
|
| 863 MsimUser *user; |
|
| 864 gchar *username, *client_info; |
|
| 865 |
|
| 866 username = msim_msg_get_string(msg, "_username"); |
|
| 867 client_info = msim_msg_get_string(msg, "msg"); |
|
| 868 |
|
| 869 g_return_val_if_fail(username != NULL, FALSE); |
|
| 870 g_return_val_if_fail(client_info != NULL, FALSE); |
|
| 871 |
|
| 872 purple_debug_info("msim", "msim_incoming_unofficial_client: %s is using client %s\n", |
|
| 873 username, client_info); |
|
| 874 |
|
| 875 user = msim_find_user(session, username); |
|
| 876 |
|
| 877 g_return_val_if_fail(user != NULL, FALSE); |
|
| 878 |
|
| 879 if (user->client_info) { |
|
| 880 g_free(user->client_info); |
|
| 881 } |
|
| 882 user->client_info = client_info; |
|
| 883 |
|
| 884 g_free(username); |
|
| 885 /* Do not free client_info - the MsimUser now owns it. */ |
|
| 886 |
|
| 887 return TRUE; |
|
| 888 } |
|
| 889 |
|
| 890 |
|
| 891 #ifdef MSIM_SEND_CLIENT_VERSION |
|
| 892 /** Send our client version to another unofficial client that understands it. */ |
|
| 893 static gboolean |
|
| 894 msim_send_unofficial_client(MsimSession *session, gchar *username) |
|
| 895 { |
|
| 896 gchar *our_info; |
|
| 897 gboolean ret; |
|
| 898 |
|
| 899 our_info = g_strdup_printf("Libpurple %d.%d.%d - msimprpl %s", |
|
| 900 PURPLE_MAJOR_VERSION, |
|
| 901 PURPLE_MINOR_VERSION, |
|
| 902 PURPLE_MICRO_VERSION, |
|
| 903 MSIM_PRPL_VERSION_STRING); |
|
| 904 |
|
| 905 ret = msim_send_bm(session, username, our_info, MSIM_BM_UNOFFICIAL_CLIENT); |
|
| 906 |
|
| 907 return ret; |
|
| 908 } |
|
| 909 #endif |
|
| 910 |
|
| 911 /** |
|
| 912 * Handle when our user starts or stops typing to another user. |
|
| 913 * |
|
| 914 * @param gc |
|
| 915 * @param name The buddy name to which our user is typing to |
|
| 916 * @param state PURPLE_TYPING, PURPLE_TYPED, PURPLE_NOT_TYPING |
|
| 917 * |
|
| 918 * @return 0 |
|
| 919 */ |
|
| 920 unsigned int |
|
| 921 msim_send_typing(PurpleConnection *gc, const gchar *name, |
|
| 922 PurpleTypingState state) |
|
| 923 { |
|
| 924 const gchar *typing_str; |
|
| 925 MsimSession *session; |
|
| 926 |
|
| 927 g_return_val_if_fail(gc != NULL, 0); |
|
| 928 g_return_val_if_fail(name != NULL, 0); |
|
| 929 |
|
| 930 session = (MsimSession *)gc->proto_data; |
|
| 931 |
|
| 932 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0); |
|
| 933 |
|
| 934 switch (state) { |
|
| 935 case PURPLE_TYPING: |
|
| 936 typing_str = "%typing%"; |
|
| 937 break; |
|
| 938 |
|
| 939 case PURPLE_TYPED: |
|
| 940 case PURPLE_NOT_TYPING: |
|
| 941 default: |
|
| 942 typing_str = "%stoptyping%"; |
|
| 943 break; |
|
| 944 } |
|
| 945 |
|
| 946 purple_debug_info("msim", "msim_send_typing(%s): %d (%s)\n", name, state, typing_str); |
|
| 947 msim_send_bm(session, name, typing_str, MSIM_BM_ACTION); |
|
| 948 return 0; |
|
| 949 } |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 /** Callback for msim_get_info(), for when user info is received. */ |
|
| 954 static void |
|
| 955 msim_get_info_cb(MsimSession *session, MsimMessage *user_info_msg, |
|
| 956 gpointer data) |
|
| 957 { |
|
| 958 MsimMessage *msg; |
|
| 959 gchar *username; |
|
| 960 PurpleNotifyUserInfo *user_info; |
|
| 961 MsimUser *user; |
|
| 962 |
|
| 963 g_return_if_fail(MSIM_SESSION_VALID(session)); |
|
| 964 |
|
| 965 /* Get user{name,id} from msim_get_info, passed as an MsimMessage for |
|
| 966 orthogonality. */ |
|
| 967 msg = (MsimMessage *)data; |
|
| 968 g_return_if_fail(msg != NULL); |
|
| 969 |
|
| 970 username = msim_msg_get_string(msg, "user"); |
|
| 971 if (!username) { |
|
| 972 purple_debug_info("msim", "msim_get_info_cb: no 'user' in msg\n"); |
|
| 973 return; |
|
| 974 } |
|
| 975 |
|
| 976 msim_msg_free(msg); |
|
| 977 purple_debug_info("msim", "msim_get_info_cb: got for user: %s\n", username); |
|
| 978 |
|
| 979 user = msim_find_user(session, username); |
|
| 980 |
|
| 981 if (!user) { |
|
| 982 /* User isn't on blist, create a temporary user to store info. */ |
|
| 983 user = g_new0(MsimUser, 1); |
|
| 984 user->temporary_user = TRUE; |
|
| 985 } |
|
| 986 |
|
| 987 /* Update user structure with new information */ |
|
| 988 msim_store_user_info(session, user_info_msg, user); |
|
| 989 |
|
| 990 user_info = purple_notify_user_info_new(); |
|
| 991 |
|
| 992 /* Append data from MsimUser to PurpleNotifyUserInfo for display, full */ |
|
| 993 msim_append_user_info(session, user_info, user, TRUE); |
|
| 994 |
|
| 995 purple_notify_userinfo(session->gc, username, user_info, NULL, NULL); |
|
| 996 purple_debug_info("msim", "msim_get_info_cb: username=%s\n", username); |
|
| 997 |
|
| 998 purple_notify_user_info_destroy(user_info); |
|
| 999 |
|
| 1000 if (user->temporary_user) { |
|
| 1001 g_free(user->client_info); |
|
| 1002 g_free(user->gender); |
|
| 1003 g_free(user->location); |
|
| 1004 g_free(user->headline); |
|
| 1005 g_free(user->display_name); |
|
| 1006 g_free(user->username); |
|
| 1007 g_free(user->image_url); |
|
| 1008 g_free(user); |
|
| 1009 } |
|
| 1010 g_free(username); |
|
| 1011 } |
|
| 1012 |
|
| 1013 /** Retrieve a user's profile. |
|
| 1014 * @param username Username, user ID, or email address to lookup. |
|
| 1015 */ |
|
| 1016 void |
|
| 1017 msim_get_info(PurpleConnection *gc, const gchar *username) |
|
| 1018 { |
|
| 1019 MsimSession *session; |
|
| 1020 MsimUser *user; |
|
| 1021 gchar *user_to_lookup; |
|
| 1022 MsimMessage *user_msg; |
|
| 1023 |
|
| 1024 g_return_if_fail(gc != NULL); |
|
| 1025 g_return_if_fail(username != NULL); |
|
| 1026 |
|
| 1027 session = (MsimSession *)gc->proto_data; |
|
| 1028 |
|
| 1029 g_return_if_fail(MSIM_SESSION_VALID(session)); |
|
| 1030 |
|
| 1031 /* Obtain uid of buddy. */ |
|
| 1032 user = msim_find_user(session, username); |
|
| 1033 |
|
| 1034 /* If is on buddy list, lookup by uid since it is faster. */ |
|
| 1035 if (user && user->id) { |
|
| 1036 user_to_lookup = g_strdup_printf("%d", user->id); |
|
| 1037 } else { |
|
| 1038 /* Looking up buddy not on blist. Lookup by whatever user entered. */ |
|
| 1039 user_to_lookup = g_strdup(username); |
|
| 1040 } |
|
| 1041 |
|
| 1042 /* Pass the username to msim_get_info_cb(), because since we lookup |
|
| 1043 * by userid, the userinfo message will only contain the uid (not |
|
| 1044 * the username) but it would be useful to display the username too. |
|
| 1045 */ |
|
| 1046 user_msg = msim_msg_new( |
|
| 1047 "user", MSIM_TYPE_STRING, g_strdup(username), |
|
| 1048 NULL); |
|
| 1049 purple_debug_info("msim", "msim_get_info, setting up lookup, user=%s\n", username); |
|
| 1050 |
|
| 1051 msim_lookup_user(session, user_to_lookup, msim_get_info_cb, user_msg); |
|
| 1052 |
|
| 1053 g_free(user_to_lookup); |
|
| 1054 } |
|
| 1055 |
|
| 1056 /** Set your status - callback for when user manually sets it. */ |
|
| 1057 void |
|
| 1058 msim_set_status(PurpleAccount *account, PurpleStatus *status) |
|
| 1059 { |
|
| 1060 PurpleStatusType *type; |
|
| 1061 PurplePresence *pres; |
|
| 1062 MsimSession *session; |
|
| 1063 guint status_code; |
|
| 1064 const gchar *message; |
|
| 1065 gchar *stripped; |
|
| 1066 gchar *unrecognized_msg; |
|
| 1067 |
|
| 1068 session = (MsimSession *)account->gc->proto_data; |
|
| 1069 |
|
| 1070 g_return_if_fail(MSIM_SESSION_VALID(session)); |
|
| 1071 |
|
| 1072 type = purple_status_get_type(status); |
|
| 1073 pres = purple_status_get_presence(status); |
|
| 1074 |
|
| 1075 switch (purple_status_type_get_primitive(type)) { |
|
| 1076 case PURPLE_STATUS_AVAILABLE: |
|
| 1077 purple_debug_info("msim", "msim_set_status: available (%d->%d)\n", PURPLE_STATUS_AVAILABLE, |
|
| 1078 MSIM_STATUS_CODE_ONLINE); |
|
| 1079 status_code = MSIM_STATUS_CODE_ONLINE; |
|
| 1080 break; |
|
| 1081 |
|
| 1082 case PURPLE_STATUS_INVISIBLE: |
|
| 1083 purple_debug_info("msim", "msim_set_status: invisible (%d->%d)\n", PURPLE_STATUS_INVISIBLE, |
|
| 1084 MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN); |
|
| 1085 status_code = MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN; |
|
| 1086 break; |
|
| 1087 |
|
| 1088 case PURPLE_STATUS_AWAY: |
|
| 1089 purple_debug_info("msim", "msim_set_status: away (%d->%d)\n", PURPLE_STATUS_AWAY, |
|
| 1090 MSIM_STATUS_CODE_AWAY); |
|
| 1091 status_code = MSIM_STATUS_CODE_AWAY; |
|
| 1092 break; |
|
| 1093 |
|
| 1094 default: |
|
| 1095 purple_debug_info("msim", "msim_set_status: unknown " |
|
| 1096 "status interpreting as online"); |
|
| 1097 status_code = MSIM_STATUS_CODE_ONLINE; |
|
| 1098 |
|
| 1099 unrecognized_msg = g_strdup_printf("msim_set_status, unrecognized status type: %d\n", |
|
| 1100 purple_status_type_get_primitive(type)); |
|
| 1101 msim_unrecognized(session, NULL, unrecognized_msg); |
|
| 1102 g_free(unrecognized_msg); |
|
| 1103 |
|
| 1104 break; |
|
| 1105 } |
|
| 1106 |
|
| 1107 message = purple_status_get_attr_string(status, "message"); |
|
| 1108 |
|
| 1109 /* Status strings are plain text. */ |
|
| 1110 if (message != NULL) |
|
| 1111 stripped = purple_markup_strip_html(message); |
|
| 1112 else |
|
| 1113 stripped = g_strdup(""); |
|
| 1114 |
|
| 1115 msim_set_status_code(session, status_code, stripped); |
|
| 1116 |
|
| 1117 /* If we should be idle, set that status. Time is irrelevant here. */ |
|
| 1118 if (purple_presence_is_idle(pres) && status_code != MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN) |
|
| 1119 msim_set_idle(account->gc, 1); |
|
| 1120 |
|
| 1121 } |
|
| 1122 |
|
| 1123 /** Go idle. */ |
|
| 1124 void |
|
| 1125 msim_set_idle(PurpleConnection *gc, int time) |
|
| 1126 { |
|
| 1127 MsimSession *session; |
|
| 1128 PurpleStatus *status; |
|
| 1129 |
|
| 1130 g_return_if_fail(gc != NULL); |
|
| 1131 |
|
| 1132 session = (MsimSession *)gc->proto_data; |
|
| 1133 |
|
| 1134 g_return_if_fail(MSIM_SESSION_VALID(session)); |
|
| 1135 |
|
| 1136 status = purple_account_get_active_status(session->account); |
|
| 1137 |
|
| 1138 if (time == 0) { |
|
| 1139 /* Going back from idle. In msim, idle is mutually exclusive |
|
| 1140 * from the other states (you can only be away or idle, but not |
|
| 1141 * both, for example), so by going non-idle I go back to what |
|
| 1142 * libpurple says I should be. |
|
| 1143 */ |
|
| 1144 msim_set_status(session->account, status); |
|
| 1145 } else { |
|
| 1146 const gchar *message; |
|
| 1147 gchar *stripped; |
|
| 1148 |
|
| 1149 /* Set the idle message to the status message from the real |
|
| 1150 * current status. |
|
| 1151 */ |
|
| 1152 message = purple_status_get_attr_string(status, "message"); |
|
| 1153 if (message != NULL) |
|
| 1154 stripped = purple_markup_strip_html(message); |
|
| 1155 else |
|
| 1156 stripped = g_strdup(""); |
|
| 1157 |
|
| 1158 /* msim doesn't support idle time, so just go idle */ |
|
| 1159 msim_set_status_code(session, MSIM_STATUS_CODE_IDLE, stripped); |
|
| 1160 } |
|
| 1161 } |
|
| 1162 |
|
| 1163 /** Set status using an MSIM_STATUS_CODE_* value. |
|
| 1164 * @param status_code An MSIM_STATUS_CODE_* value. |
|
| 1165 * @param statstring Status string, must be a dynamic string (will be freed by msim_send). |
|
| 1166 */ |
|
| 1167 static void |
|
| 1168 msim_set_status_code(MsimSession *session, guint status_code, gchar *statstring) |
|
| 1169 { |
|
| 1170 g_return_if_fail(MSIM_SESSION_VALID(session)); |
|
| 1171 g_return_if_fail(statstring != NULL); |
|
| 1172 |
|
| 1173 purple_debug_info("msim", "msim_set_status_code: going to set status to code=%d,str=%s\n", |
|
| 1174 status_code, statstring); |
|
| 1175 |
|
| 1176 if (!msim_send(session, |
|
| 1177 "status", MSIM_TYPE_INTEGER, status_code, |
|
| 1178 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 1179 "statstring", MSIM_TYPE_STRING, statstring, |
|
| 1180 "locstring", MSIM_TYPE_STRING, g_strdup(""), |
|
| 1181 NULL)) |
|
| 1182 { |
|
| 1183 purple_debug_info("msim", "msim_set_status: failed to set status\n"); |
|
| 1184 } |
|
| 1185 |
|
| 1186 } |
|
| 1187 |
|
| 1188 /** After a uid is resolved to username, tag it with the username and submit for processing. |
|
| 1189 * |
|
| 1190 * @param session |
|
| 1191 * @param userinfo Response messsage to resolving request. |
|
| 1192 * @param data MsimMessage *, the message to attach information to. |
|
| 1193 */ |
|
| 1194 static void |
|
| 1195 msim_incoming_resolved(MsimSession *session, MsimMessage *userinfo, |
|
| 1196 gpointer data) |
|
| 1197 { |
|
| 1198 gchar *username; |
|
| 1199 MsimMessage *msg, *body; |
|
| 1200 |
|
| 1201 g_return_if_fail(MSIM_SESSION_VALID(session)); |
|
| 1202 g_return_if_fail(userinfo != NULL); |
|
| 1203 |
|
| 1204 body = msim_msg_get_dictionary(userinfo, "body"); |
|
| 1205 g_return_if_fail(body != NULL); |
|
| 1206 |
|
| 1207 username = msim_msg_get_string(body, "UserName"); |
|
| 1208 g_return_if_fail(username != NULL); |
|
| 1209 /* Note: username will be owned by 'msg' below. */ |
|
| 1210 |
|
| 1211 msg = (MsimMessage *)data; |
|
| 1212 g_return_if_fail(msg != NULL); |
|
| 1213 |
|
| 1214 /* TODO: more elegant solution than below. attach whole message? */ |
|
| 1215 /* Special elements name beginning with '_', we'll use internally within the |
|
| 1216 * program (did not come directly from the wire). */ |
|
| 1217 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, username); /* This makes 'msg' the owner of 'username' */ |
|
| 1218 |
|
| 1219 /* TODO: attach more useful information, like ImageURL */ |
|
| 1220 |
|
| 1221 msim_process(session, msg); |
|
| 1222 |
|
| 1223 /* TODO: Free copy cloned from msim_preprocess_incoming(). */ |
|
| 1224 /* msim_msg_free(msg); */ |
|
| 1225 msim_msg_free(body); |
|
| 1226 } |
|
| 1227 |
|
| 1228 /* Lookup a username by userid, from buddy list. |
|
| 1229 * |
|
| 1230 * @param wanted_uid |
|
| 1231 * |
|
| 1232 * @return Username of wanted_uid, if on blist, or NULL. |
|
| 1233 * This is a static string, so don't free it. Copy it if needed. |
|
| 1234 * |
|
| 1235 */ |
|
| 1236 static const gchar * |
|
| 1237 msim_uid2username_from_blist(PurpleAccount *account, guint wanted_uid) |
|
| 1238 { |
|
| 1239 GSList *buddies, *cur; |
|
| 1240 const gchar *ret; |
|
| 1241 |
|
| 1242 buddies = purple_find_buddies(account, NULL); |
|
| 1243 |
|
| 1244 if (!buddies) |
|
| 1245 { |
|
| 1246 purple_debug_info("msim", "msim_uid2username_from_blist: no buddies?\n"); |
|
| 1247 return NULL; |
|
| 1248 } |
|
| 1249 |
|
| 1250 ret = NULL; |
|
| 1251 |
|
| 1252 for (cur = buddies; cur != NULL; cur = g_slist_next(cur)) |
|
| 1253 { |
|
| 1254 PurpleBuddy *buddy; |
|
| 1255 guint uid; |
|
| 1256 const gchar *name; |
|
| 1257 |
|
| 1258 /* See finch/gnthistory.c */ |
|
| 1259 buddy = cur->data; |
|
| 1260 |
|
| 1261 uid = purple_blist_node_get_int(&buddy->node, "UserID"); |
|
| 1262 name = purple_buddy_get_name(buddy); |
|
| 1263 |
|
| 1264 if (uid == wanted_uid) |
|
| 1265 { |
|
| 1266 ret = name; |
|
| 1267 break; |
|
| 1268 } |
|
| 1269 } |
|
| 1270 |
|
| 1271 g_slist_free(buddies); |
|
| 1272 return ret; |
|
| 1273 } |
|
| 1274 |
|
| 1275 /** Preprocess incoming messages, resolving as needed, calling msim_process() when ready to process. |
|
| 1276 * |
|
| 1277 * @param session |
|
| 1278 * @param msg MsimMessage *, freed by caller. |
|
| 1279 */ |
|
| 1280 static gboolean |
|
| 1281 msim_preprocess_incoming(MsimSession *session, MsimMessage *msg) |
|
| 1282 { |
|
| 1283 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
| 1284 g_return_val_if_fail(msg != NULL, FALSE); |
|
| 1285 |
|
| 1286 if (msim_msg_get(msg, "bm") && msim_msg_get(msg, "f")) { |
|
| 1287 guint uid; |
|
| 1288 const gchar *username; |
|
| 1289 |
|
| 1290 /* 'f' = userid message is from, in buddy messages */ |
|
| 1291 uid = msim_msg_get_integer(msg, "f"); |
|
| 1292 |
|
| 1293 username = msim_uid2username_from_blist(session->account, uid); |
|
| 1294 |
|
| 1295 if (username) { |
|
| 1296 /* Know username already, use it. */ |
|
| 1297 purple_debug_info("msim", "msim_preprocess_incoming: tagging with _username=%s\n", |
|
| 1298 username); |
|
| 1299 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username)); |
|
| 1300 return msim_process(session, msg); |
|
| 1301 |
|
| 1302 } else { |
|
| 1303 gchar *from; |
|
| 1304 |
|
| 1305 /* Send lookup request. */ |
|
| 1306 /* XXX: where is msim_msg_get_string() freed? make _strdup and _nonstrdup. */ |
|
| 1307 purple_debug_info("msim", "msim_incoming: sending lookup, setting up callback\n"); |
|
| 1308 from = msim_msg_get_string(msg, "f"); |
|
| 1309 msim_lookup_user(session, from, msim_incoming_resolved, msim_msg_clone(msg)); |
|
| 1310 g_free(from); |
|
| 1311 |
|
| 1312 /* indeterminate */ |
|
| 1313 return TRUE; |
|
| 1314 } |
|
| 1315 } else { |
|
| 1316 /* Nothing to resolve - send directly to processing. */ |
|
| 1317 return msim_process(session, msg); |
|
| 1318 } |
|
| 1319 } |
|
| 1320 |
|
| 1321 #ifdef MSIM_USE_KEEPALIVE |
|
| 1322 /** Check if the connection is still alive, based on last communication. */ |
|
| 1323 static gboolean |
|
| 1324 msim_check_alive(gpointer data) |
|
| 1325 { |
|
| 1326 MsimSession *session; |
|
| 1327 time_t delta; |
|
| 1328 |
|
| 1329 session = (MsimSession *)data; |
|
| 1330 |
|
| 1331 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
| 1332 |
|
| 1333 delta = time(NULL) - session->last_comm; |
|
| 1334 |
|
| 1335 /* purple_debug_info("msim", "msim_check_alive: delta=%d\n", delta); */ |
|
| 1336 if (delta >= MSIM_KEEPALIVE_INTERVAL) { |
|
| 1337 purple_debug_info("msim", |
|
| 1338 "msim_check_alive: %zu > interval of %d, presumed dead\n", |
|
| 1339 delta, MSIM_KEEPALIVE_INTERVAL); |
|
| 1340 purple_connection_error_reason(session->gc, |
|
| 1341 PURPLE_CONNECTION_ERROR_NETWORK_ERROR, |
|
| 1342 _("Lost connection with server")); |
|
| 1343 |
|
| 1344 return FALSE; |
|
| 1345 } |
|
| 1346 |
|
| 1347 return TRUE; |
|
| 1348 } |
|
| 1349 #endif |
|
| 1350 |
|
| 1351 /** Handle mail reply checks. */ |
|
| 1352 static void |
|
| 1353 msim_check_inbox_cb(MsimSession *session, MsimMessage *reply, gpointer data) |
|
| 1354 { |
|
| 1355 MsimMessage *body; |
|
| 1356 guint old_inbox_status; |
|
| 1357 guint i, n; |
|
| 1358 const gchar *froms[5], *tos[5], *urls[5], *subjects[5]; |
|
| 1359 |
|
| 1360 /* Information for each new inbox message type. */ |
|
| 1361 static struct |
|
| 1362 { |
|
| 1363 const gchar *key; |
|
| 1364 guint bit; |
|
| 1365 const gchar *url; |
|
| 1366 const gchar *text; |
|
| 1367 } message_types[] = { |
|
| 1368 { "Mail", MSIM_INBOX_MAIL, "http://messaging.myspace.com/index.cfm?fuseaction=mail.inbox", NULL }, |
|
| 1369 { "BlogComment", MSIM_INBOX_BLOG_COMMENT, "http://blog.myspace.com/index.cfm?fuseaction=blog", NULL }, |
|
| 1370 { "ProfileComment", MSIM_INBOX_PROFILE_COMMENT, "http://home.myspace.com/index.cfm?fuseaction=user", NULL }, |
|
| 1371 { "FriendRequest", MSIM_INBOX_FRIEND_REQUEST, "http://messaging.myspace.com/index.cfm?fuseaction=mail.friendRequests", NULL }, |
|
| 1372 { "PictureComment", MSIM_INBOX_PICTURE_COMMENT, "http://home.myspace.com/index.cfm?fuseaction=user", NULL } |
|
| 1373 }; |
|
| 1374 |
|
| 1375 /* Can't write _()'d strings in array initializers. Workaround. */ |
|
| 1376 message_types[0].text = _("New mail messages"); |
|
| 1377 message_types[1].text = _("New blog comments"); |
|
| 1378 message_types[2].text = _("New profile comments"); |
|
| 1379 message_types[3].text = _("New friend requests!"); |
|
| 1380 message_types[4].text = _("New picture comments"); |
|
| 1381 |
|
| 1382 g_return_if_fail(reply != NULL); |
|
| 1383 |
|
| 1384 msim_msg_dump("msim_check_inbox_cb: reply=%s\n", reply); |
|
| 1385 |
|
| 1386 body = msim_msg_get_dictionary(reply, "body"); |
|
| 1387 |
|
| 1388 if (body == NULL) |
|
| 1389 return; |
|
| 1390 |
|
| 1391 old_inbox_status = session->inbox_status; |
|
| 1392 |
|
| 1393 n = 0; |
|
| 1394 |
|
| 1395 for (i = 0; i < sizeof(message_types) / sizeof(message_types[0]); ++i) { |
|
| 1396 const gchar *key; |
|
| 1397 guint bit; |
|
| 1398 |
|
| 1399 key = message_types[i].key; |
|
| 1400 bit = message_types[i].bit; |
|
| 1401 |
|
| 1402 if (msim_msg_get(body, key)) { |
|
| 1403 /* Notify only on when _changes_ from no mail -> has mail |
|
| 1404 * (edge triggered) */ |
|
| 1405 if (!(session->inbox_status & bit)) { |
|
| 1406 purple_debug_info("msim", "msim_check_inbox_cb: got %s, at %d\n", |
|
| 1407 key ? key : "(NULL)", n); |
|
| 1408 |
|
| 1409 subjects[n] = message_types[i].text; |
|
| 1410 froms[n] = _("MySpace"); |
|
| 1411 tos[n] = session->username; |
|
| 1412 /* TODO: append token, web challenge, so automatically logs in. |
|
| 1413 * Would also need to free strings because they won't be static |
|
| 1414 */ |
|
| 1415 urls[n] = message_types[i].url; |
|
| 1416 |
|
| 1417 ++n; |
|
| 1418 } else { |
|
| 1419 purple_debug_info("msim", |
|
| 1420 "msim_check_inbox_cb: already notified of %s\n", |
|
| 1421 key ? key : "(NULL)"); |
|
| 1422 } |
|
| 1423 |
|
| 1424 session->inbox_status |= bit; |
|
| 1425 } |
|
| 1426 } |
|
| 1427 |
|
| 1428 if (n) { |
|
| 1429 purple_debug_info("msim", |
|
| 1430 "msim_check_inbox_cb: notifying of %d\n", n); |
|
| 1431 |
|
| 1432 /* TODO: free strings with callback _if_ change to dynamic (w/ token) */ |
|
| 1433 purple_notify_emails(session->gc, /* handle */ |
|
| 1434 n, /* count */ |
|
| 1435 TRUE, /* detailed */ |
|
| 1436 subjects, froms, tos, urls, |
|
| 1437 NULL, /* PurpleNotifyCloseCallback cb */ |
|
| 1438 NULL); /* gpointer user_data */ |
|
| 1439 |
|
| 1440 } |
|
| 1441 |
|
| 1442 msim_msg_free(body); |
|
| 1443 } |
|
| 1444 |
|
| 1445 /* Send request to check if there is new mail. */ |
|
| 1446 static gboolean |
|
| 1447 msim_check_inbox(gpointer data) |
|
| 1448 { |
|
| 1449 MsimSession *session; |
|
| 1450 |
|
| 1451 session = (MsimSession *)data; |
|
| 1452 |
|
| 1453 if (!MSIM_SESSION_VALID(session)) { |
|
| 1454 purple_debug_info("msim", "msim_check_inbox: session invalid, stopping the mail check.\n"); |
|
| 1455 return FALSE; |
|
| 1456 } |
|
| 1457 |
|
| 1458 purple_debug_info("msim", "msim_check_inbox: checking mail\n"); |
|
| 1459 g_return_val_if_fail(msim_send(session, |
|
| 1460 "persist", MSIM_TYPE_INTEGER, 1, |
|
| 1461 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 1462 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_GET, |
|
| 1463 "dsn", MSIM_TYPE_INTEGER, MG_CHECK_MAIL_DSN, |
|
| 1464 "lid", MSIM_TYPE_INTEGER, MG_CHECK_MAIL_LID, |
|
| 1465 "uid", MSIM_TYPE_INTEGER, session->userid, |
|
| 1466 "rid", MSIM_TYPE_INTEGER, |
|
| 1467 msim_new_reply_callback(session, msim_check_inbox_cb, NULL), |
|
| 1468 "body", MSIM_TYPE_STRING, g_strdup(""), |
|
| 1469 NULL), TRUE); |
|
| 1470 |
|
| 1471 /* Always return true, so that we keep checking for mail. */ |
|
| 1472 return TRUE; |
|
| 1473 } |
|
| 1474 |
|
| 1475 #ifdef MSIM_CHECK_NEWER_VERSION |
|
| 1476 /** Callback for when a currentversion.txt has been downloaded. */ |
|
| 1477 static void |
|
| 1478 msim_check_newer_version_cb(PurpleUtilFetchUrlData *url_data, |
|
| 1479 gpointer user_data, |
|
| 1480 const gchar *url_text, |
|
| 1481 gsize len, |
|
| 1482 const gchar *error_message) |
|
| 1483 { |
|
| 1484 GKeyFile *keyfile; |
|
| 1485 GError *error; |
|
| 1486 GString *data; |
|
| 1487 gchar *newest_filever; |
|
| 1488 |
|
| 1489 if (!url_text) { |
|
| 1490 purple_debug_info("msim_check_newer_version_cb", |
|
| 1491 "got error: %s\n", error_message); |
|
| 1492 return; |
|
| 1493 } |
|
| 1494 |
|
| 1495 purple_debug_info("msim_check_newer_version_cb", |
|
| 1496 "url_text=%s\n", url_text ? url_text : "(NULL)"); |
|
| 1497 |
|
| 1498 /* Prepend [group] so that GKeyFile can parse it (requires a group). */ |
|
| 1499 data = g_string_new(url_text); |
|
| 1500 purple_debug_info("msim", "data=%s\n", data->str |
|
| 1501 ? data->str : "(NULL)"); |
|
| 1502 data = g_string_prepend(data, "[group]\n"); |
|
| 1503 |
|
| 1504 purple_debug_info("msim", "data=%s\n", data->str |
|
| 1505 ? data->str : "(NULL)"); |
|
| 1506 |
|
| 1507 /* url_text is variable=data\n...†*/ |
|
| 1508 |
|
| 1509 /* Check FILEVER, 1.0.716.0. 716 is build, MSIM_CLIENT_VERSION */ |
|
| 1510 /* New (english) version can be downloaded from SETUPURL+SETUPFILE */ |
|
| 1511 |
|
| 1512 error = NULL; |
|
| 1513 keyfile = g_key_file_new(); |
|
| 1514 |
|
| 1515 /* Default list seperator is ;, but currentversion.txt doesn't have |
|
| 1516 * these, so set to an unused character to avoid parsing problems. */ |
|
| 1517 g_key_file_set_list_separator(keyfile, '\0'); |
|
| 1518 |
|
| 1519 g_key_file_load_from_data(keyfile, data->str, data->len, |
|
| 1520 G_KEY_FILE_NONE, &error); |
|
| 1521 g_string_free(data, TRUE); |
|
| 1522 |
|
| 1523 if (error != NULL) { |
|
| 1524 purple_debug_info("msim_check_newer_version_cb", |
|
| 1525 "couldn't parse, error: %d %d %s\n", |
|
| 1526 error->domain, error->code, error->message); |
|
| 1527 g_error_free(error); |
|
| 1528 return; |
|
| 1529 } |
|
| 1530 |
|
| 1531 gchar **ks; |
|
| 1532 guint n; |
|
| 1533 ks = g_key_file_get_keys(keyfile, "group", &n, NULL); |
|
| 1534 purple_debug_info("msim", "n=%d\n", n); |
|
| 1535 guint i; |
|
| 1536 for (i = 0; ks[i] != NULL; ++i) |
|
| 1537 { |
|
| 1538 purple_debug_info("msim", "%d=%s\n", i, ks[i]); |
|
| 1539 } |
|
| 1540 |
|
| 1541 newest_filever = g_key_file_get_string(keyfile, "group", |
|
| 1542 "FILEVER", &error); |
|
| 1543 |
|
| 1544 purple_debug_info("msim_check_newer_version_cb", |
|
| 1545 "newest filever: %s\n", newest_filever ? |
|
| 1546 newest_filever : "(NULL)"); |
|
| 1547 if (error != NULL) { |
|
| 1548 purple_debug_info("msim_check_newer_version_cb", |
|
| 1549 "error: %d %d %s\n", |
|
| 1550 error->domain, error->code, error->message); |
|
| 1551 g_error_free(error); |
|
| 1552 } |
|
| 1553 |
|
| 1554 g_key_file_free(keyfile); |
|
| 1555 |
|
| 1556 exit(0); |
|
| 1557 } |
|
| 1558 #endif |
|
| 1559 |
|
| 1560 /** Called when the session key arrives to check whether the user |
|
| 1561 * has a username, and set one if desired. */ |
|
| 1562 static gboolean |
|
| 1563 msim_is_username_set(MsimSession *session, MsimMessage *msg) |
|
| 1564 { |
|
| 1565 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
| 1566 g_return_val_if_fail(msg != NULL, FALSE); |
|
| 1567 g_return_val_if_fail(session->gc != NULL, FALSE); |
|
| 1568 |
|
| 1569 session->sesskey = msim_msg_get_integer(msg, "sesskey"); |
|
| 1570 purple_debug_info("msim", "SESSKEY=<%d>\n", session->sesskey); |
|
| 1571 |
|
| 1572 /* What is proof? Used to be uid, but now is 52 base64'd bytes... */ |
|
| 1573 |
|
| 1574 /* Comes with: proof,profileid,userid,uniquenick -- all same values |
|
| 1575 * some of the time, but can vary. This is our own user ID. */ |
|
| 1576 session->userid = msim_msg_get_integer(msg, "userid"); |
|
| 1577 |
|
| 1578 /* Save uid to account so this account can be looked up by uid. */ |
|
| 1579 purple_account_set_int(session->account, "uid", session->userid); |
|
| 1580 |
|
| 1581 /* Not sure what profileid is used for. */ |
|
| 1582 if (msim_msg_get_integer(msg, "profileid") != session->userid) { |
|
| 1583 msim_unrecognized(session, msg, |
|
| 1584 "Profile ID didn't match user ID, don't know why"); |
|
| 1585 } |
|
| 1586 |
|
| 1587 /* We now know are our own username, only after we're logged in.. |
|
| 1588 * which is weird, but happens because you login with your email |
|
| 1589 * address and not username. Will be freed in msim_session_destroy(). */ |
|
| 1590 session->username = msim_msg_get_string(msg, "uniquenick"); |
|
| 1591 |
|
| 1592 /* If user lacks a username, help them get one. */ |
|
| 1593 if (msim_msg_get_integer(msg, "uniquenick") == session->userid) { |
|
| 1594 purple_debug_info("msim_is_username_set", "no username is set\n"); |
|
| 1595 purple_request_yes_no(session->gc, |
|
| 1596 _("MySpaceIM - No Username Set"), |
|
| 1597 _("You appear to have no MySpace username."), |
|
| 1598 _("Would you like to set one now? (Note: THIS CANNOT BE CHANGED!)"), |
|
| 1599 0, |
|
| 1600 session->account, |
|
| 1601 NULL, |
|
| 1602 NULL, |
|
| 1603 session->gc, |
|
| 1604 G_CALLBACK(msim_set_username_cb), |
|
| 1605 G_CALLBACK(msim_do_not_set_username_cb)); |
|
| 1606 purple_debug_info("msim_is_username_set","'username not set' alert prompted\n"); |
|
| 1607 return FALSE; |
|
| 1608 } |
|
| 1609 return TRUE; |
|
| 1610 } |
|
| 1611 |
|
| 1612 /** Called after username is set, if necessary and we're open for business. */ |
|
| 1613 gboolean msim_we_are_logged_on(MsimSession *session) |
|
| 1614 { |
|
| 1615 MsimMessage *body; |
|
| 1616 |
|
| 1617 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
| 1618 |
|
| 1619 /* The session is now set up, ready to be connected. This emits the |
|
| 1620 * signedOn signal, so clients can now do anything with msimprpl, and |
|
| 1621 * we're ready for it (session key, userid, username all setup). */ |
|
| 1622 purple_connection_update_progress(session->gc, _("Connected"), 3, 4); |
|
| 1623 purple_connection_set_state(session->gc, PURPLE_CONNECTED); |
|
| 1624 |
|
| 1625 /* Set display name to username (otherwise will show email address) */ |
|
| 1626 purple_connection_set_display_name(session->gc, session->username); |
|
| 1627 |
|
| 1628 body = msim_msg_new( |
|
| 1629 "UserID", MSIM_TYPE_INTEGER, session->userid, |
|
| 1630 NULL); |
|
| 1631 |
|
| 1632 /* Request IM info about ourself. */ |
|
| 1633 msim_send(session, |
|
| 1634 "persist", MSIM_TYPE_STRING, g_strdup("persist"), |
|
| 1635 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 1636 "dsn", MSIM_TYPE_INTEGER, MG_OWN_MYSPACE_INFO_DSN, |
|
| 1637 "uid", MSIM_TYPE_INTEGER, session->userid, |
|
| 1638 "lid", MSIM_TYPE_INTEGER, MG_OWN_MYSPACE_INFO_LID, |
|
| 1639 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
|
| 1640 "body", MSIM_TYPE_DICTIONARY, body, |
|
| 1641 NULL); |
|
| 1642 |
|
| 1643 /* Request MySpace info about ourself. */ |
|
| 1644 msim_send(session, |
|
| 1645 "persist", MSIM_TYPE_STRING, g_strdup("persist"), |
|
| 1646 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 1647 "dsn", MSIM_TYPE_INTEGER, MG_OWN_IM_INFO_DSN, |
|
| 1648 "uid", MSIM_TYPE_INTEGER, session->userid, |
|
| 1649 "lid", MSIM_TYPE_INTEGER, MG_OWN_IM_INFO_LID, |
|
| 1650 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
|
| 1651 "body", MSIM_TYPE_STRING, g_strdup(""), |
|
| 1652 NULL); |
|
| 1653 |
|
| 1654 /* TODO: set options (persist cmd=514,dsn=1,lid=10) */ |
|
| 1655 /* TODO: set blocklist */ |
|
| 1656 |
|
| 1657 /* Notify servers of our current status. */ |
|
| 1658 purple_debug_info("msim", "msim_we_are_logged_on: notifying servers of status\n"); |
|
| 1659 msim_set_status(session->account, |
|
| 1660 purple_account_get_active_status(session->account)); |
|
| 1661 |
|
| 1662 /* TODO: setinfo */ |
|
| 1663 /* |
|
| 1664 body = msim_msg_new( |
|
| 1665 "TotalFriends", MSIM_TYPE_INTEGER, 666, |
|
| 1666 NULL); |
|
| 1667 msim_send(session, |
|
| 1668 "setinfo", MSIM_TYPE_BOOLEAN, TRUE, |
|
| 1669 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 1670 "info", MSIM_TYPE_DICTIONARY, body, |
|
| 1671 NULL); |
|
| 1672 */ |
|
| 1673 |
|
| 1674 /* Disable due to problems with timeouts. TODO: fix. */ |
|
| 1675 #ifdef MSIM_USE_KEEPALIVE |
|
| 1676 purple_timeout_add(MSIM_KEEPALIVE_INTERVAL_CHECK, |
|
| 1677 (GSourceFunc)msim_check_alive, session); |
|
| 1678 #endif |
|
| 1679 |
|
| 1680 /* Check mail if they want to. */ |
|
| 1681 if (purple_account_get_check_mail(session->account)) { |
|
| 1682 session->inbox_handle = purple_timeout_add(MSIM_MAIL_INTERVAL_CHECK, |
|
| 1683 (GSourceFunc)msim_check_inbox, session); |
|
| 1684 msim_check_inbox(session); |
|
| 1685 } |
|
| 1686 |
|
| 1687 msim_get_contact_list(session, MSIM_CONTACT_LIST_INITIAL_FRIENDS); |
|
| 1688 |
|
| 1689 return TRUE; |
|
| 1690 } |
|
| 1691 |
|
| 1692 /** |
|
| 1693 * Process a message. |
|
| 1694 * |
1839 * |
| 1695 * @param session |
1840 * @param session |
| 1696 * @param msg A message from the server, ready for processing (possibly with resolved username information attached). Caller frees. |
1841 * @param msg A message from the server, ready for processing (possibly with resolved username information attached). Caller frees. |
| 1697 * |
1842 * |
| 1698 * @return TRUE if successful. FALSE if processing failed. |
1843 * @return TRUE if successful. FALSE if processing failed. |
| 1699 */ |
1844 */ |
| 1700 static gboolean |
1845 static gboolean |
| 1701 msim_process(MsimSession *session, MsimMessage *msg) |
1846 msim_process(MsimSession *session, MsimMessage *msg) |
| 1702 { |
1847 { |
| 1703 g_return_val_if_fail(session != NULL, FALSE); |
1848 g_return_val_if_fail(session != NULL, FALSE); |
| 1704 g_return_val_if_fail(msg != NULL, FALSE); |
1849 g_return_val_if_fail(msg != NULL, FALSE); |
| 1705 |
1850 |
| 1730 msim_unrecognized(session, msg, "in msim_process"); |
1875 msim_unrecognized(session, msg, "in msim_process"); |
| 1731 return FALSE; |
1876 return FALSE; |
| 1732 } |
1877 } |
| 1733 } |
1878 } |
| 1734 |
1879 |
| 1735 /** Process the initial server information from the server. */ |
1880 /** |
| |
1881 * After a uid is resolved to username, tag it with the username and submit for processing. |
| |
1882 * |
| |
1883 * @param session |
| |
1884 * @param userinfo Response messsage to resolving request. |
| |
1885 * @param data MsimMessage *, the message to attach information to. |
| |
1886 */ |
| |
1887 static void |
| |
1888 msim_incoming_resolved(MsimSession *session, MsimMessage *userinfo, |
| |
1889 gpointer data) |
| |
1890 { |
| |
1891 gchar *username; |
| |
1892 MsimMessage *msg, *body; |
| |
1893 |
| |
1894 g_return_if_fail(MSIM_SESSION_VALID(session)); |
| |
1895 g_return_if_fail(userinfo != NULL); |
| |
1896 |
| |
1897 body = msim_msg_get_dictionary(userinfo, "body"); |
| |
1898 g_return_if_fail(body != NULL); |
| |
1899 |
| |
1900 username = msim_msg_get_string(body, "UserName"); |
| |
1901 g_return_if_fail(username != NULL); |
| |
1902 /* Note: username will be owned by 'msg' below. */ |
| |
1903 |
| |
1904 msg = (MsimMessage *)data; |
| |
1905 g_return_if_fail(msg != NULL); |
| |
1906 |
| |
1907 /* TODO: more elegant solution than below. attach whole message? */ |
| |
1908 /* Special elements name beginning with '_', we'll use internally within the |
| |
1909 * program (did not come directly from the wire). */ |
| |
1910 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, username); /* This makes 'msg' the owner of 'username' */ |
| |
1911 |
| |
1912 /* TODO: attach more useful information, like ImageURL */ |
| |
1913 |
| |
1914 msim_process(session, msg); |
| |
1915 |
| |
1916 /* TODO: Free copy cloned from msim_preprocess_incoming(). */ |
| |
1917 /* msim_msg_free(msg); */ |
| |
1918 msim_msg_free(body); |
| |
1919 } |
| |
1920 |
| |
1921 /** |
| |
1922 * Preprocess incoming messages, resolving as needed, calling |
| |
1923 * msim_process() when ready to process. |
| |
1924 * |
| |
1925 * @param session |
| |
1926 * @param msg MsimMessage *, freed by caller. |
| |
1927 */ |
| 1736 static gboolean |
1928 static gboolean |
| 1737 msim_process_server_info(MsimSession *session, MsimMessage *msg) |
1929 msim_preprocess_incoming(MsimSession *session, MsimMessage *msg) |
| 1738 { |
1930 { |
| 1739 MsimMessage *body; |
|
| 1740 |
|
| 1741 body = msim_msg_get_dictionary(msg, "body"); |
|
| 1742 g_return_val_if_fail(body != NULL, FALSE); |
|
| 1743 |
|
| 1744 /* Example body: |
|
| 1745 AdUnitRefreshInterval=10. |
|
| 1746 AlertPollInterval=360. |
|
| 1747 AllowChatRoomEmoticonSharing=False. |
|
| 1748 ChatRoomUserIDs=78744676;163733130;1300326231;123521495;142663391. |
|
| 1749 CurClientVersion=673. |
|
| 1750 EnableIMBrowse=True. |
|
| 1751 EnableIMStuffAvatars=False. |
|
| 1752 EnableIMStuffZaps=False. |
|
| 1753 MaxAddAllFriends=100. |
|
| 1754 MaxContacts=1000. |
|
| 1755 MinClientVersion=594. |
|
| 1756 MySpaceIM_ENGLISH=78744676. |
|
| 1757 MySpaceNowTimer=720. |
|
| 1758 PersistenceDataTimeout=900. |
|
| 1759 UseWebChallenge=1. |
|
| 1760 WebTicketGoHome=False |
|
| 1761 |
|
| 1762 Anything useful? TODO: use what is useful, and use it. |
|
| 1763 */ |
|
| 1764 purple_debug_info("msim_process_server_info", |
|
| 1765 "maximum contacts: %d\n", |
|
| 1766 msim_msg_get_integer(body, "MaxContacts")); |
|
| 1767 |
|
| 1768 session->server_info = body; |
|
| 1769 /* session->server_info freed in msim_session_destroy */ |
|
| 1770 |
|
| 1771 return TRUE; |
|
| 1772 } |
|
| 1773 |
|
| 1774 /** Process a web challenge, used to login to the web site. */ |
|
| 1775 static gboolean |
|
| 1776 msim_web_challenge(MsimSession *session, MsimMessage *msg) |
|
| 1777 { |
|
| 1778 /* TODO: web challenge, store token. #2659. */ |
|
| 1779 return FALSE; |
|
| 1780 } |
|
| 1781 |
|
| 1782 /** |
|
| 1783 * Process a persistance message reply from the server. |
|
| 1784 * |
|
| 1785 * @param session |
|
| 1786 * @param msg Message reply from server. |
|
| 1787 * |
|
| 1788 * @return TRUE if successful. |
|
| 1789 * |
|
| 1790 * msim_lookup_user sets callback for here |
|
| 1791 */ |
|
| 1792 static gboolean |
|
| 1793 msim_process_reply(MsimSession *session, MsimMessage *msg) |
|
| 1794 { |
|
| 1795 MSIM_USER_LOOKUP_CB cb; |
|
| 1796 gpointer data; |
|
| 1797 guint rid, cmd, dsn, lid; |
|
| 1798 |
|
| 1799 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
1931 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| 1800 g_return_val_if_fail(msg != NULL, FALSE); |
1932 g_return_val_if_fail(msg != NULL, FALSE); |
| 1801 |
1933 |
| 1802 msim_store_user_info(session, msg, NULL); |
1934 if (msim_msg_get(msg, "bm") && msim_msg_get(msg, "f")) { |
| 1803 |
1935 guint uid; |
| 1804 rid = msim_msg_get_integer(msg, "rid"); |
1936 const gchar *username; |
| 1805 cmd = msim_msg_get_integer(msg, "cmd"); |
1937 |
| 1806 dsn = msim_msg_get_integer(msg, "dsn"); |
1938 /* 'f' = userid message is from, in buddy messages */ |
| 1807 lid = msim_msg_get_integer(msg, "lid"); |
1939 uid = msim_msg_get_integer(msg, "f"); |
| 1808 |
1940 |
| 1809 /* Unsolicited messages */ |
1941 username = msim_uid2username_from_blist(session->account, uid); |
| 1810 if (cmd == (MSIM_CMD_BIT_REPLY | MSIM_CMD_GET)) { |
1942 |
| 1811 if (dsn == MG_SERVER_INFO_DSN && lid == MG_SERVER_INFO_LID) { |
1943 if (username) { |
| 1812 return msim_process_server_info(session, msg); |
1944 /* Know username already, use it. */ |
| 1813 } else if (dsn == MG_WEB_CHALLENGE_DSN && lid == MG_WEB_CHALLENGE_LID) { |
1945 purple_debug_info("msim", "msim_preprocess_incoming: tagging with _username=%s\n", |
| 1814 return msim_web_challenge(session, msg); |
1946 username); |
| 1815 } |
1947 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username)); |
| 1816 } |
1948 return msim_process(session, msg); |
| 1817 |
1949 |
| 1818 /* If a callback is registered for this userid lookup, call it. */ |
|
| 1819 cb = g_hash_table_lookup(session->user_lookup_cb, GUINT_TO_POINTER(rid)); |
|
| 1820 data = g_hash_table_lookup(session->user_lookup_cb_data, GUINT_TO_POINTER(rid)); |
|
| 1821 |
|
| 1822 if (cb) { |
|
| 1823 purple_debug_info("msim", "msim_process_reply: calling callback now\n"); |
|
| 1824 msim_msg_dump("for msg=%s\n", msg); |
|
| 1825 /* Clone message, so that the callback 'cb' can use it (needs to free it also). */ |
|
| 1826 cb(session, msim_msg_clone(msg), data); |
|
| 1827 g_hash_table_remove(session->user_lookup_cb, GUINT_TO_POINTER(rid)); |
|
| 1828 g_hash_table_remove(session->user_lookup_cb_data, GUINT_TO_POINTER(rid)); |
|
| 1829 } else { |
|
| 1830 purple_debug_info("msim", |
|
| 1831 "msim_process_reply: no callback for rid %d\n", rid); |
|
| 1832 } |
|
| 1833 |
|
| 1834 return TRUE; |
|
| 1835 } |
|
| 1836 |
|
| 1837 /** |
|
| 1838 * Handle an error from the server. |
|
| 1839 * |
|
| 1840 * @param session |
|
| 1841 * @param msg The message. |
|
| 1842 * |
|
| 1843 * @return TRUE if successfully reported error. |
|
| 1844 */ |
|
| 1845 static gboolean |
|
| 1846 msim_error(MsimSession *session, MsimMessage *msg) |
|
| 1847 { |
|
| 1848 gchar *errmsg, *full_errmsg; |
|
| 1849 guint err; |
|
| 1850 |
|
| 1851 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
| 1852 g_return_val_if_fail(msg != NULL, FALSE); |
|
| 1853 |
|
| 1854 err = msim_msg_get_integer(msg, "err"); |
|
| 1855 errmsg = msim_msg_get_string(msg, "errmsg"); |
|
| 1856 |
|
| 1857 full_errmsg = g_strdup_printf(_("Protocol error, code %d: %s"), err, |
|
| 1858 errmsg ? errmsg : "no 'errmsg' given"); |
|
| 1859 |
|
| 1860 g_free(errmsg); |
|
| 1861 |
|
| 1862 purple_debug_info("msim", "msim_error (sesskey=%d): %s\n", |
|
| 1863 session->sesskey, full_errmsg); |
|
| 1864 |
|
| 1865 /* Destroy session if fatal. */ |
|
| 1866 if (msim_msg_get(msg, "fatal")) { |
|
| 1867 PurpleConnectionError reason = PURPLE_CONNECTION_ERROR_NETWORK_ERROR; |
|
| 1868 purple_debug_info("msim", "fatal error, closing\n"); |
|
| 1869 |
|
| 1870 switch (err) { |
|
| 1871 case MSIM_ERROR_INCORRECT_PASSWORD: /* Incorrect password */ |
|
| 1872 reason = PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED; |
|
| 1873 if (!purple_account_get_remember_password(session->account)) |
|
| 1874 purple_account_set_password(session->account, NULL); |
|
| 1875 #ifdef MSIM_MAX_PASSWORD_LENGTH |
|
| 1876 if (session->account->password && (strlen(session->account->password) > MSIM_MAX_PASSWORD_LENGTH)) { |
|
| 1877 gchar *suggestion; |
|
| 1878 |
|
| 1879 suggestion = g_strdup_printf(_("%s Your password is " |
|
| 1880 "%d characters, greater than the " |
|
| 1881 "expected maximum length of %d for " |
|
| 1882 "MySpaceIM. Please shorten your " |
|
| 1883 "password at http://profileedit.myspace.com/index.cfm?fuseaction=accountSettings.changePassword and try again."), |
|
| 1884 full_errmsg, (int) |
|
| 1885 strlen(session->account->password), |
|
| 1886 MSIM_MAX_PASSWORD_LENGTH); |
|
| 1887 |
|
| 1888 /* Replace full_errmsg. */ |
|
| 1889 g_free(full_errmsg); |
|
| 1890 full_errmsg = suggestion; |
|
| 1891 } |
|
| 1892 #endif |
|
| 1893 break; |
|
| 1894 case MSIM_ERROR_LOGGED_IN_ELSEWHERE: /* Logged in elsewhere */ |
|
| 1895 reason = PURPLE_CONNECTION_ERROR_NAME_IN_USE; |
|
| 1896 if (!purple_account_get_remember_password(session->account)) |
|
| 1897 purple_account_set_password(session->account, NULL); |
|
| 1898 break; |
|
| 1899 } |
|
| 1900 purple_connection_error_reason (session->gc, reason, full_errmsg); |
|
| 1901 } else { |
|
| 1902 purple_notify_error(session->account, _("MySpaceIM Error"), full_errmsg, NULL); |
|
| 1903 } |
|
| 1904 |
|
| 1905 g_free(full_errmsg); |
|
| 1906 |
|
| 1907 return TRUE; |
|
| 1908 } |
|
| 1909 |
|
| 1910 /** |
|
| 1911 * Process incoming status messages. |
|
| 1912 * |
|
| 1913 * @param session |
|
| 1914 * @param msg Status update message. Caller frees. |
|
| 1915 * |
|
| 1916 * @return TRUE if successful. |
|
| 1917 */ |
|
| 1918 static gboolean |
|
| 1919 msim_incoming_status(MsimSession *session, MsimMessage *msg) |
|
| 1920 { |
|
| 1921 PurpleBuddyList *blist; |
|
| 1922 MsimUser *user; |
|
| 1923 GList *list; |
|
| 1924 gchar *status_headline, *status_headline_escaped; |
|
| 1925 gint status_code, purple_status_code; |
|
| 1926 gchar *username; |
|
| 1927 gchar *unrecognized_msg; |
|
| 1928 |
|
| 1929 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
| 1930 g_return_val_if_fail(msg != NULL, FALSE); |
|
| 1931 |
|
| 1932 msim_msg_dump("msim_status msg=%s\n", msg); |
|
| 1933 |
|
| 1934 /* Helpfully looked up by msim_incoming_resolve() for us. */ |
|
| 1935 username = msim_msg_get_string(msg, "_username"); |
|
| 1936 g_return_val_if_fail(username != NULL, FALSE); |
|
| 1937 |
|
| 1938 { |
|
| 1939 gchar *ss; |
|
| 1940 |
|
| 1941 ss = msim_msg_get_string(msg, "msg"); |
|
| 1942 purple_debug_info("msim", |
|
| 1943 "msim_status: updating status for <%s> to <%s>\n", |
|
| 1944 username, ss ? ss : "(NULL)"); |
|
| 1945 g_free(ss); |
|
| 1946 } |
|
| 1947 |
|
| 1948 /* Example fields: |
|
| 1949 * |s|0|ss|Offline |
|
| 1950 * |s|1|ss|:-)|ls||ip|0|p|0 |
|
| 1951 */ |
|
| 1952 list = msim_msg_get_list(msg, "msg"); |
|
| 1953 |
|
| 1954 status_code = msim_msg_get_integer_from_element(g_list_nth_data(list, MSIM_STATUS_ORDINAL_ONLINE)); |
|
| 1955 purple_debug_info("msim", "msim_status: %s's status code = %d\n", username, status_code); |
|
| 1956 status_headline = msim_msg_get_string_from_element(g_list_nth_data(list, MSIM_STATUS_ORDINAL_HEADLINE)); |
|
| 1957 |
|
| 1958 blist = purple_get_blist(); |
|
| 1959 |
|
| 1960 /* Add buddy if not found. |
|
| 1961 * TODO: Could this be responsible for #3444? */ |
|
| 1962 user = msim_find_user(session, username); |
|
| 1963 if (!user) { |
|
| 1964 PurpleBuddy *buddy; |
|
| 1965 |
|
| 1966 purple_debug_info("msim", |
|
| 1967 "msim_status: making new buddy for %s\n", username); |
|
| 1968 buddy = purple_buddy_new(session->account, username, NULL); |
|
| 1969 purple_blist_add_buddy(buddy, NULL, NULL, NULL); |
|
| 1970 |
|
| 1971 user = msim_get_user_from_buddy(buddy); |
|
| 1972 user->id = msim_msg_get_integer(msg, "f"); |
|
| 1973 |
|
| 1974 /* Keep track of the user ID across sessions */ |
|
| 1975 purple_blist_node_set_int(&buddy->node, "UserID", user->id); |
|
| 1976 |
|
| 1977 msim_store_user_info(session, msg, NULL); |
|
| 1978 } else { |
|
| 1979 purple_debug_info("msim", "msim_status: found buddy %s\n", username); |
|
| 1980 } |
|
| 1981 |
|
| 1982 if (status_headline && strcmp(status_headline, "") != 0) { |
|
| 1983 /* The status headline is plaintext, but libpurple treats it as HTML, |
|
| 1984 * so escape any HTML characters to their entity equivalents. */ |
|
| 1985 status_headline_escaped = g_markup_escape_text(status_headline, strlen(status_headline)); |
|
| 1986 } else { |
|
| 1987 status_headline_escaped = NULL; |
|
| 1988 } |
|
| 1989 |
|
| 1990 g_free(status_headline); |
|
| 1991 |
|
| 1992 if (user->headline) |
|
| 1993 g_free(user->headline); |
|
| 1994 |
|
| 1995 /* don't copy; let the MsimUser own the headline, memory-wise */ |
|
| 1996 user->headline = status_headline_escaped; |
|
| 1997 |
|
| 1998 /* Set user status */ |
|
| 1999 switch (status_code) { |
|
| 2000 case MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN: |
|
| 2001 purple_status_code = PURPLE_STATUS_OFFLINE; |
|
| 2002 break; |
|
| 2003 |
|
| 2004 case MSIM_STATUS_CODE_ONLINE: |
|
| 2005 purple_status_code = PURPLE_STATUS_AVAILABLE; |
|
| 2006 break; |
|
| 2007 |
|
| 2008 case MSIM_STATUS_CODE_AWAY: |
|
| 2009 purple_status_code = PURPLE_STATUS_AWAY; |
|
| 2010 break; |
|
| 2011 |
|
| 2012 case MSIM_STATUS_CODE_IDLE: |
|
| 2013 /* Treat idle as an available status. */ |
|
| 2014 purple_status_code = PURPLE_STATUS_AVAILABLE; |
|
| 2015 break; |
|
| 2016 |
|
| 2017 default: |
|
| 2018 purple_debug_info("msim", "msim_incoming_status for %s, unknown status code %d, treating as available\n", |
|
| 2019 username, status_code); |
|
| 2020 purple_status_code = PURPLE_STATUS_AVAILABLE; |
|
| 2021 |
|
| 2022 unrecognized_msg = g_strdup_printf("msim_incoming_status, unrecognized status code: %d\n", |
|
| 2023 status_code); |
|
| 2024 msim_unrecognized(session, NULL, unrecognized_msg); |
|
| 2025 g_free(unrecognized_msg); |
|
| 2026 |
|
| 2027 } |
|
| 2028 |
|
| 2029 purple_prpl_got_user_status(session->account, username, purple_primitive_get_id_from_type(purple_status_code), NULL); |
|
| 2030 |
|
| 2031 if (status_code == MSIM_STATUS_CODE_IDLE) { |
|
| 2032 purple_debug_info("msim", "msim_status: got idle: %s\n", username); |
|
| 2033 purple_prpl_got_user_idle(session->account, username, TRUE, time(NULL)); |
|
| 2034 } else { |
|
| 2035 /* All other statuses indicate going back to non-idle. */ |
|
| 2036 purple_prpl_got_user_idle(session->account, username, FALSE, time(NULL)); |
|
| 2037 } |
|
| 2038 |
|
| 2039 #ifdef MSIM_SEND_CLIENT_VERSION |
|
| 2040 if (status_code == MSIM_STATUS_CODE_ONLINE) { |
|
| 2041 /* Secretly whisper to unofficial clients our own version as they come online */ |
|
| 2042 msim_send_unofficial_client(session, username); |
|
| 2043 } |
|
| 2044 #endif |
|
| 2045 |
|
| 2046 if (status_code != MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN) { |
|
| 2047 /* Get information when they come online. |
|
| 2048 * TODO: periodically refresh? |
|
| 2049 */ |
|
| 2050 purple_debug_info("msim_incoming_status", "%s came online, looking up\n", username); |
|
| 2051 msim_lookup_user(session, username, NULL, NULL); |
|
| 2052 } |
|
| 2053 |
|
| 2054 g_free(username); |
|
| 2055 msim_msg_list_free(list); |
|
| 2056 |
|
| 2057 return TRUE; |
|
| 2058 } |
|
| 2059 |
|
| 2060 /** Add a buddy to user's buddy list. */ |
|
| 2061 void |
|
| 2062 msim_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) |
|
| 2063 { |
|
| 2064 MsimSession *session; |
|
| 2065 MsimMessage *msg; |
|
| 2066 MsimMessage *msg_persist; |
|
| 2067 MsimMessage *body; |
|
| 2068 |
|
| 2069 session = (MsimSession *)gc->proto_data; |
|
| 2070 purple_debug_info("msim", "msim_add_buddy: want to add %s to %s\n", |
|
| 2071 buddy->name, (group && group->name) ? group->name : "(no group)"); |
|
| 2072 |
|
| 2073 msg = msim_msg_new( |
|
| 2074 "addbuddy", MSIM_TYPE_BOOLEAN, TRUE, |
|
| 2075 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 2076 /* "newprofileid" will be inserted here with uid. */ |
|
| 2077 "reason", MSIM_TYPE_STRING, g_strdup(""), |
|
| 2078 NULL); |
|
| 2079 |
|
| 2080 if (!msim_postprocess_outgoing(session, msg, buddy->name, "newprofileid", "reason")) { |
|
| 2081 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("'addbuddy' command failed.")); |
|
| 2082 msim_msg_free(msg); |
|
| 2083 return; |
|
| 2084 } |
|
| 2085 msim_msg_free(msg); |
|
| 2086 |
|
| 2087 /* TODO: if addbuddy fails ('error' message is returned), delete added buddy from |
|
| 2088 * buddy list since Purple adds it locally. */ |
|
| 2089 |
|
| 2090 body = msim_msg_new( |
|
| 2091 "ContactID", MSIM_TYPE_STRING, g_strdup("<uid>"), |
|
| 2092 "GroupName", MSIM_TYPE_STRING, g_strdup(group->name), |
|
| 2093 "Position", MSIM_TYPE_INTEGER, 1000, |
|
| 2094 "Visibility", MSIM_TYPE_INTEGER, 1, |
|
| 2095 "NickName", MSIM_TYPE_STRING, g_strdup(""), |
|
| 2096 "NameSelect", MSIM_TYPE_INTEGER, 0, |
|
| 2097 NULL); |
|
| 2098 |
|
| 2099 /* TODO: Update blocklist. */ |
|
| 2100 |
|
| 2101 msg_persist = msim_msg_new( |
|
| 2102 "persist", MSIM_TYPE_INTEGER, 1, |
|
| 2103 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 2104 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_PUT, |
|
| 2105 "dsn", MSIM_TYPE_INTEGER, MC_CONTACT_INFO_DSN, |
|
| 2106 "uid", MSIM_TYPE_INTEGER, session->userid, |
|
| 2107 "lid", MSIM_TYPE_INTEGER, MC_CONTACT_INFO_LID, |
|
| 2108 /* TODO: Use msim_new_reply_callback to get rid. */ |
|
| 2109 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
|
| 2110 "body", MSIM_TYPE_DICTIONARY, body, |
|
| 2111 NULL); |
|
| 2112 |
|
| 2113 if (!msim_postprocess_outgoing(session, msg_persist, buddy->name, "body", NULL)) |
|
| 2114 { |
|
| 2115 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("persist command failed")); |
|
| 2116 msim_msg_free(msg_persist); |
|
| 2117 return; |
|
| 2118 } |
|
| 2119 msim_msg_free(msg_persist); |
|
| 2120 |
|
| 2121 } |
|
| 2122 |
|
| 2123 /** Perform actual postprocessing on a message, adding userid as specified. |
|
| 2124 * |
|
| 2125 * @param msg The message to postprocess. |
|
| 2126 * @param uid_before Name of field where to insert new field before, or NULL for end. |
|
| 2127 * @param uid_field_name Name of field to add uid to. |
|
| 2128 * @param uid The userid to insert. |
|
| 2129 * |
|
| 2130 * If the field named by uid_field_name already exists, then its string contents will |
|
| 2131 * be used for the field, except "<uid>" will be replaced by the userid. |
|
| 2132 * |
|
| 2133 * If the field named by uid_field_name does not exist, it will be added before the |
|
| 2134 * field named by uid_before, as an integer, with the userid. |
|
| 2135 * |
|
| 2136 * Does not handle sending, or scheduling userid lookup. For that, see msim_postprocess_outgoing(). |
|
| 2137 */ |
|
| 2138 static MsimMessage * |
|
| 2139 msim_do_postprocessing(MsimMessage *msg, const gchar *uid_before, |
|
| 2140 const gchar *uid_field_name, guint uid) |
|
| 2141 { |
|
| 2142 MsimMessageElement *elem; |
|
| 2143 msim_msg_dump("msim_do_postprocessing msg: %s\n", msg); |
|
| 2144 |
|
| 2145 /* First, check - if the field already exists, replace <uid> within it */ |
|
| 2146 if ((elem = msim_msg_get(msg, uid_field_name)) != NULL) { |
|
| 2147 gchar *fmt_string; |
|
| 2148 gchar *uid_str, *new_str; |
|
| 2149 |
|
| 2150 /* Get the packed element, flattening it. This allows <uid> to be |
|
| 2151 * replaced within nested data structures, since the replacement is done |
|
| 2152 * on the linear, packed data, not on a complicated data structure. |
|
| 2153 * |
|
| 2154 * For example, if the field was originally a dictionary or a list, you |
|
| 2155 * would have to iterate over all the items in it to see what needs to |
|
| 2156 * be replaced. But by packing it first, the <uid> marker is easily replaced |
|
| 2157 * just by a string replacement. |
|
| 2158 */ |
|
| 2159 fmt_string = msim_msg_pack_element_data(elem); |
|
| 2160 |
|
| 2161 uid_str = g_strdup_printf("%d", uid); |
|
| 2162 new_str = purple_strreplace(fmt_string, "<uid>", uid_str); |
|
| 2163 g_free(uid_str); |
|
| 2164 g_free(fmt_string); |
|
| 2165 |
|
| 2166 /* Free the old element data */ |
|
| 2167 msim_msg_free_element_data(elem->data); |
|
| 2168 |
|
| 2169 /* Replace it with our new data */ |
|
| 2170 elem->data = new_str; |
|
| 2171 elem->type = MSIM_TYPE_RAW; |
|
| 2172 |
|
| 2173 } else { |
|
| 2174 /* Otherwise, insert new field into outgoing message. */ |
|
| 2175 msg = msim_msg_insert_before(msg, uid_before, uid_field_name, MSIM_TYPE_INTEGER, GUINT_TO_POINTER(uid)); |
|
| 2176 } |
|
| 2177 |
|
| 2178 msim_msg_dump("msim_postprocess_outgoing_cb: postprocessed msg=%s\n", msg); |
|
| 2179 |
|
| 2180 return msg; |
|
| 2181 } |
|
| 2182 |
|
| 2183 /** Callback for msim_postprocess_outgoing() to add a userid to a message, and send it (once receiving userid). |
|
| 2184 * |
|
| 2185 * @param session |
|
| 2186 * @param userinfo The user information reply message, containing the user ID |
|
| 2187 * @param data The message to postprocess and send. |
|
| 2188 * |
|
| 2189 * The data message should contain these fields: |
|
| 2190 * |
|
| 2191 * _uid_field_name: string, name of field to add with userid from userinfo message |
|
| 2192 * _uid_before: string, name of field before field to insert, or NULL for end |
|
| 2193 * |
|
| 2194 * |
|
| 2195 */ |
|
| 2196 static void |
|
| 2197 msim_postprocess_outgoing_cb(MsimSession *session, MsimMessage *userinfo, |
|
| 2198 gpointer data) |
|
| 2199 { |
|
| 2200 gchar *uid_field_name, *uid_before, *username; |
|
| 2201 guint uid; |
|
| 2202 MsimMessage *msg, *body; |
|
| 2203 |
|
| 2204 msg = (MsimMessage *)data; |
|
| 2205 |
|
| 2206 msim_msg_dump("msim_postprocess_outgoing_cb() got msg=%s\n", msg); |
|
| 2207 |
|
| 2208 /* Obtain userid from userinfo message. */ |
|
| 2209 body = msim_msg_get_dictionary(userinfo, "body"); |
|
| 2210 g_return_if_fail(body != NULL); |
|
| 2211 |
|
| 2212 uid = msim_msg_get_integer(body, "UserID"); |
|
| 2213 msim_msg_free(body); |
|
| 2214 |
|
| 2215 username = msim_msg_get_string(msg, "_username"); |
|
| 2216 |
|
| 2217 if (!uid) { |
|
| 2218 gchar *msg; |
|
| 2219 |
|
| 2220 msg = g_strdup_printf(_("No such user: %s"), username); |
|
| 2221 if (!purple_conv_present_error(username, session->account, msg)) { |
|
| 2222 purple_notify_error(NULL, NULL, _("User lookup"), msg); |
|
| 2223 } |
|
| 2224 |
|
| 2225 g_free(msg); |
|
| 2226 g_free(username); |
|
| 2227 /* TODO: free |
|
| 2228 * msim_msg_free(msg); |
|
| 2229 */ |
|
| 2230 return; |
|
| 2231 } |
|
| 2232 |
|
| 2233 uid_field_name = msim_msg_get_string(msg, "_uid_field_name"); |
|
| 2234 uid_before = msim_msg_get_string(msg, "_uid_before"); |
|
| 2235 |
|
| 2236 msg = msim_do_postprocessing(msg, uid_before, uid_field_name, uid); |
|
| 2237 |
|
| 2238 /* Send */ |
|
| 2239 if (!msim_msg_send(session, msg)) { |
|
| 2240 msim_msg_dump("msim_postprocess_outgoing_cb: sending failed for message: %s\n", msg); |
|
| 2241 } |
|
| 2242 |
|
| 2243 |
|
| 2244 /* Free field names AFTER sending message, because MsimMessage does NOT copy |
|
| 2245 * field names - instead, treats them as static strings (which they usually are). |
|
| 2246 */ |
|
| 2247 g_free(uid_field_name); |
|
| 2248 g_free(uid_before); |
|
| 2249 g_free(username); |
|
| 2250 /* TODO: free |
|
| 2251 * msim_msg_free(msg); |
|
| 2252 */ |
|
| 2253 } |
|
| 2254 |
|
| 2255 /** Postprocess and send a message. |
|
| 2256 * |
|
| 2257 * @param session |
|
| 2258 * @param msg Message to postprocess. Will NOT be freed. |
|
| 2259 * @param username Username to resolve. Assumed to be a static string (will not be freed or copied). |
|
| 2260 * @param uid_field_name Name of new field to add, containing uid of username. Static string. |
|
| 2261 * @param uid_before Name of existing field to insert username field before. Static string. |
|
| 2262 * |
|
| 2263 * @return TRUE if successful. |
|
| 2264 */ |
|
| 2265 gboolean |
|
| 2266 msim_postprocess_outgoing(MsimSession *session, MsimMessage *msg, |
|
| 2267 const gchar *username, const gchar *uid_field_name, |
|
| 2268 const gchar *uid_before) |
|
| 2269 { |
|
| 2270 PurpleBuddy *buddy; |
|
| 2271 guint uid; |
|
| 2272 gboolean rc; |
|
| 2273 |
|
| 2274 g_return_val_if_fail(msg != NULL, FALSE); |
|
| 2275 |
|
| 2276 /* Store information for msim_postprocess_outgoing_cb(). */ |
|
| 2277 msim_msg_dump("msim_postprocess_outgoing: msg before=%s\n", msg); |
|
| 2278 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username)); |
|
| 2279 msg = msim_msg_append(msg, "_uid_field_name", MSIM_TYPE_STRING, g_strdup(uid_field_name)); |
|
| 2280 msg = msim_msg_append(msg, "_uid_before", MSIM_TYPE_STRING, g_strdup(uid_before)); |
|
| 2281 |
|
| 2282 /* First, try the most obvious. If numeric userid is given, use that directly. */ |
|
| 2283 if (msim_is_userid(username)) { |
|
| 2284 uid = atol(username); |
|
| 2285 } else { |
|
| 2286 /* Next, see if on buddy list and know uid. */ |
|
| 2287 buddy = purple_find_buddy(session->account, username); |
|
| 2288 if (buddy) { |
|
| 2289 uid = purple_blist_node_get_int(&buddy->node, "UserID"); |
|
| 2290 } else { |
1950 } else { |
| 2291 uid = 0; |
1951 gchar *from; |
| 2292 } |
1952 |
| 2293 |
1953 /* Send lookup request. */ |
| 2294 if (!buddy || !uid) { |
1954 /* XXX: where is msim_msg_get_string() freed? make _strdup and _nonstrdup. */ |
| 2295 /* Don't have uid offhand - need to ask for it, and wait until hear back before sending. */ |
1955 purple_debug_info("msim", "msim_incoming: sending lookup, setting up callback\n"); |
| 2296 purple_debug_info("msim", ">>> msim_postprocess_outgoing: couldn't find username %s in blist\n", |
1956 from = msim_msg_get_string(msg, "f"); |
| 2297 username ? username : "(NULL)"); |
1957 msim_lookup_user(session, from, msim_incoming_resolved, msim_msg_clone(msg)); |
| 2298 msim_msg_dump("msim_postprocess_outgoing - scheduling lookup, msg=%s\n", msg); |
1958 g_free(from); |
| 2299 /* TODO: where is cloned message freed? Should be in _cb. */ |
1959 |
| 2300 msim_lookup_user(session, username, msim_postprocess_outgoing_cb, msim_msg_clone(msg)); |
1960 /* indeterminate */ |
| 2301 return TRUE; /* not sure of status yet - haven't sent! */ |
1961 return TRUE; |
| 2302 } |
|
| 2303 } |
|
| 2304 |
|
| 2305 /* Already have uid, postprocess and send msg immediately. */ |
|
| 2306 purple_debug_info("msim", "msim_postprocess_outgoing: found username %s has uid %d\n", |
|
| 2307 username ? username : "(NULL)", uid); |
|
| 2308 |
|
| 2309 msg = msim_do_postprocessing(msg, uid_before, uid_field_name, uid); |
|
| 2310 |
|
| 2311 msim_msg_dump("msim_postprocess_outgoing: msg after (uid immediate)=%s\n", msg); |
|
| 2312 |
|
| 2313 rc = msim_msg_send(session, msg); |
|
| 2314 |
|
| 2315 /* TODO: free |
|
| 2316 * msim_msg_free(msg); |
|
| 2317 */ |
|
| 2318 |
|
| 2319 return rc; |
|
| 2320 } |
|
| 2321 |
|
| 2322 /** Remove a buddy from the user's buddy list. */ |
|
| 2323 void |
|
| 2324 msim_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) |
|
| 2325 { |
|
| 2326 MsimSession *session; |
|
| 2327 MsimMessage *delbuddy_msg; |
|
| 2328 MsimMessage *persist_msg; |
|
| 2329 MsimMessage *blocklist_msg; |
|
| 2330 GList *blocklist_updates; |
|
| 2331 |
|
| 2332 session = (MsimSession *)gc->proto_data; |
|
| 2333 |
|
| 2334 delbuddy_msg = msim_msg_new( |
|
| 2335 "delbuddy", MSIM_TYPE_BOOLEAN, TRUE, |
|
| 2336 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 2337 /* 'delprofileid' with uid will be inserted here. */ |
|
| 2338 NULL); |
|
| 2339 |
|
| 2340 if (!msim_postprocess_outgoing(session, delbuddy_msg, buddy->name, "delprofileid", NULL)) { |
|
| 2341 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("'delbuddy' command failed")); |
|
| 2342 msim_msg_free(delbuddy_msg); |
|
| 2343 return; |
|
| 2344 } |
|
| 2345 msim_msg_free(delbuddy_msg); |
|
| 2346 |
|
| 2347 persist_msg = msim_msg_new( |
|
| 2348 "persist", MSIM_TYPE_INTEGER, 1, |
|
| 2349 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 2350 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_DELETE, |
|
| 2351 "dsn", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_DSN, |
|
| 2352 "lid", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_LID, |
|
| 2353 "uid", MSIM_TYPE_INTEGER, session->userid, |
|
| 2354 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
|
| 2355 /* <uid> will be replaced by postprocessing */ |
|
| 2356 "body", MSIM_TYPE_STRING, g_strdup("ContactID=<uid>"), |
|
| 2357 NULL); |
|
| 2358 |
|
| 2359 if (!msim_postprocess_outgoing(session, persist_msg, buddy->name, "body", NULL)) { |
|
| 2360 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("persist command failed")); |
|
| 2361 msim_msg_free(persist_msg); |
|
| 2362 return; |
|
| 2363 } |
|
| 2364 msim_msg_free(persist_msg); |
|
| 2365 |
|
| 2366 blocklist_updates = NULL; |
|
| 2367 blocklist_updates = g_list_prepend(blocklist_updates, "a-"); |
|
| 2368 blocklist_updates = g_list_prepend(blocklist_updates, "<uid>"); |
|
| 2369 blocklist_updates = g_list_prepend(blocklist_updates, "b-"); |
|
| 2370 blocklist_updates = g_list_prepend(blocklist_updates, "<uid>"); |
|
| 2371 blocklist_updates = g_list_reverse(blocklist_updates); |
|
| 2372 |
|
| 2373 blocklist_msg = msim_msg_new( |
|
| 2374 "blocklist", MSIM_TYPE_BOOLEAN, TRUE, |
|
| 2375 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
|
| 2376 /* TODO: MsimMessage lists. Currently <uid> isn't replaced in lists. */ |
|
| 2377 /* "idlist", MSIM_TYPE_STRING, g_strdup("a-|<uid>|b-|<uid>"), */ |
|
| 2378 "idlist", MSIM_TYPE_LIST, blocklist_updates, |
|
| 2379 NULL); |
|
| 2380 |
|
| 2381 if (!msim_postprocess_outgoing(session, blocklist_msg, buddy->name, "idlist", NULL)) { |
|
| 2382 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("blocklist command failed")); |
|
| 2383 msim_msg_free(blocklist_msg); |
|
| 2384 return; |
|
| 2385 } |
|
| 2386 msim_msg_free(blocklist_msg); |
|
| 2387 } |
|
| 2388 |
|
| 2389 /** |
|
| 2390 * Returns a string of a username in canonical form. Basically removes all the |
|
| 2391 * spaces, lowercases the string, and looks up user IDs to usernames. |
|
| 2392 * Normalizing tom, TOM, Tom, and 6221 wil all return 'tom'. |
|
| 2393 * |
|
| 2394 * Borrowed this code from oscar_normalize. Added checking for |
|
| 2395 * "if userid, get name before normalizing" |
|
| 2396 */ |
|
| 2397 const char *msim_normalize(const PurpleAccount *account, const char *str) { |
|
| 2398 static char normalized[BUF_LEN]; |
|
| 2399 char *tmp1, *tmp2; |
|
| 2400 int i, j; |
|
| 2401 guint id; |
|
| 2402 |
|
| 2403 g_return_val_if_fail(str != NULL, NULL); |
|
| 2404 |
|
| 2405 if (msim_is_userid(str)) { |
|
| 2406 /* Have user ID, we need to get their username first :) */ |
|
| 2407 const char *username; |
|
| 2408 |
|
| 2409 /* If the account does not exist, we can't look up the user. */ |
|
| 2410 if (!account || !account->gc) |
|
| 2411 return str; |
|
| 2412 |
|
| 2413 id = atol(str); |
|
| 2414 username = msim_uid2username_from_blist((PurpleAccount *)account, id); |
|
| 2415 if (!username) { |
|
| 2416 /* Not in buddy list... scheisse... TODO: Manual Lookup! Bug #4631 */ |
|
| 2417 /* Note: manual lookup using msim_lookup_user() is a problem inside |
|
| 2418 * msim_normalize(), because msim_lookup_user() calls a callback function |
|
| 2419 * when the user information has been looked up, but msim_normalize() expects |
|
| 2420 * the result immediately. */ |
|
| 2421 strncpy(normalized, str, BUF_LEN); |
|
| 2422 } else { |
|
| 2423 strncpy(normalized, username, BUF_LEN); |
|
| 2424 } |
1962 } |
| 2425 } else { |
1963 } else { |
| 2426 /* Have username. */ |
1964 /* Nothing to resolve - send directly to processing. */ |
| 2427 strncpy(normalized, str, BUF_LEN); |
1965 return msim_process(session, msg); |
| 2428 } |
1966 } |
| 2429 |
|
| 2430 /* Strip spaces. */ |
|
| 2431 for (i=0, j=0; normalized[j]; i++, j++) { |
|
| 2432 while (normalized[j] == ' ') |
|
| 2433 j++; |
|
| 2434 normalized[i] = normalized[j]; |
|
| 2435 } |
|
| 2436 normalized[i] = '\0'; |
|
| 2437 |
|
| 2438 /* Lowercase and perform UTF-8 normalization. */ |
|
| 2439 tmp1 = g_utf8_strdown(normalized, -1); |
|
| 2440 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT); |
|
| 2441 g_snprintf(normalized, sizeof(normalized), "%s", tmp2); |
|
| 2442 g_free(tmp2); |
|
| 2443 g_free(tmp1); |
|
| 2444 |
|
| 2445 /* TODO: re-add caps and spacing back to what the user wanted. |
|
| 2446 * User can format their own names, for example 'msimprpl' is shown |
|
| 2447 * as 'MsIm PrPl' in the official client. |
|
| 2448 * |
|
| 2449 * TODO: file a ticket to add this enhancement. |
|
| 2450 */ |
|
| 2451 |
|
| 2452 return normalized; |
|
| 2453 } |
|
| 2454 |
|
| 2455 static GHashTable * |
|
| 2456 msim_get_account_text_table(PurpleAccount *unused) |
|
| 2457 { |
|
| 2458 GHashTable *table; |
|
| 2459 |
|
| 2460 table = g_hash_table_new(g_str_hash, g_str_equal); |
|
| 2461 |
|
| 2462 g_hash_table_insert(table, "login_label", (gpointer)_("Email Address...")); |
|
| 2463 |
|
| 2464 return table; |
|
| 2465 } |
|
| 2466 |
|
| 2467 /** Return whether the buddy can be messaged while offline. |
|
| 2468 * |
|
| 2469 * The protocol supports offline messages in just the same way as online |
|
| 2470 * messages. |
|
| 2471 */ |
|
| 2472 gboolean |
|
| 2473 msim_offline_message(const PurpleBuddy *buddy) |
|
| 2474 { |
|
| 2475 return TRUE; |
|
| 2476 } |
1967 } |
| 2477 |
1968 |
| 2478 /** |
1969 /** |
| 2479 * Callback when input available. |
1970 * Callback when input available. |
| 2480 * |
1971 * |
| 2715 } |
2222 } |
| 2716 |
2223 |
| 2717 msim_session_destroy(session); |
2224 msim_session_destroy(session); |
| 2718 } |
2225 } |
| 2719 |
2226 |
| 2720 |
2227 /** |
| 2721 /** |
2228 * Schedule an IM to be sent once the user ID is looked up. |
| 2722 * Obtain the status text for a buddy. |
2229 * |
| 2723 * |
2230 * @param gc Connection. |
| 2724 * @param buddy The buddy to obtain status text for. |
2231 * @param who A user id, email, or username to send the message to. |
| 2725 * |
2232 * @param message Instant message text to send. |
| 2726 * @return Status text, or NULL if error. Caller g_free()'s. |
2233 * @param flags Flags. |
| 2727 * |
2234 * |
| 2728 */ |
2235 * @return 1 if successful or postponed, -1 if failed |
| 2729 char * |
2236 * |
| 2730 msim_status_text(PurpleBuddy *buddy) |
2237 * Allows sending to a user by username, email address, or userid. If |
| |
2238 * a username or email address is given, the userid must be looked up. |
| |
2239 * This function does that by calling msim_postprocess_outgoing(). |
| |
2240 */ |
| |
2241 static int |
| |
2242 msim_send_im(PurpleConnection *gc, const gchar *who, const gchar *message, |
| |
2243 PurpleMessageFlags flags) |
| |
2244 { |
| |
2245 MsimSession *session; |
| |
2246 gchar *message_msim; |
| |
2247 int rc; |
| |
2248 |
| |
2249 g_return_val_if_fail(gc != NULL, -1); |
| |
2250 g_return_val_if_fail(who != NULL, -1); |
| |
2251 g_return_val_if_fail(message != NULL, -1); |
| |
2252 |
| |
2253 /* 'flags' has many options, not used here. */ |
| |
2254 |
| |
2255 session = (MsimSession *)gc->proto_data; |
| |
2256 |
| |
2257 g_return_val_if_fail(MSIM_SESSION_VALID(session), -1); |
| |
2258 |
| |
2259 message_msim = html_to_msim_markup(session, message); |
| |
2260 |
| |
2261 if (msim_send_bm(session, who, message_msim, MSIM_BM_INSTANT)) { |
| |
2262 /* Return 1 to have Purple show this IM as being sent, 0 to not. I always |
| |
2263 * return 1 even if the message could not be sent, since I don't know if |
| |
2264 * it has failed yet--because the IM is only sent after the userid is |
| |
2265 * retrieved from the server (which happens after this function returns). |
| |
2266 * If an error does occur, it should be logged to the IM window. |
| |
2267 */ |
| |
2268 rc = 1; |
| |
2269 } else { |
| |
2270 rc = -1; |
| |
2271 } |
| |
2272 |
| |
2273 g_free(message_msim); |
| |
2274 |
| |
2275 return rc; |
| |
2276 } |
| |
2277 |
| |
2278 /** |
| |
2279 * Handle when our user starts or stops typing to another user. |
| |
2280 * |
| |
2281 * @param gc |
| |
2282 * @param name The buddy name to which our user is typing to |
| |
2283 * @param state PURPLE_TYPING, PURPLE_TYPED, PURPLE_NOT_TYPING |
| |
2284 * |
| |
2285 * @return 0 |
| |
2286 */ |
| |
2287 static unsigned int |
| |
2288 msim_send_typing(PurpleConnection *gc, const gchar *name, |
| |
2289 PurpleTypingState state) |
| |
2290 { |
| |
2291 const gchar *typing_str; |
| |
2292 MsimSession *session; |
| |
2293 |
| |
2294 g_return_val_if_fail(gc != NULL, 0); |
| |
2295 g_return_val_if_fail(name != NULL, 0); |
| |
2296 |
| |
2297 session = (MsimSession *)gc->proto_data; |
| |
2298 |
| |
2299 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0); |
| |
2300 |
| |
2301 switch (state) { |
| |
2302 case PURPLE_TYPING: |
| |
2303 typing_str = "%typing%"; |
| |
2304 break; |
| |
2305 |
| |
2306 case PURPLE_TYPED: |
| |
2307 case PURPLE_NOT_TYPING: |
| |
2308 default: |
| |
2309 typing_str = "%stoptyping%"; |
| |
2310 break; |
| |
2311 } |
| |
2312 |
| |
2313 purple_debug_info("msim", "msim_send_typing(%s): %d (%s)\n", name, state, typing_str); |
| |
2314 msim_send_bm(session, name, typing_str, MSIM_BM_ACTION); |
| |
2315 return 0; |
| |
2316 } |
| |
2317 |
| |
2318 /** |
| |
2319 * Callback for msim_get_info(), for when user info is received. |
| |
2320 */ |
| |
2321 static void |
| |
2322 msim_get_info_cb(MsimSession *session, MsimMessage *user_info_msg, |
| |
2323 gpointer data) |
| |
2324 { |
| |
2325 MsimMessage *msg; |
| |
2326 gchar *username; |
| |
2327 PurpleNotifyUserInfo *user_info; |
| |
2328 MsimUser *user; |
| |
2329 |
| |
2330 g_return_if_fail(MSIM_SESSION_VALID(session)); |
| |
2331 |
| |
2332 /* Get user{name,id} from msim_get_info, passed as an MsimMessage for |
| |
2333 orthogonality. */ |
| |
2334 msg = (MsimMessage *)data; |
| |
2335 g_return_if_fail(msg != NULL); |
| |
2336 |
| |
2337 username = msim_msg_get_string(msg, "user"); |
| |
2338 if (!username) { |
| |
2339 purple_debug_info("msim", "msim_get_info_cb: no 'user' in msg\n"); |
| |
2340 return; |
| |
2341 } |
| |
2342 |
| |
2343 msim_msg_free(msg); |
| |
2344 purple_debug_info("msim", "msim_get_info_cb: got for user: %s\n", username); |
| |
2345 |
| |
2346 user = msim_find_user(session, username); |
| |
2347 |
| |
2348 if (!user) { |
| |
2349 /* User isn't on blist, create a temporary user to store info. */ |
| |
2350 user = g_new0(MsimUser, 1); |
| |
2351 user->temporary_user = TRUE; |
| |
2352 } |
| |
2353 |
| |
2354 /* Update user structure with new information */ |
| |
2355 msim_store_user_info(session, user_info_msg, user); |
| |
2356 |
| |
2357 user_info = purple_notify_user_info_new(); |
| |
2358 |
| |
2359 /* Append data from MsimUser to PurpleNotifyUserInfo for display, full */ |
| |
2360 msim_append_user_info(session, user_info, user, TRUE); |
| |
2361 |
| |
2362 purple_notify_userinfo(session->gc, username, user_info, NULL, NULL); |
| |
2363 purple_debug_info("msim", "msim_get_info_cb: username=%s\n", username); |
| |
2364 |
| |
2365 purple_notify_user_info_destroy(user_info); |
| |
2366 |
| |
2367 if (user->temporary_user) { |
| |
2368 g_free(user->client_info); |
| |
2369 g_free(user->gender); |
| |
2370 g_free(user->location); |
| |
2371 g_free(user->headline); |
| |
2372 g_free(user->display_name); |
| |
2373 g_free(user->username); |
| |
2374 g_free(user->image_url); |
| |
2375 g_free(user); |
| |
2376 } |
| |
2377 g_free(username); |
| |
2378 } |
| |
2379 |
| |
2380 /** |
| |
2381 * Retrieve a user's profile. |
| |
2382 * @param username Username, user ID, or email address to lookup. |
| |
2383 */ |
| |
2384 static void |
| |
2385 msim_get_info(PurpleConnection *gc, const gchar *username) |
| 2731 { |
2386 { |
| 2732 MsimSession *session; |
2387 MsimSession *session; |
| 2733 MsimUser *user; |
2388 MsimUser *user; |
| 2734 const gchar *display_name, *headline; |
2389 gchar *user_to_lookup; |
| 2735 |
2390 MsimMessage *user_msg; |
| 2736 g_return_val_if_fail(buddy != NULL, NULL); |
2391 |
| 2737 |
2392 g_return_if_fail(gc != NULL); |
| 2738 user = msim_get_user_from_buddy(buddy); |
2393 g_return_if_fail(username != NULL); |
| 2739 |
2394 |
| 2740 session = (MsimSession *)buddy->account->gc->proto_data; |
2395 session = (MsimSession *)gc->proto_data; |
| 2741 g_return_val_if_fail(MSIM_SESSION_VALID(session), NULL); |
2396 |
| 2742 |
2397 g_return_if_fail(MSIM_SESSION_VALID(session)); |
| 2743 display_name = headline = NULL; |
2398 |
| 2744 |
2399 /* Obtain uid of buddy. */ |
| 2745 /* Retrieve display name and/or headline, depending on user preference. */ |
2400 user = msim_find_user(session, username); |
| 2746 if (purple_account_get_bool(session->account, "show_headline", TRUE)) { |
2401 |
| 2747 headline = user->headline; |
2402 /* If is on buddy list, lookup by uid since it is faster. */ |
| 2748 } |
2403 if (user && user->id) { |
| 2749 |
2404 user_to_lookup = g_strdup_printf("%d", user->id); |
| 2750 if (purple_account_get_bool(session->account, "show_display_name", FALSE)) { |
2405 } else { |
| 2751 display_name = user->display_name; |
2406 /* Looking up buddy not on blist. Lookup by whatever user entered. */ |
| 2752 } |
2407 user_to_lookup = g_strdup(username); |
| 2753 |
2408 } |
| 2754 /* Return appropriate combination of display name and/or headline, or neither. */ |
2409 |
| 2755 |
2410 /* Pass the username to msim_get_info_cb(), because since we lookup |
| 2756 if (display_name && headline) { |
2411 * by userid, the userinfo message will only contain the uid (not |
| 2757 return g_strconcat(display_name, " ", headline, NULL); |
2412 * the username) but it would be useful to display the username too. |
| 2758 } else if (display_name) { |
2413 */ |
| 2759 return g_strdup(display_name); |
2414 user_msg = msim_msg_new( |
| 2760 } else if (headline) { |
2415 "user", MSIM_TYPE_STRING, g_strdup(username), |
| 2761 return g_strdup(headline); |
2416 NULL); |
| 2762 } |
2417 purple_debug_info("msim", "msim_get_info, setting up lookup, user=%s\n", username); |
| 2763 |
2418 |
| 2764 return NULL; |
2419 msim_lookup_user(session, user_to_lookup, msim_get_info_cb, user_msg); |
| 2765 } |
2420 |
| 2766 |
2421 g_free(user_to_lookup); |
| 2767 /** |
2422 } |
| 2768 * Obtain the tooltip text for a buddy. |
2423 |
| 2769 * |
2424 /** |
| 2770 * @param buddy Buddy to obtain tooltip text on. |
2425 * Set status using an MSIM_STATUS_CODE_* value. |
| 2771 * @param user_info Variable modified to have the tooltip text. |
2426 * @param status_code An MSIM_STATUS_CODE_* value. |
| 2772 * @param full TRUE if should obtain full tooltip text. |
2427 * @param statstring Status string, must be a dynamic string (will be freed by msim_send). |
| 2773 * |
2428 */ |
| 2774 */ |
2429 static void |
| 2775 void |
2430 msim_set_status_code(MsimSession *session, guint status_code, gchar *statstring) |
| 2776 msim_tooltip_text(PurpleBuddy *buddy, PurpleNotifyUserInfo *user_info, |
2431 { |
| 2777 gboolean full) |
2432 g_return_if_fail(MSIM_SESSION_VALID(session)); |
| 2778 { |
2433 g_return_if_fail(statstring != NULL); |
| 2779 MsimUser *user; |
2434 |
| 2780 |
2435 purple_debug_info("msim", "msim_set_status_code: going to set status to code=%d,str=%s\n", |
| 2781 g_return_if_fail(buddy != NULL); |
2436 status_code, statstring); |
| 2782 g_return_if_fail(user_info != NULL); |
2437 |
| 2783 |
2438 if (!msim_send(session, |
| 2784 user = msim_get_user_from_buddy(buddy); |
2439 "status", MSIM_TYPE_INTEGER, status_code, |
| 2785 |
2440 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| 2786 if (PURPLE_BUDDY_IS_ONLINE(buddy)) { |
2441 "statstring", MSIM_TYPE_STRING, statstring, |
| 2787 MsimSession *session; |
2442 "locstring", MSIM_TYPE_STRING, g_strdup(""), |
| 2788 |
2443 NULL)) |
| 2789 session = (MsimSession *)buddy->account->gc->proto_data; |
2444 { |
| 2790 |
2445 purple_debug_info("msim", "msim_set_status: failed to set status\n"); |
| 2791 g_return_if_fail(MSIM_SESSION_VALID(session)); |
2446 } |
| 2792 |
2447 } |
| 2793 /* TODO: if (full), do something different? */ |
2448 |
| 2794 |
2449 /** |
| 2795 /* TODO: request information? have to figure out how to do |
2450 * Set your status - callback for when user manually sets it. |
| 2796 * the asynchronous lookup like oscar does (tooltip shows |
2451 */ |
| 2797 * 'retrieving...' if not yet available, then changes when it is). |
2452 static void |
| 2798 * |
2453 msim_set_status(PurpleAccount *account, PurpleStatus *status) |
| 2799 * Right now, only show what we have on hand. |
2454 { |
| |
2455 PurpleStatusType *type; |
| |
2456 PurplePresence *pres; |
| |
2457 MsimSession *session; |
| |
2458 guint status_code; |
| |
2459 const gchar *message; |
| |
2460 gchar *stripped; |
| |
2461 gchar *unrecognized_msg; |
| |
2462 |
| |
2463 session = (MsimSession *)account->gc->proto_data; |
| |
2464 |
| |
2465 g_return_if_fail(MSIM_SESSION_VALID(session)); |
| |
2466 |
| |
2467 type = purple_status_get_type(status); |
| |
2468 pres = purple_status_get_presence(status); |
| |
2469 |
| |
2470 switch (purple_status_type_get_primitive(type)) { |
| |
2471 case PURPLE_STATUS_AVAILABLE: |
| |
2472 purple_debug_info("msim", "msim_set_status: available (%d->%d)\n", PURPLE_STATUS_AVAILABLE, |
| |
2473 MSIM_STATUS_CODE_ONLINE); |
| |
2474 status_code = MSIM_STATUS_CODE_ONLINE; |
| |
2475 break; |
| |
2476 |
| |
2477 case PURPLE_STATUS_INVISIBLE: |
| |
2478 purple_debug_info("msim", "msim_set_status: invisible (%d->%d)\n", PURPLE_STATUS_INVISIBLE, |
| |
2479 MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN); |
| |
2480 status_code = MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN; |
| |
2481 break; |
| |
2482 |
| |
2483 case PURPLE_STATUS_AWAY: |
| |
2484 purple_debug_info("msim", "msim_set_status: away (%d->%d)\n", PURPLE_STATUS_AWAY, |
| |
2485 MSIM_STATUS_CODE_AWAY); |
| |
2486 status_code = MSIM_STATUS_CODE_AWAY; |
| |
2487 break; |
| |
2488 |
| |
2489 default: |
| |
2490 purple_debug_info("msim", "msim_set_status: unknown " |
| |
2491 "status interpreting as online"); |
| |
2492 status_code = MSIM_STATUS_CODE_ONLINE; |
| |
2493 |
| |
2494 unrecognized_msg = g_strdup_printf("msim_set_status, unrecognized status type: %d\n", |
| |
2495 purple_status_type_get_primitive(type)); |
| |
2496 msim_unrecognized(session, NULL, unrecognized_msg); |
| |
2497 g_free(unrecognized_msg); |
| |
2498 |
| |
2499 break; |
| |
2500 } |
| |
2501 |
| |
2502 message = purple_status_get_attr_string(status, "message"); |
| |
2503 |
| |
2504 /* Status strings are plain text. */ |
| |
2505 if (message != NULL) |
| |
2506 stripped = purple_markup_strip_html(message); |
| |
2507 else |
| |
2508 stripped = g_strdup(""); |
| |
2509 |
| |
2510 msim_set_status_code(session, status_code, stripped); |
| |
2511 |
| |
2512 /* If we should be idle, set that status. Time is irrelevant here. */ |
| |
2513 if (purple_presence_is_idle(pres) && status_code != MSIM_STATUS_CODE_OFFLINE_OR_HIDDEN) |
| |
2514 msim_set_idle(account->gc, 1); |
| |
2515 } |
| |
2516 |
| |
2517 /** |
| |
2518 * Go idle. |
| |
2519 */ |
| |
2520 static void |
| |
2521 msim_set_idle(PurpleConnection *gc, int time) |
| |
2522 { |
| |
2523 MsimSession *session; |
| |
2524 PurpleStatus *status; |
| |
2525 |
| |
2526 g_return_if_fail(gc != NULL); |
| |
2527 |
| |
2528 session = (MsimSession *)gc->proto_data; |
| |
2529 |
| |
2530 g_return_if_fail(MSIM_SESSION_VALID(session)); |
| |
2531 |
| |
2532 status = purple_account_get_active_status(session->account); |
| |
2533 |
| |
2534 if (time == 0) { |
| |
2535 /* Going back from idle. In msim, idle is mutually exclusive |
| |
2536 * from the other states (you can only be away or idle, but not |
| |
2537 * both, for example), so by going non-idle I go back to what |
| |
2538 * libpurple says I should be. |
| 2800 */ |
2539 */ |
| 2801 |
2540 msim_set_status(session->account, status); |
| 2802 /* Show abbreviated user info. */ |
2541 } else { |
| 2803 msim_append_user_info(session, user_info, user, FALSE); |
2542 const gchar *message; |
| 2804 } |
2543 gchar *stripped; |
| 2805 } |
2544 |
| 2806 |
2545 /* Set the idle message to the status message from the real |
| 2807 /** Add contact from server to buddy list, after looking up username. |
2546 * current status. |
| 2808 * Callback from msim_add_contact_from_server(). |
2547 */ |
| 2809 * |
2548 message = purple_status_get_attr_string(status, "message"); |
| 2810 * @param data An MsimMessage * of the contact information. Will be freed. |
2549 if (message != NULL) |
| |
2550 stripped = purple_markup_strip_html(message); |
| |
2551 else |
| |
2552 stripped = g_strdup(""); |
| |
2553 |
| |
2554 /* msim doesn't support idle time, so just go idle */ |
| |
2555 msim_set_status_code(session, MSIM_STATUS_CODE_IDLE, stripped); |
| |
2556 } |
| |
2557 } |
| |
2558 |
| |
2559 /** |
| |
2560 * Add a buddy to user's buddy list. |
| 2811 */ |
2561 */ |
| 2812 static void |
2562 static void |
| 2813 msim_add_contact_from_server_cb(MsimSession *session, MsimMessage *user_lookup_info, gpointer data) |
2563 msim_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) |
| 2814 { |
2564 { |
| 2815 MsimMessage *contact_info, *user_lookup_info_body; |
2565 MsimSession *session; |
| 2816 PurpleGroup *group; |
2566 MsimMessage *msg; |
| 2817 PurpleBuddy *buddy; |
2567 MsimMessage *msg_persist; |
| 2818 MsimUser *user; |
2568 MsimMessage *body; |
| 2819 gchar *username, *group_name; |
2569 |
| 2820 guint uid; |
2570 session = (MsimSession *)gc->proto_data; |
| 2821 |
2571 purple_debug_info("msim", "msim_add_buddy: want to add %s to %s\n", |
| 2822 contact_info = (MsimMessage *)data; |
2572 buddy->name, (group && group->name) ? group->name : "(no group)"); |
| 2823 purple_debug_info("msim_add_contact_from_server_cb", "contact_info addr=%p\n", contact_info); |
2573 |
| 2824 uid = msim_msg_get_integer(contact_info, "ContactID"); |
2574 msg = msim_msg_new( |
| 2825 |
2575 "addbuddy", MSIM_TYPE_BOOLEAN, TRUE, |
| 2826 if (!user_lookup_info) { |
2576 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| 2827 username = g_strdup(msim_uid2username_from_blist(session->account, uid)); |
2577 /* "newprofileid" will be inserted here with uid. */ |
| 2828 g_return_if_fail(username != NULL); |
2578 "reason", MSIM_TYPE_STRING, g_strdup(""), |
| 2829 } else { |
2579 NULL); |
| 2830 user_lookup_info_body = msim_msg_get_dictionary(user_lookup_info, "body"); |
2580 |
| 2831 username = msim_msg_get_string(user_lookup_info_body, "UserName"); |
2581 if (!msim_postprocess_outgoing(session, msg, buddy->name, "newprofileid", "reason")) { |
| 2832 msim_msg_free(user_lookup_info_body); |
2582 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("'addbuddy' command failed.")); |
| 2833 g_return_if_fail(username != NULL); |
2583 msim_msg_free(msg); |
| 2834 } |
2584 return; |
| 2835 |
2585 } |
| 2836 purple_debug_info("msim_add_contact_from_server_cb", |
2586 msim_msg_free(msg); |
| 2837 "*** about to add/update username=%s\n", username); |
2587 |
| 2838 |
2588 /* TODO: if addbuddy fails ('error' message is returned), delete added buddy from |
| 2839 /* 1. Creates a new group, or gets existing group if it exists (or so |
2589 * buddy list since Purple adds it locally. */ |
| 2840 * the documentation claims). */ |
2590 |
| 2841 group_name = msim_msg_get_string(contact_info, "GroupName"); |
2591 body = msim_msg_new( |
| 2842 if (!group_name || (*group_name == '\0')) { |
2592 "ContactID", MSIM_TYPE_STRING, g_strdup("<uid>"), |
| 2843 g_free(group_name); |
2593 "GroupName", MSIM_TYPE_STRING, g_strdup(group->name), |
| 2844 group_name = g_strdup(_("IM Friends")); |
2594 "Position", MSIM_TYPE_INTEGER, 1000, |
| 2845 purple_debug_info("myspace", "No GroupName specified, defaulting to '%s'.\n", group_name); |
2595 "Visibility", MSIM_TYPE_INTEGER, 1, |
| 2846 } |
2596 "NickName", MSIM_TYPE_STRING, g_strdup(""), |
| 2847 group = purple_find_group(group_name); |
2597 "NameSelect", MSIM_TYPE_INTEGER, 0, |
| 2848 if (!group) { |
2598 NULL); |
| 2849 group = purple_group_new(group_name); |
2599 |
| 2850 /* Add group to beginning. See #2752. */ |
2600 /* TODO: Update blocklist. */ |
| 2851 purple_blist_add_group(group, NULL); |
2601 |
| 2852 } |
2602 msg_persist = msim_msg_new( |
| 2853 g_free(group_name); |
2603 "persist", MSIM_TYPE_INTEGER, 1, |
| 2854 |
2604 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| 2855 /* 2. Get or create buddy */ |
2605 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_PUT, |
| 2856 buddy = purple_find_buddy(session->account, username); |
2606 "dsn", MSIM_TYPE_INTEGER, MC_CONTACT_INFO_DSN, |
| 2857 if (!buddy) { |
2607 "uid", MSIM_TYPE_INTEGER, session->userid, |
| 2858 purple_debug_info("msim_add_contact_from_server_cb", |
2608 "lid", MSIM_TYPE_INTEGER, MC_CONTACT_INFO_LID, |
| 2859 "creating new buddy: %s\n", username); |
2609 /* TODO: Use msim_new_reply_callback to get rid. */ |
| 2860 buddy = purple_buddy_new(session->account, username, NULL); |
2610 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
| 2861 } |
2611 "body", MSIM_TYPE_DICTIONARY, body, |
| 2862 |
2612 NULL); |
| 2863 /* TODO: use 'Position' in contact_info to take into account where buddy is */ |
2613 |
| 2864 purple_blist_add_buddy(buddy, NULL, group, NULL /* insertion point */); |
2614 if (!msim_postprocess_outgoing(session, msg_persist, buddy->name, "body", NULL)) |
| 2865 |
2615 { |
| 2866 /* 3. Update buddy information */ |
2616 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("persist command failed")); |
| 2867 user = msim_get_user_from_buddy(buddy); |
2617 msim_msg_free(msg_persist); |
| 2868 |
2618 return; |
| 2869 user->id = uid; |
2619 } |
| 2870 /* Keep track of the user ID across sessions */ |
2620 msim_msg_free(msg_persist); |
| 2871 purple_blist_node_set_int(&buddy->node, "UserID", uid); |
2621 } |
| 2872 |
2622 |
| 2873 /* Stores a few fields in the MsimUser, relevant to the buddy itself. |
2623 /** |
| 2874 * AvatarURL, Headline, ContactID. */ |
2624 * Remove a buddy from the user's buddy list. |
| 2875 msim_store_user_info(session, contact_info, NULL); |
2625 */ |
| 2876 |
|
| 2877 /* TODO: other fields, store in 'user' */ |
|
| 2878 msim_msg_free(contact_info); |
|
| 2879 |
|
| 2880 g_free(username); |
|
| 2881 } |
|
| 2882 |
|
| 2883 /** Add first ContactID in contact_info to buddy's list. Used to add |
|
| 2884 * server-side buddies to client-side list. |
|
| 2885 * |
|
| 2886 * @return TRUE if added. |
|
| 2887 * */ |
|
| 2888 static gboolean |
|
| 2889 msim_add_contact_from_server(MsimSession *session, MsimMessage *contact_info) |
|
| 2890 { |
|
| 2891 guint uid; |
|
| 2892 const gchar *username; |
|
| 2893 |
|
| 2894 uid = msim_msg_get_integer(contact_info, "ContactID"); |
|
| 2895 g_return_val_if_fail(uid != 0, FALSE); |
|
| 2896 |
|
| 2897 /* Lookup the username, since NickName and IMName is unreliable */ |
|
| 2898 username = msim_uid2username_from_blist(session->account, uid); |
|
| 2899 if (!username) { |
|
| 2900 gchar *uid_str; |
|
| 2901 |
|
| 2902 uid_str = g_strdup_printf("%d", uid); |
|
| 2903 purple_debug_info("msim_add_contact_from_server", |
|
| 2904 "contact_info addr=%p\n", contact_info); |
|
| 2905 msim_lookup_user(session, uid_str, msim_add_contact_from_server_cb, (gpointer)msim_msg_clone(contact_info)); |
|
| 2906 g_free(uid_str); |
|
| 2907 } else { |
|
| 2908 msim_add_contact_from_server_cb(session, NULL, (gpointer)msim_msg_clone(contact_info)); |
|
| 2909 } |
|
| 2910 |
|
| 2911 /* Say that the contact was added, even if we're still looking up |
|
| 2912 * their username. */ |
|
| 2913 return TRUE; |
|
| 2914 } |
|
| 2915 |
|
| 2916 /** Called when contact list is received from server. */ |
|
| 2917 static void |
2626 static void |
| 2918 msim_got_contact_list(MsimSession *session, MsimMessage *reply, gpointer user_data) |
2627 msim_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) |
| 2919 { |
2628 { |
| 2920 MsimMessage *body, *body_node; |
2629 MsimSession *session; |
| 2921 gchar *msg; |
2630 MsimMessage *delbuddy_msg; |
| 2922 guint buddy_count; |
2631 MsimMessage *persist_msg; |
| 2923 |
2632 MsimMessage *blocklist_msg; |
| 2924 msim_msg_dump("msim_got_contact_list: reply=%s", reply); |
2633 GList *blocklist_updates; |
| 2925 |
2634 |
| 2926 body = msim_msg_get_dictionary(reply, "body"); |
2635 session = (MsimSession *)gc->proto_data; |
| 2927 if (!body) { |
2636 |
| 2928 /* No friends. Not an error. */ |
2637 delbuddy_msg = msim_msg_new( |
| |
2638 "delbuddy", MSIM_TYPE_BOOLEAN, TRUE, |
| |
2639 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| |
2640 /* 'delprofileid' with uid will be inserted here. */ |
| |
2641 NULL); |
| |
2642 |
| |
2643 if (!msim_postprocess_outgoing(session, delbuddy_msg, buddy->name, "delprofileid", NULL)) { |
| |
2644 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("'delbuddy' command failed")); |
| |
2645 msim_msg_free(delbuddy_msg); |
| 2929 return; |
2646 return; |
| 2930 } |
2647 } |
| 2931 |
2648 msim_msg_free(delbuddy_msg); |
| 2932 buddy_count = 0; |
2649 |
| 2933 |
2650 persist_msg = msim_msg_new( |
| 2934 for (body_node = body; |
|
| 2935 body_node != NULL; |
|
| 2936 body_node = msim_msg_get_next_element_node(body_node)) |
|
| 2937 { |
|
| 2938 MsimMessageElement *elem; |
|
| 2939 |
|
| 2940 elem = (MsimMessageElement *)body_node->data; |
|
| 2941 |
|
| 2942 if (g_str_equal(elem->name, "ContactID")) |
|
| 2943 { |
|
| 2944 /* Will look for first contact in body_node */ |
|
| 2945 if (msim_add_contact_from_server(session, body_node)) { |
|
| 2946 ++buddy_count; |
|
| 2947 } |
|
| 2948 } |
|
| 2949 } |
|
| 2950 |
|
| 2951 switch (GPOINTER_TO_UINT(user_data)) { |
|
| 2952 case MSIM_CONTACT_LIST_IMPORT_ALL_FRIENDS: |
|
| 2953 msg = g_strdup_printf(ngettext("%d buddy was added or updated from the server (including buddies already on the server-side list)", |
|
| 2954 "%d buddies were added or updated from the server (including buddies already on the server-side list)", |
|
| 2955 buddy_count), |
|
| 2956 buddy_count); |
|
| 2957 purple_notify_info(session->account, _("Add contacts from server"), msg, NULL); |
|
| 2958 g_free(msg); |
|
| 2959 break; |
|
| 2960 |
|
| 2961 case MSIM_CONTACT_LIST_IMPORT_TOP_FRIENDS: |
|
| 2962 /* TODO */ |
|
| 2963 break; |
|
| 2964 |
|
| 2965 case MSIM_CONTACT_LIST_INITIAL_FRIENDS: |
|
| 2966 /* Nothing */ |
|
| 2967 break; |
|
| 2968 } |
|
| 2969 |
|
| 2970 msim_msg_free(body); |
|
| 2971 } |
|
| 2972 |
|
| 2973 /* Get contact list, calling msim_got_contact_list() with what_to_do_after as user_data gpointer. */ |
|
| 2974 static gboolean |
|
| 2975 msim_get_contact_list(MsimSession *session, int what_to_do_after) |
|
| 2976 { |
|
| 2977 return msim_send(session, |
|
| 2978 "persist", MSIM_TYPE_INTEGER, 1, |
2651 "persist", MSIM_TYPE_INTEGER, 1, |
| 2979 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
2652 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| 2980 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_GET, |
2653 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_DELETE, |
| 2981 "dsn", MSIM_TYPE_INTEGER, MG_LIST_ALL_CONTACTS_DSN, |
2654 "dsn", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_DSN, |
| 2982 "lid", MSIM_TYPE_INTEGER, MG_LIST_ALL_CONTACTS_LID, |
2655 "lid", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_LID, |
| 2983 "uid", MSIM_TYPE_INTEGER, session->userid, |
2656 "uid", MSIM_TYPE_INTEGER, session->userid, |
| 2984 "rid", MSIM_TYPE_INTEGER, |
2657 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
| 2985 msim_new_reply_callback(session, msim_got_contact_list, GUINT_TO_POINTER(what_to_do_after)), |
2658 /* <uid> will be replaced by postprocessing */ |
| 2986 "body", MSIM_TYPE_STRING, g_strdup(""), |
2659 "body", MSIM_TYPE_STRING, g_strdup("ContactID=<uid>"), |
| 2987 NULL); |
2660 NULL); |
| 2988 } |
2661 |
| 2989 |
2662 if (!msim_postprocess_outgoing(session, persist_msg, buddy->name, "body", NULL)) { |
| 2990 |
2663 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("persist command failed")); |
| 2991 /** Called when friends have been imported to buddy list on server. */ |
2664 msim_msg_free(persist_msg); |
| 2992 static void |
|
| 2993 msim_import_friends_cb(MsimSession *session, MsimMessage *reply, gpointer user_data) |
|
| 2994 { |
|
| 2995 MsimMessage *body; |
|
| 2996 gchar *completed; |
|
| 2997 msim_msg_dump("msim_import_friends_cb=%s", reply); |
|
| 2998 |
|
| 2999 /* Check if the friends were imported successfully. */ |
|
| 3000 body = msim_msg_get_dictionary(reply, "body"); |
|
| 3001 g_return_if_fail(body != NULL); |
|
| 3002 completed = msim_msg_get_string(body, "Completed"); |
|
| 3003 g_return_if_fail(body != NULL); |
|
| 3004 msim_msg_free(body); |
|
| 3005 if (!g_str_equal(completed, "True")) |
|
| 3006 { |
|
| 3007 purple_debug_info("msim_import_friends_cb", |
|
| 3008 "failed to import friends: %s", completed); |
|
| 3009 purple_notify_error(session->account, _("Add friends from MySpace.com"), |
|
| 3010 _("Importing friends failed"), NULL); |
|
| 3011 g_free(completed); |
|
| 3012 return; |
2665 return; |
| 3013 } |
2666 } |
| 3014 g_free(completed); |
2667 msim_msg_free(persist_msg); |
| 3015 |
2668 |
| 3016 purple_debug_info("msim_import_friends_cb", |
2669 blocklist_updates = NULL; |
| 3017 "added friends to server-side buddy list, requesting new contacts from server"); |
2670 blocklist_updates = g_list_prepend(blocklist_updates, "a-"); |
| 3018 |
2671 blocklist_updates = g_list_prepend(blocklist_updates, "<uid>"); |
| 3019 msim_get_contact_list(session, MSIM_CONTACT_LIST_IMPORT_ALL_FRIENDS); |
2672 blocklist_updates = g_list_prepend(blocklist_updates, "b-"); |
| 3020 |
2673 blocklist_updates = g_list_prepend(blocklist_updates, "<uid>"); |
| 3021 /* TODO: show, X friends have been added */ |
2674 blocklist_updates = g_list_reverse(blocklist_updates); |
| 3022 } |
2675 |
| 3023 |
2676 blocklist_msg = msim_msg_new( |
| 3024 /** Import friends from myspace.com. */ |
2677 "blocklist", MSIM_TYPE_BOOLEAN, TRUE, |
| 3025 static void msim_import_friends(PurplePluginAction *action) |
2678 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
| 3026 { |
2679 /* TODO: MsimMessage lists. Currently <uid> isn't replaced in lists. */ |
| 3027 PurpleConnection *gc; |
2680 /* "idlist", MSIM_TYPE_STRING, g_strdup("a-|<uid>|b-|<uid>"), */ |
| |
2681 "idlist", MSIM_TYPE_LIST, blocklist_updates, |
| |
2682 NULL); |
| |
2683 |
| |
2684 if (!msim_postprocess_outgoing(session, blocklist_msg, buddy->name, "idlist", NULL)) { |
| |
2685 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("blocklist command failed")); |
| |
2686 msim_msg_free(blocklist_msg); |
| |
2687 return; |
| |
2688 } |
| |
2689 msim_msg_free(blocklist_msg); |
| |
2690 } |
| |
2691 |
| |
2692 /** |
| |
2693 * Returns a string of a username in canonical form. Basically removes all the |
| |
2694 * spaces, lowercases the string, and looks up user IDs to usernames. |
| |
2695 * Normalizing tom, TOM, Tom, and 6221 wil all return 'tom'. |
| |
2696 * |
| |
2697 * Borrowed this code from oscar_normalize. Added checking for |
| |
2698 * "if userid, get name before normalizing" |
| |
2699 */ |
| |
2700 static const char *msim_normalize(const PurpleAccount *account, const char *str) { |
| |
2701 static char normalized[BUF_LEN]; |
| |
2702 char *tmp1, *tmp2; |
| |
2703 int i, j; |
| |
2704 guint id; |
| |
2705 |
| |
2706 g_return_val_if_fail(str != NULL, NULL); |
| |
2707 |
| |
2708 if (msim_is_userid(str)) { |
| |
2709 /* Have user ID, we need to get their username first :) */ |
| |
2710 const char *username; |
| |
2711 |
| |
2712 /* If the account does not exist, we can't look up the user. */ |
| |
2713 if (!account || !account->gc) |
| |
2714 return str; |
| |
2715 |
| |
2716 id = atol(str); |
| |
2717 username = msim_uid2username_from_blist((PurpleAccount *)account, id); |
| |
2718 if (!username) { |
| |
2719 /* Not in buddy list... scheisse... TODO: Manual Lookup! Bug #4631 */ |
| |
2720 /* Note: manual lookup using msim_lookup_user() is a problem inside |
| |
2721 * msim_normalize(), because msim_lookup_user() calls a callback function |
| |
2722 * when the user information has been looked up, but msim_normalize() expects |
| |
2723 * the result immediately. */ |
| |
2724 strncpy(normalized, str, BUF_LEN); |
| |
2725 } else { |
| |
2726 strncpy(normalized, username, BUF_LEN); |
| |
2727 } |
| |
2728 } else { |
| |
2729 /* Have username. */ |
| |
2730 strncpy(normalized, str, BUF_LEN); |
| |
2731 } |
| |
2732 |
| |
2733 /* Strip spaces. */ |
| |
2734 for (i=0, j=0; normalized[j]; i++, j++) { |
| |
2735 while (normalized[j] == ' ') |
| |
2736 j++; |
| |
2737 normalized[i] = normalized[j]; |
| |
2738 } |
| |
2739 normalized[i] = '\0'; |
| |
2740 |
| |
2741 /* Lowercase and perform UTF-8 normalization. */ |
| |
2742 tmp1 = g_utf8_strdown(normalized, -1); |
| |
2743 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT); |
| |
2744 g_snprintf(normalized, sizeof(normalized), "%s", tmp2); |
| |
2745 g_free(tmp2); |
| |
2746 g_free(tmp1); |
| |
2747 |
| |
2748 /* TODO: re-add caps and spacing back to what the user wanted. |
| |
2749 * User can format their own names, for example 'msimprpl' is shown |
| |
2750 * as 'MsIm PrPl' in the official client. |
| |
2751 * |
| |
2752 * TODO: file a ticket to add this enhancement. |
| |
2753 */ |
| |
2754 |
| |
2755 return normalized; |
| |
2756 } |
| |
2757 |
| |
2758 /** |
| |
2759 * Return whether the buddy can be messaged while offline. |
| |
2760 * |
| |
2761 * The protocol supports offline messages in just the same way as online |
| |
2762 * messages. |
| |
2763 */ |
| |
2764 static gboolean |
| |
2765 msim_offline_message(const PurpleBuddy *buddy) |
| |
2766 { |
| |
2767 return TRUE; |
| |
2768 } |
| |
2769 |
| |
2770 /** |
| |
2771 * Send raw data to the server, possibly with embedded NULs. |
| |
2772 * |
| |
2773 * Used in prpl_info struct, so that plugins can have the most possible |
| |
2774 * control of what is sent over the connection. Inside this prpl, |
| |
2775 * msim_send_raw() is used, since it sends NUL-terminated strings (easier). |
| |
2776 * |
| |
2777 * @param gc PurpleConnection |
| |
2778 * @param buf Buffer to send |
| |
2779 * @param total_bytes Size of buffer to send |
| |
2780 * |
| |
2781 * @return Bytes successfully sent, or -1 on error. |
| |
2782 */ |
| |
2783 static int |
| |
2784 msim_send_really_raw(PurpleConnection *gc, const char *buf, int total_bytes) |
| |
2785 { |
| |
2786 int total_bytes_sent; |
| 3028 MsimSession *session; |
2787 MsimSession *session; |
| 3029 gchar *group_name; |
2788 |
| 3030 |
2789 g_return_val_if_fail(gc != NULL, -1); |
| 3031 gc = (PurpleConnection *)action->context; |
2790 g_return_val_if_fail(buf != NULL, -1); |
| |
2791 g_return_val_if_fail(total_bytes >= 0, -1); |
| |
2792 |
| 3032 session = (MsimSession *)gc->proto_data; |
2793 session = (MsimSession *)gc->proto_data; |
| 3033 |
2794 |
| 3034 group_name = "MySpace Friends"; |
2795 g_return_val_if_fail(MSIM_SESSION_VALID(session), -1); |
| 3035 |
2796 |
| 3036 g_return_if_fail(msim_send(session, |
2797 /* Loop until all data is sent, or a failure occurs. */ |
| 3037 "persist", MSIM_TYPE_INTEGER, 1, |
2798 total_bytes_sent = 0; |
| 3038 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
2799 do { |
| 3039 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_PUT, |
2800 int bytes_sent; |
| 3040 "dsn", MSIM_TYPE_INTEGER, MC_IMPORT_ALL_FRIENDS_DSN, |
2801 |
| 3041 "lid", MSIM_TYPE_INTEGER, MC_IMPORT_ALL_FRIENDS_LID, |
2802 bytes_sent = send(session->fd, buf + total_bytes_sent, |
| 3042 "uid", MSIM_TYPE_INTEGER, session->userid, |
2803 total_bytes - total_bytes_sent, 0); |
| 3043 "rid", MSIM_TYPE_INTEGER, |
2804 |
| 3044 msim_new_reply_callback(session, msim_import_friends_cb, NULL), |
2805 if (bytes_sent < 0) { |
| 3045 "body", MSIM_TYPE_STRING, |
2806 purple_debug_info("msim", "msim_send_raw(%s): send() failed: %s\n", |
| 3046 g_strdup_printf("GroupName=%s", group_name), |
2807 buf, g_strerror(errno)); |
| 3047 NULL)); |
2808 return total_bytes_sent; |
| 3048 |
2809 } |
| 3049 |
2810 total_bytes_sent += bytes_sent; |
| 3050 } |
2811 |
| 3051 |
2812 } while(total_bytes_sent < total_bytes); |
| 3052 /** Actions menu for account. */ |
2813 |
| 3053 GList * |
2814 return total_bytes_sent; |
| 3054 msim_actions(PurplePlugin *plugin, gpointer context) |
2815 } |
| 3055 { |
2816 |
| 3056 PurpleConnection *gc; |
2817 /** |
| 3057 GList *menu; |
2818 * Send raw data (given as a NUL-terminated string) to the server. |
| 3058 PurplePluginAction *act; |
2819 * |
| 3059 |
2820 * @param session |
| 3060 gc = (PurpleConnection *)context; |
2821 * @param msg The raw data to send, in a NUL-terminated string. |
| 3061 |
2822 * |
| 3062 menu = NULL; |
2823 * @return TRUE if succeeded, FALSE if not. |
| 3063 |
2824 * |
| 3064 #if 0 |
2825 */ |
| 3065 /* TODO: find out how */ |
2826 gboolean |
| 3066 act = purple_plugin_action_new(_("Find people..."), msim_); |
2827 msim_send_raw(MsimSession *session, const gchar *msg) |
| 3067 menu = g_list_append(menu, act); |
2828 { |
| 3068 |
2829 size_t len; |
| 3069 act = purple_plugin_action_new(_("Change IM name..."), NULL); |
2830 |
| 3070 menu = g_list_append(menu, act); |
2831 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
| 3071 #endif |
2832 g_return_val_if_fail(msg != NULL, FALSE); |
| 3072 |
2833 |
| 3073 act = purple_plugin_action_new(_("Add friends from MySpace.com"), msim_import_friends); |
2834 purple_debug_info("msim", "msim_send_raw: writing <%s>\n", msg); |
| 3074 menu = g_list_append(menu, act); |
2835 len = strlen(msg); |
| 3075 |
2836 |
| 3076 return menu; |
2837 return msim_send_really_raw(session->gc, msg, len) == len; |
| 3077 } |
2838 } |
| 3078 |
2839 |
| 3079 /** Callbacks called by Purple, to access this plugin. */ |
2840 static GHashTable * |
| |
2841 msim_get_account_text_table(PurpleAccount *unused) |
| |
2842 { |
| |
2843 GHashTable *table; |
| |
2844 |
| |
2845 table = g_hash_table_new(g_str_hash, g_str_equal); |
| |
2846 |
| |
2847 g_hash_table_insert(table, "login_label", (gpointer)_("Email Address...")); |
| |
2848 |
| |
2849 return table; |
| |
2850 } |
| |
2851 |
| |
2852 /** |
| |
2853 * Callbacks called by Purple, to access this plugin. |
| |
2854 */ |
| 3080 static PurplePluginProtocolInfo prpl_info = { |
2855 static PurplePluginProtocolInfo prpl_info = { |
| 3081 /* options */ |
2856 /* options */ |
| 3082 OPT_PROTO_USE_POINTSIZE /* specify font size in sane point size */ |
2857 OPT_PROTO_USE_POINTSIZE /* specify font size in sane point size */ |
| 3083 | OPT_PROTO_MAIL_CHECK, |
2858 | OPT_PROTO_MAIL_CHECK, |
| 3084 |
2859 |
| 3085 /* | OPT_PROTO_IM_IMAGE - TODO: direct images. */ |
2860 /* | OPT_PROTO_IM_IMAGE - TODO: direct images. */ |
| 3086 NULL, /* user_splits */ |
2861 NULL, /* user_splits */ |
| 3087 NULL, /* protocol_options */ |
2862 NULL, /* protocol_options */ |
| 3088 NO_BUDDY_ICONS, /* icon_spec - TODO: eventually should add this */ |
2863 NO_BUDDY_ICONS, /* icon_spec - TODO: eventually should add this */ |
| 3089 msim_list_icon, /* list_icon */ |
2864 msim_list_icon, /* list_icon */ |
| 3090 NULL, /* list_emblems */ |
2865 NULL, /* list_emblems */ |