/soc/2012/sanket/statscollector-2.x.y: 3f8f3e40148a: Streamline ...

Sanket Agarwal sanket at soc.pidgin.im
Fri Apr 5 01:36:50 EDT 2013


Changeset: 3f8f3e40148a8dc2b278d793f2081c252d9515e9
Author:	 Sanket Agarwal <sanket at soc.pidgin.im>
Date:	 2013-04-04 21:36 +0530
Branch:	 soc.2012.statscollector
URL: https://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/3f8f3e40148a

Description:

Streamline workflow of code.

* Added separate functions to init and uninit global vars.
* Used glib functions where possible (checksum/md5).
* Fixed a bunch of memory leaks (lots still remain!).

diffstat:

 libpurple/plugins/statscollector.c |  364 ++++++++++++++++--------------------
 1 files changed, 167 insertions(+), 197 deletions(-)

diffs (truncated from 656 to 300 lines):

diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
--- a/libpurple/plugins/statscollector.c
+++ b/libpurple/plugins/statscollector.c
@@ -58,12 +58,14 @@ enum
 # include <dlfcn.h>
 #endif
 
-PurplePlugin *plugin_g;
-xmlnode *root_stats, *cpuinfo_xml, *ui_info;
-GHashTable *stats_acc_ht, *stats_plugins_ht, *stats_uis_ht;
-GHashTable *public_server_cache_ht=NULL;
+static PurplePlugin *plugin_g;
+static xmlnode *cpuinfo_xml;
+static GHashTable *stats_acc_ht, *stats_plugins_ht, *stats_uis_ht;
+static GHashTable *public_server_cache_ht = NULL;
+static gboolean init_stats_run = FALSE;
 
-int save_timer = 0, send_handle = 0, pref_cb_id = -1, public_server_handle = 0;
+static int save_timer = 0, send_handle = 0, pref_cb_id = -1;
+static int public_server_handle = 0;
 
 /* Types of Operating Systems */
 enum OS_TYPES {WINDOWS, APPLE, UNIX};
@@ -74,7 +76,7 @@ static gboolean send_stats();
 static gboolean plugin_load(PurplePlugin *);
 static gboolean plugin_unload(PurplePlugin *plugin);
 static gboolean refresh_public_server_cache(gpointer data);
-static xmlnode *init_stats();
+static void init_stats(PurplePlugin *plugin);
 static void acc_sign_on_event(PurpleAccount *account);
 static void url_callback(PurpleUtilFetchUrlData *url_data, gpointer user_data,
                 const gchar *url_text, gsize len, const gchar *error_message);
@@ -132,9 +134,6 @@ save_xml()
         /* Load CPUinfo strucutre */
         xmlnode_insert_child(nroot, xmlnode_copy(cpuinfo_xml));
 
-        /* Load UI info */
-        xmlnode_insert_child(nroot, xmlnode_copy(ui_info));
-
         nacc = xmlnode_new_child(nroot, "accounts");
         nplugins = xmlnode_new_child(nroot, "plugins");
         nuis = xmlnode_new_child(nroot, "uis");
@@ -155,8 +154,8 @@ save_xml()
 
         data = xmlnode_to_formatted_str(nroot, NULL);
         purple_util_write_data_to_file("stats.xml", data, -1);
+
         xmlnode_free(nroot);
-
         g_free(data);
 }
 
@@ -222,18 +221,18 @@ refresh_public_server_cache_cb(PurpleUti
                                         (void *)hash_id,
                                         NULL);
                         }
-                        public_server_handle = purple_timeout_add_seconds(
-                                PUBLIC_SERVER_CACHE_REFRESH,
-                                refresh_public_server_cache,
-                                NULL);
+                        //public_server_handle = purple_timeout_add_seconds(
+                        //        PUBLIC_SERVER_CACHE_REFRESH,
+                        //        refresh_public_server_cache,
+                        //        NULL);
 
                         /* Re-evaluate accounts for public domains in usernames. */
                         loaded_accounts = purple_accounts_get_all_active();
                         g_list_foreach(loaded_accounts, (GFunc)acc_sign_on_event, NULL);
                 }
         } else {
-                public_server_handle = purple_timeout_add_seconds(
-                        10, refresh_public_server_cache, NULL);
+                // public_server_handle = purple_timeout_add_seconds(
+                //        10, refresh_public_server_cache, NULL);
         }
 }
 
@@ -398,7 +397,6 @@ get_os_name()
         xmlnode *major_version, *minor_version;
         OSVERSIONINFO osvi;
 #elif defined __APPLE__
