comparison mcabber/src/commands.c @ 1305:9bc68473f8a3

-n and -f flags to message-sending commands Allows sending normal and headline messages
author Michal 'vorner' Vaner <vorner@ucw.cz>
date Sat, 08 Sep 2007 13:21:07 +0200
parents 37b41ed9ed35
children 0dda8238af21
comparison
equal deleted inserted replaced
1304:8ada97e5eb75 1305:9bc68473f8a3
79 static void do_chat_disable(char *arg); 79 static void do_chat_disable(char *arg);
80 static void do_source(char *arg); 80 static void do_source(char *arg);
81 static void do_color(char *arg); 81 static void do_color(char *arg);
82 static void do_otr(char *arg); 82 static void do_otr(char *arg);
83 static void do_otrpolicy(char *arg); 83 static void do_otrpolicy(char *arg);
84
85 static void do_say_internal(char *arg, int parse_flags);
84 86
85 // Global variable for the commands list 87 // Global variable for the commands list
86 static GSList *Commands; 88 static GSList *Commands;
87 89
88 90
345 if (sl_com) // Command has been found. 347 if (sl_com) // Command has been found.
346 return (cmd*)sl_com->data; 348 return (cmd*)sl_com->data;
347 return NULL; 349 return NULL;
348 } 350 }
349 351
350 // send_message(msg)
351 // Write the message in the buddy's window and send the message on
352 // the network.
353 static void send_message(const char *msg, const char *subj)
354 {
355 const char *bjid;
356 gint crypted;
357
358 if (!jb_getonline()) {
359 scr_LogPrint(LPRINT_NORMAL, "You are not connected.");
360 return;
361 }
362
363 if (!current_buddy) {
364 scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
365 return;
366 }
367
368 bjid = CURRENT_JID;
369 if (!bjid) {
370 scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
371 return;
372 }
373
374 // Network part
375 jb_send_msg(bjid, msg, buddy_gettype(BUDDATA(current_buddy)), subj, NULL,
376 &crypted);
377
378 if (crypted == -1) {
379 scr_LogPrint(LPRINT_LOGNORM, "Encryption error. Message was not sent.");
380 return;
381 }
382
383 // Hook
384 if (buddy_gettype(BUDDATA(current_buddy)) != ROSTER_TYPE_ROOM) {
385 // local part (UI, logging, etc.)
386 gchar *hmsg;
387 if (subj)
388 hmsg = g_strdup_printf("[%s]\n%s", subj, msg);
389 else
390 hmsg = (char*)msg;
391 hk_message_out(bjid, NULL, 0, hmsg, crypted);
392 if (hmsg != msg) g_free(hmsg);
393 }
394 }
395
396 // process_command(line, iscmd) 352 // process_command(line, iscmd)
397 // Process a command line. 353 // Process a command line.
398 // If iscmd is TRUE, process the command even if verbatim mmode is set; 354 // If iscmd is TRUE, process the command even if verbatim mmode is set;
399 // it is intended to be used for key bindings. 355 // it is intended to be used for key bindings.
400 // Return 255 if this is the /quit command, and 0 for the other commands. 356 // Return 255 if this is the /quit command, and 0 for the other commands.
489 if (*line != COMMAND_CHAR) { 445 if (*line != COMMAND_CHAR) {
490 // This isn't a command 446 // This isn't a command
491 if (scr_get_multimode()) 447 if (scr_get_multimode())
492 scr_append_multiline(line); 448 scr_append_multiline(line);
493 else 449 else
494 do_say(line); 450 do_say_internal(line, 0);
495 return 0; 451 return 0;
496 } 452 }
497 453
498 /* It is _probably_ a command -- except for verbatim multi-line mode */ 454 /* It is _probably_ a command -- except for verbatim multi-line mode */
499 return process_command(line, FALSE); 455 return process_command(line, FALSE);
1057 buddylist_build(); 1013 buddylist_build();
1058 update_roster = TRUE; 1014 update_roster = TRUE;
1059 if (leave_buddywindow) scr_ShowBuddyWindow(); 1015 if (leave_buddywindow) scr_ShowBuddyWindow();
1060 } 1016 }
1061 1017
1062 static int send_message_to(const char *fjid, const char *msg, const char *subj) 1018 static int send_message_to(const char *fjid, const char *msg, const char *subj,
1019 const char *type_overwrite)
1063 { 1020 {
1064 char *bare_jid, *rp; 1021 char *bare_jid, *rp;
1065 char *hmsg; 1022 char *hmsg;
1066 gint crypted; 1023 gint crypted;
1067 gint retval = 0; 1024 gint retval = 0;
1068 1025 int isroom;
1026
1027 if (!jb_getonline()) {
1028 scr_LogPrint(LPRINT_NORMAL, "You are not connected.");
1029 return 1;
1030 }
1069 if (!fjid || !*fjid) { 1031 if (!fjid || !*fjid) {
1070 scr_LogPrint(LPRINT_NORMAL, "You must specify a Jabber ID."); 1032 scr_LogPrint(LPRINT_NORMAL, "You must specify a Jabber ID.");
1071 return 1; 1033 return 1;
1072 } 1034 }
1073 if (!msg || !*msg) { 1035 if (!msg || !*msg) {
1088 // Jump to window, create one if needed 1050 // Jump to window, create one if needed
1089 scr_RosterJumpJid(bare_jid); 1051 scr_RosterJumpJid(bare_jid);
1090 1052
1091 // Check if we're sending a message to a conference room 1053 // Check if we're sending a message to a conference room
1092 // If not, we must make sure rp is NULL, for hk_message_out() 1054 // If not, we must make sure rp is NULL, for hk_message_out()
1055 isroom = !!roster_find(bare_jid, jidsearch, ROSTER_TYPE_ROOM);
1093 if (rp) { 1056 if (rp) {
1094 if (roster_find(bare_jid, jidsearch, ROSTER_TYPE_ROOM)) rp++; 1057 if (isroom) rp++;
1095 else rp = NULL; 1058 else rp = NULL;
1096 } 1059 }
1060 isroom = isroom && (!rp || !*rp);
1097 1061
1098 // local part (UI, logging, etc.) 1062 // local part (UI, logging, etc.)
1099 if (subj) 1063 if (subj)
1100 hmsg = g_strdup_printf("[%s]\n%s", subj, msg); 1064 hmsg = g_strdup_printf("[%s]\n%s", subj, msg);
1101 else 1065 else
1102 hmsg = (char*)msg; 1066 hmsg = (char*)msg;
1103 1067
1104 // Network part 1068 // Network part
1105 jb_send_msg(fjid, msg, ROSTER_TYPE_USER, subj, NULL, &crypted); 1069 jb_send_msg(fjid, msg, (isroom ? ROSTER_TYPE_ROOM : ROSTER_TYPE_USER),
1070 subj, NULL, &crypted,
1071 type_overwrite);
1106 1072
1107 if (crypted == -1) { 1073 if (crypted == -1) {
1108 scr_LogPrint(LPRINT_LOGNORM, "Encryption error. Message was not sent."); 1074 scr_LogPrint(LPRINT_LOGNORM, "Encryption error. Message was not sent.");
1109 retval = 1; 1075 retval = 1;
1110 goto send_message_to_return; 1076 goto send_message_to_return;
1111 } 1077 }
1112 1078
1113 // Hook 1079 // Hook
1114 hk_message_out(bare_jid, rp, 0, hmsg, crypted); 1080 if(!isroom)
1081 hk_message_out(bare_jid, rp, 0, hmsg, crypted);
1115 1082
1116 send_message_to_return: 1083 send_message_to_return:
1117 if (hmsg != msg) g_free(hmsg); 1084 if (hmsg != msg) g_free(hmsg);
1118 if (rp) g_free(bare_jid); 1085 if (rp) g_free(bare_jid);
1119 return retval; 1086 return retval;
1120 } 1087 }
1121 1088
1122 static void do_say(char *arg) 1089 // send_message(msg, subj, type_overwrite)
1090 // Write the message in the buddy's window and send the message on
1091 // the network.
1092 static void send_message(const char *msg, const char *subj,
1093 const char *type_overwrite)
1094 {
1095 const char *bjid;
1096
1097 if (!current_buddy) {
1098 scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
1099 return;
1100 }
1101
1102 bjid = CURRENT_JID;
1103 if (!bjid) {
1104 scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
1105 return;
1106 }
1107
1108 send_message_to(bjid, msg, subj, type_overwrite);
1109 }
1110
1111 static const char *scan_mtype(char **arg)
1112 {
1113 //Try splitting it
1114 char **parlist = split_arg(*arg, 2, 1);
1115 const char *result = NULL;
1116 //Is it any good parameter?
1117 if(parlist && *parlist) {
1118 if(!strcmp("-n", *parlist)) {
1119 result = TMSG_NORMAL;
1120 } else if(!strcmp("-h", *parlist)) {
1121 result = TMSG_HEADLINE;
1122 }
1123 if(result || (!strcmp("--", *parlist)))
1124 *arg += strlen(*arg) - (parlist[1] ? strlen(parlist[1]) : 0);
1125 }
1126 //Anything found? -> skip it
1127 free_arg_lst(parlist);
1128 return result;
1129 }
1130
1131 static void do_say_internal(char *arg, int parse_flags)
1123 { 1132 {
1124 gpointer bud; 1133 gpointer bud;
1134 const char *msgtype = NULL;
1125 1135
1126 scr_set_chatmode(TRUE); 1136 scr_set_chatmode(TRUE);
1127 scr_ShowBuddyWindow(); 1137 scr_ShowBuddyWindow();
1128 1138
1129 if (!current_buddy) { 1139 if (!current_buddy) {
1138 scr_LogPrint(LPRINT_NORMAL, "This is not a user."); 1148 scr_LogPrint(LPRINT_NORMAL, "This is not a user.");
1139 return; 1149 return;
1140 } 1150 }
1141 1151
1142 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE); 1152 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
1153 if(parse_flags)
1154 msgtype = scan_mtype(&arg);
1143 arg = to_utf8(arg); 1155 arg = to_utf8(arg);
1144 send_message(arg, NULL); 1156 send_message(arg, NULL, msgtype);
1145 g_free(arg); 1157 g_free(arg);
1158 }
1159
1160 static void do_say(char *arg) {
1161 do_say_internal(arg, 1);
1146 } 1162 }
1147 1163
1148 static void do_msay(char *arg) 1164 static void do_msay(char *arg)
1149 { 1165 {
1150 /* Parameters: begin verbatim abort send send_to */ 1166 /* Parameters: begin verbatim abort send send_to */
1219 scr_ShowBuddyWindow(); 1235 scr_ShowBuddyWindow();
1220 1236
1221 if (!strcasecmp(subcmd, "send_to")) { 1237 if (!strcasecmp(subcmd, "send_to")) {
1222 int err = FALSE; 1238 int err = FALSE;
1223 gchar *msg_utf8; 1239 gchar *msg_utf8;
1240 const char *msg_type = scan_mtype(&arg);
1224 // Let's send to the specified JID. We leave now if there 1241 // Let's send to the specified JID. We leave now if there
1225 // has been an error (so we don't leave multi-line mode). 1242 // has been an error (so we don't leave multi-line mode).
1226 arg = to_utf8(arg); 1243 arg = to_utf8(arg);
1227 msg_utf8 = to_utf8(scr_get_multiline()); 1244 msg_utf8 = to_utf8(scr_get_multiline());
1228 if (msg_utf8) { 1245 if (msg_utf8) {
1229 err = send_message_to(arg, msg_utf8, scr_get_multimode_subj()); 1246 err = send_message_to(arg, msg_utf8, scr_get_multimode_subj(), msg_type);
1230 g_free(msg_utf8); 1247 g_free(msg_utf8);
1231 } 1248 }
1232 g_free(arg); 1249 g_free(arg);
1233 if (err) 1250 if (err)
1234 goto do_msay_return; 1251 goto do_msay_return;
1249 } 1266 }
1250 1267
1251 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE); 1268 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
1252 msg_utf8 = to_utf8(scr_get_multiline()); 1269 msg_utf8 = to_utf8(scr_get_multiline());
1253 if (msg_utf8) { 1270 if (msg_utf8) {
1254 send_message(msg_utf8, scr_get_multimode_subj()); 1271 send_message(msg_utf8, scr_get_multimode_subj(), scan_mtype(&arg));
1255 g_free(msg_utf8); 1272 g_free(msg_utf8);
1256 } 1273 }
1257 } 1274 }
1258 scr_set_multimode(FALSE, NULL); 1275 scr_set_multimode(FALSE, NULL);
1259 scr_LogPrint(LPRINT_NORMAL, "You have left multi-line message mode."); 1276 scr_LogPrint(LPRINT_NORMAL, "You have left multi-line message mode.");
1263 1280
1264 static void do_say_to(char *arg) 1281 static void do_say_to(char *arg)
1265 { 1282 {
1266 char **paramlst; 1283 char **paramlst;
1267 char *fjid, *msg; 1284 char *fjid, *msg;
1285 const char *msg_type = NULL;
1268 1286
1269 if (!jb_getonline()) { 1287 if (!jb_getonline()) {
1270 scr_LogPrint(LPRINT_NORMAL, "You are not connected."); 1288 scr_LogPrint(LPRINT_NORMAL, "You are not connected.");
1271 return; 1289 return;
1272 } 1290 }
1273 1291
1292 msg_type = scan_mtype(&arg);
1274 paramlst = split_arg(arg, 2, 1); // jid, message 1293 paramlst = split_arg(arg, 2, 1); // jid, message
1275 fjid = *paramlst; 1294 fjid = *paramlst;
1276 msg = *(paramlst+1); 1295 msg = *(paramlst+1);
1277 1296
1278 if (!fjid || !strcmp(fjid, ".")) { 1297 if (!fjid || !strcmp(fjid, ".")) {
1282 } 1301 }
1283 1302
1284 fjid = to_utf8(fjid); 1303 fjid = to_utf8(fjid);
1285 msg = to_utf8(msg); 1304 msg = to_utf8(msg);
1286 1305
1287 send_message_to(fjid, msg, NULL); 1306 send_message_to(fjid, msg, NULL, msg_type);
1288 1307
1289 g_free(fjid); 1308 g_free(fjid);
1290 g_free(msg); 1309 g_free(msg);
1291 free_arg_lst(paramlst); 1310 free_arg_lst(paramlst);
1292 } 1311 }
2284 } 2303 }
2285 2304
2286 arg = to_utf8(arg); 2305 arg = to_utf8(arg);
2287 // Set the topic 2306 // Set the topic
2288 msg = g_strdup_printf("%s has set the topic to: %s", mkcmdstr("me"), arg); 2307 msg = g_strdup_printf("%s has set the topic to: %s", mkcmdstr("me"), arg);
2289 jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg, NULL, NULL); 2308 jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg, NULL, NULL, NULL);
2290 g_free(arg); 2309 g_free(arg);
2291 g_free(msg); 2310 g_free(msg);
2292 } 2311 }
2293 2312
2294 static void room_destroy(gpointer bud, char *arg) 2313 static void room_destroy(gpointer bud, char *arg)