cmd.c
changeset 17 e26e6692b0d5
parent 16 19d13d97b3a1
child 18 887d772326a0
equal deleted inserted replaced
16:19d13d97b3a1 17:e26e6692b0d5
    43 	"Recognizes options cmd_shell, cmd_redirect_stderr, cmd_header and cmd_header_inline\n" \
    43 	"Recognizes options cmd_shell, cmd_redirect_stderr, cmd_header and cmd_header_inline\n" \
    44 	"Provides command /cmd" )
    44 	"Provides command /cmd" )
    45 
    45 
    46 static module_info_t info_cmd_experimental = {
    46 static module_info_t info_cmd_experimental = {
    47 	.branch          = "experimental",
    47 	.branch          = "experimental",
    48 	.api             = 5,
    48 	.api             = 10,
    49 	.version         = PROJECT_VERSION,
    49 	.version         = PROJECT_VERSION,
    50 	.description     = DESCRIPTION,
    50 	.description     = DESCRIPTION,
    51 	.requires        = NULL,
    51 	.requires        = NULL,
    52 	.init            = mcmd_init,
    52 	.init            = mcmd_init,
    53 	.uninit          = mcmd_uninit,
    53 	.uninit          = mcmd_uninit,
    54 	.next            = NULL,
    54 	.next            = NULL,
    55 };
    55 };
    56 
    56 
    57 module_info_t info_cmd = {
    57 module_info_t info_cmd = {
    58 	.branch          = "dev",
    58 	.branch          = "dev",
    59 	.api             = 4,
    59 	.api             = 11,
    60 	.version         = PROJECT_VERSION,
    60 	.version         = PROJECT_VERSION,
    61 	.description     = DESCRIPTION,
    61 	.description     = DESCRIPTION,
    62 	.requires        = NULL,
    62 	.requires        = NULL,
    63 	.init            = mcmd_init,
    63 	.init            = mcmd_init,
    64 	.uninit          = mcmd_uninit,
    64 	.uninit          = mcmd_uninit,
    88 	gint     crypted;
    88 	gint     crypted;
    89 
    89 
    90 	xmpp_send_msg (to, body, room ? ROSTER_TYPE_ROOM : ROSTER_TYPE_USER, room ? NULL : subject, FALSE /* ? */, &crypted, LM_MESSAGE_SUB_TYPE_NOT_SET, &xep184);
    90 	xmpp_send_msg (to, body, room ? ROSTER_TYPE_ROOM : ROSTER_TYPE_USER, room ? NULL : subject, FALSE /* ? */, &crypted, LM_MESSAGE_SUB_TYPE_NOT_SET, &xep184);
    91 
    91 
    92 	if (crypted == -1) {
    92 	if (crypted == -1) {
    93 		scr_LogPrint (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent.");
    93 		scr_log_print (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent.");
    94 		return FALSE;
    94 		return FALSE;
    95 	}
    95 	}
    96 
    96 
    97 	if (!room) {
    97 	if (!room) {
    98 		if (subject) {
    98 		if (subject) {
   117 		GError       *error              = NULL;
   117 		GError       *error              = NULL;
   118 
   118 
   119 		chstat = g_io_channel_read_chars (channel, buf, HBB_BLOCKSIZE, &endpos, &error);
   119 		chstat = g_io_channel_read_chars (channel, buf, HBB_BLOCKSIZE, &endpos, &error);
   120 
   120 
   121 		if (error) {
   121 		if (error) {
   122 			scr_LogPrint (LPRINT_DEBUG, "cmd: Reading error: %s.", error -> message);
   122 			scr_log_print (LPRINT_DEBUG, "cmd: Reading error: %s.", error -> message);
   123 			g_clear_error (&error);
   123 			g_clear_error (&error);
   124 		}
   124 		}
   125 
   125 
   126 		if (chstat == G_IO_STATUS_ERROR || chstat == G_IO_STATUS_EOF) {
   126 		if (chstat == G_IO_STATUS_ERROR || chstat == G_IO_STATUS_EOF) {
   127 			cb->source = 0;
   127 			cb->source = 0;
   135 			gchar   *utf8    = NULL;
   135 			gchar   *utf8    = NULL;
   136 
   136 
   137 			g_string_append_len (input, buf, endpos);
   137 			g_string_append_len (input, buf, endpos);
   138 
   138 
   139 			if (!xmpp_is_online ()) {
   139 			if (!xmpp_is_online ()) {
   140 				scr_LogPrint (LPRINT_LOGNORM, "cmd: Connection is not ready, delaying data");
   140 				scr_log_print (LPRINT_LOGNORM, "cmd: Connection is not ready, delaying data");
   141 				return TRUE;
   141 				return TRUE;
   142 			}
   142 			}
   143 
   143 
   144 			// usual g_locale_to_utf8 seem to be unable to detect locale charset
   144 			// usual g_locale_to_utf8 seem to be unable to detect locale charset
   145 			// maybe, proper solution will be to call setlocale on module loading,
   145 			// maybe, proper solution will be to call setlocale on module loading,
   164 					if (written - sent > HBB_BLOCKSIZE) {
   164 					if (written - sent > HBB_BLOCKSIZE) {
   165 						gchar *c = utf8 + sent + HBB_BLOCKSIZE;
   165 						gchar *c = utf8 + sent + HBB_BLOCKSIZE;
   166 						c = g_utf8_find_prev_char (utf8 + sent, c);
   166 						c = g_utf8_find_prev_char (utf8 + sent, c);
   167 
   167 
   168 						if (!c) {
   168 						if (!c) {
   169 							scr_LogPrint (LPRINT_LOGNORM, "cmd: Cannot determine utf8 character end! End of data chunk will be discarded!");
   169 							scr_log_print (LPRINT_LOGNORM, "cmd: Cannot determine utf8 character end! End of data chunk will be discarded!");
   170 							break;
   170 							break;
   171 						}
   171 						}
   172 
   172 
   173 						len  = c - utf8 - sent;
   173 						len  = c - utf8 - sent;
   174 						bbuf = g_strndup (utf8 + sent, len);
   174 						bbuf = g_strndup (utf8 + sent, len);
   178 
   178 
   179 					if (cb -> subject)
   179 					if (cb -> subject)
   180 						subject = g_strdup_printf (cb -> subject, cb -> seq);
   180 						subject = g_strdup_printf (cb -> subject, cb -> seq);
   181 
   181 
   182 					if (!cmd_send_msg (cb->jid, subject, len ? bbuf : (utf8 + sent)))
   182 					if (!cmd_send_msg (cb->jid, subject, len ? bbuf : (utf8 + sent)))
   183 						scr_LogPrint (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent.");
   183 						scr_log_print (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent.");
   184 
   184 
   185 					if (subject)
   185 					if (subject)
   186 						g_free (subject);
   186 						g_free (subject);
   187 
   187 
   188 					if (!len)
   188 					if (!len)
   197 
   197 
   198 				g_string_erase (input, 0, bread);
   198 				g_string_erase (input, 0, bread);
   199 
   199 
   200 			} else {
   200 			} else {
   201 
   201 
   202 				scr_LogPrint (LPRINT_LOGNORM, "cmd: Character conversion error: %s", error->message);
   202 				scr_log_print (LPRINT_LOGNORM, "cmd: Character conversion error: %s", error->message);
   203 				g_error_free (error);
   203 				g_error_free (error);
   204 				cb->source = 0;
   204 				cb->source = 0;
   205 				return FALSE;
   205 				return FALSE;
   206 			}
   206 			}
   207 		}
   207 		}
   237 
   237 
   238 	if (!*arg)
   238 	if (!*arg)
   239 		return;
   239 		return;
   240 	
   240 	
   241 	if (!jid) {
   241 	if (!jid) {
   242 		scr_LogPrint (LPRINT_LOGNORM, "Unsuitable buddy selected.");
   242 		scr_log_print (LPRINT_LOGNORM, "Unsuitable buddy selected.");
   243 		return;
   243 		return;
   244 	}
   244 	}
   245 	
   245 	
   246 	if (pipe (fd)) {
   246 	if (pipe (fd)) {
   247 		scr_LogPrint (LPRINT_LOGNORM, "Cannot create pipe: %s.", strerror (errno));
   247 		scr_log_print (LPRINT_LOGNORM, "Cannot create pipe: %s.", strerror (errno));
   248 		return;
   248 		return;
   249 	}
   249 	}
   250 
   250 
   251 	{
   251 	{
   252 		GIOChannel *channel;
   252 		GIOChannel *channel;
   272 				execl (shell, shell, "-c", arg, NULL);
   272 				execl (shell, shell, "-c", arg, NULL);
   273 			}
   273 			}
   274 		}
   274 		}
   275 
   275 
   276 		if (res == -1) {
   276 		if (res == -1) {
   277 			scr_LogPrint (LPRINT_NORMAL, "Cannot fork child: %s.", strerror (errno));
   277 			scr_log_print (LPRINT_NORMAL, "Cannot fork child: %s.", strerror (errno));
   278 			close (fd[0]);
   278 			close (fd[0]);
   279 			close (fd[1]);
   279 			close (fd[1]);
   280 			return;
   280 			return;
   281 		}
   281 		}
   282 
   282 
   287 
   287 
   288 			channel = g_io_channel_unix_new (fd[0]);
   288 			channel = g_io_channel_unix_new (fd[0]);
   289 
   289 
   290 			g_io_channel_set_encoding (channel, NULL, &error);
   290 			g_io_channel_set_encoding (channel, NULL, &error);
   291 			if (error) {
   291 			if (error) {
   292 				scr_LogPrint (LPRINT_DEBUG, "cmd: Cannot unset channel encoding: %s.", error -> message);
   292 				scr_log_print (LPRINT_DEBUG, "cmd: Cannot unset channel encoding: %s.", error -> message);
   293 				g_clear_error (&error);
   293 				g_clear_error (&error);
   294 			}
   294 			}
   295 			g_io_channel_set_buffered (channel, FALSE);
   295 			g_io_channel_set_buffered (channel, FALSE);
   296 			g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error);
   296 			g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error);
   297 			if (error) {
   297 			if (error) {
   298 				scr_LogPrint (LPRINT_DEBUG, "cmd: Cannot set nonblocking flag on channel: %s.", error -> message);
   298 				scr_log_print (LPRINT_DEBUG, "cmd: Cannot set nonblocking flag on channel: %s.", error -> message);
   299 				g_error_free (error);
   299 				g_error_free (error);
   300 			}
   300 			}
   301 			g_io_channel_set_close_on_unref (channel, TRUE);
   301 			g_io_channel_set_close_on_unref (channel, TRUE);
   302 		}
   302 		}
   303 
   303