-        int mib[2];
         size_t length;
         SInt32 major_version,minor_version,bug_fix_version;
         char sys_ver_file[] = "/System/Library/CoreServices/SystemVersion.plist";
@@ -408,7 +406,6 @@ get_os_name()
         xmlnode *product_version_xml;
         xmlnode *dict_sys_ver_xml, *key_sys_ver_xml, *value_sys_ver_xml;
         xmlnode *major_version_xml, *minor_version_xml, *bug_version_xml;
-        OSErr err1, err2, err3;
 
         /* Get handle for Gestalt by dlopen */
         int (*gestalt_ext)(int, int *);
@@ -542,6 +539,7 @@ get_cpuinfo_xml()
         xmlnode *root, *languages, *node, *ver, *cpu, *app_arch;
         xmlnode *os_arch, *arch_type;
         const char * const * langs;
+        char *debug_info;
         int i;
 
         root = xmlnode_new("cpuinfo");
@@ -576,7 +574,10 @@ get_cpuinfo_xml()
         xmlnode_insert_data(ver, purple_core_get_version(), -1);
         /* Add the version information to the root cpuinfo */
         xmlnode_insert_child(root, ver);
-        purple_debug_info("STATS", "%s\n", xmlnode_to_formatted_str(root, NULL));
+        debug_info = xmlnode_to_formatted_str(root, NULL);
+        purple_debug_info("STATS", "%s\n", debug_info);
+
+        free(debug_info);
 
         return root;
 }
