jingle/action-handlers.c
changeset 45 c007e112dd54
parent 44 4738d78edc05
child 46 f33c814d5b3a
equal deleted inserted replaced
44:4738d78edc05 45:c007e112dd54
    31 #include <jingle/sessions.h>
    31 #include <jingle/sessions.h>
    32 #include <jingle/register.h>
    32 #include <jingle/register.h>
    33 #include <jingle/send.h>
    33 #include <jingle/send.h>
    34 #include <jingle/action-handlers.h>
    34 #include <jingle/action-handlers.h>
    35 
    35 
    36 void handle_content_accept(LmMessage *m, JingleNode *jn)
    36 void handle_content_accept(JingleNode *jn)
    37 {
    37 {
    38   GError *err = NULL;
    38   GError *err = NULL;
    39   GSList *child = NULL;
    39   GSList *child = NULL;
    40   JingleContent *cn;
    40   JingleContent *cn;
    41   JingleAppFuncs *appfuncs; 
    41   JingleAppFuncs *appfuncs; 
    45   JingleSession *sess;
    45   JingleSession *sess;
    46 
    46 
    47   if (!check_contents(jn, &err)) {
    47   if (!check_contents(jn, &err)) {
    48     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
    48     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
    49                   err->message);
    49                   err->message);
    50     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
    50     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
    51     return;
    51     return;
    52   }
    52   }
    53 
    53 
    54   /* it's better if there is at least one content elem */
    54   /* it's better if there is at least one content elem */
    55   if (g_slist_length(jn->content) < 1) {
    55   if (g_slist_length(jn->content) < 1) {
    56     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
    56     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
    57     return;
    57     return;
    58   }
    58   }
    59   
    59   
    60   // if a session with the same sid doesn't already exists
    60   // if a session with the same sid doesn't already exists
    61   if ((sess = session_find(jn)) == NULL) {
    61   if ((sess = session_find(jn)) == NULL) {
    62     jingle_send_iq_error(m, "cancel", "item-not-found", "unknown-session");
    62     jingle_send_iq_error(jn->message, "cancel", "item-not-found", "unknown-session");
    63     return;
    63     return;
    64   }
    64   }
    65 
    65 
    66   jingle_ack_iq(m);
    66   jingle_ack_iq(jn->message);
    67 
    67 
    68   for (child = jn->content; child; child = child->next) {
    68   for (child = jn->content; child; child = child->next) {
    69     cn = (JingleContent *)(child->data);
    69     cn = (JingleContent *)(child->data);
    70     session_changestate_sessioncontent(sess, cn->name, ACTIVE);
    70     session_changestate_sessioncontent(sess, cn->name, ACTIVE);
    71   }
    71   }
    72 }
    72 }
    73 
    73 
    74 void handle_content_add(LmMessage *m, JingleNode *jn)
    74 void handle_content_add(JingleNode *jn)
    75 {
    75 {
    76   GError *err = NULL;
    76   GError *err = NULL;
    77   GSList *child = NULL;
    77   GSList *child = NULL;
    78   JingleContent *cn;
    78   JingleContent *cn;
    79   JingleAppFuncs *appfuncs; 
    79   JingleAppFuncs *appfuncs; 
    87   
    87   
    88   
    88   
    89   if (!check_contents(jn, &err)) {
    89   if (!check_contents(jn, &err)) {
    90     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
    90     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
    91                   err->message);
    91                   err->message);
    92     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
    92     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
    93     return;
    93     return;
    94   }
    94   }
    95 
    95 
    96   /* it's better if there is at least one content elem */
    96   /* it's better if there is at least one content elem */
    97   if (g_slist_length(jn->content) < 1) {
    97   if (g_slist_length(jn->content) < 1) {
    98     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
    98     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
    99     return;
    99     return;
   100   }
   100   }
   101   
   101   
   102   // if a session with the same sid doesn't already exists
   102   // if a session with the same sid doesn't already exists
   103   if ((sess = session_find(jn)) == NULL) {
   103   if ((sess = session_find(jn)) == NULL) {
   104     jingle_send_iq_error(m, "cancel", "item-not-found", "unknown-session");
   104     jingle_send_iq_error(jn->message, "cancel", "item-not-found", "unknown-session");
   105     return;
   105     return;
   106   }
   106   }
   107 
   107 
   108   jingle_ack_iq(m);
   108   jingle_ack_iq(jn->message);
   109 
   109 
   110   accept.action = JINGLE_CONTENT_ACCEPT;
   110   accept.action = JINGLE_CONTENT_ACCEPT;
   111   accept.sid = jn->sid;
   111   accept.sid = jn->sid;
   112   accept.content = NULL;
   112   accept.content = NULL;
   113   
   113   
   139     session_add_content(sess, cn, ACTIVE);
   139     session_add_content(sess, cn, ACTIVE);
   140     accept.content = g_slist_append(accept.content, cn);
   140     accept.content = g_slist_append(accept.content, cn);
   141   }
   141   }
   142   
   142   
   143   if (g_slist_length(accept.content) != 0) {
   143   if (g_slist_length(accept.content) != 0) {
   144     r = lm_message_from_jinglenode(&accept, lm_message_get_from(m));
   144     r = lm_message_from_jinglenode(&accept, lm_message_get_from(jn->message));
   145     if (r) {
   145     if (r) {
   146       lm_connection_send(lconnection, r, NULL);
   146       lm_connection_send(lconnection, r, NULL);
   147       lm_message_unref(r);
   147       lm_message_unref(r);
   148     }
   148     }
   149   }
   149   }
   150   
   150   
   151   if (g_slist_length(reject.content) != 0) {
   151   if (g_slist_length(reject.content) != 0) {
   152     r = lm_message_from_jinglenode(&reject, lm_message_get_from(m));
   152     r = lm_message_from_jinglenode(&reject, lm_message_get_from(jn->message));
   153     if (r) {
   153     if (r) {
   154       lm_connection_send(lconnection, r, NULL);
   154       lm_connection_send(lconnection, r, NULL);
   155       lm_message_unref(r);
   155       lm_message_unref(r);
   156     }
   156     }
   157   }
   157   }
   158 }
   158 }
   159 
   159 
   160 void handle_content_reject(LmMessage *m, JingleNode *jn)
   160 void handle_content_reject(JingleNode *jn)
   161 {
   161 {
   162   GError *err = NULL;
   162   GError *err = NULL;
   163   GSList *child = NULL;
   163   GSList *child = NULL;
   164   JingleContent *cn;
   164   JingleContent *cn;
   165   JingleAppFuncs *appfuncs; 
   165   JingleAppFuncs *appfuncs; 
   169   JingleSession *sess;
   169   JingleSession *sess;
   170 
   170 
   171   if (!check_contents(jn, &err)) {
   171   if (!check_contents(jn, &err)) {
   172     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
   172     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
   173                   err->message);
   173                   err->message);
   174     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
   174     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
   175     return;
   175     return;
   176   }
   176   }
   177 
   177 
   178   /* it's better if there is at least one content elem */
   178   /* it's better if there is at least one content elem */
   179   if (g_slist_length(jn->content) < 1) {
   179   if (g_slist_length(jn->content) < 1) {
   180     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
   180     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
   181     return;
   181     return;
   182   }
   182   }
   183   
   183   
   184   // if a session with the same sid doesn't already exists
   184   // if a session with the same sid doesn't already exists
   185   if ((sess = session_find(jn)) == NULL) {
   185   if ((sess = session_find(jn)) == NULL) {
   186     jingle_send_iq_error(m, "cancel", "item-not-found", "unknown-session");
   186     jingle_send_iq_error(jn->message, "cancel", "item-not-found", "unknown-session");
   187     return;
   187     return;
   188   }
   188   }
   189 
   189 
   190   jingle_ack_iq(m);
   190   jingle_ack_iq(jn->message);
   191 
   191 
   192   for (child = jn->content; child; child = child->next) {
   192   for (child = jn->content; child; child = child->next) {
   193     cn = (JingleContent *)(child->data);
   193     cn = (JingleContent *)(child->data);
   194     session_remove_sessioncontent(sess, cn->name);
   194     session_remove_sessioncontent(sess, cn->name);
   195   }
   195   }
   200     session_delete(sess);
   200     session_delete(sess);
   201     return;
   201     return;
   202   }
   202   }
   203 }
   203 }
   204 
   204 
   205 void handle_content_remove(LmMessage *m, JingleNode *jn)
   205 void handle_content_remove(JingleNode *jn)
   206 {
   206 {
   207   GError *err = NULL;
   207   GError *err = NULL;
   208   GSList *child = NULL;
   208   GSList *child = NULL;
   209   JingleContent *cn;
   209   JingleContent *cn;
   210   JingleAppFuncs *appfuncs; 
   210   JingleAppFuncs *appfuncs; 
   214   JingleSession *sess;
   214   JingleSession *sess;
   215 
   215 
   216   if (!check_contents(jn, &err)) {
   216   if (!check_contents(jn, &err)) {
   217     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
   217     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
   218                   err->message);
   218                   err->message);
   219     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
   219     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
   220     return;
   220     return;
   221   }
   221   }
   222 
   222 
   223   /* it's better if there is at least one content elem */
   223   /* it's better if there is at least one content elem */
   224   if (g_slist_length(jn->content) < 1) {
   224   if (g_slist_length(jn->content) < 1) {
   225     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
   225     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
   226     return;
   226     return;
   227   }
   227   }
   228   
   228   
   229   // if a session with the same sid doesn't already exists
   229   // if a session with the same sid doesn't already exists
   230   if ((sess = session_find(jn)) == NULL) {
   230   if ((sess = session_find(jn)) == NULL) {
   231     jingle_send_iq_error(m, "cancel", "item-not-found", "unknown-session");
   231     jingle_send_iq_error(jn->message, "cancel", "item-not-found", "unknown-session");
   232     return;
   232     return;
   233   }
   233   }
   234 
   234 
   235   jingle_ack_iq(m);
   235   jingle_ack_iq(jn->message);
   236 
   236 
   237   for (child = jn->content; child; child = child->next) {
   237   for (child = jn->content; child; child = child->next) {
   238     cn = (JingleContent *)(child->data);
   238     cn = (JingleContent *)(child->data);
   239     session_remove_sessioncontent(sess, cn->name);
   239     session_remove_sessioncontent(sess, cn->name);
   240   }
   240   }
   241 }
   241 }
   242 
   242 
   243 void handle_session_initiate(LmMessage *m, JingleNode *jn)
   243 void handle_session_initiate(JingleNode *jn)
   244 {
   244 {
   245   GError *err = NULL;
   245   GError *err = NULL;
   246   gboolean valid_disposition = FALSE;
   246   gboolean valid_disposition = FALSE;
   247   JingleContent *cn;
   247   JingleContent *cn;
   248   GString *sbuf;
   248   GString *sbuf;
   250   LmMessage *r;
   250   LmMessage *r;
   251   
   251   
   252   if (!check_contents(jn, &err)) {
   252   if (!check_contents(jn, &err)) {
   253     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
   253     scr_log_print(LPRINT_DEBUG, "jingle: One of the content element was invalid (%s)",
   254                   err->message);
   254                   err->message);
   255     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
   255     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
   256     return;
   256     return;
   257   }
   257   }
   258 
   258 
   259   // a session-initiate message must contains at least one <content> element
   259   // a session-initiate message must contains at least one <content> element
   260   if (g_slist_length(jn->content) < 1) {
   260   if (g_slist_length(jn->content) < 1) {
   261     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
   261     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
   262     return;
   262     return;
   263   }
   263   }
   264   
   264   
   265   /* When sending a session-initiate at least one <content/> element MUST have
   265   /* When sending a session-initiate at least one <content/> element MUST have
   266    * a disposition of "session"); if this rule is violated, the responder MUST
   266    * a disposition of "session"); if this rule is violated, the responder MUST
   270     cn = (JingleContent*)(child->data);
   270     cn = (JingleContent*)(child->data);
   271     if (g_strcmp0(cn->disposition, "session") == 0 || cn->disposition == NULL)
   271     if (g_strcmp0(cn->disposition, "session") == 0 || cn->disposition == NULL)
   272       valid_disposition = TRUE;
   272       valid_disposition = TRUE;
   273   }
   273   }
   274   if (!valid_disposition) {
   274   if (!valid_disposition) {
   275     jingle_send_iq_error(m, "cancel", "bad-request", NULL);
   275     jingle_send_iq_error(jn->message, "cancel", "bad-request", NULL);
   276     return;  
   276     return;  
   277   }
   277   }
   278   
   278   
   279   // if a session with the same sid already exists
   279   // if a session with the same sid already exists
   280   if (session_find(jn) != NULL) {
   280   if (session_find(jn) != NULL) {
   281     jingle_send_iq_error(m, "cancel", "unexpected-request", "out-of-order");
   281     jingle_send_iq_error(jn->message, "cancel", "unexpected-request", "out-of-order");
   282     return;
   282     return;
   283   }
   283   }
   284 
   284 
   285   jingle_ack_iq(m);
   285   jingle_ack_iq(jn->message);
   286 
   286 
   287   // Wait that user accept the jingle
   287   // Wait that user accept the jingle
   288   sbuf = g_string_new("");
   288   sbuf = g_string_new("");
   289   g_string_printf(sbuf, "Received an invitation for a jingle session from <%s>", lm_message_get_from(m));
   289   g_string_printf(sbuf, "Received an invitation for a jingle session from <%s>", lm_message_get_from(jn->message));
   290 
   290 
   291   scr_WriteIncomingMessage(jidtodisp(lm_message_get_from(m)), sbuf->str, 0, HBB_PREFIX_INFO, 0);
   291   scr_WriteIncomingMessage(jidtodisp(lm_message_get_from(jn->message)), sbuf->str, 0, HBB_PREFIX_INFO, 0);
   292   scr_LogPrint(LPRINT_LOGNORM, "%s", sbuf->str);
   292   scr_LogPrint(LPRINT_LOGNORM, "%s", sbuf->str);
   293 
   293 
   294   {
   294   {
   295     const char *id;
   295     const char *id;
   296     char *desc = g_strdup_printf("<%s> invites you to do a jingle session", lm_message_get_from(m));
   296     char *desc = g_strdup_printf("<%s> invites you to do a jingle session", lm_message_get_from(jn->message));
   297 
   297 
   298     id = evs_new(desc, NULL, 0, evscallback_jingle, jn, (GDestroyNotify)NULL);
   298     id = evs_new(desc, NULL, 0, evscallback_jingle, jn, (GDestroyNotify)NULL);
   299     g_free(desc);
   299     g_free(desc);
   300     if (id)
   300     if (id)
   301       g_string_printf(sbuf, "Please use /event %s accept|reject", id);
   301       g_string_printf(sbuf, "Please use /event %s accept|reject", id);
   302     else
   302     else
   303       g_string_printf(sbuf, "Unable to create a new event!");
   303       g_string_printf(sbuf, "Unable to create a new event!");
   304   }
   304   }
   305 }
   305 }
   306 
   306 
   307 void handle_session_terminate(LmMessage *m, JingleNode *jn)
   307 void handle_session_terminate(JingleNode *jn)
   308 {
   308 {
   309   JingleSession *sess;
   309   JingleSession *sess;
   310   if ((sess = session_find(jn)) == NULL) {
   310   if ((sess = session_find(jn)) == NULL) {
   311     jingle_send_iq_error(m, "cancel", "item-not-found", "unknown-session");
   311     jingle_send_iq_error(jn->message, "cancel", "item-not-found", "unknown-session");
   312     return;
   312     return;
   313   }
   313   }
   314   session_delete(sess);
   314   session_delete(sess);
   315   jingle_ack_iq(m);
   315   jingle_ack_iq(jn->message);
   316 }
   316 }