# HG changeset patch
# Parent 92fa48ef53c909928706ab4c51518953339a38e4

diff -r 92fa48ef53c9 mcabber/mcabber/commands.c
--- a/mcabber/mcabber/commands.c	Sun Jan 27 00:40:37 2013 +0200
+++ b/mcabber/mcabber/commands.c	Thu Feb 21 02:40:11 2013 +0200
@@ -2143,6 +2143,7 @@
   }
 }
 
+// /rename [-j jid] name
 static void do_rename(char *arg)
 {
   gpointer bud;
@@ -2151,9 +2152,43 @@
   char *newname, *p;
   char *name_utf8;
 
-  if (!current_buddy)
-    return;
-  bud = BUDDATA(current_buddy);
+  if (arg[0] == '-' && arg[1] == 'j' && arg[2] == ' ') {
+    GSList *found;
+    char *jid;
+    char **args = split_arg(arg, 3, 1); // option, jid, group
+    if (!args[1] || !*args[1]) {
+      scr_log_print(LPRINT_NORMAL, "Option -j requires a jid argument.");
+      free_arg_lst(args);
+      return;
+    }
+    jid = to_utf8(args[1]);
+    if (check_jid_syntax(jid)) {
+      scr_log_print(LPRINT_NORMAL, "You must specify a valid jid!");
+      g_free(jid);
+      free_arg_lst(args);
+      return;
+    }
+    found = roster_find (jid, jidsearch, ROSTER_TYPE_USER |
+                         ROSTER_TYPE_ROOM | ROSTER_TYPE_AGENT);
+    if (!found) {
+      scr_log_print(LPRINT_NORMAL, "No buddy with jid %s in roster!", jid);
+      g_free(jid);
+      free_arg_lst(args);
+      return;
+    }
+    g_free(jid);
+    bud = found->data;
+    if (args[2]) // split_arg can return NULL here
+      newname = g_strdup(args[2]);
+    else
+      newname = g_strdup("");
+    free_arg_lst(args);
+  } else {
+    if (!current_buddy)
+      return;
+    bud = BUDDATA(current_buddy);
+    newname = g_strdup(arg);
+  }
 
   bjid   = buddy_getjid(bud);
   group  = buddy_getgroupname(bud);
@@ -2162,11 +2197,13 @@
 
   if (type & ROSTER_TYPE_SPECIAL) {
     scr_LogPrint(LPRINT_NORMAL, "You can't rename this item.");
+    g_free(newname);
     return;
   }
 
-  if (!*arg && !(type & ROSTER_TYPE_GROUP)) {
+  if ((!newname || !*newname) && !(type & ROSTER_TYPE_GROUP)) {
     scr_LogPrint(LPRINT_NORMAL, "Please specify a new name.");
+    g_free(newname);
     return;
   }
 
@@ -2181,7 +2218,6 @@
   //  }
   //}
 
-  newname = g_strdup(arg);
   // Remove trailing space
   for (p = newname; *p; p++) ;
   while (p > newname && *p == ' ') *p = 0;
@@ -2221,6 +2257,7 @@
   update_roster = TRUE;
 }
 
+// /move [-j jid] groupname
 static void do_move(char *arg)
 {
   gpointer bud;
@@ -2229,9 +2266,43 @@
   char *newgroupname, *p;
   char *group_utf8;
 
-  if (!current_buddy)
-    return;
-  bud = BUDDATA(current_buddy);
+  if (arg[0] == '-' && arg[1] == 'j' && arg[2] == ' ') {
+    GSList *found;
+    char *jid;
+    char **args = split_arg(arg, 3, 1); // option, jid, group
+    if (!args[1] || !*args[1]) {
+      scr_log_print(LPRINT_NORMAL, "Option -j requires a jid argument.");
+      free_arg_lst(args);
+      return;
+    }
+    jid = to_utf8(args[1]);
+    if (check_jid_syntax(jid)) {
+      scr_log_print(LPRINT_NORMAL, "You must specify a valid jid!");
+      g_free(jid);
+      free_arg_lst(args);
+      return;
+    }
+    found = roster_find (jid, jidsearch, ROSTER_TYPE_USER |
+                         ROSTER_TYPE_ROOM | ROSTER_TYPE_AGENT);
+    if (!found) {
+      scr_log_print(LPRINT_NORMAL, "No buddy with jid %s in roster!", jid);
+      g_free(jid);
+      free_arg_lst(args);
+      return;
+    }
+    g_free(jid);
+    bud = found->data;
+    if (args[2]) // split_arg can return NULL here
+      newgroupname = g_strdup(args[2]);
+    else
+      newgroupname = g_strdup("");
+    free_arg_lst(args);
+  } else {
+    if (!current_buddy)
+      return;
+    bud = BUDDATA(current_buddy);
+    newgroupname = g_strdup(arg);
+  }
 
   bjid = buddy_getjid(bud);
   name = buddy_getname(bud);
@@ -2242,14 +2313,15 @@
 
   if (type & ROSTER_TYPE_GROUP) {
     scr_LogPrint(LPRINT_NORMAL, "You can't move groups!");
+    g_free(newgroupname);
     return;
   }
   if (type & ROSTER_TYPE_SPECIAL) {
     scr_LogPrint(LPRINT_NORMAL, "You can't move this item.");
+    g_free(newgroupname);
     return;
   }
 
-  newgroupname = g_strdup(arg);
   // Remove trailing space
   for (p = newgroupname; *p; p++) ;
   while (p > newgroupname && *p == ' ') *p-- = 0;