@@ -588,12 +589,12 @@ static void
 get_ui_info()
 {
         GHashTable *ui_info_ht;
-        xmlnode *ui_info_xml, *ui_version_xml;
+        xmlnode *ui_info_xml = NULL, *ui_version_xml;
         char *ui_name, *ui_version;
 
         ui_info_ht = purple_core_get_ui_info();
-        ui_name = g_hash_table_lookup(ui_info_ht, "name");
-        ui_version = g_hash_table_lookup(ui_info_ht, "version");
+        ui_name = g_strdup(g_hash_table_lookup(ui_info_ht, "name"));
+        ui_version = g_strdup(g_hash_table_lookup(ui_info_ht, "version"));
 
         /* If it's a null-client (?) then perhaps we should make it look like
          * a client we don't perhaps know about!
@@ -605,60 +606,39 @@ get_ui_info()
                 ui_version = "other";
         }
 
-        /* Check if the UI info is already present in the hashtable */
-        if (g_hash_table_lookup(stats_uis_ht, ui_name)) {
-                return;
+        /* Check if the UI info is not present in the hashtable */
+        if (!g_hash_table_lookup(stats_uis_ht, ui_name)) {
+                ui_info_xml = xmlnode_new("ui");
+                ui_version_xml = xmlnode_new("version");
+
+                xmlnode_set_attrib(ui_info_xml, "id", ui_name);
+                xmlnode_insert_data(ui_version_xml, ui_version, -1);
+                xmlnode_insert_child(ui_info_xml, ui_version_xml);
+
+                g_hash_table_insert(stats_uis_ht,
+                                (void *)g_strdup(ui_name),
+                                xmlnode_copy(ui_info_xml));
+                schedule_stats_save();
         }
 
-        ui_info_xml = xmlnode_new("ui");
-        ui_version_xml = xmlnode_new("version");
-
-        xmlnode_set_attrib(ui_info_xml, "id", ui_name);
-        xmlnode_insert_data(ui_version_xml, ui_version, -1);
-        xmlnode_insert_child(ui_info_xml, ui_version_xml);
-
-        g_hash_table_insert(stats_uis_ht, (void *)ui_name,
-                        xmlnode_copy(ui_info_xml));
-
-        schedule_stats_save();
-
         /* Free OWNED objects */
+        g_free(ui_name);
+        g_free(ui_version);
         xmlnode_free(ui_info_xml);
 }
 
-
-static gchar *
-md5(const guchar *str)
-{
-        PurpleCipherContext *context;
-        gchar digest[41];
-
-        context = purple_cipher_context_new_by_name("md5", NULL);
-        g_return_val_if_fail(context != NULL, NULL);
-
-        purple_cipher_context_append(context, str, strlen((char*)str));
-
-        if (!purple_cipher_context_digest_to_str(
-                                context, sizeof(digest), digest, NULL)) {
-                return NULL;
-        }
-
-        purple_cipher_context_destroy(context);
-
-        return g_strdup(digest);
-}
-
 /* Generates a id from username and prpl_id */
 static char *
 get_acc_id(const char *username, const char *prpl_id)
 {
-        char *id, *md5_str;
-        id = g_new0(char, 500);
-        strcat(id, username);
-        strcat(id, "-");
-        strcat(id, prpl_id);
-        md5_str = md5((guchar *)id);
+        gchar *id;
+        gchar *md5_str;
+        id = g_strdup_printf("%s-%s", username, prpl_id);
+        md5_str = g_compute_checksum_for_string(
+                        G_CHECKSUM_MD5, id, strlen(id));
+
         g_free(id);
+
         return md5_str;
 }
 
@@ -669,7 +649,6 @@ get_acc_id(const char *username, const c
 static void
 acc_sign_on_event(PurpleAccount *account)
 {
-
         const char *username, *protocol, *connect_server = "";
         char *id, *username_dup, *len_str;
         xmlnode *acc, *p_node, *len_node, *connect_server_node;
@@ -701,10 +680,9 @@ acc_sign_on_event(PurpleAccount *account
         /* Number of buddies in account list */
         buddies_in_account = purple_find_buddies(account, NULL);
         len = g_slist_length(buddies_in_account);
-        len_node = xmlnode_new("buddies");
+        len_node = xmlnode_new_child(acc, "buddies");
         len_str = g_strdup_printf("%d", len);
         xmlnode_insert_data(len_node, len_str, -1);
-        xmlnode_insert_child(acc, len_node);
 
         /* We can also send ``username-splits'' as they will be helpful
          * in general
@@ -735,22 +713,24 @@ acc_sign_on_event(PurpleAccount *account
                                 value = "";
                                 value_md5 = NULL;
                         } else {
-                                value_md5 = md5((guchar *)value);
+                                value_md5 = g_compute_checksum_for_string(
+                                                G_CHECKSUM_MD5, value,
+                                                strlen(value));
                         }
                         /* Check if the Hash is in the public_server_hash_table */
                         split_text = g_strdup(split->text);
-                        user_split_node = xmlnode_new(split_text);
+                        user_split_node = xmlnode_new_child(acc, split_text);
                         if (g_hash_table_lookup_extended(
                                                 public_server_cache_ht,
                                                 value_md5, NULL, NULL)) {
                                 xmlnode_insert_data(user_split_node, value, -1);
                         }
-                        xmlnode_insert_child(acc, user_split_node);
+
                         /* Normal MD5 node */
                         value_md5_tag = g_strdup_printf("%s_hash", split->text);
-                        user_split_node_hash = xmlnode_new(value_md5_tag);
+                        user_split_node_hash = xmlnode_new_child(acc,
+                                        value_md5_tag);
                         xmlnode_insert_data(user_split_node_hash, value_md5, -1);
-                        xmlnode_insert_child(acc, user_split_node_hash);
 
                         g_free(value_md5);
                         g_free(value_md5_tag);
@@ -766,21 +746,14 @@ acc_sign_on_event(PurpleAccount *account
                  * instead as it's chat server.
                  */
                 connect_server = purple_account_get_string(account, "connect_server", "");
-                connect_server_node = xmlnode_new("connect-server");
+                connect_server_node = xmlnode_new_child(acc, "connect-server");
                 xmlnode_insert_data(connect_server_node, connect_server, -1);
-                xmlnode_insert_child(acc, connect_server_node);
-        }
-        user_splits = PURPLE_PLUGIN_PROTOCOL_INFO(purple_find_prpl
-                        (purple_account_get_protocol_id(account)))->user_splits;
-        if (user_splits != NULL) {
         }
 
-        /* check if the account already exist in our XML file */
-        if (g_hash_table_lookup(stats_acc_ht, id)) {
-                g_hash_table_replace(stats_acc_ht, id, (void *)acc);
-        } else {
-                g_hash_table_insert(stats_acc_ht, id, (void *)acc);
-        }
+        /* If the value exists, it'll be replaced else there will be a new key
+         * inserted
+         */
+        g_hash_table_insert(stats_acc_ht, id, acc);
         schedule_stats_save();
 
         /* Free OWNED objects */
@@ -793,50 +766,22 @@ static void
 plugin_load_event(PurplePlugin *plugin)
 {
 
-        const char *id, *name, *version, *author, *summary, *description;
+        const char *id;
         xmlnode *plugin_xml;
         PurplePluginType plugin_type;
 
         id = purple_plugin_get_id(plugin);
-        name = purple_plugin_get_name(plugin);



More information about the Commits mailing list