int
oscar_send_im(PurpleConnection *gc, const char *name, const char *message, PurpleMessageFlags imflags)
{
OscarData *od;
PurpleAccount *account;
PeerConnection *conn;
int ret;
char *tmp1, *tmp2;
gboolean is_sms, is_html;
od = purple_connection_get_protocol_data(gc);
account = purple_connection_get_account(gc);
ret = 0;
is_sms = oscar_util_valid_name_sms(name);
if (od->icq && is_sms) {
/*
* We're sending to a phone number and this is ICQ,
* so send the message as an SMS using aim_icq_sendsms()
*/
int ret;
purple_debug_info("oscar", "Sending SMS to %s.\n", name);
ret = aim_icq_sendsms(od, name, message, purple_account_get_username(account));
return (ret >= 0 ? 1 : ret);
}
if (imflags & PURPLE_MESSAGE_AUTO_RESP)
tmp1 = oscar_util_format_string(message, name);
else
tmp1 = g_strdup(message);
// ====== Костыль для jasmine icq ==========
unsigned int i, msg_len = strlen(tmp1);
for (i = 0; i < msg_len; ++i) {
if (strncmp(&tmp1[i], "<br>", 4) == 0) {
i += 4;
tmp1 = (char *) realloc(tmp1, msg_len + 2);
memmove(tmp1 + i + 1, tmp1 + i, msg_len - i);
tmp1[i] = '\n';
i += 1; msg_len += 1;
}
}
tmp1[msg_len] = 0;
// =========================================
conn = peer_connection_find_by_type(od, name, OSCAR_CAPABILITY_DIRECTIM);
if ((conn != NULL) && (conn->ready))
{
/* If we're directly connected, send a direct IM */
purple_debug_info("oscar", "Sending direct IM with flags %i\n", imflags);
purple_odc_send_im(conn, tmp1, imflags);
} else {
struct buddyinfo *bi;
struct aim_sendimext_args args;
PurpleConversation *conv;
PurpleStoredImage *img;
PurpleBuddy *buddy;
conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, account);
if (strstr(tmp1, "<IMG "))
purple_conversation_write(conv, "",
_("Your IM Image was not sent. "
"You must be Direct Connected to send IM Images."),
PURPLE_MESSAGE_ERROR, time(NULL));
buddy = purple_find_buddy(account, name);
bi = g_hash_table_lookup(od->buddyinfo, purple_normalize(account, name));
if (!bi) {
bi = g_new0(struct buddyinfo, 1);
g_hash_table_insert(od->buddyinfo, g_strdup(purple_normalize(account, name)), bi);
}
args.flags = 0;
if (!is_sms && (!buddy || !PURPLE_BUDDY_IS_ONLINE(buddy)))
args.flags |= AIM_IMFLAGS_OFFLINE;
if (od->icq) {
args.features = features_icq;
args.featureslen = sizeof(features_icq);
} else {
args.features = features_aim;
args.featureslen = sizeof(features_aim);
if (imflags & PURPLE_MESSAGE_AUTO_RESP)
args.flags |= AIM_IMFLAGS_AWAY;
}
if (bi->ico_need) {
purple_debug_info("oscar",
"Sending buddy icon request with message\n");
args.flags |= AIM_IMFLAGS_BUDDYREQ;
bi->ico_need = FALSE;
}
img = purple_buddy_icons_find_account_icon(account);
if (img) {
gconstpointer data = purple_imgstore_get_data(img);
args.iconlen = purple_imgstore_get_size(img);
args.iconsum = aimutil_iconsum(data, args.iconlen);
args.iconstamp = purple_buddy_icons_get_account_icon_timestamp(account);
if ((args.iconlen != bi->ico_me_len) || (args.iconsum != bi->ico_me_csum) || (args.iconstamp != bi->ico_me_time)) {
bi->ico_informed = FALSE;
bi->ico_sent = FALSE;
}
/*
* TODO:
* For some reason sending our icon to people only works
* when we're the ones who initiated the conversation. If
* the other person sends the first IM then they never get
* the icon. We should fix that.
*/
if (!bi->ico_informed) {
purple_debug_info("oscar",
"Claiming to have a buddy icon\n");
args.flags |= AIM_IMFLAGS_HASICON;
bi->ico_me_len = args.iconlen;
bi->ico_me_csum = args.iconsum;
bi->ico_me_time = args.iconstamp;
bi->ico_informed = TRUE;
}
purple_imgstore_unref(img);
}
args.destbn = name;
if (oscar_util_valid_name_sms(name)) {
/* Messaging an SMS (mobile) user--strip HTML */
tmp2 = purple_markup_strip_html(tmp1);
is_html = FALSE;
} else {
/* ICQ 6 wants its HTML wrapped in these tags. Oblige it. */
tmp2 = g_strdup_printf("<HTML><BODY>%s</BODY></HTML>", tmp1);
is_html = TRUE;
}
g_free(tmp1);
tmp1 = tmp2;
args.msg = oscar_encode_im(tmp1, &args.msglen, &args.charset, NULL);
if (is_html && (args.msglen > MAXMSGLEN)) {
/* If the length was too long, try stripping the HTML and then running it back through
* purple_strdup_withhtml() and the encoding process. The result may be shorter. */
g_free((char *)args.msg);
tmp2 = purple_markup_strip_html(tmp1);
g_free(tmp1);
/* re-escape the entities */
tmp1 = g_markup_escape_text(tmp2, -1);
g_free(tmp2);
tmp2 = purple_strdup_withhtml(tmp1);
g_free(tmp1);
tmp1 = tmp2;
args.msg = oscar_encode_im(tmp1, &args.msglen, &args.charset, NULL);
purple_debug_info("oscar", "Sending %s as %s because the original was too long.\n",
message, (char *)args.msg);
}
purple_debug_info("oscar", "Sending IM, charset=0x%04hx, length=%" G_GSIZE_FORMAT "\n", args.charset, args.msglen);
ret = aim_im_sendch1_ext(od, &args);
g_free((char *)args.msg);
}
g_free(tmp1);
if (ret >= 0)
return 1;
return ret;
}