pidgin-gmchess-0.02/0000755000175000017500000000000011444415210013362 5ustar leroslerospidgin-gmchess-0.02/internal.h0000644000175000017500000002336411444415210015357 0ustar lerosleros/** * @file internal.h Internal definitions and includes * @ingroup core */ /* purple * * Purple is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * source distribution. * * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */ #ifndef _PURPLE_INTERNAL_H_ #define _PURPLE_INTERNAL_H_ #ifdef HAVE_CONFIG_H # include #endif /* for SIOCGIFCONF in SKYOS */ #ifdef SKYOS #include #endif /* * If we're using NLS, make sure gettext works. If not, then define * dummy macros in place of the normal gettext macros. * * Also, the perl XS config.h file sometimes defines _ So we need to * make sure _ isn't already defined before trying to define it. * * The Singular/Plural/Number ngettext dummy definition below was * taken from an email to the texinfo mailing list by Manuel Guerrero. * Thank you Manuel, and thank you Alex's good friend Google. */ #ifdef ENABLE_NLS # include # include # define _(String) ((const char *)dgettext(PACKAGE, String)) # ifdef gettext_noop # define N_(String) gettext_noop (String) # else # define N_(String) (String) # endif #else # include # define N_(String) (String) # ifndef _ # define _(String) ((const char *)String) # endif # define ngettext(Singular, Plural, Number) ((Number == 1) ? ((const char *)Singular) : ((const char *)Plural)) # define dngettext(Domain, Singular, Plural, Number) ((Number == 1) ? ((const char *)Singular) : ((const char *)Plural)) #endif #ifdef HAVE_ENDIAN_H # include #endif #define MSG_LEN 2048 /* The above should normally be the same as BUF_LEN, * but just so we're explicitly asking for the max message * length. */ #define BUF_LEN MSG_LEN #define BUF_LONG BUF_LEN * 2 #include #include #ifndef _WIN32 #include #include #include #endif #include #include #include #include #include #include #include #include #ifdef HAVE_ICONV #include #endif #ifdef HAVE_LANGINFO_CODESET #include #endif #include #ifdef PURPLE_PLUGINS # ifdef HAVE_DLFCN_H # include # endif #endif #ifndef _WIN32 # include # include # include # include # include # include # include # include #endif /* MAXPATHLEN should only be used with readlink() on glib < 2.4.0. For * anything else, use g_file_read_link() or other dynamic functions. This is * important because Hurd has no hard limits on path length. */ #if !GLIB_CHECK_VERSION(2,4,0) # ifndef MAXPATHLEN # ifdef PATH_MAX # define MAXPATHLEN PATH_MAX # else # define MAXPATHLEN 1024 # endif # endif #endif #ifndef HOST_NAME_MAX # define HOST_NAME_MAX 255 #endif #include #if !GLIB_CHECK_VERSION(2,4,0) # define G_MAXUINT32 ((guint32) 0xffffffff) #endif #ifndef G_MAXSIZE # if GLIB_SIZEOF_LONG == 8 # define G_MAXSIZE ((gsize) 0xffffffffffffffff) # else # define G_MAXSIZE ((gsize) 0xffffffff) # endif #endif #ifndef G_MAXSSIZE # if GLIB_SIZEOF_LONG == 8 # define G_MAXSSIZE ((gssize) 0x7fffffffffffffff) # else # define G_MAXSSIZE ((gssize) 0x7fffffff) # endif #endif #if GLIB_CHECK_VERSION(2,6,0) # include #endif #if !GLIB_CHECK_VERSION(2,6,0) # define g_freopen freopen # define g_fopen fopen # define g_rmdir rmdir # define g_remove remove # define g_unlink unlink # define g_lstat lstat # define g_stat stat # define g_mkdir mkdir # define g_rename rename # define g_open open #endif #if !GLIB_CHECK_VERSION(2,8,0) && !defined _WIN32 # define g_access access #endif #if !GLIB_CHECK_VERSION(2,10,0) # define g_slice_new(type) g_new(type, 1) # define g_slice_new0(type) g_new0(type, 1) # define g_slice_free(type, mem) g_free(mem) #endif #ifdef _WIN32 #include "win32dep.h" #endif /* ugly ugly ugly */ /* This is a workaround for the fact that G_GINT64_MODIFIER and G_GSIZE_FORMAT * are only defined in Glib >= 2.4 */ #ifndef G_GINT64_MODIFIER # if GLIB_SIZEOF_LONG == 8 # define G_GINT64_MODIFIER "l" # else # define G_GINT64_MODIFIER "ll" # endif #endif #ifndef G_GSIZE_MODIFIER # if GLIB_SIZEOF_LONG == 8 # define G_GSIZE_MODIFIER "l" # else # define G_GSIZE_MODIFIER "" # endif #endif #ifndef G_GSIZE_FORMAT # if GLIB_SIZEOF_LONG == 8 # define G_GSIZE_FORMAT "lu" # else # define G_GSIZE_FORMAT "u" # endif #endif #ifndef G_GSSIZE_FORMAT # if GLIB_SIZEOF_LONG == 8 # define G_GSSIZE_FORMAT "li" # else # define G_GSSIZE_FORMAT "i" # endif #endif #ifndef G_GNUC_NULL_TERMINATED # if __GNUC__ >= 4 # define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) # else # define G_GNUC_NULL_TERMINATED # endif #endif #ifdef HAVE_CONFIG_H #if SIZEOF_TIME_T == 4 # define PURPLE_TIME_T_MODIFIER "lu" #elif SIZEOF_TIME_T == 8 # define PURPLE_TIME_T_MODIFIER "zu" #else #error Unknown size of time_t #endif #endif #include #ifndef G_DEFINE_TYPE #define G_DEFINE_TYPE(TypeName, type_name, TYPE_PARENT) \ \ static void type_name##_init (TypeName *self); \ static void type_name##_class_init (TypeName##Class *klass); \ static gpointer type_name##_parent_class = NULL; \ static void type_name##_class_intern_init (gpointer klass) \ { \ type_name##_parent_class = g_type_class_peek_parent (klass); \ type_name##_class_init ((TypeName##Class*) klass); \ } \ \ GType \ type_name##_get_type (void) \ { \ static GType g_define_type_id = 0; \ if (G_UNLIKELY (g_define_type_id == 0)) \ { \ g_define_type_id = \ g_type_register_static_simple (TYPE_PARENT, \ g_intern_static_string (#TypeName), \ sizeof (TypeName##Class), \ (GClassInitFunc)type_name##_class_intern_init, \ sizeof (TypeName), \ (GInstanceInitFunc)type_name##_init, \ (GTypeFlags) 0); \ } \ return g_define_type_id; \ } /* closes type_name##_get_type() */ #endif /* Safer ways to work with static buffers. When using non-static * buffers, either use g_strdup_* functions (preferred) or use * g_strlcpy/g_strlcpy directly. */ #define purple_strlcpy(dest, src) g_strlcpy(dest, src, sizeof(dest)) #define purple_strlcat(dest, src) g_strlcat(dest, src, sizeof(dest)) #define PURPLE_WEBSITE "http://pidgin.im/" #define PURPLE_DEVEL_WEBSITE "http://developer.pidgin.im/" /* INTERNAL FUNCTIONS */ #include "account.h" #include "connection.h" /* This is for the accounts code to notify the buddy icon code that * it's done loading. We may want to replace this with a signal. */ void _purple_buddy_icons_account_loaded_cb(void); /* This is for the buddy list to notify the buddy icon code that * it's done loading. We may want to replace this with a signal. */ void _purple_buddy_icons_blist_loaded_cb(void); /* This is for the purple_core_migrate() code to tell the buddy * icon subsystem about the old icons directory so it can * migrate any icons in use. */ void _purple_buddy_icon_set_old_icons_dir(const char *dirname); /** * Creates a connection to the specified account and either connects * or attempts to register a new account. If you are logging in, * the connection uses the current active status for this account. * So if you want to sign on as "away," for example, you need to * have called purple_account_set_status(account, "away"). * (And this will call purple_account_connect() automatically). * * @note This function should only be called by purple_account_connect() * in account.c. If you're trying to sign on an account, use that * function instead. * * @param account The account the connection should be connecting to. * @param regist Whether we are registering a new account or just * trying to do a normal signon. * @param password The password to use. */ void _purple_connection_new(PurpleAccount *account, gboolean regist, const char *password); /** * Tries to unregister the account on the server. If the account is not * connected, also creates a new connection. * * @note This function should only be called by purple_account_unregister() * in account.c. * * @param account The account to unregister * @param password The password to use. * @param cb Optional callback to be called when unregistration is complete * @param user_data user data to pass to the callback */ void _purple_connection_new_unregister(PurpleAccount *account, const char *password, PurpleAccountUnregistrationCb cb, void *user_data); /** * Disconnects and destroys a PurpleConnection. * * @note This function should only be called by purple_account_disconnect() * in account.c. If you're trying to sign off an account, use that * function instead. * * @param gc The purple connection to destroy. */ void _purple_connection_destroy(PurpleConnection *gc); #endif /* _PURPLE_INTERNAL_H_ */ pidgin-gmchess-0.02/network.txt0000644000175000017500000000440511444415210015617 0ustar lerosleros gmchess protocol(0.01 试用 2010.4.10) 0.game:gmchess --游戏标识 1.id:xxxxx --此次游戏会话id,随机生成并保存,以后比较 2.action:ask/reply/working --此次游戏状态 3.status:start/rue/draw/lose/close --请求的具体内容 status:ok/no --问话的回答 4.role:red/black 1/0 --此次游戏本人角色 5.number:xx --此次游戏进行的阶段数 6.moves:c2b2 --此步的走法 7.enemy_name:xxx 8.my_name:xxx 9.version:0.01 10.error:xxx a发起请求 [{game:gmchess,id:18293839,action:ask,status:start,role:red,number:0,moves:null}] b返回请求 [{game:gmchess,id:18293839,action:reply,status:ok,role:black,number:0,moves:null}] a发开始 [{game:gmchess,id:18293839,action:start,status:null,role:red,number:0,moves:null,enemy_name:wind,name:lerosua}] b回发确认开始 [{game:gmchess,id:18293839,action:start,status:ok,role:black,number:0,moves:null,enemy_name:lerosua,name:wind}] 如此,a和b都将gmchess调至网战状态。 a下第一步棋并发送 [{game:gmchess,id:18293839,action:working,status:null,role:red,number:1,moves:c2b2}] b收到并反馈确认 [{game:gmchess,id:18293839,action:reply,status:ok,role:black,number:1,moves:c2b2}] b发送第二步棋 [{game:gmchess,id:18293839,action:working,status:null,role:black,number:2,moves:c8b2}] a收到并反馈确认 [{game:gmchess,id:18293839,action:reply,status:ok,role:red,number:2,moves:c8b2}] or a收到的信息并不匹配上一次的走法,则返回错误的走法,要求重发走法 [{game:gmchess,id:18293839,action:reply,status:no,role:red,number:2,moves:c8b2,error:1}] b收到后则重发一走法 .... a想悔棋 [{game:gmchess,id:18293839,action:ask_rue,status:null,role:red,number:23,moves:null}] b不同意 [{game:gmchess,id:18293839,action:reply_rue,status:no,role:black,number:23,moves:null}] or b同意 [{game:gmchess,id:18293839,action:reply_rue,status:ok,role:black,number:23,moves:null}] 则两人做悔棋动作 ... a求和 [{game:gmchess,id:18293839,action:ask,status:draw,role:red,number:33,moves:null}] b不同意 [{game:gmchess,id:18293839,action:reply,status:no,role:black,number:33,moves:null}] a认输 [{game:gmchess,id:18293839,action:lose,status:null,role:red,number:34,moves:null}] pidgin-gmchess-0.02/Makefile0000644000175000017500000000070211444415210015021 0ustar leroslerosall: gcc -fPIC `pkg-config pidgin --cflags --libs` --shared -Wall -O2 gmchess-network.c -o gmchess-network.so install: cp gmchess-network.so /usr/lib/pidgin #cp gmchess-network.so ${HOME}/.purple/plugins/ mkdir -p /usr/share/icons/hicolor/22x22/apps/ cp gmchess.png /usr/share/icons/hicolor/22x22/apps/ install-user: mkdir -p ${HOME}/.purple/plugins cp gmchess-network.so ${HOME}/.purple/plugins/ clean: rm -rf *.o *.a *.so pidgin-gmchess-0.02/gmchess.png0000644000175000017500000000311311444415210015517 0ustar leroslerosPNG  IHDRĴl;bKGDC pHYs oy vpAgXIDAT8˵KhU}̗y$IN8ϊEH EP *[).JE ,"bb-6Is&{w9.5Upٝ{w9Vַ;<? ;w?='(B;gᵩY{\5TKGɓk6;իoxJ (""ONLTnٲKKתCc>_/,iȔJ̱c$ A j ?R:5 yX{9/2``)(\&ɢ^޵[[tu]R}[̧R*MNw7Al(+O7{֔usrͥƾ֎C ;}HiiSSH3 A"8ưqƦ5 )S)GQEgF2~ֻaLAfp![TV,d-$4=kgM5*n1"#cR[uS|zorBw|؛PfT-3E*QwT^mATBP@ŗB}ͻw;ȩMNŎmpq~~%0}g %Y\LXia7+O.zwWgztd rJdqkl p) ͬ"Ǒ}{:m/gzQzM[Žyrh [ 8m/:qEG`~> h"1IA.u@kX}XXc4Rd(ǁR.B5`D҇G=.&0&1XXk (%5 y4SmB: nv `濾^8]p`B_.!_};84:@휿%LT@5 WՃMsD; #include #include "internal.h" #include "debug.h" #include "plugin.h" #include "signals.h" #include "version.h" #include "pidgin.h" #include "gtkconv.h" #include "gtkconvwin.h" #include "gtkimhtml.h" #include "account.h" #include "request.h" #include "conversation.h" #include #include #include #include #include /** Plugin id : type-author-name (to guarantee uniqueness) */ #define GMCHESS_NETWORK_PLUGIN_ID "gmchess-network-game" #define GMVERSION "version:0.01" #define GMPORT 20126 enum { QUES_NO = 0, QUES_PLAY, QUES_DRAW, QUES_RUE }; /** * role : 0 is black, 1 is red. * * gmstatus 里的ask值: * ask : 0 则无问题 * 1 请求开局 * 2 请求和棋 * 3 请求悔棋 */ typedef struct gmstatus_ { guint32 id; int role; int number; int ask; int respond; guint timeout_id; PidginConversation *conv; } gmstatus; static void send_gmchess(const char *mv); static guint32 get_session_id(const gchar * idstr); static gboolean timeout_call(gpointer data); GIOChannel *io_channel; int source_id; int fd; static gmstatus _global_status; //_global_status.timeout_id=0; static void init_gm_status() { _global_status.id = 0; _global_status.role = 0; _global_status.number = 0; _global_status.ask = 0; _global_status.respond = 0; if (_global_status.timeout_id) { g_source_remove(_global_status.timeout_id); _global_status.timeout_id = 0; } _global_status.conv = NULL; } static void gtk_info_msg(const gchar * msg) { purple_notify_info(NULL, "pidgin-gmchess", _("info"), msg); } static guint32 get_session_id(const gchar * idstr) { guint32 _id; if (strstr(idstr, "id:") == NULL) return 0; gchar **getid; getid = g_strsplit(idstr, ":", -1); g_assert(getid); _id = g_ascii_strtoll(getid[1], NULL, 16); g_strfreev(getid); return _id; } static int create_socket() { int sockfd; struct sockaddr_in srvaddr; if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))) return -1; bzero(&srvaddr, sizeof(srvaddr)); srvaddr.sin_family = AF_INET; srvaddr.sin_port = htons(GMPORT); srvaddr.sin_addr.s_addr = htonl(INADDR_ANY); int on = 1; if (-1 == (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))) return -1; if (bind(sockfd, (struct sockaddr *) &srvaddr, sizeof(struct sockaddr)) == -1) { return -1; } if (-1 == listen(sockfd, 128)) return -1; return sockfd; } gboolean read_socket(GIOChannel * source, GIOCondition condition, gpointer data) { int fd_cli; fd_cli = -1; if (-1 == (fd_cli = accept(fd, NULL, 0))); char buf[1024]; size_t len = 0; len = read(fd_cli, &buf[0], 1023); buf[len] = 0; gchar *joinstr; if (len > 0) { if (_global_status.conv) { purple_debug(PURPLE_DEBUG_INFO, "plugins", "gmchess send %s.\n",buf); gchar *enemy_name = g_strdup_printf("%s", _global_status.conv-> active_conv->name); gchar *my_name = g_strdup_printf("%s", _global_status.conv-> active_conv->account-> username); if (strstr(buf, "moves:") != NULL) { _global_status.number++; joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:working,status:NULL,role:%d,number:%d,%s,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, buf, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML (_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); g_free(joinstr); } else if (strstr(buf, "resign") != NULL) { joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:ask,status:lose,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML (_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); g_free(joinstr); init_gm_status(); } else if (strstr(buf, "close") != NULL) { joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:ask,status:close,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML (_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); g_free(joinstr); init_gm_status(); } else if (strstr(buf, "rue") != NULL) { joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:ask,status:rue,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML (_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); _global_status.ask = QUES_RUE; g_free(joinstr); } else if (strstr(buf, "draw") != NULL) { joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:ask,status:draw,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML (_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); _global_status.ask = QUES_DRAW; g_free(joinstr); } g_free(enemy_name); g_free(my_name); } } return TRUE; } // check the pacake is valid static gboolean check_package(const char *buf) { if (strstr(buf, "[{game:gmchess,") == NULL) return FALSE; if (strstr(buf, ",id:") == NULL) return FALSE; if (strstr(buf, ",action:") == NULL) return FALSE; if (strstr(buf, ",status:") == NULL) return FALSE; if (strstr(buf, ",role:") == NULL) return FALSE; if (strstr(buf, ",number:") == NULL) return FALSE; if (strstr(buf, ",moves:") == NULL) return FALSE; if (strstr(buf, ",enemy_name:") == NULL) return FALSE; if (strstr(buf, ",my_name:") == NULL) return FALSE; if (strstr(buf, "}]") == NULL) return FALSE; return TRUE; } static void ok_poune(const char *m) { //ok,start the gmchess game //send_gmchess("network-game-black"); _global_status.role = 0; _global_status.number = 0; //then send reply to parter gchar *joinstr = NULL; gchar *enemy_name = g_strdup_printf("%s", _global_status.conv->active_conv->name); gchar *my_name = g_strdup_printf("%s", _global_status.conv-> active_conv->account->username); switch(_global_status.ask){ case QUES_PLAY: { gchar *snd_str; snd_str = g_strdup_printf("network-game-black,enemy_name:%s,my_name:%s", enemy_name, my_name); send_gmchess(snd_str); g_free(snd_str); joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:reply,status:ok,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML(_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); } break; case QUES_DRAW: { joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:reply,status:ok,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML(_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); send_gmchess("network-game-draw"); init_gm_status(); } break; case QUES_RUE: { joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:reply,status:ok,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML(_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); send_gmchess("network-game-rue"); } break; } g_free(joinstr); g_free(enemy_name); g_free(my_name); } static void no_poune(const char *m) { //no, do nothing gchar *joinstr; gchar *enemy_name = g_strdup_printf("%s", _global_status.conv->active_conv->name); gchar *my_name = g_strdup_printf("%s", _global_status.conv-> active_conv->account->username); joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:reply,status:no,role:%d,number:%d,moves:NULL,enemy_name:%s,my_name:%s}]", _global_status.id, _global_status.role, _global_status.number, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML(_global_status.conv->entry), joinstr, FALSE); g_signal_emit_by_name(_global_status.conv->entry, "message_send"); g_free(joinstr); g_free(enemy_name); g_free(my_name); } static void send_gmchess(const char *mv) { gchar *argv[3]; argv[0] = "gmchess"; argv[1] = (gchar *) mv; argv[2] = NULL; GError *err; GSpawnFlags flas = (GSpawnFlags) (G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL); g_spawn_async(NULL, argv, NULL, flas, NULL, NULL, NULL, &err); } static gboolean writing_im_msg_cb(PurpleAccount * account, const char *who, char **buffer, PurpleConversation * conv, PurpleMessageFlags flags, void *data) { gchar **wrk; //if ((strstr(*buffer, "[{game:gmchess,") != NULL)&&(strstr(*buffer,"}]")!=NULL)) { if (check_package(*buffer)) { wrk = g_strsplit(*buffer, ",", -1); g_assert(wrk); gchar *my_name; my_name = g_strdup_printf("my_name:%s", account->username); if (strstr(wrk[8], my_name) != NULL) { purple_debug(PURPLE_DEBUG_INFO, "plugins", "receive the package of my %s\n", *buffer); g_free(my_name); return TRUE; } g_free(my_name); if (strstr(wrk[2], "action:ask") != NULL) { if (strstr(wrk[3], "status:start") != NULL) { _global_status.conv = PIDGIN_CONVERSATION(conv); if (strstr(*buffer, "version:0.01") == NULL) { gtk_info_msg (_("gmchess for pidgin plugins version not matching!")); return TRUE; } _global_status.id = get_session_id(wrk[1]); _global_status.ask = QUES_PLAY; char *ask; ask = g_strdup_printf (" %s is asking you play gmchess together!", who); purple_request_action ("gmchess ask", "gmchess ask", ask, "Do you want to play the game?", 0, account, who, conv, "test", 2, "Yes", ok_poune, "No", no_poune); g_free(ask); } else if (strstr(wrk[3], "status:rue") != NULL) { _global_status.ask = QUES_RUE; char *ask; ask = g_strdup_printf (" %s is asking you to rue a move!", who); purple_request_action ("gmchess ask", "gmchess ask", ask, "Do you agree rue move for him?", 0, account, who, conv, "test", 2, "Yes", ok_poune, "No", no_poune); g_free(ask); } else if (strstr(wrk[3], "status:draw") != NULL) { _global_status.ask = QUES_DRAW; char *ask; ask = g_strdup_printf (" %s is asking you to draw the game!", who); purple_request_action ("gmchess ask", "gmchess ask", ask, "Do you agree draw?", 0, account, who, conv, "test", 2, "Yes", ok_poune, "No", no_poune); g_free(ask); } else if (strstr(wrk[3], "status:lose") != NULL) { //对方认输。结束此局 send_gmchess("network-game-win"); init_gm_status(); } else if (strstr(wrk[3], "status:close") != NULL) { //对方关闭棋局,先当输的处理 send_gmchess("network-game-win"); init_gm_status(); } } else if (strstr(wrk[2], "action:reply") != NULL) { if (strstr(wrk[3], "status:ok") != NULL) { gchar *snd_str; switch (_global_status.ask) { case QUES_NO: break; case QUES_PLAY: //answer the start play _global_status.respond = 1; snd_str = g_strdup_printf ("network-game-red,%s,%s", wrk[7], wrk[8]); send_gmchess(snd_str); if (_global_status.timeout_id) { g_source_remove(_global_status.timeout_id); _global_status.timeout_id = 0; } g_free(snd_str); _global_status.ask = QUES_NO; _global_status.role = 1; //gtk_info_msg("network gmchess start"); break; case QUES_RUE: send_gmchess("network-game-rue"); _global_status.ask = QUES_NO; _global_status.role = 1; break; case QUES_DRAW: send_gmchess("network-game-draw"); _global_status.ask = QUES_NO; _global_status.role = 1; break; }; } else if (strstr(wrk[3], "status:no") != NULL) { switch (_global_status.ask) { case 0: break; case QUES_PLAY: //answer the start play init_gm_status(); gtk_info_msg(_("The other side deny the invite")); break; case QUES_RUE: send_gmchess("network-game-norue"); _global_status.ask = QUES_NO; _global_status.role = 1; break; case QUES_DRAW: send_gmchess("network-game-nodraw"); _global_status.ask = QUES_NO; _global_status.role = 1; break; }; } } else if (strstr(wrk[2], "action:working") != NULL) { purple_debug(PURPLE_DEBUG_INFO, "plugins", "should send the gmchess'moves: %s\n", *buffer); send_gmchess(wrk[6]); /* char * moves; char* mv; moves=g_strdup_printf("bestmoves:%s",mv); send_gmchess(moves); g_free(moves); */ } else { g_strfreev(wrk); return FALSE; } g_strfreev(wrk); return TRUE; } else { return FALSE; } } static gboolean timeout_call(gpointer data) { if (1 == _global_status.respond && 1 == _global_status.ask) { } else { purple_debug(PURPLE_DEBUG_INFO, "plugins", "send the ask start game not respond,give up it.\n"); gtk_info_msg(_("there has no respond. the other side maybe not install the gmchess plugins")); init_gm_status(); } return FALSE; } static void gmchess_button_cb(GtkButton * button, PidginConversation * gtkconv) { /** 如果id不为0,则可能在下棋中。退出*/ if (_global_status.id != 0) { gtk_info_msg(_("Please do not repeat invite. ")); return; } //gtk_info_msg("发送邀请中"); gchar *enemy_name = g_strdup_printf("%s", gtkconv->active_conv->name); gchar *my_name = g_strdup_printf("%s", gtkconv->active_conv->account->username); gchar *joinstr; guint32 session_id_; session_id_ = g_random_int(); joinstr = g_strdup_printf ("[{game:gmchess,id:%X,action:ask,status:start,role:0,number:0,moves:NULL,enemy_name:%s,my_name:%s,version:0.01}]", session_id_, enemy_name, my_name); gtk_imhtml_append_text(GTK_IMHTML(gtkconv->entry), joinstr, FALSE); g_signal_emit_by_name(gtkconv->entry, "message_send"); purple_debug(PURPLE_DEBUG_INFO, "plugins", "send joinstr: %s\n", joinstr); g_free(joinstr); g_free(enemy_name); g_free(my_name); _global_status.id = session_id_; _global_status.ask = 1; _global_status.conv = gtkconv; _global_status.timeout_id = g_timeout_add_seconds(30, timeout_call, NULL); } static void create_gmchess_button_pidgin(PidginConversation * conv) { GtkWidget *button, *image, *sep; //GtkIconInfo *icoinfo; //const gchar *iconames[]={"gmchess-pidgin", "gmchess-pidgin"}; button = gtk_button_new(); gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gmchess_button_cb), conv); #ifdef DATADIR //image = gtk_image_new_from_file(g_build_filename(DATADIR,"pixmaps","hicolor","22x22","apps","gmchess.png",NULL)); //image = gtk_image_new_from_file(g_build_filename("/usr/share","icons","hicolor","22x22","apps","gmchess.png",NULL)); image = gtk_image_new_from_file ("/usr/share/icons/hicolor/22x22/apps/gmchess.png"); #else //image = gtk_image_new_from_file(g_build_filename("/usr/share","icons","hicolor","22x22","apps","gmchess.png",NULL)); image = gtk_image_new_from_file ("/usr/share/icons/hicolor/22x22/apps/gmchess.png"); #endif gtk_container_add((GtkContainer *) button, image); sep = gtk_vseparator_new(); gtk_widget_show(sep); gtk_widget_show(image); gtk_widget_show(button); gtk_box_pack_start(GTK_BOX(conv->toolbar), sep, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(conv->toolbar), button, FALSE, FALSE, 0); g_object_set_data(G_OBJECT(conv->toolbar), "gmchess_separator", sep); g_object_set_data(G_OBJECT(conv->toolbar), "gmchess_button", button); } static void remove_gmchess_button_pidgin(PidginConversation * gtkconv) { GtkWidget *gmchess_button = NULL, *separator = NULL; gmchess_button = g_object_get_data(G_OBJECT(gtkconv->toolbar), "gmchess_button"); if (gmchess_button != NULL) { gtk_container_remove(GTK_CONTAINER (gtkconv->toolbar), gmchess_button); g_signal_handlers_disconnect_by_func (G_OBJECT(gmchess_button), G_CALLBACK(gmchess_button_cb), gtkconv); gtk_widget_destroy(gmchess_button); } separator = g_object_get_data(G_OBJECT(gtkconv->toolbar), "gmchess_separator"); if (gmchess_button != NULL) { gtk_container_remove(GTK_CONTAINER(gtkconv->toolbar), separator); gtk_widget_destroy(separator); } } static gboolean plugin_load(PurplePlugin * plugin) { GList *convs = purple_get_conversations(); void *conv_handle = purple_conversations_get_handle(); void *gtk_conv_handle = pidgin_conversations_get_handle(); purple_signal_connect(gtk_conv_handle, "conversation-displayed", plugin, PURPLE_CALLBACK (create_gmchess_button_pidgin), NULL); purple_signal_connect(gtk_conv_handle, "conversation-hiding", plugin, PURPLE_CALLBACK (remove_gmchess_button_pidgin), NULL); while (convs) { PurpleConversation *conv = (PurpleConversation *) convs->data; /* Setup gmchess button */ if (PIDGIN_IS_PIDGIN_CONVERSATION(conv)) { create_gmchess_button_pidgin(PIDGIN_CONVERSATION(conv)); } convs = convs->next; } purple_signal_connect(conv_handle, "writing-im-msg", plugin, PURPLE_CALLBACK(writing_im_msg_cb), NULL); purple_debug(PURPLE_DEBUG_INFO, "plugins", "gmchess support plugin loaded.\n"); //int fd; fd = create_socket(); if (fd == -1) purple_debug(PURPLE_DEBUG_INFO, "plugins", "gmchess pidgin bind socket error\n"); else { io_channel = g_io_channel_unix_new(fd); g_io_channel_set_encoding(io_channel, NULL, NULL); source_id = g_io_add_watch(io_channel, G_IO_IN, read_socket, &fd); } init_gm_status(); return TRUE; } static gboolean plugin_unload(PurplePlugin * plugin) { GList *convs = purple_get_conversations(); while (convs) { PurpleConversation *conv = (PurpleConversation *) convs->data; if (PIDGIN_IS_PIDGIN_CONVERSATION(conv)) { remove_gmchess_button_pidgin(PIDGIN_CONVERSATION(conv)); } convs = convs->next; } purple_debug(PURPLE_DEBUG_INFO, "plugins", "gmchess support plugin unloaded.\n"); if (fd != -1) { g_source_remove(source_id); g_io_channel_shutdown(io_channel, TRUE, NULL); g_io_channel_unref(io_channel); } init_gm_status(); return TRUE; } static PurplePluginInfo info = { PURPLE_PLUGIN_MAGIC, PURPLE_MAJOR_VERSION, PURPLE_MINOR_VERSION, PURPLE_PLUGIN_STANDARD, /**< type */ NULL, /**< ui_requirement */ 0, /**< flags */ NULL, /**< dependencies */ PURPLE_PRIORITY_DEFAULT, /**< priority */ GMCHESS_NETWORK_PLUGIN_ID, /**< id */ N_("gmchess network play game support."), /**< name */ "0.01", /**< version */ /** summary */ N_("gmchess network play game support."), /** description */ N_("play chinese game in which use pidgin."), "lerosua ", /**< author */ "http://github.com/gmcore/pidgin-gmchess/", /**< homepage */ plugin_load, /**< load */ plugin_unload, /**< unload */ NULL, /**< destroy */ NULL, /**< ui_info */ NULL, /**< extra_info */ NULL, NULL, /* Padding */ NULL, NULL, NULL, NULL }; static void init_plugin(PurplePlugin * plugin) { } PURPLE_INIT_PLUGIN(pidgin - gmchess, init_plugin, info) pidgin-gmchess-0.02/COPYING0000644000175000017500000004311211444415210014416 0ustar lerosleros GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License.