⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gg.c

📁 Linux下的多协议即时通讯程序源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/** * @file gg.c Gadu-Gadu protocol plugin * * purple * * Copyright (C) 2005  Bartosz Oler <bartosz@bzimage.us> * * Some parts of the code are adapted or taken from the previous implementation * of this plugin written by Arkadiusz Miskiewicz <misiek@pld.org.pl> * * Thanks to Google's Summer of Code Program. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include "internal.h"#include "plugin.h"#include "version.h"#include "notify.h"#include "status.h"#include "blist.h"#include "accountopt.h"#include "debug.h"#include "util.h"#include "request.h"#include <libgadu.h>#include "gg.h"#include "confer.h"#include "search.h"#include "buddylist.h"#include "gg-utils.h"static PurplePlugin *my_protocol = NULL;/* ---------------------------------------------------------------------- *//* ----- EXTERNAL CALLBACKS --------------------------------------------- *//* ---------------------------------------------------------------------- *//* ----- HELPERS -------------------------------------------------------- *//** * Set up libgadu's proxy. * * @param account Account for which to set up the proxy. * * @return Zero if proxy setup is valid, otherwise -1. *//* static int ggp_setup_proxy(PurpleAccount *account) {{{ */static int ggp_setup_proxy(PurpleAccount *account){	PurpleProxyInfo *gpi;	gpi = purple_proxy_get_setup(account);	if ((purple_proxy_info_get_type(gpi) != PURPLE_PROXY_NONE) &&	    (purple_proxy_info_get_host(gpi) == NULL ||	     purple_proxy_info_get_port(gpi) <= 0)) {		gg_proxy_enabled = 0;		purple_notify_error(NULL, NULL, _("Invalid proxy settings"),				  _("Either the host name or port number specified for your given proxy type is invalid."));		return -1;	} else if (purple_proxy_info_get_type(gpi) != PURPLE_PROXY_NONE) {		gg_proxy_enabled = 1;		gg_proxy_host = g_strdup(purple_proxy_info_get_host(gpi));		gg_proxy_port = purple_proxy_info_get_port(gpi);		gg_proxy_username = g_strdup(purple_proxy_info_get_username(gpi));		gg_proxy_password = g_strdup(purple_proxy_info_get_password(gpi));	} else {		gg_proxy_enabled = 0;	}	return 0;}/* }}} *//* *//* static void ggp_async_token_handler(gpointer _gc, gint fd, PurpleInputCondition cond) {{{ */static void ggp_async_token_handler(gpointer _gc, gint fd, PurpleInputCondition cond){	PurpleConnection *gc = _gc;	GGPInfo *info = gc->proto_data;	GGPToken *token = info->token;	GGPTokenCallback cb;	struct gg_token *t = NULL;	purple_debug_info("gg", "token_handler: token->req: check = %d; state = %d;\n",			token->req->check, token->req->state);	if (gg_token_watch_fd(token->req) == -1 || token->req->state == GG_STATE_ERROR) {		purple_debug_error("gg", "token error (1): %d\n", token->req->error);		purple_input_remove(token->inpa);		gg_token_free(token->req);		token->req = NULL;		purple_notify_error(purple_connection_get_account(gc),				  _("Token Error"),				  _("Unable to fetch the token.\n"), NULL);		return;	}	if (token->req->state != GG_STATE_DONE) {		purple_input_remove(token->inpa);		token->inpa = purple_input_add(token->req->fd,						   (token->req->check == 1)						   	? PURPLE_INPUT_WRITE							: PURPLE_INPUT_READ,						   ggp_async_token_handler, gc);		return;	}	if (!(t = token->req->data) || !token->req->body) {		purple_debug_error("gg", "token error (2): %d\n", token->req->error);		purple_input_remove(token->inpa);		gg_token_free(token->req);		token->req = NULL;		purple_notify_error(purple_connection_get_account(gc),				  _("Token Error"),				  _("Unable to fetch the token.\n"), NULL);		return;	}	purple_input_remove(token->inpa);	token->id = g_strdup(t->tokenid);	token->size = token->req->body_size;	token->data = g_new0(char, token->size);	memcpy(token->data, token->req->body, token->size);	purple_debug_info("gg", "TOKEN! tokenid = %s; size = %d\n",			token->id, token->size);	gg_token_free(token->req);	token->req = NULL;	token->inpa = 0;	cb = token->cb;	token->cb = NULL;	cb(gc);}/* }}} *//* *//* static void ggp_token_request(PurpleConnection *gc, GGPTokenCallback cb) {{{ */static void ggp_token_request(PurpleConnection *gc, GGPTokenCallback cb){	PurpleAccount *account;	struct gg_http *req;	GGPInfo *info;	account = purple_connection_get_account(gc);	if (ggp_setup_proxy(account) == -1)		return;	info = gc->proto_data;	if ((req = gg_token(1)) == NULL) {		purple_notify_error(account,				  _("Token Error"),				  _("Unable to fetch the token.\n"), NULL);		return;	}	info->token = g_new(GGPToken, 1);	info->token->cb = cb;	info->token->req = req;	info->token->inpa = purple_input_add(req->fd, PURPLE_INPUT_READ,					   ggp_async_token_handler, gc);}/* }}} *//* ---------------------------------------------------------------------- *//** * Request buddylist from the server. * Buddylist is received in the ggp_callback_recv(). * * @param Current action handler. *//* static void ggp_action_buddylist_get(PurplePluginAction *action) {{{ */static void ggp_action_buddylist_get(PurplePluginAction *action){	PurpleConnection *gc = (PurpleConnection *)action->context;	GGPInfo *info = gc->proto_data;	purple_debug_info("gg", "Downloading...\n");	gg_userlist_request(info->session, GG_USERLIST_GET, NULL);}/* }}} *//** * Upload the buddylist to the server. * * @param action Current action handler. *//* static void ggp_action_buddylist_put(PurplePluginAction *action) {{{ */static void ggp_action_buddylist_put(PurplePluginAction *action){	PurpleConnection *gc = (PurpleConnection *)action->context;	GGPInfo *info = gc->proto_data;	char *buddylist = ggp_buddylist_dump(purple_connection_get_account(gc));	purple_debug_info("gg", "Uploading...\n");		if (buddylist == NULL)		return;	gg_userlist_request(info->session, GG_USERLIST_PUT, buddylist);	g_free(buddylist);}/* }}} *//** * Delete buddylist from the server. * * @param action Current action handler. *//* static void ggp_action_buddylist_delete(PurplePluginAction *action) {{{ */static void ggp_action_buddylist_delete(PurplePluginAction *action){	PurpleConnection *gc = (PurpleConnection *)action->context;	GGPInfo *info = gc->proto_data;	purple_debug_info("gg", "Deleting...\n");	gg_userlist_request(info->session, GG_USERLIST_PUT, NULL);}/* }}} *//* *//* static void ggp_callback_buddylist_save_ok(PurpleConnection *gc, const char *file) {{{ */static void ggp_callback_buddylist_save_ok(PurpleConnection *gc, const char *file){	PurpleAccount *account = purple_connection_get_account(gc);	FILE *fh;	char *buddylist = ggp_buddylist_dump(account);	gchar *msg;	purple_debug_info("gg", "Saving...\n");	purple_debug_info("gg", "file = %s\n", file);	if (buddylist == NULL) {		purple_notify_info(account, _("Save Buddylist..."),			 _("Your buddylist is empty, nothing was written to the file."),			 NULL);		return;	}	if ((fh = g_fopen(file, "wb")) == NULL) {		msg = g_strconcat(_("Couldn't open file"), ": ", file, "\n", NULL);		purple_debug_error("gg", "Could not open file: %s\n", file);		purple_notify_error(account, _("Couldn't open file"), msg, NULL);		g_free(msg);		g_free(buddylist);		return;	}	fwrite(buddylist, sizeof(char), g_utf8_strlen(buddylist, -1), fh);	fclose(fh);	g_free(buddylist);	purple_notify_info(account, _("Save Buddylist..."),			 _("Buddylist saved successfully!"), NULL);}/* }}} *//* *//* static void ggp_callback_buddylist_load_ok(PurpleConnection *gc, gchar *file) {{{ */static void ggp_callback_buddylist_load_ok(PurpleConnection *gc, gchar *file){	PurpleAccount *account = purple_connection_get_account(gc);	GError *error = NULL;	char *buddylist = NULL;	gsize length;	purple_debug_info("gg", "file_name = %s\n", file);	if (!g_file_get_contents(file, &buddylist, &length, &error)) {		purple_notify_error(account,				_("Couldn't load buddylist"),				_("Couldn't load buddylist"),				error->message);		purple_debug_error("gg",			"Couldn't load buddylist. file = %s; error = %s\n",			file, error->message);		g_error_free(error);		return;	}	ggp_buddylist_load(gc, buddylist);	g_free(buddylist);	purple_notify_info(account,			 _("Load Buddylist..."),			 _("Buddylist loaded successfully!"), NULL);}/* }}} *//* *//* static void ggp_action_buddylist_save(PurplePluginAction *action) {{{ */static void ggp_action_buddylist_save(PurplePluginAction *action){	PurpleConnection *gc = (PurpleConnection *)action->context;	purple_request_file(action, _("Save buddylist..."), NULL, TRUE,			G_CALLBACK(ggp_callback_buddylist_save_ok), NULL,			purple_connection_get_account(gc), NULL, NULL,			gc);}/* }}} *//* *//* static void ggp_action_buddylist_load(PurplePluginAction *action) {{{ */static void ggp_action_buddylist_load(PurplePluginAction *action){	PurpleConnection *gc = (PurpleConnection *)action->context;	purple_request_file(action, "Load buddylist from file...", NULL, FALSE,			G_CALLBACK(ggp_callback_buddylist_load_ok), NULL,			purple_connection_get_account(gc), NULL, NULL,			gc);}/* }}} *//* *//* static void ggp_callback_register_account_ok(PurpleConnection *gc, PurpleRequestFields *fields) {{{ */static void ggp_callback_register_account_ok(PurpleConnection *gc,					     PurpleRequestFields *fields){	PurpleAccount *account;	GGPInfo *info = gc->proto_data;	struct gg_http *h = NULL;	struct gg_pubdir *s;	uin_t uin;	gchar *email, *p1, *p2, *t;	GGPToken *token = info->token;	email = charset_convert(purple_request_fields_get_string(fields, "email"),			     "UTF-8", "CP1250");	p1  = charset_convert(purple_request_fields_get_string(fields, "password1"),			     "UTF-8", "CP1250");	p2  = charset_convert(purple_request_fields_get_string(fields, "password2"),			     "UTF-8", "CP1250");	t   = charset_convert(purple_request_fields_get_string(fields, "token"),			     "UTF-8", "CP1250");	account = purple_connection_get_account(gc);	if (email == NULL || p1 == NULL || p2 == NULL || t == NULL ||	    *email == '\0' || *p1 == '\0' || *p2 == '\0' || *t == '\0') {		purple_connection_error(gc, _("Fill in the registration fields."));		goto exit_err;	}	if (g_utf8_collate(p1, p2) != 0) {		purple_connection_error(gc, _("Passwords do not match."));		goto exit_err;	}	purple_debug_info("gg", "register_account_ok: token_id = %d; t = %s\n",			token->id, t);	h = gg_register3(email, p1, token->id, t, 0);	if (h == NULL || !(s = h->data) || !s->success) {		purple_connection_error(gc,			_("Unable to register new account. Error occurred.\n"));		goto exit_err;	}	uin = s->uin;	purple_debug_info("gg", "registered uin: %d\n", uin);	g_free(t);	t = g_strdup_printf("%u", uin);	purple_account_set_username(account, t);	/* Save the password if remembering passwords for the account */	purple_account_set_password(account, p1);	purple_notify_info(NULL, _("New Gadu-Gadu Account Registered"),			 _("Registration completed successfully!"), NULL);	/* TODO: the currently open Accounts Window will not be updated withthe	 * new username and etc, we need to somehow have it refresh at this	 * point	 */	/* Need to disconnect or actually log in. For now, we disconnect. */	purple_connection_destroy(gc);exit_err:	gg_register_free(h);	g_free(email);	g_free(p1);	g_free(p2);	g_free(t);	g_free(token->id);	g_free(token);}/* }}} *//* *//* static void ggp_callback_register_account_cancel(PurpleConnection *gc, PurpleRequestFields *fields) {{{ */static void ggp_callback_register_account_cancel(PurpleConnection *gc,						 PurpleRequestFields *fields){	GGPInfo *info = gc->proto_data;	GGPToken *token = info->token;	purple_connection_destroy(gc);	g_free(token->id);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -