-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgq-pidgin.c
212 lines (174 loc) · 5.59 KB
/
msgq-pidgin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#define PURPLE_PLUGINS
#define VERSION "0.0.2"
#define PLUGIN_ID "gtk-msgq-comnot"
#define PLUGIN_NAME "msgq-pidgin"
#include <glib.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "notify.h"
#include "plugin.h"
#include "version.h"
#include "debug.h"
#include "cmds.h"
#include "gtkconv.h"
#include "prefs.h"
#include "gtkprefs.h"
#include "gtkutils.h"
#include "gtkplugin.h"
#include "gtkblist.h"
#define DEFAULT_ADDR "127.0.0.1"
#define DEFAULT_PORT 1024
#define DEFAULT_MOD "*"
#define DEFAULT_TOPIC "*"
static char *msg_tmpl = "{\"msg\":\"%s\", \"mod\":\"%s\", \"topic\":\"%s\"}";
static int sockfd_create(char *addr, int port)
{
struct sockaddr_in servaddr;
int sockfd;
if (addr == NULL)
addr = DEFAULT_ADDR;
if (port == 0)
port = DEFAULT_PORT;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, addr, &servaddr.sin_addr);
servaddr.sin_port = htons(port);
connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
return sockfd;
}
static void sockfd_close(int sockfd)
{
close(sockfd);
}
static char *msg_create(const char *msg, const char *topic, const char *mod)
{
char *buf;
if (topic == NULL)
topic = DEFAULT_TOPIC;
if (mod == NULL)
mod = DEFAULT_MOD;
buf = malloc(sizeof(char) * (strlen(msg_tmpl) + strlen(msg) + strlen(mod)\
+strlen(topic) + 1 - 6));
sprintf(buf, msg_tmpl, msg, mod, topic);
return buf;
}
static void msg_destory(char *buf)
{
free(buf);
}
static void msg_send(char *addr, int port, const char *msg,
const char *topic, const char *mod)
{
int fd;
char *json;
fd = sockfd_create(addr, port);
json = msg_create(msg, topic, mod);
write(fd, json, strlen(json));
msg_destory(json);
sockfd_close(fd);
}
static gboolean purple_status_is_avaliable(PurpleStatus *status)
{
PurpleStatusPrimitive primitive = PURPLE_STATUS_UNSET;
if (!status)
return FALSE;
primitive = purple_status_type_get_primitive(purple_status_get_type(status));
return (primitive == PURPLE_STATUS_AVAILABLE);
}
static void msg_received(PurpleAccount *account, char *sender, char *message,
PurpleConversation *conv, PurpleMessageFlags flags)
{
char *topic, *mod, *host, *_port;
int port;
PurpleStatus *status;
topic = (char *) purple_prefs_get_string("/plugins/gtk/"PLUGIN_ID"/topic");
mod = (char *) purple_prefs_get_string("/plugins/gtk/"PLUGIN_ID"/mod");
host = (char *) purple_prefs_get_string("/plugins/gtk/"PLUGIN_ID"/host");
_port = (char *) purple_prefs_get_string("/plugins/gtk/"PLUGIN_ID"/port");
port = atoi(_port);
purple_debug_info(PLUGIN_NAME, sender);
purple_debug_info(PLUGIN_NAME, message);
purple_debug_info(PLUGIN_NAME, topic);
purple_debug_info(PLUGIN_NAME, mod);
status = purple_account_get_active_status(account);
if (status != NULL && !purple_status_is_avaliable(status)) {
msg_send(host, port, (const char *) message, topic, mod);
}
}
static GtkWidget *plugin_config_frame(PurplePlugin *plugin)
{
GtkWidget *frame;
GtkWidget *vbox;
GtkSizeGroup *sg;
frame = gtk_vbox_new(FALSE, 18);
gtk_container_set_border_width(GTK_CONTAINER(frame), 12);
vbox = pidgin_make_frame(frame, "Server settings...");
sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
pidgin_prefs_labeled_entry(vbox, "IP",
"/plugins/gtk/"PLUGIN_ID"/ip", sg);
pidgin_prefs_labeled_entry(vbox, "Port",
"/plugins/gtk/"PLUGIN_ID"/port", sg);
pidgin_prefs_labeled_entry(vbox, "Mod",
"/plugins/gtk/"PLUGIN_ID"/mod", sg);
pidgin_prefs_labeled_entry(vbox, "Topic",
"/plugins/gtk/"PLUGIN_ID"/topic", sg);
gtk_widget_show_all(frame);
return frame;
}
static void init_plugin(PurplePlugin *plugin)
{
/* load configuration view */
purple_prefs_add_none("/plugins/gtk/"PLUGIN_ID);
purple_prefs_add_string("/plugins/gtk/"PLUGIN_ID"/host", "127.0.0.1");
purple_prefs_add_string("/plugins/gtk/"PLUGIN_ID"/port", "1024");
purple_prefs_add_string("/plugins/gtk/"PLUGIN_ID"/topic", "received");
purple_prefs_add_string("/plugins/gtk/"PLUGIN_ID"/mod", "message");
}
static gboolean plugin_load(PurplePlugin *plugin)
{
purple_signal_connect(purple_conversations_get_handle(),
"received-im-msg", plugin,
PURPLE_CALLBACK(msg_received), NULL);
return TRUE;
}
static gboolean plugin_unload(PurplePlugin *plugin)
{
purple_signal_disconnect(purple_conversations_get_handle(),
"received-im-msg", plugin,
PURPLE_CALLBACK(msg_received));
return TRUE;
}
static PidginPluginUiInfo ui_info = {
plugin_config_frame,
0
};
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
PIDGIN_PLUGIN_TYPE,
0,
NULL,
PURPLE_PRIORITY_DEFAULT,
PLUGIN_ID,
PLUGIN_NAME,
VERSION,
"msgQ plugin for pidgin",
"Send some message to the queue when new im message ping you up.",
"hbc <[email protected]>",
"http://youknowmymind.com",
plugin_load, /* load */
plugin_unload, /* unload */
NULL, /* destroy */
&ui_info,
NULL,
NULL,
NULL
};
PURPLE_INIT_PLUGIN(comnot, init_plugin, info);