comparison mcabber/src/commands.c @ 552:ba5271b49f21

Split do_room()
author Mikael Berthe <mikael@lilotux.net>
date Tue, 29 Nov 2005 21:58:53 +0100
parents 448e299e45da
children 2424bbf0a6db
comparison
equal deleted inserted replaced
551:c71699efa5cc 552:ba5271b49f21
875 875
876 g_free(buffer); 876 g_free(buffer);
877 } 877 }
878 878
879 // room_names() is a variation of do_info(), for chatrooms only 879 // room_names() is a variation of do_info(), for chatrooms only
880 static void room_names(void) 880 static void room_names(gpointer bud, char *arg)
881 { 881 {
882 gpointer bud;
883 const char *jid; 882 const char *jid;
884 char *buffer; 883 char *buffer;
885 GSList *resources; 884 GSList *resources;
886 885
887 if (!current_buddy) return; 886 if (*arg) {
888 bud = BUDDATA(current_buddy); 887 scr_LogPrint(LPRINT_NORMAL, "Unknown parameter");
889
890 if (buddy_gettype(bud) != ROSTER_TYPE_ROOM) {
891 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
892 return; 888 return;
893 } 889 }
894 890
895 jid = buddy_getjid(bud); 891 jid = buddy_getjid(bud);
896 892
1107 scr_LogPrint(LPRINT_NORMAL, "Please read the manual page" 1103 scr_LogPrint(LPRINT_NORMAL, "Please read the manual page"
1108 " before using /rawxml :-)"); 1104 " before using /rawxml :-)");
1109 } 1105 }
1110 } 1106 }
1111 1107
1112 static void do_room(char *arg) 1108 // skip_space_after_command(arg, param_needed, buddy_must_be_a_room)
1113 { 1109 // - Check if this is a room, if buddy_must_be_a_room is not null
1114 gpointer bud; 1110 // - Check there is at least 1 parameter, if param_needed is true
1115 1111 // - Return null if one of the checks fails, or a pointer to the first
1116 if (!jb_getonline()) { 1112 // non-space character.
1117 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); 1113 static char *skip_space_after_command(char *arg, bool param_needed,
1118 return; 1114 gpointer buddy_must_be_a_room)
1119 } 1115 {
1120 1116 if (buddy_must_be_a_room &&
1121 if (!arg || (!*arg)) { 1117 !(buddy_gettype(buddy_must_be_a_room) & ROSTER_TYPE_ROOM)) {
1122 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); 1118 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
1123 return; 1119 return NULL;
1124 } 1120 }
1125 1121
1126 if (!current_buddy) return; 1122 if (param_needed) {
1127 bud = BUDDATA(current_buddy); 1123 // A parameter is given if the first char is a space char
1128 1124 // (trailing space has been stripped previously)
1129 if (!strncasecmp(arg, "join", 4)) {
1130 char *roomname, *nick;
1131
1132 arg += 4;
1133 if (*arg++ != ' ') { 1125 if (*arg++ != ' ') {
1134 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter"); 1126 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter");
1135 return; 1127 return NULL;
1136 } 1128 }
1137 for (; *arg && *arg == ' '; arg++) 1129 }
1138 ; 1130
1139 1131 // Skip leading space
1140 if (strchr(arg, '/')) { 1132 for (; *arg && *arg == ' '; arg++)
1141 scr_LogPrint(LPRINT_NORMAL, "Invalid room name"); 1133 ;
1142 return; 1134 return arg;
1143 } 1135 }
1144 1136
1145 roomname = g_strdup(arg); 1137 static void room_join(gpointer bud, char *arg)
1146 nick = strchr(roomname, ' '); 1138 {
1147 if (!nick) { 1139 char *roomname, *nick;
1148 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (nickname)"); 1140
1149 g_free(roomname); 1141 if (strchr(arg, '/')) {
1150 return; 1142 scr_LogPrint(LPRINT_NORMAL, "Invalid room name");
1151 } 1143 return;
1152 1144 }
1153 *nick++ = 0; 1145
1154 while (*nick && *nick == ' ') 1146 roomname = g_strdup(arg);
1155 nick++; 1147 nick = strchr(roomname, ' ');
1156 if (!*nick) { 1148 if (!nick) {
1157 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (nickname)"); 1149 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (nickname)");
1158 g_free(roomname);
1159 return;
1160 }
1161
1162 mc_strtolower(roomname);
1163 jb_room_join(roomname, nick);
1164
1165 g_free(roomname); 1150 g_free(roomname);
1166 buddylist_build(); 1151 return;
1167 update_roster = TRUE; 1152 }
1168 } else if (!strncasecmp(arg, "invite", 6)) { 1153
1169 const gchar *roomname; 1154 *nick++ = 0;
1170 gchar* jid; 1155 while (*nick && *nick == ' ')
1171 arg += 6; 1156 nick++;
1172 if (*arg++ != ' ') { 1157 if (!*nick) {
1173 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter"); 1158 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (nickname)");
1174 return; 1159 g_free(roomname);
1175 } 1160 return;
1176 for (; *arg && *arg == ' '; arg++) 1161 }
1177 ; 1162
1178 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) { 1163 mc_strtolower(roomname);
1179 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom"); 1164 jb_room_join(roomname, nick);
1180 return; 1165
1181 } 1166 g_free(roomname);
1182 if (!*arg) { 1167 buddylist_build();
1183 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); 1168 update_roster = TRUE;
1184 return; 1169 }
1185 } 1170
1186 jid = g_strdup(arg); 1171 static void room_invite(gpointer bud, char *arg)
1187 arg = strchr(jid, ' '); 1172 {
1188 if (arg) { 1173 const gchar *roomname;
1189 *arg++ = 0; 1174 gchar* jid;
1190 for (; *arg && *arg == ' '; arg++) 1175
1191 ; 1176 if (!*arg) {
1192 if (!*arg) arg = NULL; 1177 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1193 } 1178 return;
1194 roomname = buddy_getjid(bud); 1179 }
1195 jb_room_invite(roomname, jid, arg); 1180 jid = g_strdup(arg);
1196 scr_LogPrint(LPRINT_LOGNORM, "Invitation sent to <%s>", jid); 1181 arg = strchr(jid, ' ');
1197 g_free(jid); 1182 if (arg) {
1198 } else if (!strncasecmp(arg, "leave", 5)) {
1199 gchar *roomid, *utf8_nickname;
1200 arg += 5;
1201 for (; *arg && *arg == ' '; arg++)
1202 ;
1203 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) {
1204 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
1205 return;
1206 }
1207 utf8_nickname = to_utf8(buddy_getnickname(bud));
1208 roomid = g_strdup_printf("%s/%s", buddy_getjid(bud), utf8_nickname);
1209 jb_setstatus(offline, roomid, arg);
1210 g_free(utf8_nickname);
1211 g_free(roomid);
1212 buddy_setnickname(bud, NULL);
1213 buddy_del_all_resources(bud);
1214 scr_LogPrint(LPRINT_LOGNORM, "You have left %s", buddy_getjid(bud));
1215 } else if (!strcasecmp(arg, "names")) {
1216 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) {
1217 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
1218 return;
1219 }
1220 room_names();
1221 } else if (!strncasecmp(arg, "nick", 4)) {
1222 gchar *cmd;
1223 arg += 4;
1224 if (*arg++ != ' ') {
1225 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter");
1226 return;
1227 }
1228 for (; *arg && *arg == ' '; arg++)
1229 ;
1230 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) {
1231 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
1232 return;
1233 }
1234 cmd = g_strdup_printf("join %s %s", buddy_getjid(bud), arg);
1235 do_room(cmd);
1236 g_free(cmd);
1237 } else if (!strncasecmp(arg, "privmsg", 7)) {
1238 gchar *nick, *cmd;
1239 arg += 7;
1240 if (*arg++ != ' ') {
1241 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter");
1242 return;
1243 }
1244 for (; *arg && *arg == ' '; arg++)
1245 ;
1246 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) {
1247 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
1248 return;
1249 }
1250 if (!*arg) {
1251 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1252 return;
1253 }
1254 nick = g_strdup(arg);
1255 arg = strchr(nick, ' ');
1256 if (!arg) {
1257 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1258 return;
1259 }
1260 *arg++ = 0; 1183 *arg++ = 0;
1261 for (; *arg && *arg == ' '; arg++) 1184 for (; *arg && *arg == ' '; arg++)
1262 ; 1185 ;
1263 cmd = g_strdup_printf("%s/%s %s", buddy_getjid(bud), nick, arg); 1186 if (!*arg) arg = NULL;
1264 do_say_to(cmd); 1187 }
1265 g_free(cmd); 1188 roomname = buddy_getjid(bud);
1266 g_free(nick); 1189 jb_room_invite(roomname, jid, arg);
1190 scr_LogPrint(LPRINT_LOGNORM, "Invitation sent to <%s>", jid);
1191 g_free(jid);
1192 }
1193
1194 static void room_leave(gpointer bud, char *arg)
1195 {
1196 gchar *roomid, *utf8_nickname;
1197
1198 utf8_nickname = to_utf8(buddy_getnickname(bud));
1199 roomid = g_strdup_printf("%s/%s", buddy_getjid(bud), utf8_nickname);
1200 jb_setstatus(offline, roomid, arg);
1201 g_free(utf8_nickname);
1202 g_free(roomid);
1203 buddy_setnickname(bud, NULL);
1204 buddy_del_all_resources(bud);
1205 scr_LogPrint(LPRINT_LOGNORM, "You have left %s", buddy_getjid(bud));
1206 }
1207
1208 static void room_nick(gpointer bud, char *arg)
1209 {
1210 gchar *cmd;
1211
1212 cmd = g_strdup_printf("join %s %s", buddy_getjid(bud), arg);
1213 do_room(cmd);
1214 g_free(cmd);
1215 }
1216
1217 static void room_privmsg(gpointer bud, char *arg)
1218 {
1219 gchar *nick, *cmd;
1220
1221 if (!*arg) {
1222 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1223 return;
1224 }
1225 nick = g_strdup(arg);
1226 arg = strchr(nick, ' ');
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);
1235 do_say_to(cmd);
1236 g_free(cmd);
1237 g_free(nick);
1238 }
1239
1240 static void room_remove(gpointer bud, char *arg)
1241 {
1242 if (*arg) {
1243 scr_LogPrint(LPRINT_NORMAL, "Unknown parameter");
1244 return;
1245 }
1246
1247 // Quick check: if there are resources, we haven't left
1248 if (buddy_isresource(bud)) {
1249 scr_LogPrint(LPRINT_NORMAL, "You haven't left this room!");
1250 return;
1251 }
1252 // Delete the room
1253 roster_del_user(buddy_getjid(bud));
1254 buddylist_build();
1255 update_roster = TRUE;
1256 }
1257
1258 static void room_topic(gpointer bud, char *arg)
1259 {
1260 gchar *msg;
1261
1262 // If no parameter is given, display the current topic
1263 if (!*arg) {
1264 const char *topic = buddy_gettopic(bud);
1265 if (topic)
1266 scr_LogPrint(LPRINT_NORMAL, "Topic: %s", topic);
1267 else
1268 scr_LogPrint(LPRINT_NORMAL, "No topic has been set");
1269 return;
1270 }
1271
1272 // Set the topic
1273 msg = g_strdup_printf("/me has set the topic to: %s", arg);
1274 jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg);
1275 g_free(msg);
1276 }
1277
1278 static void room_unlock(gpointer bud, char *arg)
1279 {
1280 if (*arg) {
1281 scr_LogPrint(LPRINT_NORMAL, "Unknown parameter");
1282 return;
1283 }
1284
1285 jb_room_unlock(buddy_getjid(bud));
1286 }
1287
1288 static void do_room(char *arg)
1289 {
1290 gpointer bud;
1291
1292 if (!jb_getonline()) {
1293 scr_LogPrint(LPRINT_NORMAL, "You are not connected");
1294 return;
1295 }
1296
1297 if (!arg || (!*arg)) {
1298 scr_LogPrint(LPRINT_NORMAL, "Missing parameter");
1299 return;
1300 }
1301
1302 if (!current_buddy) return;
1303 bud = BUDDATA(current_buddy);
1304
1305 if (!strncasecmp(arg, "join", 4)) {
1306 if ((arg = skip_space_after_command(arg+4, TRUE, NULL)) != NULL)
1307 room_join(bud, arg);
1308 } else if (!strncasecmp(arg, "invite", 6)) {
1309 if ((arg = skip_space_after_command(arg+6, TRUE, bud)) != NULL)
1310 room_invite(bud, arg);
1311 } else if (!strncasecmp(arg, "leave", 5)) {
1312 if ((arg = skip_space_after_command(arg+5, FALSE, bud)) != NULL)
1313 room_leave(bud, arg);
1314 } else if (!strcasecmp(arg, "names")) {
1315 if ((arg = skip_space_after_command(arg+5, FALSE, bud)) != NULL)
1316 room_names(bud, arg);
1317 } else if (!strncasecmp(arg, "nick", 4)) {
1318 if ((arg = skip_space_after_command(arg+4, TRUE, bud)) != NULL)
1319 room_nick(bud, arg);
1320 } else if (!strncasecmp(arg, "privmsg", 7)) {
1321 if ((arg = skip_space_after_command(arg+7, TRUE, bud)) != NULL)
1322 room_privmsg(bud, arg);
1267 } else if (!strcasecmp(arg, "remove")) { 1323 } else if (!strcasecmp(arg, "remove")) {
1268 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) { 1324 if ((arg = skip_space_after_command(arg+6, FALSE, bud)) != NULL)
1269 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom"); 1325 room_remove(bud, arg);
1270 return;
1271 }
1272 // Quick check: if there are resources, we haven't left
1273 if (buddy_isresource(bud)) {
1274 scr_LogPrint(LPRINT_NORMAL, "You haven't left this room!");
1275 return;
1276 }
1277 // Delete the room
1278 roster_del_user(buddy_getjid(bud));
1279 buddylist_build();
1280 update_roster = TRUE;
1281 } else if (!strcasecmp(arg, "unlock")) { 1326 } else if (!strcasecmp(arg, "unlock")) {
1282 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) { 1327 if ((arg = skip_space_after_command(arg+6, FALSE, bud)) != NULL)
1283 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom"); 1328 room_unlock(bud, arg);
1284 return;
1285 }
1286 jb_room_unlock(buddy_getjid(bud));
1287 } else if (!strncasecmp(arg, "topic", 5)) { 1329 } else if (!strncasecmp(arg, "topic", 5)) {
1288 gchar *msg; 1330 if ((arg = skip_space_after_command(arg+5, FALSE, bud)) != NULL)
1289 arg += 5; 1331 room_topic(bud, arg);
1290 for (; *arg && *arg == ' '; arg++)
1291 ;
1292 if (!*arg) {
1293 const char *topic = buddy_gettopic(bud);
1294 if (topic)
1295 scr_LogPrint(LPRINT_NORMAL, "Topic: %s", topic);
1296 else
1297 scr_LogPrint(LPRINT_NORMAL, "No topic has been set");
1298 return;
1299 }
1300 for (; *arg && *arg == ' '; arg++)
1301 ;
1302 if (!(buddy_gettype(bud) & ROSTER_TYPE_ROOM)) {
1303 scr_LogPrint(LPRINT_NORMAL, "This isn't a chatroom");
1304 return;
1305 }
1306 msg = g_strdup_printf("/me has set the topic to: %s", arg);
1307 jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg);
1308 g_free(msg);
1309 } else { 1332 } else {
1310 scr_LogPrint(LPRINT_NORMAL, "Unrecognized parameter!"); 1333 scr_LogPrint(LPRINT_NORMAL, "Unrecognized parameter!");
1311 } 1334 }
1312 } 1335 }
1313 1336