Mercurial > pidgin
comparison libpurple/protocols/myspace/myspace.c @ 17676:e7612bd6835b
Be more defensive about null pointers, especially null strings in %s format
strings. Trying to fix crashes on Windows.
author | Jeffrey Connelly <jaconnel@calpoly.edu> |
---|---|
date | Mon, 02 Jul 2007 04:24:16 +0000 |
parents | e24d44e3b6d2 |
children | 11988aee28f2 |
comparison
equal
deleted
inserted
replaced
17675:a752e1017fe9 | 17676:e7612bd6835b |
---|---|
282 | 282 |
283 /* TODO: Find out why >8 is problematic. The web site lets you have | 283 /* TODO: Find out why >8 is problematic. The web site lets you have |
284 * long passwords, but reportedly the official IM client does not | 284 * long passwords, but reportedly the official IM client does not |
285 * allow more than 8 characters to be entered. Just entering the first | 285 * allow more than 8 characters to be entered. Just entering the first |
286 * 8 does not, on first try, appear to work. */ | 286 * 8 does not, on first try, appear to work. */ |
287 str = g_strdup_printf(_("Sorry, passwords over %d characters in length (yours is %d) are " | 287 /* Note: official client lets you have 10 characters in password. */ |
288 "currently not supported by the MySpaceIM plugin."), MSIM_MAX_PASSWORD_LENGTH, | 288 str = g_strdup_printf( |
289 _("Sorry, passwords over %d characters in length (yours is " | |
290 "%d) are currently not supported by the MySpaceIM plugin."), | |
291 MSIM_MAX_PASSWORD_LENGTH, | |
289 (int)strlen(acct->password)); | 292 (int)strlen(acct->password)); |
290 | 293 |
291 /* Notify an error message also, because this is important! */ | 294 /* Notify an error message also, because this is important! */ |
292 purple_notify_error(acct, g_strdup(_("MySpaceIM Error")), str, NULL); | 295 purple_notify_error(acct, g_strdup(_("MySpaceIM Error")), str, NULL); |
293 | 296 |
766 decor = atol(decor_str); | 769 decor = atol(decor_str); |
767 else | 770 else |
768 decor = 0; | 771 decor = 0; |
769 | 772 |
770 gs_begin = g_string_new(""); | 773 gs_begin = g_string_new(""); |
771 g_string_printf(gs_begin, "<font face='%s' size='%d'>", face, | 774 if (!face) |
772 msim_font_size_to_purple(msim_font_height_to_point(height))); | 775 g_string_printf(gs_begin, "<font size='%d'>", |
776 msim_font_size_to_purple(msim_font_height_to_point(height))); | |
777 else | |
778 g_string_printf(gs_begin, "<font face='%s' size='%d'>", face, | |
779 msim_font_size_to_purple(msim_font_height_to_point(height))); | |
780 | |
773 /* No support for font-size CSS? */ | 781 /* No support for font-size CSS? */ |
774 /* g_string_printf(gs_begin, "<span style='font-family: %s; font-size: %dpt'>", face, | 782 /* g_string_printf(gs_begin, "<span style='font-family: %s; font-size: %dpt'>", face, |
775 msim_font_height_to_point(height)); */ | 783 msim_font_height_to_point(height)); */ |
776 | 784 |
777 gs_end = g_string_new("</font>"); | 785 gs_end = g_string_new("</font>"); |
813 static void msim_markup_c_to_html(xmlnode *root, gchar **begin, gchar **end) | 821 static void msim_markup_c_to_html(xmlnode *root, gchar **begin, gchar **end) |
814 { | 822 { |
815 const gchar *color; | 823 const gchar *color; |
816 | 824 |
817 color = xmlnode_get_attrib(root, "v"); | 825 color = xmlnode_get_attrib(root, "v"); |
826 if (!color) | |
827 { | |
828 purple_debug_info("msim", "msim_markup_c_to_html: <c> tag w/o v attr"); | |
829 *begin = g_strdup(""); | |
830 *end = g_strdup(""); | |
831 /* TODO: log as unrecognized */ | |
832 return; | |
833 } | |
818 | 834 |
819 /* TODO: parse rgb(255,0,0) into #FF0000, etc. | 835 /* TODO: parse rgb(255,0,0) into #FF0000, etc. |
820 * And do something about rgba (alpha) and transparent. | 836 * And do something about rgba (alpha) and transparent. |
821 */ | 837 */ |
822 /* *begin = g_strdup_printf("<font color='%s'>", color); */ | 838 *begin = g_strdup_printf("<font color='%s'>", color); |
823 *begin = g_strdup_printf("<span style='color: %s'>", color); | 839 /* *begin = g_strdup_printf("<span style='color: %s'>", color); */ |
824 *end = g_strdup("</p>"); | 840 *end = g_strdup("</font>"); |
825 } | 841 } |
826 | 842 |
827 /** Convert the msim markup <b> tag (background color) into HTML. TODO: Test */ | 843 /** Convert the msim markup <b> tag (background color) into HTML. TODO: Test */ |
828 static void msim_markup_b_to_html(xmlnode *root, gchar **begin, gchar **end) | 844 static void msim_markup_b_to_html(xmlnode *root, gchar **begin, gchar **end) |
829 { | 845 { |
830 const gchar *color; | 846 const gchar *color; |
831 | 847 |
832 color = xmlnode_get_attrib(root, "v"); | 848 color = xmlnode_get_attrib(root, "v"); |
849 if (!color) | |
850 { | |
851 *begin = g_strdup(""); | |
852 *end = g_strdup(""); | |
853 purple_debug_info("msim", "msim_markup_b_to_html: <b> w/o v attr"); | |
854 /* TODO: log as unrecognized. */ | |
855 return; | |
856 } | |
833 | 857 |
834 /* TODO: parse color same as msim_markup_c_to_html(). */ | 858 /* TODO: parse color same as msim_markup_c_to_html(). */ |
859 /* TODO: find out how to set background color. */ | |
835 *begin = g_strdup_printf("<span style='background-color: %s'>", color); | 860 *begin = g_strdup_printf("<span style='background-color: %s'>", color); |
836 *end = g_strdup("</p>"); | 861 *end = g_strdup("</p>"); |
837 } | 862 } |
838 | 863 |
839 /** Convert the msim markup <i> tag (emoticon image) into HTML. TODO: Test */ | 864 /** Convert the msim markup <i> tag (emoticon image) into HTML. TODO: Test */ |
840 static void msim_markup_i_to_html(xmlnode *root, gchar **begin, gchar **end) | 865 static void msim_markup_i_to_html(xmlnode *root, gchar **begin, gchar **end) |
841 { | 866 { |
842 const gchar *name; | 867 const gchar *name; |
843 | 868 |
844 name = xmlnode_get_attrib(root, "n"); | 869 name = xmlnode_get_attrib(root, "n"); |
870 if (!name) | |
871 { | |
872 purple_debug_info("msim", "msim_markup_i_to_html: <i> w/o n"); | |
873 *begin = g_strdup(""); | |
874 *end = g_strdup(""); | |
875 /* TODO: log as unrecognized */ | |
876 return; | |
877 } | |
845 | 878 |
846 /* TODO: Support these emoticons: | 879 /* TODO: Support these emoticons: |
847 * | 880 * |
848 * bigsmile growl mad scared tongue devil happy messed sidefrown upset | 881 * bigsmile growl mad scared tongue devil happy messed sidefrown upset |
849 * frazzled heart nerd sinister wink geek laugh oops smirk worried | 882 * frazzled heart nerd sinister wink geek laugh oops smirk worried |
862 { | 895 { |
863 xmlnode *node; | 896 xmlnode *node; |
864 gchar *begin, *inner, *end; | 897 gchar *begin, *inner, *end; |
865 gchar *final; | 898 gchar *final; |
866 | 899 |
867 if (!root) | 900 if (!root || !root->name) |
868 return g_strdup(""); | 901 return g_strdup(""); |
869 | 902 |
870 purple_debug_info("msim", "msim_markup_xmlnode_to_html: got root=%s\n", | 903 purple_debug_info("msim", "msim_markup_xmlnode_to_html: got root=%s\n", |
871 root->name); | 904 root->name); |
872 | 905 |
1097 gchar *body_str; | 1130 gchar *body_str; |
1098 MsimMessage *msg; | 1131 MsimMessage *msg; |
1099 gchar *user; | 1132 gchar *user; |
1100 PurpleNotifyUserInfo *user_info; | 1133 PurpleNotifyUserInfo *user_info; |
1101 PurpleBuddy *buddy; | 1134 PurpleBuddy *buddy; |
1102 gchar *song; | 1135 const gchar *str, *str2; |
1103 | 1136 |
1104 | 1137 |
1105 /* Get user{name,id} from msim_get_info, passed as an MsimMessage for orthogonality. */ | 1138 /* Get user{name,id} from msim_get_info, passed as an MsimMessage for |
1139 orthogonality. */ | |
1106 msg = (MsimMessage *)data; | 1140 msg = (MsimMessage *)data; |
1107 user = g_strdup(msim_msg_get_string(msg, "user")); | 1141 user = msim_msg_get_string(msg, "user"); |
1142 if (!user) | |
1143 { | |
1144 purple_debug_info("msim", "msim_get_info_cb: no 'user' in msg"); | |
1145 return; | |
1146 } | |
1147 | |
1108 purple_debug_info("msim", "msim_get_info_cb: got for user: %s\n", user); | 1148 purple_debug_info("msim", "msim_get_info_cb: got for user: %s\n", user); |
1109 msim_msg_free(msg); | 1149 msim_msg_free(msg); |
1110 | 1150 |
1111 | 1151 |
1112 body_str = msim_msg_get_string(user_info_msg, "body"); | 1152 body_str = msim_msg_get_string(user_info_msg, "body"); |
1117 /* Note: don't assume buddy is non-NULL; will be if lookup random user not on blist. */ | 1157 /* Note: don't assume buddy is non-NULL; will be if lookup random user not on blist. */ |
1118 | 1158 |
1119 user_info = purple_notify_user_info_new(); | 1159 user_info = purple_notify_user_info_new(); |
1120 | 1160 |
1121 /* Identification */ | 1161 /* Identification */ |
1122 purple_notify_user_info_add_pair(user_info, _("User"), user); | 1162 purple_notify_user_info_add_pair(user_info, _("User"), g_strdup(user)); |
1123 | 1163 |
1124 /* note: g_hash_table_lookup does not create a new string! */ | 1164 /* note: g_hash_table_lookup does not create a new string! */ |
1125 purple_notify_user_info_add_pair(user_info, _("User ID"), | 1165 str = g_hash_table_lookup(body, "UserID"); |
1126 g_strdup(g_hash_table_lookup(body, "UserID"))); | 1166 if (str) |
1167 purple_notify_user_info_add_pair(user_info, _("User ID"), | |
1168 g_strdup(str)); | |
1127 | 1169 |
1128 /* a/s/l...the vitals */ | 1170 /* a/s/l...the vitals */ |
1129 purple_notify_user_info_add_pair(user_info, _("Age"), | 1171 str = g_hash_table_lookup(body, "Age"); |
1130 g_strdup(g_hash_table_lookup(body, "Age"))); | 1172 if (str) |
1131 | 1173 purple_notify_user_info_add_pair(user_info, _("Age"), g_strdup(str)); |
1132 purple_notify_user_info_add_pair(user_info, _("Gender"), | 1174 |
1133 g_strdup(g_hash_table_lookup(body, "Gender"))); | 1175 str = g_hash_table_lookup(body, "Gender"); |
1134 | 1176 if (str) |
1135 purple_notify_user_info_add_pair(user_info, _("Location"), | 1177 purple_notify_user_info_add_pair(user_info, _("Gender"), g_strdup(str)); |
1136 g_strdup(g_hash_table_lookup(body, "Location"))); | 1178 |
1179 str = g_hash_table_lookup(body, "Location"); | |
1180 if (str) | |
1181 purple_notify_user_info_add_pair(user_info, _("Location"), | |
1182 g_strdup(str)); | |
1137 | 1183 |
1138 /* Other information */ | 1184 /* Other information */ |
1139 | 1185 |
1140 /* Headline comes from buddy status messages */ | 1186 /* Headline comes from buddy status messages */ |
1141 if (buddy && purple_blist_node_get_string(&buddy->node, "Headline")) | 1187 if (buddy) |
1142 purple_notify_user_info_add_pair(user_info, "Headline", | 1188 { |
1143 purple_blist_node_get_string(&buddy->node, "Headline")); | 1189 str = purple_blist_node_get_string(&buddy->node, "Headline"); |
1144 | 1190 if (str) |
1145 song = g_strdup_printf("%s - %s", | 1191 purple_notify_user_info_add_pair(user_info, "Headline", str); |
1146 (gchar *)g_hash_table_lookup(body, "BandName"), | 1192 } |
1147 (gchar *)g_hash_table_lookup(body, "SongName")); | 1193 |
1148 | 1194 |
1149 | 1195 str = g_hash_table_lookup(body, "BandName"); |
1150 purple_notify_user_info_add_pair(user_info, _("Song"), song); | 1196 str2 = g_hash_table_lookup(body, "SongName"); |
1151 /* Do not free song - used by user_info. */ | 1197 if (str || str2) |
1198 { | |
1199 purple_notify_user_info_add_pair(user_info, _("Song"), | |
1200 g_strdup_printf("%s - %s", | |
1201 str ? str : "Unknown Artist", | |
1202 str2 ? str2 : "Unknown Song")); | |
1203 } | |
1204 | |
1152 | 1205 |
1153 /* Total friends only available if looked up by uid, not username. */ | 1206 /* Total friends only available if looked up by uid, not username. */ |
1154 if (g_hash_table_lookup(body, "TotalFriends")) | 1207 str = g_hash_table_lookup(body, "TotalFriends"); |
1208 if (str) | |
1155 purple_notify_user_info_add_pair(user_info, _("Total Friends"), | 1209 purple_notify_user_info_add_pair(user_info, _("Total Friends"), |
1156 g_strdup(g_hash_table_lookup(body, "TotalFriends"))); | 1210 g_strdup(str)); |
1157 | 1211 |
1158 purple_notify_userinfo(session->gc, user, user_info, NULL, NULL); | 1212 purple_notify_userinfo(session->gc, user, user_info, NULL, NULL); |
1159 purple_debug_info("msim", "msim_get_info_cb: username=%s\n", user); | 1213 purple_debug_info("msim", "msim_get_info_cb: username=%s\n", user); |
1160 //purple_notify_user_info_destroy(user_info); | 1214 //purple_notify_user_info_destroy(user_info); |
1161 /* Do not free username, since it will be used by user_info. */ | 1215 /* Do not free username, since it will be used by user_info. */ |
1623 g_return_val_if_fail(msg != NULL, FALSE); | 1677 g_return_val_if_fail(msg != NULL, FALSE); |
1624 | 1678 |
1625 err = msim_msg_get_integer(msg, "err"); | 1679 err = msim_msg_get_integer(msg, "err"); |
1626 errmsg = msim_msg_get_string(msg, "errmsg"); | 1680 errmsg = msim_msg_get_string(msg, "errmsg"); |
1627 | 1681 |
1628 full_errmsg = g_strdup_printf(_("Protocol error, code %d: %s"), err, errmsg); | 1682 full_errmsg = g_strdup_printf(_("Protocol error, code %d: %s"), err, |
1683 errmsg ? errmsg : "no 'errmsg' given"); | |
1629 | 1684 |
1630 g_free(errmsg); | 1685 g_free(errmsg); |
1631 | 1686 |
1632 purple_debug_info("msim", "msim_error: %s\n", full_errmsg); | 1687 purple_debug_info("msim", "msim_error: %s\n", full_errmsg); |
1633 | 1688 |
2249 session = (MsimSession *)gc->proto_data; | 2304 session = (MsimSession *)gc->proto_data; |
2250 | 2305 |
2251 if (source < 0) | 2306 if (source < 0) |
2252 { | 2307 { |
2253 purple_connection_error(gc, _("Couldn't connect to host")); | 2308 purple_connection_error(gc, _("Couldn't connect to host")); |
2254 purple_connection_error(gc, g_strdup_printf(_("Couldn't connect to host: %s (%d)"), | 2309 purple_connection_error(gc, g_strdup_printf( |
2255 error_message, source)); | 2310 _("Couldn't connect to host: %s (%d)"), |
2311 error_message ? error_message : "no message given", | |
2312 source)); | |
2256 return; | 2313 return; |
2257 } | 2314 } |
2258 | 2315 |
2259 session->fd = source; | 2316 session->fd = source; |
2260 | 2317 |
2446 "dsn", MSIM_TYPE_INTEGER, dsn, | 2503 "dsn", MSIM_TYPE_INTEGER, dsn, |
2447 "uid", MSIM_TYPE_INTEGER, session->userid, | 2504 "uid", MSIM_TYPE_INTEGER, session->userid, |
2448 "lid", MSIM_TYPE_INTEGER, lid, | 2505 "lid", MSIM_TYPE_INTEGER, lid, |
2449 "rid", MSIM_TYPE_INTEGER, rid, | 2506 "rid", MSIM_TYPE_INTEGER, rid, |
2450 /* TODO: dictionary field type */ | 2507 /* TODO: dictionary field type */ |
2451 "body", MSIM_TYPE_STRING, g_strdup_printf("%s=%s", field_name, user), | 2508 "body", MSIM_TYPE_STRING, |
2509 g_strdup_printf("%s=%s", field_name, user), | |
2452 NULL)); | 2510 NULL)); |
2453 } | 2511 } |
2454 | 2512 |
2455 | 2513 |
2456 /** | 2514 /** |