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>
22 #include "conversation.h"
24 #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 */
43 char filename
[MAXPATHLEN
];
45 char *command
, *arg1
, *arg2
;
48 snprintf(filename
, MAXPATHLEN
, "%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 purple_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 */
147 char filename
[MAXPATHLEN
];
149 snprintf(filename
, MAXPATHLEN
, "%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 */
163 char filename
[MAXPATHLEN
];
165 snprintf(filename
, MAXPATHLEN
, "%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
++);
219 static PurplePluginInfo
*
220 plugin_query(GError
**error
)
222 const gchar
* const authors
[] = {
223 "Eric Warmenhoven <eric@warmenhoven.org>",
227 return purple_plugin_info_new(
228 "id", FILECTL_PLUGIN_ID
,
229 "name", N_("File Control"),
230 "version", DISPLAY_VERSION
,
231 "category", N_("Utility"),
232 "summary", N_("Allows control by entering commands in a file."),
233 "description", N_("Allows control by entering commands in a file."),
235 "website", PURPLE_WEBSITE
,
236 "abi-version", PURPLE_ABI_VERSION
,
242 plugin_load(PurplePlugin
*plugin
, GError
**error
)
245 check
= purple_timeout_add_seconds(5, (GSourceFunc
)check_file
, NULL
);
251 plugin_unload(PurplePlugin
*plugin
, GError
**error
)
253 purple_timeout_remove(check
);
258 PURPLE_PLUGIN_INIT(filectl
, plugin_query
, plugin_load
, plugin_unload
);