comparison mcabber/src/commands.c @ 1432:46e5eb9917bc

Add an option "-f" (file) to /say_to
author Mikael Berthe <mikael@lilotux.net>
date Sat, 23 Feb 2008 11:42:07 +0100
parents 12847b0ea8c9
children 0c030b00306d
comparison
equal deleted inserted replaced
1431:486809ccffc0 1432:46e5eb9917bc
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA 19 * USA
20 */ 20 */
21 21
22 #include <string.h> 22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
23 26
24 #include "commands.h" 27 #include "commands.h"
25 #include "help.h" 28 #include "help.h"
26 #include "jabglue.h" 29 #include "jabglue.h"
27 #include "roster.h" 30 #include "roster.h"
1339 scr_LogPrint(LPRINT_NORMAL, "You have left multi-line message mode."); 1342 scr_LogPrint(LPRINT_NORMAL, "You have left multi-line message mode.");
1340 do_msay_return: 1343 do_msay_return:
1341 free_arg_lst(paramlst); 1344 free_arg_lst(paramlst);
1342 } 1345 }
1343 1346
1347 // load_message_from_file(filename)
1348 // Read the whole content of a file.
1349 // The data are converted to UTF8, they should be freed by the caller after
1350 // use.
1351 char *load_message_from_file(const char *filename)
1352 {
1353 struct stat buf;
1354 FILE *fd;
1355 char *msgbuf, *msgbuf_utf8;
1356 char *eol;
1357
1358 fd = fopen(filename, "r");
1359
1360 if (!fd || fstat(fileno(fd), &buf)) {
1361 scr_LogPrint(LPRINT_LOGNORM, "Cannot open message file (%s)", filename);
1362 return NULL;
1363 }
1364 if (!buf.st_size || buf.st_size >= HBB_BLOCKSIZE) {
1365 if (!buf.st_size)
1366 scr_LogPrint(LPRINT_LOGNORM, "Message file is empty (%s)", filename);
1367 else
1368 scr_LogPrint(LPRINT_LOGNORM, "Message file is too big (%s)", filename);
1369 fclose(fd);
1370 return NULL;
1371 }
1372
1373 msgbuf = g_new0(char, HBB_BLOCKSIZE);
1374 fread(msgbuf, HBB_BLOCKSIZE-1, 1, fd);
1375 fclose(fd);
1376
1377 // Strip trailing newlines
1378 for (eol = msgbuf ; *eol ; eol++)
1379 ;
1380 if (eol > msgbuf)
1381 eol--;
1382 while (eol > msgbuf && *eol == '\n')
1383 *eol-- = 0;
1384
1385 // It could be empty, once the trailing newlines are gone
1386 if (eol == msgbuf && *eol == '\n') {
1387 scr_LogPrint(LPRINT_LOGNORM, "Message file is empty (%s)", filename);
1388 g_free(msgbuf);
1389 return NULL;
1390 }
1391
1392 msgbuf_utf8 = to_utf8(msgbuf);
1393
1394 if (!msgbuf_utf8 && msgbuf)
1395 scr_LogPrint(LPRINT_LOGNORM, "Message file charset conversion error (%s)",
1396 filename);
1397 g_free(msgbuf);
1398 return msgbuf_utf8;
1399 }
1400
1344 static void do_say_to(char *arg) 1401 static void do_say_to(char *arg)
1345 { 1402 {
1346 char **paramlst; 1403 char **paramlst;
1347 char *fjid, *msg; 1404 char *fjid, *msg;
1405 char *file = NULL;
1348 const char *msg_type = NULL; 1406 const char *msg_type = NULL;
1349 bool quiet = FALSE; 1407 bool quiet = FALSE;
1350 1408
1351 if (!jb_getonline()) { 1409 if (!jb_getonline()) {
1352 scr_LogPrint(LPRINT_NORMAL, "You are not connected."); 1410 scr_LogPrint(LPRINT_NORMAL, "You are not connected.");
1354 } 1412 }
1355 1413
1356 msg_type = scan_mtype(&arg); 1414 msg_type = scan_mtype(&arg);
1357 paramlst = split_arg(arg, 2, 1); // jid, message (or option, jid, message) 1415 paramlst = split_arg(arg, 2, 1); // jid, message (or option, jid, message)
1358 1416
1417 if (!*paramlst) { // No parameter?
1418 scr_LogPrint(LPRINT_NORMAL, "Please specify a Jabber ID.");
1419 free_arg_lst(paramlst);
1420 return;
1421 }
1422
1359 // Check for an option parameter 1423 // Check for an option parameter
1360 if (*paramlst && !strcmp(*paramlst, "-q")) { 1424 while (TRUE) {
1361 char **oldparamlst = paramlst; 1425 if (!strcmp(*paramlst, "-q")) {
1362 paramlst = split_arg(*(oldparamlst+1), 2, 1); // jid, message 1426 char **oldparamlst = paramlst;
1363 free_arg_lst(oldparamlst); 1427 paramlst = split_arg(*(oldparamlst+1), 2, 1); // jid, message
1364 quiet = TRUE; 1428 free_arg_lst(oldparamlst);
1429 quiet = TRUE;
1430 } else if (!strcmp(*paramlst, "-f")) {
1431 char **oldparamlst = paramlst;
1432 paramlst = split_arg(*(oldparamlst+1), 2, 1); // filename, jid
1433 free_arg_lst(oldparamlst);
1434 file = g_strdup(*paramlst);
1435 // One more parameter shift...
1436 oldparamlst = paramlst;
1437 paramlst = split_arg(*(oldparamlst+1), 2, 1); // jid, nothing
1438 free_arg_lst(oldparamlst);
1439 } else
1440 break;
1365 } 1441 }
1366 1442
1367 fjid = *paramlst; 1443 fjid = *paramlst;
1368 msg = *(paramlst+1); 1444 msg = *(paramlst+1);
1369 1445
1370 if (!fjid || !strcmp(fjid, ".")) { 1446 if (!strcmp(fjid, ".") || check_jid_syntax(fjid)) {
1371 scr_LogPrint(LPRINT_NORMAL, "Please specify a Jabber ID."); 1447 scr_LogPrint(LPRINT_NORMAL, "Please specify a valid Jabber ID.");
1372 free_arg_lst(paramlst); 1448 free_arg_lst(paramlst);
1373 return; 1449 return;
1374 } 1450 }
1375 1451
1376 fjid = to_utf8(fjid); 1452 fjid = to_utf8(fjid);
1377 msg = to_utf8(msg); 1453 if (!file) {
1454 msg = to_utf8(msg);
1455 } else {
1456 if (msg)
1457 scr_LogPrint(LPRINT_NORMAL, "say_to: extra parameter ignored.");
1458 msg = load_message_from_file(file);
1459 g_free(file);
1460 }
1378 1461
1379 send_message_to(fjid, msg, NULL, msg_type, quiet); 1462 send_message_to(fjid, msg, NULL, msg_type, quiet);
1380 1463
1381 g_free(fjid); 1464 g_free(fjid);
1382 g_free(msg); 1465 g_free(msg);