void translate_sending_chat_message_cb(const gchar *original_phrase, const gchar *translated_phrase, const gchar *detected_language, gpointer userdata) { struct TranslateConvMessage *convmsg = userdata; gchar *html_text; int err = 0; html_text = purple_strdup_withhtml(translated_phrase); err = serv_chat_send(purple_account_get_connection(convmsg->account), purple_conv_chat_get_id(PURPLE_CONV_CHAT(convmsg->conv)), html_text, convmsg->flags); g_free(html_text); html_text = purple_strdup_withhtml(original_phrase); //if (err > 0) //{ // purple_conversation_write(convmsg->conv, convmsg->sender, html_text, convmsg->flags, time(NULL)); //} purple_signal_emit(purple_conversations_get_handle(), "sent-chat-msg", convmsg->account, html_text, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convmsg->conv))); g_free(html_text); g_free(convmsg->sender); g_free(convmsg); }
int yahoo_c_send(PurpleConnection *gc, int id, const char *what, PurpleMessageFlags flags) { PurpleConversation *c; int ret; YahooData *yd; yd = (YahooData *) gc->proto_data; if (!yd) return -1; c = purple_find_chat(gc, id); if (!c) return -1; if (id != YAHOO_CHAT_ID) { ret = yahoo_conf_send(gc, purple_connection_get_display_name(gc), purple_conversation_get_name(c), purple_conv_chat_get_users(PURPLE_CONV_CHAT(c)), what); } else { ret = yahoo_chat_send(gc, purple_connection_get_display_name(gc), purple_conversation_get_name(c), what, flags); if (!ret) serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(c)), purple_connection_get_display_name(gc), flags, what, time(NULL)); } return ret; }
static int waprpl_send_chat(PurpleConnection *gc, int id, const char *message, PurpleMessageFlags flags) { whatsapp_connection * wconn = purple_connection_get_protocol_data(gc); PurpleAccount *account = purple_connection_get_account(gc); PurpleConversation *convo = purple_find_chat(gc, id); PurpleBlistNode* node = purple_blist_get_root(); GHashTable* hasht = NULL; while (node != 0) { if (PURPLE_BLIST_NODE_IS_CHAT(node)) { PurpleChat * ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) == account) { hasht = purple_chat_get_components(ch); if (chatid_to_convo(g_hash_table_lookup(hasht, "id")) == id) { break; } } } node = purple_blist_node_next(node,FALSE); } char * chat_id = g_hash_table_lookup(hasht, "id"); waAPI_sendchat(wconn->waAPI,chat_id,message); waprpl_check_output(gc); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), purple_connection_get_display_name(gc), PURPLE_MESSAGE_SEND, message, time(NULL)); purple_conv_im_write(PURPLE_CONV_CHAT(convo), purple_connection_get_display_name(gc), message, PURPLE_MESSAGE_SEND, time(NULL)); return 1; }
void irc_msg_kick(struct irc_conn *irc, const char *name, const char *from, char **args) { PurpleConnection *gc = purple_account_get_connection(irc->account); PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, args[0], irc->account); char *nick = irc_mask_nick(from), *buf; if (!gc) { g_free(nick); return; } if (!convo) { purple_debug(PURPLE_DEBUG_ERROR, "irc", "Recieved a KICK for unknown channel %s\n", args[0]); g_free(nick); return; } if (!purple_utf8_strcasecmp(purple_connection_get_display_name(gc), args[1])) { buf = g_strdup_printf(_("You have been kicked by %s: (%s)"), nick, args[2]); purple_conv_chat_write(PURPLE_CONV_CHAT(convo), args[0], buf, PURPLE_MESSAGE_SYSTEM, time(NULL)); g_free(buf); serv_got_chat_left(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo))); } else { buf = g_strdup_printf(_("Kicked by %s (%s)"), nick, args[2]); purple_conv_chat_remove_user(PURPLE_CONV_CHAT(convo), args[1], buf); g_free(buf); } g_free(nick); return; }
void yahoo_process_conference_decline(PurpleConnection *gc, struct yahoo_packet *pkt) { GSList *l; char *room = NULL; char *who = NULL; char *msg = NULL; PurpleConversation *c = NULL; int utf8 = 0; for (l = pkt->hash; l; l = l->next) { struct yahoo_pair *pair = l->data; switch (pair->key) { case 57: g_free(room); room = yahoo_string_decode(gc, pair->value, FALSE); break; case 54: who = pair->value; break; case 14: g_free(msg); msg = yahoo_string_decode(gc, pair->value, FALSE); break; case 97: utf8 = strtol(pair->value, NULL, 10); break; } } if (!purple_privacy_check(purple_connection_get_account(gc), who)) { g_free(room); g_free(msg); return; } if (who && room) { /* make sure we're in the room before we process a decline message for it */ if((c = yahoo_find_conference(gc, room))) { char *tmp = NULL, *msg_tmp = NULL; if(msg) { msg_tmp = yahoo_string_decode(gc, msg, utf8); msg = yahoo_codes_to_html(msg_tmp); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(c)), who, 0, msg, time(NULL)); g_free(msg_tmp); g_free(msg); } tmp = g_strdup_printf(_("%s has declined to join."), who); purple_conversation_write(c, NULL, tmp, PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NO_LINKIFY, time(NULL)); g_free(tmp); } g_free(room); } }
JabberChat *jabber_chat_find_by_conv(PurpleConversation *conv) { PurpleAccount *account = purple_conversation_get_account(conv); PurpleConnection *gc = purple_account_get_connection(account); JabberStream *js = gc->proto_data; int id = purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)); return jabber_chat_find_by_id(js, id); }
static void conv_add_message(PurpleConnection * gc, const char *who, const char *msg, const char *author, unsigned long timestamp) { if (isgroup(who)) { PurpleConversation *convo = get_open_combo(who, gc); if (convo) serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), author, PURPLE_MESSAGE_RECV, msg, timestamp); } else { serv_got_im(gc, who, msg, PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_IMAGES, timestamp); } }
void purple_chat_invite( struct groupchat *gc, char *who, char *message ) { PurpleConversation *pc = gc->data; PurpleConvChat *pcc = PURPLE_CONV_CHAT( pc ); serv_chat_invite( purple_account_get_connection( gc->ic->proto_data ), purple_conv_chat_get_id( pcc ), message && *message ? message : "Please join my chat", who ); }
void PurpleChatMngr::ChatJoinedCbk(PurpleConversation *conv) { mConvInfo_t *mConv = (mConvInfo_t *) conv->ui_data; if (mConv->pending_invites) { for (GList *l = mConv->pending_invites; l != NULL; l = l->next) { serv_chat_invite(purple_conversation_get_gc(conv), purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)), "Join my conference...", (char *)l->data); } } }
static VALUE conversation_get_chat_id(VALUE self) { PurpleConversation *conversation; Data_Get_Struct(self, PurpleConversation, conversation); PurpleConvChat *chat_data = PURPLE_CONV_CHAT(conversation); if (chat_data != NULL) { int id = purple_conv_chat_get_id(chat_data); return INT2FIX(id); } return Qnil; }
static void datacast_inform_user(NPSwitchBoard *swboard, const char *who, const char *msg, const char *filename) { char *username, *str; PurpleAccount *account; PurpleBuddy *b; PurpleConnection *pc; gboolean chat; account = swboard->session->account; pc = purple_account_get_connection(account); if ((b = purple_find_buddy(account, who)) != NULL) username = g_markup_escape_text(purple_buddy_get_alias(b), -1); else username = g_markup_escape_text(who, -1); str = g_strdup_printf(msg, username, filename); g_free(username); swboard->flag |= NP_SB_FLAG_IM; if (swboard->current_users > 1) chat = TRUE; else chat = FALSE; if (swboard->conv == NULL) { if (chat) swboard->conv = purple_find_chat(account->gc, swboard->chat_id); else { swboard->conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account); if (swboard->conv == NULL) swboard->conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, who); } } if (chat) serv_got_chat_in(pc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(swboard->conv)), who, PURPLE_MESSAGE_RECV|PURPLE_MESSAGE_SYSTEM, str, time(NULL)); else serv_got_im(pc, who, str, PURPLE_MESSAGE_RECV|PURPLE_MESSAGE_SYSTEM, time(NULL)); g_free(str); }
void yahoo_process_conference_message(PurpleConnection *gc, struct yahoo_packet *pkt) { GSList *l; char *room = NULL; char *who = NULL; char *msg = NULL; int utf8 = 0; PurpleConversation *c; for (l = pkt->hash; l; l = l->next) { struct yahoo_pair *pair = l->data; switch (pair->key) { case 57: g_free(room); room = yahoo_string_decode(gc, pair->value, FALSE); break; case 3: who = pair->value; break; case 14: msg = pair->value; break; case 97: utf8 = strtol(pair->value, NULL, 10); break; } } if (room && who && msg) { char *msg2; c = yahoo_find_conference(gc, room); if (!c) { g_free(room); return; } msg2 = yahoo_string_decode(gc, msg, utf8); msg = yahoo_codes_to_html(msg2); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(c)), who, 0, msg, time(NULL)); g_free(msg); g_free(msg2); } g_free(room); }
bool PurpleIMChat::addContactCbk(void * dataIn) { Mutex::ScopedLock lock(PurpleIMChat::_mutex); PurpleIMChatCallbackData* cbData = (PurpleIMChatCallbackData*) dataIn; PurpleIMChat* imChat = cbData->getPurpleIMChat(); IMChatSession* chatSession = cbData->getIMChatSession(); PurpleAccount* gAccount = getPurpleAccount( imChat->getIMAccount() ); // const char * contactId = (const char *)((misc_t *)data)->data_ptr2; mConvInfo_t *mConv = FindChatStructById(chatSession->getId()); int BuddyNbr = chatSession->getIMContactSet().size(); PurpleConversation *gConv = NULL; if (mConv) gConv = mConv->purple_conv_session; //VOXOX - JRT - 2009.07.09 else LOG_FATAL("ConvInfo not created !!!"); if (purple_conversation_get_type(gConv) == PURPLE_CONV_TYPE_IM) { GList *mlist = NULL; IMContactSet& chatContact = const_cast<IMContactSet&>(chatSession->getIMContactSet()); IMContactSet::const_iterator it = chatContact.begin(); const std::string & firstContactId = it->getContactId(); PurpleConnection *gGC; gConv = mConv->purple_conv_session; //VOXOX - JRT - 2009.07.09 gGC = purple_conversation_get_gc(gConv); mlist = g_list_append(mlist, const_cast<char*>(cbData->getContactId().c_str()) ); mlist = g_list_append(mlist, (char *) firstContactId.c_str()); createPurpleChat(gGC, chatSession->getId(), mlist, *imChat, gAccount); //VOXOX - JRT - 2009.07.10 } else if (purple_conversation_get_type(gConv) == PURPLE_CONV_TYPE_CHAT) { serv_chat_invite(purple_conversation_get_gc(gConv), purple_conv_chat_get_id(PURPLE_CONV_CHAT(gConv)), NULL, cbData->getContactId().c_str() ); } timeoutRemove( cbData ); delete cbData; return TRUE; }
static void irc_msg_handle_privmsg(struct irc_conn *irc, const char *name, const char *from, const char *to, const char *rawmsg, gboolean notice) { PurpleConnection *gc = purple_account_get_connection(irc->account); PurpleConversation *convo; char *tmp; char *msg; char *nick; if (!gc) return; nick = irc_mask_nick(from); tmp = irc_parse_ctcp(irc, nick, to, rawmsg, notice); if (!tmp) { g_free(nick); return; } msg = irc_escape_privmsg(tmp, -1); g_free(tmp); tmp = irc_mirc2html(msg); g_free(msg); msg = tmp; if (notice) { tmp = g_strdup_printf("(notice) %s", msg); g_free(msg); msg = tmp; } if (!purple_utf8_strcasecmp(to, purple_connection_get_display_name(gc))) { serv_got_im(gc, nick, msg, 0, time(NULL)); } else { convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, to, irc->account); if (convo) serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), nick, 0, msg, time(NULL)); else purple_debug_error("irc", "Got a %s on %s, which does not exist\n", notice ? "NOTICE" : "PRIVMSG", to); } g_free(msg); g_free(nick); }
void irc_msg_part(struct irc_conn *irc, const char *name, const char *from, char **args) { PurpleConnection *gc = purple_account_get_connection(irc->account); PurpleConversation *convo; char *nick, *msg, *channel; if (!args || !args[0] || !gc) return; /* Undernet likes to :-quote the channel name, for no good reason * that I can see. This catches that. */ channel = (args[0][0] == ':') ? &args[0][1] : args[0]; convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, channel, irc->account); if (!convo) { purple_debug(PURPLE_DEBUG_INFO, "irc", "Got a PART on %s, which doesn't exist -- probably closed\n", channel); return; } nick = irc_mask_nick(from); if (!purple_utf8_strcasecmp(nick, purple_connection_get_display_name(gc))) { char *escaped = g_markup_escape_text(args[1], -1); msg = g_strdup_printf(_("You have parted the channel%s%s"), (args[1] && *args[1]) ? ": " : "", (escaped && *escaped) ? escaped : ""); g_free(escaped); purple_conv_chat_write(PURPLE_CONV_CHAT(convo), channel, msg, PURPLE_MESSAGE_SYSTEM, time(NULL)); g_free(msg); serv_got_chat_left(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo))); } else { msg = args[1] ? irc_mirc2txt(args[1]) : NULL; purple_conv_chat_remove_user(PURPLE_CONV_CHAT(convo), nick, msg); g_free(msg); } g_free(nick); }
void PurpleLine::write_message(line::Message &msg, bool replay) { std::string text; int flags = 0; time_t mtime = (time_t)(msg.createdTime / 1000); bool sent = (msg.from == profile.mid); if (std::find(recent_messages.cbegin(), recent_messages.cend(), msg.id) != recent_messages.cend()) { // We already processed this message. User is probably talking with himself. return; } // Hack if (msg.from == msg.to) push_recent_message(msg.id); PurpleConversation *conv = purple_find_conversation_with_account( (msg.toType == line::MIDType::USER ? PURPLE_CONV_TYPE_IM : PURPLE_CONV_TYPE_CHAT), ((!sent && msg.toType == line::MIDType::USER) ? msg.from.c_str() : msg.to.c_str()), acct); // If this is a new received IM, create the conversation if it doesn't exist if (!conv && !sent && msg.toType == line::MIDType::USER) conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, acct, msg.from.c_str()); // If this is a new conversation, we're not replaying history and history hasn't been fetched // yet, queue the message instead of showing it. if (conv && !replay) { auto *queue = (std::vector<line::Message> *) purple_conversation_get_data(conv, "line-message-queue"); if (queue) { queue->push_back(msg); return; } } // Replaying messages from history // Unfortunately Pidgin displays messages with this flag with odd formatting and no username. // Disable for now. //if (replay) // flags |= PURPLE_MESSAGE_NO_LOG; switch (msg.contentType) { case line::ContentType::NONE: // actually text case line::ContentType::LOCATION: if (msg.__isset.location) { line::Location &loc = msg.location; text = markup_escape(loc.title) + " | <a href=\"https://maps.google.com/?q=" + url_encode(loc.address) + "&ll=" + std::to_string(loc.latitude) + "," + std::to_string(loc.longitude) + "\">" + (loc.address.size() ? markup_escape(loc.address) : "(no address)") + "</a>"; } else { text = markup_escape(msg.text); } break; case line::ContentType::STICKER: { std::string id = get_sticker_id(msg); if (id == "") { text = "<em>[Broken sticker]</em>"; purple_debug_warning("line", "Got a broken sticker.\n"); } else { text = id; if (conv && purple_conv_custom_smiley_add(conv, id.c_str(), "id", id.c_str(), TRUE)) { http.request(get_sticker_url(msg), [this, id, conv](int status, const guchar *data, gsize len) { if (status == 200 && data && len > 0) { purple_conv_custom_smiley_write( conv, id.c_str(), data, len); } else { purple_debug_warning( "line", "Couldn't download sticker. Status: %d\n", status); } purple_conv_custom_smiley_close(conv, id.c_str()); }); } } } break; case line::ContentType::IMAGE: case line::ContentType::VIDEO: // Videos could really benefit from streaming... { std::string type_std = line::_ContentType_VALUES_TO_NAMES.at(msg.contentType); std::string id = "[LINE " + type_std + " " + msg.id + "]"; text = id; if (conv) { text += " <font color=\"#888888\">/open " + conv_attachment_add(conv, msg.contentType, msg.id) + "</font>"; } if (!conv || !purple_conv_custom_smiley_add(conv, id.c_str(), "id", id.c_str(), TRUE)) { break; } if (msg.contentPreview.size() > 0) { purple_conv_custom_smiley_write( conv, id.c_str(), (const guchar *)msg.contentPreview.c_str(), msg.contentPreview.size()); purple_conv_custom_smiley_close(conv, id.c_str()); } else { std::string preview_url = msg.contentMetadata.count("PREVIEW_URL") ? msg.contentMetadata["PREVIEW_URL"] : std::string(LINE_OS_URL) + "os/m/" + msg.id + "/preview"; http.request(preview_url, HTTPFlag::AUTH | HTTPFlag::LARGE, [this, id, conv](int status, const guchar *data, gsize len) { if (status == 200 && data && len > 0) { purple_conv_custom_smiley_write( conv, id.c_str(), data, len); } else { purple_debug_warning( "line", "Couldn't download image message. Status: %d\n", status); } purple_conv_custom_smiley_close(conv, id.c_str()); }); } } break; case line::ContentType::AUDIO: { text = "[Audio message"; if (msg.contentMetadata.count("AUDLEN")) { int len = 0; try { len = std::stoi(msg.contentMetadata["AUDLEN"]); } catch(...) { /* ignore */ } if (len > 0) { text += " " + std::to_string(len / 1000) + "." + std::to_string((len % 1000) / 100) + "s"; } } text += "]"; if (conv) { text += " <font color=\"#888888\">/open " + conv_attachment_add(conv, msg.contentType, msg.id) + "</font>"; } } break; // TODO: other content types default: text = "<em>[Not implemented: "; text += line::_ContentType_VALUES_TO_NAMES.at(msg.contentType); text += " message]</em>"; break; } if (sent) { // Messages sent by user (sync from other devices) write_message(conv, msg.from, text, mtime, flags | PURPLE_MESSAGE_SEND); } else { // Messages received from other users flags |= PURPLE_MESSAGE_RECV; if (replay) { // Write replayed messages instead of serv_got_* to avoid Pidgin's IM sound write_message(conv, msg.from, text, mtime, flags); } else { if (msg.toType == line::MIDType::USER) { serv_got_im( conn, msg.from.c_str(), text.c_str(), (PurpleMessageFlags)flags, mtime); } else if (msg.toType == line::MIDType::GROUP || msg.toType == line::MIDType::ROOM) { serv_got_chat_in( conn, purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)), msg.from.c_str(), (PurpleMessageFlags)flags, text.c_str(), mtime); } } } }
void PurpleChatMngr::ChatLeftCbk(PurpleConversation *conv) { serv_chat_leave(purple_conversation_get_gc(conv), purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)) ); }
void coincoin_parse_message(HttpHandler* handler, gchar* response, gsize len, gpointer userdata) { CoinCoinAccount* cca = handler->data; PurpleConversation* convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, "board", cca->account); if(!convo) return; // not on the board channel xmlnode* node = coincoin_xmlparse(response, len); xmlnode* post; GSList *last_msg = cca->messages; GSList *iter; GSList *messages = NULL; unsigned i; if(!node) { purple_debug(PURPLE_DEBUG_ERROR, "coincoin", "Unable to parse response.\n"); return; } for(post = xmlnode_get_child(node, "post"); post; post = xmlnode_get_next_twin(post)) { CoinCoinMessage* msg; gint64 id = strtoul(xmlnode_get_attrib(post, "id"), NULL, 10); /* Check if this message has already been showed. */ for(iter = last_msg; iter && ((CoinCoinMessage*)iter->data)->id != id; iter = iter->next) ; if(iter) break; msg = coincoin_message_new(id, post); if(!msg) continue; messages = g_slist_prepend(messages, msg); if(strcmp(msg->from, purple_connection_get_display_name(cca->pc))) { PurpleConvChatBuddy* cb = purple_conv_chat_cb_find(PURPLE_CONV_CHAT(convo), msg->from); if(!cb) purple_conv_chat_add_user(PURPLE_CONV_CHAT(convo), msg->from, msg->info, PURPLE_CBFLAGS_NONE, FALSE); } } /* Flush messages (in reversed order) */ for(iter = messages; iter; ) { CoinCoinMessage* msg = iter->data; if(!purple_account_get_bool(cca->account, "no_reformat_messages", FALSE)) coincoin_message_ref(msg, cca->messages); serv_got_chat_in(cca->pc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), msg->from, PURPLE_MESSAGE_DELAYED, msg->message, msg->timestamp); if(cca->messages && ((CoinCoinMessage*)cca->messages->data)->timestamp == msg->timestamp) { msg->multiple = ((CoinCoinMessage*)cca->messages->data)->multiple = TRUE; msg->ref = ((CoinCoinMessage*)cca->messages->data)->ref + 1; } GSList* link = iter; iter = iter->next; link->next = cca->messages; cca->messages = link; } /* Now purge extra-messages */ for(i = 0, iter = last_msg; iter; ++i) { if(i < CC_LAST_MESSAGE_MAX) iter = iter->next; else if(i == CC_LAST_MESSAGE_MAX) { GSList* prev; prev = iter; iter = iter->next; prev->next = NULL; } else { /* This user doesn't participate to conversation * anymore. So it can leave channel. */ CoinCoinMessage* cur = iter->data; if(strcmp(cur->from, purple_connection_get_display_name(cca->pc)) && purple_conv_chat_cb_find(PURPLE_CONV_CHAT(convo), cur->from)) { GSList* it = cca->messages; while(it && it != iter && strcmp(((CoinCoinMessage*)it->data)->from, cur->from)) it = it->next; if(it == iter || !it) purple_conv_chat_remove_user(PURPLE_CONV_CHAT(convo), cur->from, NULL); } coincoin_message_free(cur); iter->data = NULL; iter = g_slist_delete_link(iter, iter); } } xmlnode_free(node); }
static void nap_callback(gpointer data, gint source, PurpleInputCondition condition) { PurpleConnection *gc = data; struct nap_data *ndata = gc->proto_data; PurpleAccount *account = NULL; PurpleConversation *c = NULL; PurpleNotifyUserInfo *pnui = NULL; gchar *buf = NULL, *buf2 = NULL, *buf3 = NULL, **res = NULL; unsigned short header[2] = { 0 , 0 }; int len = 0; int command = 0; int i; account = purple_connection_get_account(gc); if (read(source, (void*)header, 4) != 4) { purple_connection_error(gc, _("Unable to read header from server")); return; } len = header[0]; command = header[1]; buf = (gchar *)g_malloc((len + 1) * sizeof(gchar)); buf[len] = '\0'; i = 0; do { int tmp = read(source, buf + i, len - i); if (tmp <= 0) { g_free(buf); buf = g_strdup_printf(_("Unable to read message from server: %s. Command is %hd, length is %hd."), strerror(errno), len, command); purple_connection_error(gc, buf); g_free(buf); return; } i += tmp; } while (i != len); purple_debug(PURPLE_DEBUG_MISC, "napster", "R %3hd: %s\n", command, buf); switch (command) { case 000: /* MSG_SERVER_ERROR */ purple_notify_error(gc, NULL, buf, NULL); purple_input_remove(gc->inpa); gc->inpa = 0; close(source); purple_connection_error(gc, _("Unknown server error.")); break; case 003: /* MSG_SERVER_EMAIL */ purple_debug(PURPLE_DEBUG_MISC, "napster", "Registered with e-mail address: %s\n", buf); ndata->email = g_strdup(buf); /* Our signon is complete */ purple_connection_set_state(gc, PURPLE_CONNECTED); /* Send the server our buddy list */ nap_send_buddylist(gc); break; case 201: /* MSG_SERVER_SEARCH_RESULT */ res = g_strsplit(buf, " ", 0); purple_prpl_got_user_status(account, res[0], "available", NULL); g_strfreev(res); break; case 202: /* MSG_SERVER_SEARCH_END */ purple_prpl_got_user_status(account, buf, "offline", NULL); break; case 205: /* MSG_CLIENT_PRIVMSG */ res = g_strsplit(buf, " ", 2); buf2 = g_markup_escape_text(res[1], -1); serv_got_im(gc, res[0], buf2, 0, time(NULL)); g_free(buf2); g_strfreev(res); break; case 209: /* MSG_SERVER_USER_SIGNON */ /* USERNAME SPEED */ res = g_strsplit(buf, " ", 2); purple_prpl_got_user_status(account, res[0], "available", NULL); g_strfreev(res); break; case 210: /* MSG_SERVER_USER_SIGNOFF */ /* USERNAME SPEED */ res = g_strsplit(buf, " ", 2); purple_prpl_got_user_status(account, res[0], "offline", NULL); g_strfreev(res); break; case 214: /* MSG_SERVER_STATS */ res = g_strsplit(buf, " ", 3); buf2 = g_strdup_printf(_("users: %s, files: %s, size: %sGB"), res[0], res[1], res[2]); serv_got_im(gc, "server", buf2, 0, time(NULL)); g_free(buf2); g_strfreev(res); break; case 301: /* MSG_SERVER_HOTLIST_ACK */ /* Our buddy was added successfully */ break; case 302: /* MSG_SERVER_HOTLIST_ERROR */ buf2 = g_strdup_printf(_("Unable to add \"%s\" to your Napster hotlist"), buf); purple_notify_error(gc, NULL, buf2, NULL); g_free(buf2); break; case 316: /* MSG_SERVER_DISCONNECTING */ /* we have been kicked off =^( */ purple_connection_error(gc, _("You were disconnected from the server.")); break; case 401: /* MSG_CLIENT_PART */ c = nap_find_chat(gc, buf); if (c) serv_got_chat_left(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(c))); break; case 403: /* MSG_SERVER_PUBLIC */ res = g_strsplit(buf, " ", 3); c = nap_find_chat(gc, res[0]); if (c) serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(c)), res[1], 0, res[2], time((time_t)NULL)); g_strfreev(res); break; case 404: /* MSG_SERVER_NOSUCH */ /* abused by opennap servers to broadcast stuff */ buf2 = g_markup_escape_text(buf, -1); serv_got_im(gc, "server", buf2, 0, time(NULL)); g_free(buf2); break; case 405: /* MSG_SERVER_JOIN_ACK */ c = nap_find_chat(gc, buf); if (!c) serv_got_joined_chat(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(c)), buf); break; case 407: /* MSG_SERVER_PART */ res = g_strsplit(buf, " ", 0); c = nap_find_chat(gc, res[0]); purple_conv_chat_remove_user(PURPLE_CONV_CHAT(c), res[1], NULL); g_strfreev(res); break; case 406: /* MSG_SERVER_JOIN */ case 408: /* MSG_SERVER_CHANNEL_USER_LIST */ res = g_strsplit(buf, " ", 4); c = nap_find_chat(gc, res[0]); purple_conv_chat_add_user(PURPLE_CONV_CHAT(c), res[1], NULL, PURPLE_CBFLAGS_NONE, TRUE); g_strfreev(res); break; case 409: /* MSG_SERVER_CHANNEL_USER_LIST_END */ break; case 410: /* MSG_SERVER_TOPIC */ /* display the topic in the channel */ res = g_strsplit(buf, " ", 2); c = nap_find_chat(gc, res[0]); purple_conv_chat_set_topic(PURPLE_CONV_CHAT(c), res[0], res[1]); g_strfreev(res); break; case 603: /* MSG_CLIENT_WHOIS */ buf2 = g_strdup_printf(_("%s requested your information"), buf); serv_got_im(gc, "server", buf2, 0, time(NULL)); g_free(buf2); break; case 604: /* MSG_SERVER_WHOIS_RESPONSE */ /* XXX - Format is: "Elite" 37 " " "Active" 0 0 0 0 "purple 0.63cvs" 0 0 192.168.1.41 32798 0 unknown flounder */ res = g_strsplit(buf, " ", 2); /* res[0] == username */ pnui = purple_notify_user_info_new(); purple_notify_user_info_add_pair(pnui, _("Napster User Info:"), res[1]); purple_notify_userinfo(gc, res[0], pnui, NULL, NULL); g_strfreev(res); break; case 621: case 622: /* MSG_CLIENT_MOTD */ /* also replaces MSG_SERVER_MOTD, so we should display it */ buf2 = g_markup_escape_text(buf, -1); serv_got_im(gc, "motd", buf2, 0, time(NULL)); g_free(buf2); break; case 627: /* MSG_CLIENT_WALLOP */ /* abused by opennap server maintainers to broadcast stuff */ buf2 = g_markup_escape_text(buf, -1); serv_got_im(gc, "wallop", buf2, 0, time(NULL)); g_free(buf2); break; case 628: /* MSG_CLIENT_ANNOUNCE */ buf2 = g_markup_escape_text(buf, -1); serv_got_im(gc, "announce", buf2, 0, time(NULL)); g_free(buf); break; case 748: /* MSG_SERVER_GHOST */ /* Looks like someone logged in as us! =-O */ purple_connection_error(gc, _("You have signed on from another location.")); break; case 751: /* MSG_CLIENT_PING */ buf2 = g_strdup_printf(_("%s requested a PING"), buf); serv_got_im(gc, "server", buf2, 0, time(NULL)); g_free(buf2); /* send back a pong */ /* MSG_CLIENT_PONG */ nap_write_packet(gc, 752, "%s", buf); break; case 752: /* MSG_CLIENT_PONG */ buf2 = g_strdup_printf("Received pong from %s", buf); purple_notify_info(gc, NULL, buf2, NULL); g_free(buf2); break; case 824: /* MSG_CLIENT_EMOTE */ res = g_strsplit(buf, " ", 3); buf2 = g_strndup(res[2]+1, strlen(res[2]) - 2); /* chomp off the surround quotes */ buf3 = g_strdup_printf("/me %s", buf2); g_free(buf2); if ((c = nap_find_chat(gc, res[0]))) { purple_conv_chat_write(PURPLE_CONV_CHAT(c), res[1], buf3, PURPLE_MESSAGE_NICK, time(NULL)); } g_free(buf3); g_strfreev(res); break; default: purple_debug(PURPLE_DEBUG_MISC, "napster", "Unknown packet %hd: %s\n", command, buf); break; } g_free(buf); }
static void waprpl_process_incoming_events(PurpleConnection *gc) { whatsapp_connection * wconn = purple_connection_get_protocol_data(gc); PurpleAccount * acc = purple_connection_get_account(gc); switch (waAPI_loginstatus(wconn->waAPI)) { case 0: purple_connection_update_progress(gc, "Connecting", 0, 4); break; case 1: purple_connection_update_progress(gc, "Sending auth", 1, 4); break; case 2: purple_connection_update_progress(gc, "Waiting response", 2, 4); break; case 3: purple_connection_update_progress(gc, "Connected", 3, 4); purple_connection_set_state(gc, PURPLE_CONNECTED); if (!wconn->connected) waprpl_insert_contacts(gc); wconn->connected = 1; break; default: break; }; char * msg, * who, * prev, * url, *author; int status; int size; double lat,lng; // Incoming messages while (waAPI_querychat(wconn->waAPI, &who, &msg, &author)) { purple_debug_info(WHATSAPP_ID, "Got chat message from %s: %s\n", who,msg); if (isgroup(who)) { // Search fot the combo PurpleBlistNode* node = purple_blist_get_root(); GHashTable* hasht = NULL; while (node != 0) { if (PURPLE_BLIST_NODE_IS_CHAT(node)) { PurpleChat * ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) == acc) { hasht = purple_chat_get_components(ch); if (strcmp(g_hash_table_lookup(hasht, "id"),who) == 0) { break; } } } node = purple_blist_node_next(node,FALSE); } int convo_id = chatid_to_convo(who); PurpleConversation *convo = purple_find_chat(gc, convo_id); // Create a window if it's not open yet if (!convo) waprpl_chat_join(gc,hasht); if (convo != NULL) { serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), author, PURPLE_MESSAGE_RECV, msg, time(NULL)); }else{ printf("Received group message but could not find the group! %s\n",msg); } }else{ // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, msg, PURPLE_MESSAGE_RECV, time(NULL)); } } while (waAPI_querychatimage(wconn->waAPI, &who, &prev, &size, &url)) { printf("Got chat image %s %s\n",who,url); purple_debug_info(WHATSAPP_ID, "Got image from %s: %s\n", who,url); // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, g_strdup_printf("<a href=\"%s\"><img id=\"%u\"></a>",url,imgid), PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_IMAGES, time(NULL)); } while (waAPI_querychatlocation(wconn->waAPI, &who, &prev, &size, &lat, &lng)) { purple_debug_info(WHATSAPP_ID, "Got geomessage from: %s Coordinates (%f %f)\n", who,(float)lat,(float)lng); // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, g_strdup_printf("<a href=\"http://openstreetmap.org/?lat=%f&lon=%f&zoom=16\"><img src=\"%u\"></a>",lat,lng,imgid), PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_IMAGES, time(NULL)); } while (waAPI_querychatsound(wconn->waAPI, &who, &url)) { purple_debug_info(WHATSAPP_ID, "Got chat sound from %s: %s\n", who,url); // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, g_strdup_printf("<a href=\"%s\">%s</a>",url,url), PURPLE_MESSAGE_RECV , time(NULL)); } // User status change while (waAPI_querystatus(wconn->waAPI, &who, &status)) { if (status == 1) { purple_prpl_got_user_status(acc, who, "available", "message","", NULL); } else { purple_prpl_got_user_status(acc, who, "unavailable", "message","", NULL); } } // User typing info notify while (waAPI_querytyping(wconn->waAPI, &who, &status)) { if (status == 1) { serv_got_typing(gc,who,0,PURPLE_TYPING); } else { serv_got_typing_stopped(gc,who); } } // User profile picture char * icon, * hash; int len; while (waAPI_queryicon(wconn->waAPI, &who, &icon, &len, &hash)) { purple_buddy_icons_set_for_user(acc,who, g_memdup(icon,len),len, hash); } // Groups update if (waAPI_getgroupsupdated(wconn->waAPI)) { // Delete/update the chats that are in our list PurpleBlistNode* node = purple_blist_get_root(); while (node != 0) { if (PURPLE_BLIST_NODE_IS_CHAT(node)) { PurpleChat * ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) == acc) { int found = 0; GHashTable *hasht = purple_chat_get_components(ch); char * grid = g_hash_table_lookup(hasht, "id"); char * glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist,",",0); while (*gplist) { if (strcmp(*gplist,grid) == 0) { // The group is in the system, update the fields char *id,*sub,*own; waAPI_getgroupinfo(wconn->waAPI, *gplist, &sub, &own, 0); g_hash_table_insert(hasht, g_strdup("subject"), g_strdup(sub)); g_hash_table_insert(hasht, g_strdup("owner"), g_strdup(own)); found = 1; break; } gplist++; } // The group was deleted if (!found) { PurpleBlistNode* del = node; node = purple_blist_node_next(node,FALSE); purple_blist_remove_chat(del); } } } node = purple_blist_node_next(node,FALSE); } // Add new groups char * glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist,",",0); while (*gplist) { int found = 0; PurpleBlistNode* node = purple_blist_get_root(); PurpleChat * ch; while (node != 0) { if (PURPLE_BLIST_NODE_IS_CHAT(node)) { ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) == acc) { char * grid = g_hash_table_lookup(purple_chat_get_components(ch), "id"); if (strcmp(*gplist,grid) == 0) { found = 1; break; } } } node = purple_blist_node_next(node,FALSE); } if (!found) { char *sub,*own; waAPI_getgroupinfo(wconn->waAPI, *gplist, &sub, &own, 0); purple_debug_info("waprpl", "New group found %s %s\n", *gplist,sub); GHashTable * htable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); g_hash_table_insert(htable, g_strdup("subject"), g_strdup(sub)); g_hash_table_insert(htable, g_strdup("id"), g_strdup(*gplist)); g_hash_table_insert(htable, g_strdup("owner"), g_strdup(own)); ch = purple_chat_new(acc,sub,htable); purple_blist_add_chat(ch,NULL,NULL); } // Now update the open conversation that may exist char * id = g_hash_table_lookup(purple_chat_get_components(ch), "id"); int prplid = chatid_to_convo(id); PurpleConversation * conv = purple_find_chat(gc, prplid); if (conv) { char *subject, *owner, *part; if (!waAPI_getgroupinfo(wconn->waAPI, id, &subject, &owner, &part)) return; purple_conv_chat_clear_users(purple_conversation_get_chat_data(conv)); gchar **plist = g_strsplit(part,",",0); while (*plist) { purple_conv_chat_add_user (purple_conversation_get_chat_data(conv), *plist,"",PURPLE_CBFLAGS_NONE | (!strcmp(owner,*plist) ? PURPLE_CBFLAGS_FOUNDER : 0),FALSE); plist++; } } gplist++; } } }