comparison mcabber/src/commands.c @ 554:2424bbf0a6db

Some more work on do_room()
author Mikael Berthe <mikael@lilotux.net>
date Tue, 29 Nov 2005 23:25:01 +0100
parents ba5271b49f21
children 11ac80e41c7d
comparison
equal deleted inserted replaced
553:b1d71f5107a1 554:2424bbf0a6db
1103 scr_LogPrint(LPRINT_NORMAL, "Please read the manual page" 1103 scr_LogPrint(LPRINT_NORMAL, "Please read the manual page"
1104 " before using /rawxml :-)"); 1104 " before using /rawxml :-)");
1105 } 1105 }
1106 } 1106 }
1107 1107
1108 // skip_space_after_command(arg, param_needed, buddy_must_be_a_room) 1108 // check_room_subcommand(arg, param_needed, buddy_must_be_a_room)
1109 // - Check if this is a room, if buddy_must_be_a_room is not null 1109 // - Check if this is a room, if buddy_must_be_a_room is not null
1110 // - Check there is at least 1 parameter, if param_needed is true 1110 // - Check there is at least 1 parameter, if param_needed is true
1111 // - Return null if one of the checks fails, or a pointer to the first 1111 // - Return null if one of the checks fails, or a pointer to the first
1112 // non-space character. 1112 // non-space character.
1113 static char *skip_space_after_command(char *arg, bool param_needed, 1113 static char *check_room_subcommand(char *arg, bool param_needed,
1114 gpointer buddy_must_be_a_room) 1114 gpointer buddy_must_be_a_room)
1115 { 1115 {
1116 if (buddy_must_be_a_room && 1116 if (buddy_must_be_a_room &&
1117 !(buddy_gettype(buddy_must_be_a_room) & ROSTER_TYPE_ROOM)) { 1117 !(buddy_gettype(buddy_must_be_a_room) & ROSTER_TYPE_ROOM)) {
1118 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom"); 1118 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
1119 return NULL; 1119 return NULL;
1120 } 1120 }
1121 1121
1122 if (param_needed) { 1122 if (param_needed) {
1123 // A parameter is given if the first char is a space char 1123 if (!arg) {
1124 // (trailing space has been stripped previously)
1125 if (*arg++ != ' ') {
1126 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter"); 1124 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter");
1127 return NULL; 1125 return NULL;
1128 } 1126 }
1129 } 1127 }
1130 1128
1131 // Skip leading space 1129 if (arg)
1132 for (; *arg && *arg == ' '; arg++) 1130 return arg;
1133 ; 1131 else
1134 return arg; 1132 return "";
1135 } 1133 }
1136 1134
1137 static void room_join(gpointer bud, char *arg) 1135 static void room_join(gpointer bud, char *arg)
1138 { 1136 {
1137 char **paramlst;
1139 char *roomname, *nick; 1138 char *roomname, *nick;
1140 1139
1141 if (strchr(arg, '/')) { 1140 paramlst = split_arg(arg, 2, 0); // roomid, nickname
1141 roomname = *paramlst;
1142 nick = *(paramlst+1);
1143
1144
1145 if (!roomname || strchr(roomname, '/')) {
1142 scr_LogPrint(LPRINT_NORMAL, "Invalid room name"); 1146 scr_LogPrint(LPRINT_NORMAL, "Invalid room name");
1143 return; 1147 free_arg_lst(paramlst);
1144 } 1148 return;
1145 1149 }
1146 roomname = g_strdup(arg); 1150
1147 nick = strchr(roomname, ' ');
1148 if (!nick) { 1151 if (!nick) {
1149 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (nickname)"); 1152 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (nickname)");
1150 g_free(roomname); 1153 free_arg_lst(paramlst);
1151 return; 1154 return;
1152 } 1155 }
1153 1156
1154 *nick++ = 0; 1157 // Note that roomname is part of the array allocated by split_arg(),
1155 while (*nick && *nick == ' ') 1158 // so we can modify it.
1156 nick++;
1157 if (!*nick) {
1158 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (nickname)");
1159 g_free(roomname);
1160 return;
1161 }
1162
1163 mc_strtolower(roomname); 1159 mc_strtolower(roomname);
1164 jb_room_join(roomname, nick); 1160 jb_room_join(roomname, nick);
1165 1161
1166 g_free(roomname);
1167 buddylist_build(); 1162 buddylist_build();
1168 update_roster = TRUE; 1163 update_roster = TRUE;
1164 free_arg_lst(paramlst);
1169 } 1165 }
1170 1166
1171 static void room_invite(gpointer bud, char *arg) 1167 static void room_invite(gpointer bud, char *arg)
1172 { 1168 {
1169 char **paramlst;
1173 const gchar *roomname; 1170 const gchar *roomname;
1174 gchar* jid; 1171 char* jid;
1175 1172
1176 if (!*arg) { 1173 paramlst = split_arg(arg, 2, 0); // jid, [reason]
1177 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); 1174 jid = *paramlst;
1178 return; 1175 arg = *(paramlst+1);
1179 } 1176 // An empty reason is no reason...
1180 jid = g_strdup(arg); 1177 if (arg && !*arg)
1181 arg = strchr(jid, ' '); 1178 arg = NULL;
1182 if (arg) { 1179
1183 *arg++ = 0; 1180 if (!jid || !*jid) {
1184 for (; *arg && *arg == ' '; arg++) 1181 scr_LogPrint(LPRINT_NORMAL, "Missing or incorrect parameter");
1185 ; 1182 free_arg_lst(paramlst);
1186 if (!*arg) arg = NULL; 1183 return;
1187 } 1184 }
1185
1188 roomname = buddy_getjid(bud); 1186 roomname = buddy_getjid(bud);
1189 jb_room_invite(roomname, jid, arg); 1187 jb_room_invite(roomname, jid, arg);
1190 scr_LogPrint(LPRINT_LOGNORM, "Invitation sent to <%s>", jid); 1188 scr_LogPrint(LPRINT_LOGNORM, "Invitation sent to <%s>", jid);
1191 g_free(jid); 1189 free_arg_lst(paramlst);
1192 } 1190 }
1193 1191
1194 static void room_leave(gpointer bud, char *arg) 1192 static void room_leave(gpointer bud, char *arg)
1195 { 1193 {
1196 gchar *roomid, *utf8_nickname; 1194 gchar *roomid, *utf8_nickname;
1197 1195
1196 strip_arg_special_chars(arg);
1198 utf8_nickname = to_utf8(buddy_getnickname(bud)); 1197 utf8_nickname = to_utf8(buddy_getnickname(bud));
1199 roomid = g_strdup_printf("%s/%s", buddy_getjid(bud), utf8_nickname); 1198 roomid = g_strdup_printf("%s/%s", buddy_getjid(bud), utf8_nickname);
1200 jb_setstatus(offline, roomid, arg); 1199 jb_setstatus(offline, roomid, arg);
1201 g_free(utf8_nickname); 1200 g_free(utf8_nickname);
1202 g_free(roomid); 1201 g_free(roomid);
1207 1206
1208 static void room_nick(gpointer bud, char *arg) 1207 static void room_nick(gpointer bud, char *arg)
1209 { 1208 {
1210 gchar *cmd; 1209 gchar *cmd;
1211 1210
1211 strip_arg_special_chars(arg);
1212 cmd = g_strdup_printf("join %s %s", buddy_getjid(bud), arg); 1212 cmd = g_strdup_printf("join %s %s", buddy_getjid(bud), arg);
1213 do_room(cmd); 1213 do_room(cmd);
1214 g_free(cmd); 1214 g_free(cmd);
1215 } 1215 }
1216 1216
1217 static void room_privmsg(gpointer bud, char *arg) 1217 static void room_privmsg(gpointer bud, char *arg)
1218 { 1218 {
1219 char **paramlst;
1219 gchar *nick, *cmd; 1220 gchar *nick, *cmd;
1220 1221
1221 if (!*arg) { 1222 paramlst = split_arg(arg, 2, 0); // nickname, message
1223 nick = *paramlst;
1224 arg = *(paramlst+1);
1225
1226 if (!nick || !*nick || !arg || !*arg) {
1222 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); 1227 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1223 return; 1228 free_arg_lst(paramlst);
1224 } 1229 return;
1225 nick = g_strdup(arg); 1230 }
1226 arg = strchr(nick, ' '); 1231
1227 if (!arg) {
1228 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1229 return;
1230 }
1231 *arg++ = 0;
1232 for (; *arg && *arg == ' '; arg++)
1233 ;
1234 cmd = g_strdup_printf("%s/%s %s", buddy_getjid(bud), nick, arg); 1232 cmd = g_strdup_printf("%s/%s %s", buddy_getjid(bud), nick, arg);
1235 do_say_to(cmd); 1233 do_say_to(cmd);
1236 g_free(cmd); 1234 g_free(cmd);
1237 g_free(nick); 1235 free_arg_lst(paramlst);
1238 } 1236 }
1239 1237
1240 static void room_remove(gpointer bud, char *arg) 1238 static void room_remove(gpointer bud, char *arg)
1241 { 1239 {
1242 if (*arg) { 1240 if (*arg) {
1268 scr_LogPrint(LPRINT_NORMAL, "No topic has been set"); 1266 scr_LogPrint(LPRINT_NORMAL, "No topic has been set");
1269 return; 1267 return;
1270 } 1268 }
1271 1269
1272 // Set the topic 1270 // Set the topic
1271 strip_arg_special_chars(arg);
1273 msg = g_strdup_printf("/me has set the topic to: %s", arg); 1272 msg = g_strdup_printf("/me has set the topic to: %s", arg);
1274 jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg); 1273 jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg);
1275 g_free(msg); 1274 g_free(msg);
1276 } 1275 }
1277 1276
1285 jb_room_unlock(buddy_getjid(bud)); 1284 jb_room_unlock(buddy_getjid(bud));
1286 } 1285 }
1287 1286
1288 static void do_room(char *arg) 1287 static void do_room(char *arg)
1289 { 1288 {
1289 char **paramlst;
1290 char *subcmd;
1290 gpointer bud; 1291 gpointer bud;
1291 1292
1292 if (!jb_getonline()) { 1293 if (!jb_getonline()) {
1293 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); 1294 scr_LogPrint(LPRINT_NORMAL, "You are not connected");
1294 return; 1295 return;
1295 } 1296 }
1296 1297
1297 if (!arg || (!*arg)) {
1298 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1299 return;
1300 }
1301
1302 if (!current_buddy) return; 1298 if (!current_buddy) return;
1303 bud = BUDDATA(current_buddy); 1299 bud = BUDDATA(current_buddy);
1304 1300
1305 if (!strncasecmp(arg, "join", 4)) { 1301 paramlst = split_arg(arg, 2, 1); // subcmd, arg
1306 if ((arg = skip_space_after_command(arg+4, TRUE, NULL)) != NULL) 1302 subcmd = *paramlst;
1303 arg = *(paramlst+1);
1304
1305 if (!subcmd || !*subcmd) {
1306 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1307 free_arg_lst(paramlst);
1308 return;
1309 }
1310
1311 if (!strcasecmp(subcmd, "join")) {
1312 if ((arg = check_room_subcommand(arg, TRUE, NULL)) != NULL)
1307 room_join(bud, arg); 1313 room_join(bud, arg);
1308 } else if (!strncasecmp(arg, "invite", 6)) { 1314 } else if (!strcasecmp(subcmd, "invite")) {
1309 if ((arg = skip_space_after_command(arg+6, TRUE, bud)) != NULL) 1315 if ((arg = check_room_subcommand(arg, TRUE, bud)) != NULL)
1310 room_invite(bud, arg); 1316 room_invite(bud, arg);
1311 } else if (!strncasecmp(arg, "leave", 5)) { 1317 } else if (!strcasecmp(subcmd, "leave")) {
1312 if ((arg = skip_space_after_command(arg+5, FALSE, bud)) != NULL) 1318 if ((arg = check_room_subcommand(arg, FALSE, bud)) != NULL)
1313 room_leave(bud, arg); 1319 room_leave(bud, arg);
1314 } else if (!strcasecmp(arg, "names")) { 1320 } else if (!strcasecmp(subcmd, "names")) {
1315 if ((arg = skip_space_after_command(arg+5, FALSE, bud)) != NULL) 1321 if ((arg = check_room_subcommand(arg, FALSE, bud)) != NULL)
1316 room_names(bud, arg); 1322 room_names(bud, arg);
1317 } else if (!strncasecmp(arg, "nick", 4)) { 1323 } else if (!strcasecmp(subcmd, "nick")) {
1318 if ((arg = skip_space_after_command(arg+4, TRUE, bud)) != NULL) 1324 if ((arg = check_room_subcommand(arg, TRUE, bud)) != NULL)
1319 room_nick(bud, arg); 1325 room_nick(bud, arg);
1320 } else if (!strncasecmp(arg, "privmsg", 7)) { 1326 } else if (!strcasecmp(subcmd, "privmsg")) {
1321 if ((arg = skip_space_after_command(arg+7, TRUE, bud)) != NULL) 1327 if ((arg = check_room_subcommand(arg, TRUE, bud)) != NULL)
1322 room_privmsg(bud, arg); 1328 room_privmsg(bud, arg);
1323 } else if (!strcasecmp(arg, "remove")) { 1329 } else if (!strcasecmp(subcmd, "remove")) {
1324 if ((arg = skip_space_after_command(arg+6, FALSE, bud)) != NULL) 1330 if ((arg = check_room_subcommand(arg, FALSE, bud)) != NULL)
1325 room_remove(bud, arg); 1331 room_remove(bud, arg);
1326 } else if (!strcasecmp(arg, "unlock")) { 1332 } else if (!strcasecmp(subcmd, "unlock")) {
1327 if ((arg = skip_space_after_command(arg+6, FALSE, bud)) != NULL) 1333 if ((arg = check_room_subcommand(arg, FALSE, bud)) != NULL)
1328 room_unlock(bud, arg); 1334 room_unlock(bud, arg);
1329 } else if (!strncasecmp(arg, "topic", 5)) { 1335 } else if (!strcasecmp(subcmd, "topic")) {
1330 if ((arg = skip_space_after_command(arg+5, FALSE, bud)) != NULL) 1336 if ((arg = check_room_subcommand(arg, FALSE, bud)) != NULL)
1331 room_topic(bud, arg); 1337 room_topic(bud, arg);
1332 } else { 1338 } else {
1333 scr_LogPrint(LPRINT_NORMAL, "Unrecognized parameter!"); 1339 scr_LogPrint(LPRINT_NORMAL, "Unrecognized parameter!");
1334 } 1340 }
1341
1342 free_arg_lst(paramlst);
1335 } 1343 }
1336 1344
1337 static void do_connect(char *arg) 1345 static void do_connect(char *arg)
1338 { 1346 {
1339 mcabber_connect(); 1347 mcabber_connect();