[2872] in BarnOwl Developers
[PATCH] Rewrite aimwrite
daemon@ATHENA.MIT.EDU (David Benjamin)
Tue Jan 25 01:02:40 2011
From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Tue, 25 Jan 2011 01:02:27 -0500
This now makes more gratuitous use of glib and should, as a bonus, be
more sensible. In particular, we now consistently handle multi-word
recipients with and without -m. Also avoid duplicating sending code.
---
commands.c | 84 +++++++++++++++++++----------------------------------------
functions.c | 24 ++++++++--------
2 files changed, 39 insertions(+), 69 deletions(-)
diff --git a/commands.c b/commands.c
index a513f9a..1422075 100644
--- a/commands.c
+++ b/commands.c
@@ -1973,81 +1973,51 @@ char *owl_command_zwrite(int argc, const char *const *argv, const char *buff)
char *owl_command_aimwrite(int argc, const char *const *argv, const char *buff)
{
- char *newbuff, *recip;
+ char *message = NULL;
+ GString *recip = g_string_new("");
const char *const *myargv;
- int i, j, myargc;
- owl_message *m;
+ int myargc;
if (!owl_global_is_aimloggedin(&g)) {
- owl_function_makemsg("You are not logged in to AIM.");
- return(NULL);
- }
-
- if (argc < 2) {
- owl_function_makemsg("Not enough arguments to the aimwrite command.");
- return(NULL);
+ owl_function_error("You are not logged in to AIM.");
+ goto err;
}
- myargv=argv;
- if (argc<0) {
- owl_function_error("Unbalanced quotes in aimwrite");
- return(NULL);
- }
- myargc=argc;
- if (myargc && *(myargv[0])!='-') {
- myargc--;
- myargv++;
- }
+ /* Skip argv[0]. */
+ myargv = argv+1;
+ myargc = argc-1;
while (myargc) {
if (!strcmp(myargv[0], "-m")) {
- if (myargc<2) {
- break;
+ if (myargc <= 1) {
+ owl_function_error("No message specified.");
+ goto err;
}
-
/* Once we have -m, gobble up everything else on the line */
myargv++;
myargc--;
- newbuff=owl_strdup("");
- while (myargc) {
- newbuff=owl_realloc(newbuff, strlen(newbuff)+strlen(myargv[0])+5);
- strcat(newbuff, myargv[0]);
- strcat(newbuff, " ");
- myargc--;
- myargv++;
- }
- if (strlen(newbuff) >= 1)
- newbuff[strlen(newbuff) - 1] = '\0'; /* remove last space */
-
- recip=owl_strdup(argv[1]);
- owl_aim_send_im(recip, newbuff);
- m=owl_function_make_outgoing_aim(newbuff, recip);
- if (m) {
- owl_global_messagequeue_addmsg(&g, m);
- } else {
- owl_function_error("Could not create outgoing AIM message");
- }
-
- owl_free(recip);
- owl_free(newbuff);
- return(NULL);
+ message = g_strjoinv(" ", (char**)myargv);
+ break;
} else {
- /* we don't care */
+ /* squish arguments together to make one screenname w/o spaces for now */
+ g_string_append(recip, myargv[0]);
myargv++;
myargc--;
}
}
- /* squish arguments together to make one screenname w/o spaces for now */
- newbuff=owl_malloc(strlen(buff)+5);
- sprintf(newbuff, "%s ", argv[0]);
- j=argc-1;
- for (i=0; i<j; i++) {
- strcat(newbuff, argv[i+1]);
+ if (recip->str[0] == '\0') {
+ owl_function_error("No recipient specified");
+ goto err;
}
-
- owl_function_aimwrite_setup(newbuff);
- owl_free(newbuff);
- return(NULL);
+
+ if (message != NULL)
+ owl_function_aimwrite(recip->str, message, false);
+ else
+ owl_function_aimwrite_setup(recip->str);
+ err:
+ g_string_free(recip, true);
+ owl_free(message);
+ return NULL;
}
char *owl_command_loopwrite(int argc, const char *const *argv, const char *buff)
diff --git a/functions.c b/functions.c
index 1edae0e..f059dbf 100644
--- a/functions.c
+++ b/functions.c
@@ -349,14 +349,17 @@ void owl_function_zwrite_setup(owl_zwrite *z)
z, (void(*)(void*))owl_zwrite_delete);
}
-void owl_function_aimwrite_setup(const char *line)
+void owl_function_aimwrite_setup(const char *to)
{
+ /* TODO: We probably actually want an owl_aimwrite object like
+ * owl_zwrite. */
+ char *line = owl_sprintf("aimwrite %s", to);
owl_function_write_setup("message");
owl_function_start_edit_win(line,
&owl_callback_aimwrite,
- owl_strdup(line),
+ owl_strdup(to),
owl_free);
-
+ owl_free(line);
}
void owl_function_loopwrite_setup(void)
@@ -476,23 +479,20 @@ void owl_function_zcrypt(owl_zwrite *z, const char *msg)
}
void owl_callback_aimwrite(owl_editwin *e) {
- char *command = owl_editwin_get_cbdata(e);
- owl_function_aimwrite(command,
- owl_editwin_get_text(e));
+ char *to = owl_editwin_get_cbdata(e);
+ owl_function_aimwrite(to, owl_editwin_get_text(e), true);
}
-void owl_function_aimwrite(const char *line, const char *msg)
+void owl_function_aimwrite(const char *to, const char *msg, bool unwrap)
{
int ret;
- const char *to;
char *format_msg;
owl_message *m;
- to = line + 9;
-
/* make a formatted copy of the message */
- format_msg=owl_strdup(msg);
- owl_text_wordunwrap(format_msg);
+ format_msg = owl_strdup(msg);
+ if (unwrap)
+ owl_text_wordunwrap(format_msg);
/* send the message */
ret=owl_aim_send_im(to, format_msg);
--
1.7.3.3.399.g06ddf