2 * Send commands to Purple via ~/.purple/control
4 * Originally by Eric Warmenhoven <eric@warmenhoven.org>
5 * Compile fixes/mini hacks Alex Bennee <alex@bennee.com>
6 * and Brian Tarricone <bjt23@users.sourceforge.net>
13 #include <sys/types.h>
21 #include "conversation.h"
23 #include "eventloop.h"
28 #define FILECTL_PLUGIN_ID "core-filectl"
32 static void init_file(void);
33 static gboolean
check_file(void);
35 /* parse char * as if were word array */
36 char *getarg(char *, int, int);
38 /* go through file and run any commands */
45 char *command
, *arg1
, *arg2
;
48 sprintf(filename
, "%s" G_DIR_SEPARATOR_S
"control", purple_user_dir());
50 file
= g_fopen(filename
, "r+");
51 while (fgets(buffer
, sizeof(buffer
), file
)) {
53 /* Read the next command */
54 if (buffer
[strlen(buffer
) - 1] == '\n')
55 buffer
[strlen(buffer
) - 1] = 0;
56 purple_debug_misc("filectl", "read: %s\n", buffer
);
57 command
= getarg(buffer
, 0, 0);
59 if (!g_ascii_strncasecmp(command
, "login", 6)) {
60 PurpleAccount
*account
;
62 arg1
= getarg(buffer
, 1, 0);
63 arg2
= getarg(buffer
, 2, 1);
65 account
= purple_accounts_find(arg1
, arg2
);
66 if (account
!= NULL
) /* username found */
67 purple_account_connect(account
);
72 } else if (!g_ascii_strncasecmp(command
, "logout", 7)) {
73 PurpleAccount
*account
;
75 arg1
= getarg(buffer
, 1, 1);
76 arg2
= getarg(buffer
, 2, 1);
78 account
= purple_accounts_find(arg1
, arg2
);
81 purple_account_disconnect(account
);
83 else if (arg1
== NULL
)
84 purple_connections_disconnect_all();
89 /* purple_find_conversation() is gone in 2.0.0. */
91 } else if (!g_ascii_strncasecmp(command
, "send", 4)) {
92 PurpleConversation
*conv
;
94 arg1
= getarg(buffer
, 1, 0);
95 arg2
= getarg(buffer
, 2, 1);
97 conv
= purple_find_conversation(PURPLE_CONV_TYPE_ANY
, arg1
);
101 purple_conversation_write(conv, arg2, WFLAG_SEND, NULL, time(NULL), -1);
102 serv_send_im(conv->gc, arg1, arg2, 0);
110 } else if (!g_ascii_strncasecmp(command
, "away", 4)) {
111 arg1
= getarg(buffer
, 1, 1);
112 /* serv_set_away_all(arg1); */
115 } else if (!g_ascii_strncasecmp(command
, "hide", 4)) {
116 purple_blist_set_visible(FALSE
);
118 } else if (!g_ascii_strncasecmp(command
, "unhide", 6)) {
119 purple_blist_set_visible(TRUE
);
121 } else if (!g_ascii_strncasecmp(command
, "back", 4)) {
124 } else if (!g_ascii_strncasecmp(command
, "quit", 4)) {
134 if (g_stat(filename
, &finfo
) != 0)
136 mtime
= finfo
.st_mtime
;
140 * Check to see if the size of the file is > 0. if so, run commands.
145 /* most of this was taken from Bash v2.04 by the FSF */
149 sprintf(filename
, "%s" G_DIR_SEPARATOR_S
"control", purple_user_dir());
151 if ((g_stat(filename
, &finfo
) == 0) && (finfo
.st_size
> 0))
156 * Check to see if we need to run commands from the file.
161 /* most of this was taken from Bash v2.04 by the FSF */
165 sprintf(filename
, "%s" G_DIR_SEPARATOR_S
"control", purple_user_dir());
167 if ((g_stat(filename
, &finfo
) == 0) && (finfo
.st_size
> 0))
169 if (mtime
!= finfo
.st_mtime
) {
170 purple_debug_info("filectl", "control changed, checking\n");
179 getarg(char *line
, int which
, int remain
)
187 for (i
= 0; i
< strlen(line
) && count
< which
; i
++) {
189 case 0: /* in whitespace, expecting word */
190 if (isalnum(line
[i
])) {
195 case 1: /* inside word, waiting for whitespace */
196 if (isspace(line
[i
])) {
203 arr
= strdup(&line
[i
- 1]);
207 for (i
= 0; i
< strlen(arr
) && isalnum(arr
[i
]); i
++);
220 plugin_load(PurplePlugin
*plugin
)
223 check
= purple_timeout_add_seconds(5, (GSourceFunc
)check_file
, NULL
);
229 plugin_unload(PurplePlugin
*plugin
)
231 purple_timeout_remove(check
);
236 static PurplePluginInfo info
=
239 PURPLE_MAJOR_VERSION
,
240 PURPLE_MINOR_VERSION
,
241 PURPLE_PLUGIN_STANDARD
, /**< type */
242 NULL
, /**< ui_requirement */
244 NULL
, /**< dependencies */
245 PURPLE_PRIORITY_DEFAULT
, /**< priority */
247 FILECTL_PLUGIN_ID
, /**< id */
248 N_("File Control"), /**< name */
249 DISPLAY_VERSION
, /**< version */
251 N_("Allows control by entering commands in a file."),
253 N_("Allows control by entering commands in a file."),
254 "Eric Warmenhoven <eric@warmenhoven.org>", /**< author */
255 PURPLE_WEBSITE
, /**< homepage */
257 plugin_load
, /**< load */
258 plugin_unload
, /**< unload */
259 NULL
, /**< destroy */
261 NULL
, /**< ui_info */
262 NULL
/**< extra_info */
266 init_plugin(PurplePlugin
*plugin
)
270 PURPLE_INIT_PLUGIN(filectl
, init_plugin
, info
)