📄 hostinfo.c
字号:
#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <stdio.h>#include <glib.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <pwd.h>#include <gconf/gconf-client.h>#include "common.h"static GConfClient *client=NULL;static gchar *groupname=NULL;static gchar *nickname=NULL;static gchar *groupname_proto=NULL;static gchar *nickname_proto=NULL;static gchar *hostname=NULL;static gchar *logfile_string=NULL;static gchar *cite_string=NULL;static gboolean current_absent_state=FALSE;static int absent_id=-1;static GSList *cached_prio;GStaticMutex hostinfo_mutex = G_STATIC_MUTEX_INIT;static const char *keys[] = { HOSTINFO_KEY_PORT, HOSTINFO_KEY_GROUP, HOSTINFO_KEY_NICKNAME, HOSTINFO_KEY_MSGSEC, HOSTINFO_KEY_CONFIRM_MSG, HOSTINFO_KEY_BROADCASTS, HOSTINFO_KEY_POPUP, HOSTINFO_KEY_SOUND, HOSTINFO_KEY_ENCLOSE, HOSTINFO_KEY_CITATION, HOSTINFO_KEY_LOGFILEPATH, HOSTINFO_KEY_ENABLE_LOG, HOSTINFO_KEY_LOG_NAME, HOSTINFO_KEY_LOG_IPADDR, HOSTINFO_KEY_CITE_STRING, HOSTINFO_KEY_ABS_TITLE, HOSTINFO_KEY_ABS_MSGS, HOSTINFO_KEY_DEBUG, HOSTINFO_KEY_MSGWIN_WIDTH, HOSTINFO_KEY_MSGWIN_HEIGHT, HOSTINFO_KEY_RECVWIN_WIDTH, HOSTINFO_KEY_RECVWIN_HEIGHT, HOSTINFO_KEY_ATTACHWIN_WIDTH, HOSTINFO_KEY_ATTACHWIN_HEIGHT, HOSTINFO_KEY_ABSENCE_WIDTH, HOSTINFO_KEY_ABSENCE_HEIGHT, HOSTINFO_KEY_IPV6, HOSTINFO_KEY_DIALUP, HOSTINFO_KEY_GET_HLIST, HOSTINFO_KEY_ALLOW_HLIST, HOSTINFO_KEY_USE_SYSTRAY, HOSTINFO_KEY_HEADER_VISIBLE, HOSTINFO_KEY_HEADER_ORDER, HOSTINFO_KEY_SORT_GROUP, HOSTINFO_KEY_SUB_SORT_ID, HOSTINFO_KEY_SORT_GROUP_DESCENDING, HOSTINFO_KEY_SUB_SORT_DESCENDING, NULL};gbooleanhostinfo_refer_debug_state(void) { if (!client) return FALSE; return gconf_client_get_bool(client,HOSTINFO_KEY_DEBUG,NULL);}inthostinfo_send_broad_cast(const udp_con_t *con,const char *msg,size_t len){ GSList *addr_top,*addr_list; udp_send_broadcast(con,msg,len); addr_top=addr_list=hostinfo_get_ipmsg_broadcast_list(); while(addr_list != NULL) { gchar *element = addr_list->data; dbg_out("Send Broad cast:%s\n",element); udp_send_broadcast_with_addr(con,element,msg,len); g_free(element); addr_list=g_slist_next(addr_list); } if (addr_top) g_slist_free(addr_top); return 0;}inthostinfo_set_ipmsg_broadcast_list(GSList *list){ if (!list) return -EINVAL; gconf_client_set_list(client,HOSTINFO_KEY_BROADCASTS,GCONF_VALUE_STRING,list,NULL); gconf_client_clear_cache(client); return 0;}GSList*hostinfo_get_ipmsg_broadcast_list(void){ return gconf_client_get_list(client,HOSTINFO_KEY_BROADCASTS,GCONF_VALUE_STRING,NULL);}int hostinfo_refer_absent_length(int *max_index){ int rc=-ESRCH; GSList *title_top,*title_list; if (!max_index) return -EINVAL; title_top=title_list=gconf_client_get_list(client,HOSTINFO_KEY_ABS_TITLE,GCONF_VALUE_STRING,NULL); if (title_top) { *max_index=g_slist_length(title_top); rc=0; } while(title_list != NULL) { gchar *element = title_list->data; dbg_out("free absent title:%s\n",element); g_free(element); title_list=g_slist_next(title_list); } if (title_top) g_slist_free(title_top); return rc;}inthostinfo_get_absent_title(int index,const char **title){ int rc=0; GSList *title_top,*title_list; GSList *title_ref; title_top=title_list=gconf_client_get_list(client,HOSTINFO_KEY_ABS_TITLE,GCONF_VALUE_STRING,NULL); if (!title_top) return -ESRCH; title_ref=g_slist_nth(title_top,index); if (title_ref) { *title=(title_ref->data)?(title_ref->data):(_("Undefined")); title_top=g_slist_remove_link(title_top,title_ref); g_slist_free_1(title_ref); }else{ rc=-ESRCH; } title_list=title_top; while(title_list != NULL) { gchar *element = title_list->data; dbg_out("Free absent title:%s\n",element); g_free(element); title_list=g_slist_next(title_list); } if (title_top) g_slist_free(title_top); return rc;}inthostinfo_set_ipmsg_absent_title(int index,const char *title){ int rc=0; GSList *title_top,*title_list; GSList *title_ref; if (!title) return -EINVAL; title_top=title_list=gconf_client_get_list(client,HOSTINFO_KEY_ABS_TITLE,GCONF_VALUE_STRING,NULL); if (!title_top) return -ESRCH; rc=-ESRCH; title_ref=g_slist_nth(title_top,index); if (!title_ref) goto free_list_out; g_free(title_ref->data); title_ref->data=strdup(title); g_assert(title_ref->data); /* ここで続行すると設定の整合性がとれなくなる */ if (!(title_ref->data)) { rc=-ENOMEM; title_top=g_slist_remove_link(title_top,title_ref); g_slist_free_1(title_ref); goto free_list_out; } gconf_client_set_list(client,HOSTINFO_KEY_ABS_TITLE,GCONF_VALUE_STRING,title_top,NULL); gconf_client_clear_cache(client); rc=0; free_list_out: title_list=title_top; while(title_list != NULL) { gchar *element = title_list->data; dbg_out("Free absent title:%s\n",element); g_free(element); title_list=g_slist_next(title_list); } if (title_top) g_slist_free(title_top); return rc;}/* absent msgs */int hostinfo_refer_absent_message_slots(int *max_index){ int rc=-ESRCH; GSList *message_top,*message_list; if (!max_index) return -EINVAL; message_top=message_list=gconf_client_get_list(client,HOSTINFO_KEY_ABS_MSGS,GCONF_VALUE_STRING,NULL); if (message_top) { *max_index=g_slist_length(message_top); rc=0; } while(message_list != NULL) { gchar *element = message_list->data; dbg_out("free absent message:%s\n",element); g_free(element); message_list=g_slist_next(message_list); } if (message_top) g_slist_free(message_top); return rc;}inthostinfo_get_absent_message(int index,const char **message){ int rc=0; GSList *message_top,*message_list; GSList *message_ref; message_top=message_list=gconf_client_get_list(client,HOSTINFO_KEY_ABS_MSGS,GCONF_VALUE_STRING,NULL); if (!message_top) return -ESRCH; message_ref=g_slist_nth(message_top,index); if (message_ref) { *message=(message_ref->data)?(message_ref->data):(_("Undefined")); message_top=g_slist_remove_link(message_top,message_ref); g_slist_free_1(message_ref); }else{ rc=-ESRCH; } message_list=message_top; while(message_list != NULL) { gchar *element = message_list->data; dbg_out("Free absent message:%s\n",element); g_free(element); message_list=g_slist_next(message_list); } if (message_top) g_slist_free(message_top); return rc;}inthostinfo_set_ipmsg_absent_message(int index,const char *message){ int rc=0; GSList *message_top,*message_list; GSList *message_ref; if (!message) return -EINVAL; message_top=message_list=gconf_client_get_list(client,HOSTINFO_KEY_ABS_MSGS,GCONF_VALUE_STRING,NULL); if (!message_top) return -ESRCH; rc=-ESRCH; message_ref=g_slist_nth(message_top,index); if (!message_ref) goto free_list_out; g_free(message_ref->data); message_ref->data=strdup(message); g_assert(message_ref->data); /* ここで続行すると設定の整合性がとれなくなる */ if (!(message_ref->data)) { rc=-ENOMEM; message_top=g_slist_remove_link(message_top,message_ref); g_slist_free_1(message_ref); goto free_list_out; } gconf_client_set_list(client,HOSTINFO_KEY_ABS_MSGS,GCONF_VALUE_STRING,message_top,NULL); gconf_client_clear_cache(client); rc=0; free_list_out: message_list=message_top; while(message_list != NULL) { gchar *element = message_list->data; dbg_out("Free absent message:%s\n",element); g_free(element); message_list=g_slist_next(message_list); } if (message_top) g_slist_free(message_top); return rc;}/* ids */int hostinfo_get_absent_id(int *index){ int max_index; int max_slots; if (!index) return -EINVAL; if (hostinfo_refer_absent_length(&max_index)) return -EINVAL; if (hostinfo_refer_absent_message_slots(&max_slots)) return -EINVAL; g_assert(max_index==max_slots); if (absent_id<0) return -EINVAL; if (absent_id>max_index) return -EINVAL; *index=absent_id; return 0;}int hostinfo_set_absent_id(int index){ int max_index; int max_slots; if (index<0) return -EINVAL; if (hostinfo_refer_absent_length(&max_index)) return -EINVAL; if (hostinfo_refer_absent_message_slots(&max_slots)) return -EINVAL; g_assert(max_index==max_slots); if (index>max_index) return -EINVAL; absent_id=index; return 0;}gbooleanhostinfo_is_ipmsg_absent(void) { return current_absent_state;}gbooleanhostinfo_set_ipmsg_absent(gboolean state) { current_absent_state=state; return current_absent_state;}gbooleanhostinfo_refer_ipmsg_default_secret(void) { return gconf_client_get_bool(client,HOSTINFO_KEY_MSGSEC,NULL);}gbooleanhostinfo_set_ipmsg_default_secret(gboolean val) { gconf_client_clear_cache(client); return gconf_client_set_bool(client,HOSTINFO_KEY_MSGSEC,val,NULL);}gbooleanhostinfo_refer_ipmsg_default_confirm(void) { return gconf_client_get_bool(client,HOSTINFO_KEY_CONFIRM_MSG,NULL);}gbooleanhostinfo_set_ipmsg_default_confirm(gboolean val) { gconf_client_clear_cache(client); return gconf_client_set_bool(client,HOSTINFO_KEY_CONFIRM_MSG,val,NULL);}gbooleanhostinfo_refer_ipmsg_default_popup(void) { return gconf_client_get_bool(client,HOSTINFO_KEY_POPUP,NULL);}gbooleanhostinfo_set_ipmsg_default_popup(gboolean val) { gconf_client_clear_cache(client); return gconf_client_set_bool(client,HOSTINFO_KEY_POPUP,val,NULL);}gbooleanhostinfo_refer_ipmsg_default_sound(void) { return gconf_client_get_bool(client,HOSTINFO_KEY_SOUND,NULL);}gbooleanhostinfo_set_ipmsg_default_sound(gboolean val) { gconf_client_clear_cache(client); return gconf_client_set_bool(client,HOSTINFO_KEY_SOUND,val,NULL);}gbooleanhostinfo_refer_ipmsg_default_enclose(void) { return gconf_client_get_bool(client,HOSTINFO_KEY_ENCLOSE,NULL);}gbooleanhostinfo_set_ipmsg_default_enclose(gboolean val) { gconf_client_clear_cache(client); return gconf_client_set_bool(client,HOSTINFO_KEY_ENCLOSE,val,NULL);}gbooleanhostinfo_refer_ipmsg_default_citation(void) { return gconf_client_get_bool(client,HOSTINFO_KEY_CITATION,NULL);}gbooleanhostinfo_set_ipmsg_default_citation(gboolean val) { gconf_client_clear_cache(client); return gconf_client_set_bool(client,HOSTINFO_KEY_CITATION,val,NULL);}gbooleanhostinfo_refer_ipmsg_ipaddr_logging(void) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -