purpleline_login.cpp 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
#include "purpleline.hpp"

#include <sstream>
#include <iomanip>

#include <core.h>

#include <gcrypt.h>

static std::string hex_to_bytes(std::string hex) {
    if (hex.size() % 2 != 0)
        hex = std::string("0") + hex;

    std::string result(hex.size() / 2, '\0');
    for (size_t i = 0; i < result.size(); i++)
        result[i] = std::stoi(hex.substr(i * 2, 2), nullptr, 16);

    return result;
}

static std::string bytes_to_hex(std::string bytes) {
    std::ostringstream ss;

    for (size_t i = 0; i < bytes.size(); i++)
        ss << std::hex << std::setfill('0') << std::setw(2) << (unsigned)(bytes[i] & 0xff);

    return ss.str();
}

void PurpleLine::login_start() {
    purple_connection_set_state(conn, PURPLE_CONNECTING);
    purple_connection_update_progress(conn, "Logging in", 0, 3);

    std::string auth_token = purple_account_get_string(acct, LINE_ACCOUNT_AUTH_TOKEN, "");

    if (auth_token != "") {
        // There's a stored authentication token, see if it works

        c_out->send_getLastOpRevision();
        c_out->send([this, auth_token]() {
            int64_t local_rev;

            try {
                local_rev = c_out->recv_getLastOpRevision();
            } catch (line::TalkException &err) {
                if (err.code == line::ErrorCode::AUTHENTICATION_FAILED
                    || err.code == line::ErrorCode::NOT_AUTHORIZED_DEVICE)
                {
                    // Auth token expired, get a new one

                    purple_debug_info("line", "Existing auth token expired.\n");

                    purple_account_remove_setting(acct, LINE_ACCOUNT_AUTH_TOKEN);

                    get_auth_token();
                    return;
                }

                // Unknown error
                throw;
            }

            set_auth_token(auth_token);

            poller.set_local_rev(local_rev);

            // Already got the last op revision, no need to call get_last_op_revision()

            get_profile();
        });
    } else {
        // Get a new auth token

        get_auth_token();
    }
}

void PurpleLine::get_auth_token() {
    purple_debug_info("line", "Logging in with credentials to get new auth token.\n");

    c_out->send_getRSAKeyInfo(line::IdentityProvider::LINE);
    c_out->send([this]() {
        line::RSAKey key;
        std::string credentials;

        try {
            c_out->recv_getRSAKeyInfo(key);

            credentials = get_encrypted_credentials(key);
        } catch (std::exception &ex) {
            std::string msg = std::string("Could not log in. ") + ex.what();

            conn->wants_to_die = TRUE;
            purple_connection_error(
                conn,
                msg.c_str());

            return;
        }

        std::string certificate(purple_account_get_string(acct, LINE_ACCOUNT_CERTIFICATE, ""));

        std::string ui_name = "purple-line";

        GHashTable *ui_info = purple_core_get_ui_info();
        gpointer ui_name_p = g_hash_table_lookup(ui_info, "name");
        if (ui_name_p)
            ui_name = (char *)ui_name_p;

        c_out->send_loginWithIdentityCredentialForCertificate(
            line::IdentityProvider::LINE,
            key.keynm,
            credentials,
            true,
            "127.0.0.1",
            ui_name,
            certificate);
        c_out->send([this]() {
            line::LoginResult result;

            try {
                c_out->recv_loginWithIdentityCredentialForCertificate(result);
            } catch (line::TalkException &err) {
                std::string msg = "Could not log in. " + err.reason;

                conn->wants_to_die = TRUE;
                purple_connection_error(
                    conn,
                    msg.c_str());

                return;
            }

            if (result.type == line::LoginResultType::SUCCESS && result.authToken != "")
            {
                set_auth_token(result.authToken);

                get_last_op_revision();
            }
            else if (result.type == line::LoginResultType::REQUIRE_DEVICE_CONFIRM)
            {
                purple_debug_info("line", "Starting PIN verification.\n");

                pin_verifier.verify(result, [this](std::string auth_token, std::string certificate) {
                    if (certificate != "") {
                        purple_account_set_string(
                            acct,
                            LINE_ACCOUNT_CERTIFICATE,
                            certificate.c_str());
                    }

                    set_auth_token(auth_token);

                    get_last_op_revision();
                });
            }
            else
            {
                std::stringstream ss("Could not log in. Bad LoginResult type: ");
                ss << result.type;
                std::string msg = ss.str();

                purple_connection_error(
                    conn,
                    msg.c_str());
            }
        });
    });
}

