jingle-ft/filetransfer.c
changeset 156 653fa009fea3
parent 152 a8623ca21574
child 159 1df5f5e3f1e7
equal deleted inserted replaced
155:35e603b584a3 156:653fa009fea3
    40 #include <jingle/send.h>
    40 #include <jingle/send.h>
    41 
    41 
    42 #include "filetransfer.h"
    42 #include "filetransfer.h"
    43 
    43 
    44 
    44 
    45 static gconstpointer check(JingleContent *cn, GError **err);
    45 static gconstpointer newfrommessage(JingleContent *cn, GError **err);
    46 static gboolean handle(JingleAction action, gconstpointer data, LmMessageNode *node);
    46 static JingleHandleStatus handle(JingleAction action, gconstpointer data,
       
    47                                  LmMessageNode *node, GError **err);
    47 static void tomessage(gconstpointer data, LmMessageNode *node);
    48 static void tomessage(gconstpointer data, LmMessageNode *node);
    48 static gboolean handle_data(gconstpointer data, const gchar *data2, guint len);
    49 static gboolean handle_data(gconstpointer data, const gchar *data2, guint len);
    49 static void start(session_content *sc);
    50 static void start(session_content *sc);
    50 static void send(session_content *sc);
    51 static void send(session_content *sc);
    51 static void stop(gconstpointer data);
    52 static void stop(gconstpointer data);
    77   "REJECT",
    78   "REJECT",
    78   "ERROR"
    79   "ERROR"
    79 };
    80 };
    80 
    81 
    81 static JingleAppFuncs funcs = {
    82 static JingleAppFuncs funcs = {
    82   .check        = check,
    83   .newfrommessage   = newfrommessage,
    83   .handle       = handle,
    84   .handle           = handle,
    84   .tomessage    = tomessage,
    85   .tomessage        = tomessage,
    85   .handle_data  = handle_data,
    86   .handle_data      = handle_data,
    86   .start        = start,
    87   .start            = start,
    87   .send         = send,
    88   .send             = send,
    88   .stop         = stop,
    89   .stop             = stop,
    89   .info         = info
    90   .info             = info
    90 };
    91 };
    91 
    92 
    92 module_info_t info_jingle_ft = {
    93 module_info_t info_jingle_ft = {
    93   .branch          = MCABBER_BRANCH,
    94   .branch          = MCABBER_BRANCH,
    94   .api             = MCABBER_API_VERSION,
    95   .api             = MCABBER_API_VERSION,
   106  *
   107  *
   107  * @param cn the jinglecontent a node description
   108  * @param cn the jinglecontent a node description
   108  * @param err contain an error of the domain JINGLE_CHECK_ERROR
   109  * @param err contain an error of the domain JINGLE_CHECK_ERROR
   109  * @return a gconstpointer, which is a new allocated JingleFT
   110  * @return a gconstpointer, which is a new allocated JingleFT
   110  */
   111  */
   111 static gconstpointer check(JingleContent *cn, GError **err)
   112 static gconstpointer newfrommessage(JingleContent *cn, GError **err)
   112 {
   113 {
   113   JingleFT *ft = NULL;
   114   JingleFT *ft = NULL;
   114   LmMessageNode *node;
   115   LmMessageNode *node;
   115   gint64 tmpsize;
   116   gint64 tmpsize;
   116   const gchar *datestr, *sizestr;
   117   const gchar *datestr, *sizestr;
   203  * @param action The action which have been received
   204  * @param action The action which have been received
   204  * @param data   Contain the JingleFT of the content concerned
   205  * @param data   Contain the JingleFT of the content concerned
   205  * @param node   The node himself
   206  * @param node   The node himself
   206  * @return       TRUE if the action was handled, FALSE otherwise
   207  * @return       TRUE if the action was handled, FALSE otherwise
   207  */
   208  */
   208 static gboolean handle(JingleAction action, gconstpointer data,
   209 static JingleHandleStatus handle(JingleAction action, gconstpointer data,
   209                        LmMessageNode *node)
   210                                  LmMessageNode *node, GError **err)
   210 {
   211 {
   211   if (action == JINGLE_SESSION_INFO) {
   212   if (action == JINGLE_SESSION_INFO) {
   212     if (!g_strcmp0(lm_message_node_get_attribute(node, "xmlns"),
   213     if (!g_strcmp0(lm_message_node_get_attribute(node, "xmlns"),
   213                    NS_JINGLE_APP_FT_INFO)
   214                    NS_JINGLE_APP_FT_INFO)
   214         && !g_strcmp0(node->name, "hash")) {
   215         && !g_strcmp0(node->name, "hash")) {
   215       ((JingleFT *)data)->hash = g_strdup(lm_message_node_get_value(node));
   216       ((JingleFT *)data)->hash = g_strdup(lm_message_node_get_value(node));
   216       return TRUE;
   217       return JINGLE_STATUS_HANDLED;
   217 	}
   218 	}
   218 	return FALSE;
   219 	return JINGLE_STATUS_NOT_HANDLED;
   219   }
   220   }
   220   return FALSE;
   221   return JINGLE_STATUS_NOT_HANDLED;
   221 }
   222 }
   222 
   223 
   223 static gboolean _is_md5_hash(const gchar *hash)
   224 static gboolean _is_md5_hash(const gchar *hash)
   224 {
   225 {
   225   int i = 0;
   226   int i = 0;
   256       scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: %s %s", err->message,
   257       scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: %s %s", err->message,
   257                    jft->name);
   258                    jft->name);
   258     //TODO: propagate the GError ?
   259     //TODO: propagate the GError ?
   259       g_error_free(err);
   260       g_error_free(err);
   260       return FALSE;
   261       return FALSE;
   261 	}
   262     }
   262 	jft->state = JINGLE_FT_STARTING;
   263     jft->state = JINGLE_FT_STARTING;
   263 	status = g_io_channel_set_encoding(jft->outfile, NULL, &err);
   264     status = g_io_channel_set_encoding(jft->outfile, NULL, &err);
   264 	if (status != G_IO_STATUS_NORMAL || err != NULL) {
   265     if (status != G_IO_STATUS_NORMAL || err != NULL) {
   265      scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: %s %s", err->message,
   266      scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: %s %s", err->message,
   266                   jft->name);
   267                   jft->name);
   267      g_error_free(err);
   268      g_error_free(err);
   268      return FALSE;
   269      return FALSE;
   269    }
   270    }
   270   }
   271   }
   271   
   272   
   272   jft->state = JINGLE_FT_STARTING;
   273   jft->state = JINGLE_FT_STARTING;
   273 	
   274 
   274   status = g_io_channel_write_chars(jft->outfile, data, (gssize) len,
   275   status = g_io_channel_write_chars(jft->outfile, data, (gssize) len,
   275                                     &bytes_written, &err);
   276                                     &bytes_written, &err);
   276   if (status != G_IO_STATUS_NORMAL || err != NULL) {
   277   if (status != G_IO_STATUS_NORMAL || err != NULL) {
   277      scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: %s %s", err->message,
   278      scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: %s %s", err->message,
   278                   jft->name);
   279                   jft->name);
   704   if (jft->hash != NULL && jft->md5 != NULL) {
   705   if (jft->hash != NULL && jft->md5 != NULL) {
   705     if (_check_hash(jft->hash, jft->md5) == FALSE) {
   706     if (_check_hash(jft->hash, jft->md5) == FALSE) {
   706       scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: File corrupt (%s)",
   707       scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: File corrupt (%s)",
   707                    jft->name);
   708                    jft->name);
   708     } else {
   709     } else {
   709       scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: transfer finished (%s)"
   710       scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: Transfer finished (%s)"
   710                    " and verified", jft->name);
   711                    " and verified", jft->name);
   711     }
   712     }
   712   } else {
   713   } else {
   713     scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: transfer finished (%s)"
   714     scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: Transfer finished (%s)"
   714                  " but not verified", jft->name);
   715                  " but not verified", jft->name);
   715   }
   716   }
   716 }
   717 }
   717 
   718 
   718 static gchar *_convert_size(guint64 size)
   719 static gchar *_convert_size(guint64 size)