// This may throw.
std::string PurpleLine::get_encrypted_credentials(line::RSAKey &key) {
    if (!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P)) {
        if (!gcry_check_version(GCRYPT_VERSION))
            throw new std::runtime_error("libgcrypt version mismatch.");

        gcry_control(GCRYCTL_DISABLE_SECMEM, 0);

        gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
    }

    std::string username = purple_account_get_username(acct);
    std::string password = purple_account_get_password(acct);

    if (username.size() > 255)
        throw std::runtime_error("Username is too long.");

    if (password.size() > 255)
        throw std::runtime_error("Password is too long.");

    std::ostringstream buf;

    buf << (char)key.sessionKey.size() << key.sessionKey;
    buf << (char)username.size() << username;
    buf << (char)password.size() << password;

    std::string modulus = hex_to_bytes(key.nvalue);
    std::string exponent = hex_to_bytes(key.evalue);

    std::string credentials = buf.str();

    gpg_error_t err;

    gcry_sexp_t public_key, plaintext, ciphertext, result;

    err = gcry_sexp_build(
        &public_key, nullptr,
        "(public-key (rsa (n %b) (e %b)))",
        (int)modulus.size(), modulus.c_str(),
        (int)exponent.size(), exponent.c_str());

    if (err)
        throw std::runtime_error(std::string("ligbcrypt public key error: ") + gpg_strerror(err));

    err = gcry_sexp_build(
        &plaintext, nullptr,
        "(data (flags pkcs1) (value %b))",
        (int)credentials.size(), credentials.c_str());

    if (err) {
        gcry_sexp_release(public_key);
        throw std::runtime_error(std::string("ligbcrypt data error: ") + gpg_strerror(err));
    }

    err = gcry_pk_encrypt(&ciphertext, plaintext, public_key);

    gcry_sexp_release(plaintext);
    gcry_sexp_release(public_key);

    if (err)
        throw std::runtime_error(std::string("libgcrypt encryption error: ") + gpg_strerror(err));

    result = gcry_sexp_find_token(ciphertext, "a", 0);

    gcry_sexp_release(ciphertext);

    if (!result)
        throw std::runtime_error("libgcrypt result token not found");

    size_t result_data_len;
    const char *result_data = gcry_sexp_nth_data(result, 1, &result_data_len);

    if (!result_data) {
        gcry_sexp_release(result);
        throw std::runtime_error("libgcrypt result token value not found");
    }

    std::string result_bytes(result_data, result_data_len);

    gcry_sexp_release(result);

    return bytes_to_hex(result_bytes);
}

void PurpleLine::set_auth_token(std::string auth_token) {
    purple_account_set_string(acct, LINE_ACCOUNT_AUTH_TOKEN, auth_token.c_str());

    // Re-open output client to update persistent headers
    c_out->close();
    c_out->set_auto_reconnect(true);
    c_out->set_path(LINE_COMMAND_PATH);
}

void PurpleLine::get_last_op_revision() {
    c_out->send_getLastOpRevision();
    c_out->send([this]() {
        poller.set_local_rev(c_out->recv_getLastOpRevision());

        get_profile();
    });
}

void PurpleLine::get_profile() {
    c_out->send_getProfile();
    c_out->send([this]() {
        c_out->recv_getProfile(profile);

        profile_contact.mid = profile.mid;
        profile_contact.displayName = profile.displayName;

        // Update display name
        purple_account_set_alias(acct, profile.displayName.c_str());

        purple_connection_set_state(conn, PURPLE_CONNECTED);
        purple_connection_update_progress(conn, "Synchronizing buddy list", 1, 3);

        // Update account icon (not sure if there's a way to tell whether it has changed, maybe
        // pictureStatus?)
        if (profile.picturePath != "") {
            std::string pic_path = profile.picturePath.substr(1) + "/preview";
            //if (icon_path != purple_account_get_string(acct, "icon_path", "")) {
                http.request(LINE_OS_URL + pic_path, HTTPFlag::AUTH,
                    [this](int status, const guchar *data, gsize len)
                {
                    if (status != 200 || !data)
                        return;

                    purple_buddy_icons_set_account_icon(
                        acct,
                        (guchar *)g_memdup(data, len),
                        len);

                    //purple_account_set_string(acct, "icon_path", icon_path.c_str());
                });
            //}
        } else {
            // TODO: Delete icon
        }

        get_contacts();
    });
}

void PurpleLine::get_contacts() {
    c_out->send_getAllContactIds();
    c_out->send([this]() {
        std::vector<std::string> uids;
        c_out->recv_getAllContactIds(uids);

        c_out->send_getContacts(uids);
        c_out->send([this]() {
            std::vector<line::Contact> contacts;
            c_out->recv_getContacts(contacts);

            std::set<PurpleBuddy *> buddies_to_delete = blist_find<PurpleBuddy>();

            for (line::Contact &contact: contacts) {
                if (contact.status == line::ContactStatus::FRIEND)
                    buddies_to_delete.erase(blist_update_buddy(contact));
            }

            for (PurpleBuddy *buddy: buddies_to_delete)
                blist_remove_buddy(purple_buddy_get_name(buddy));

            {
                // Add self as buddy for those lonely debugging conversations
                // TODO: Remove

                line::Contact self;
                self.mid = profile.mid;
                self.displayName = profile.displayName + " [Profile]";
                self.statusMessage = profile.statusMessage;
                self.picturePath = profile.picturePath;

                blist_update_buddy(self);
            }

            get_groups();
        });
    });
}

void PurpleLine::get_groups() {
    c_out->send_getGroupIdsJoined();
    c_out->send([this]() {
        std::vector<std::string> gids;
        c_out->recv_getGroupIdsJoined(gids);

        c_out->send_getGroups(gids);
        c_out->send([this]() {
            std::vector<line::Group> groups;
            c_out->recv_getGroups(groups);

            std::set<PurpleChat *> chats_to_delete = blist_find_chats_by_type(ChatType::GROUP);

            for (line::Group &group: groups)
                chats_to_delete.erase(blist_update_chat(group));

            for (PurpleChat *chat: chats_to_delete)
                purple_blist_remove_chat(chat);

            get_rooms();
        });
    });
}

void PurpleLine::get_rooms() {
    c_out->send_getMessageBoxCompactWrapUpList(1, 65535);
    c_out->send([this]() {
        line::MessageBoxWrapUpList wrap_up_list;
        c_out->recv_getMessageBoxCompactWrapUpList(wrap_up_list);

        std::set<std::string> uids;

        for (line::MessageBoxWrapUp &ent: wrap_up_list.messageBoxWrapUpList) {
            if (ent.messageBox.midType != line::MIDType::ROOM)
                continue;

            for (line::Contact &c: ent.contacts)
                uids.insert(c.mid);
        }

        if (!uids.empty()) {
            // Room contacts don't contain full contact information, so pull separately to get names

            c_out->send_getContacts(std::vector<std::string>(uids.begin(), uids.end()));
            c_out->send([this, wrap_up_list]{
                std::vector<line::Contact> contacts;
                c_out->recv_getContacts(contacts);

                for (line::Contact &c: contacts)
                    this->contacts[c.mid] = c;

                update_rooms(wrap_up_list);
            });
        } else {
            update_rooms(wrap_up_list);
        }
    });
}

void PurpleLine::update_rooms(line::MessageBoxWrapUpList wrap_up_list) {
    std::set<PurpleChat *> chats_to_delete = blist_find_chats_by_type(ChatType::ROOM);

    for (line::MessageBoxWrapUp &ent: wrap_up_list.messageBoxWrapUpList) {
        if (ent.messageBox.midType != line::MIDType::ROOM)
            continue;

        line::Room room;
        room.mid = ent.messageBox.id;
        room.contacts = ent.contacts;

        chats_to_delete.erase(blist_update_chat(room));
    }

    for (PurpleChat *chat: chats_to_delete)
        purple_blist_remove_chat(chat);

    get_group_invites();
}

void PurpleLine::get_group_invites() {
    c_out->send_getGroupIdsInvited();
    c_out->send([this]() {
        std::vector<std::string> gids;
        c_out->recv_getGroupIdsInvited(gids);

        if (gids.size() == 0) {
            login_done();
            return;
        }

        c_out->send_getGroups(gids);
        c_out->send([this]() {
            std::vector<line::Group> groups;
            c_out->recv_getGroups(groups);

            for (line::Group &g: groups)
                handle_group_invite(g, profile_contact, no_contact);

            login_done();
        });
    });
}

void PurpleLine::login_done() {
    poller.start();

    purple_connection_update_progress(conn, "Connected", 2, 3);
}