Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 204 lines (167 sloc) 5.675 kb
2eeedb9 @pbrisbin fixed
authored
1 #define _GNU_SOURCE
2
bfae985 @pbrisbin initial commit
authored
3 #include <stdio.h>
4 #include <stdlib.h>
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
5 #include <stdarg.h>
bfae985 @pbrisbin initial commit
authored
6 #include <string.h>
2eeedb9 @pbrisbin fixed
authored
7 #include <unistd.h>
bfae985 @pbrisbin initial commit
authored
8 #include <netinet/in.h>
9 #include <sys/socket.h>
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
10 #include <getopt.h>
e89be16 @pbrisbin retry on EINTR
authored
11 #include <errno.h>
dc234ae @pbrisbin Use libnotify directly
authored
12 #include <libnotify/notify.h>
bfae985 @pbrisbin initial commit
authored
13
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
14 #define MAXBUF 1024
15 #define SEP "/"
e273a49 @pbrisbin use #defines for string constants
authored
16
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
17 #define STREQ(a, b) strcmp((a),(b)) == 0
18 #define STRDUP(a, b) if ((b)) a = strdup((b))
e8c9da7 @pbrisbin messy commit: refactor, simplify, macro, etc
authored
19
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
20 static int portno = 10600;
44c9772 @pbrisbin parse_message and handle_message
authored
21
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
22 enum etype {
7399ee9 @pbrisbin use enum for message type
authored
23 Ring,
24 SMS,
25 MMS,
26 Battery,
27 Ping,
28 Unknown
29 };
30
1d4bf67 @pbrisbin formatting changes
authored
31 struct message_t {
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
32 int version;
33 char * device_id;
34 char * notification_id;
35 enum etype event_type;
36 char * data;
37 char * event_contents;
44c9772 @pbrisbin parse_message and handle_message
authored
38 };
39
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
40 static void parse_options(int argc, char *argv[]) {
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
41 int opt, option_index = 0;
e8c9da7 @pbrisbin messy commit: refactor, simplify, macro, etc
authored
42 char *token;
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
43
44 static struct option opts[] = {
45 { "port" , required_argument, 0, 'p'},
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
46 { "help" , no_argument, 0, 'h'},
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
47 { 0 , 0 , 0, 0 }
48 };
49
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
50 while ((opt = getopt_long(argc, argv, "hp:", opts, &option_index)) != -1) {
51 switch (opt) {
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
52 case 'p':
53 portno = strtol(optarg, &token, 10);
1bc36a7 @pbrisbin whitespace
authored
54 if (*token != '\0' || portno <= 0 || portno > 65535) {
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
55 fprintf(stderr, "invalid port number\n");
56 exit(EXIT_FAILURE);
726c83c @pbrisbin use options vs the insource #defines
authored
57 }
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
58 break;
59
60 case 'h':
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
61 printf("usage: android-receiver [ -p <port> ]\n");
62 exit(EXIT_SUCCESS);
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
63
64 default:
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
65 fprintf(stderr, "invalid option, try -h or --help\n");
66 exit(EXIT_FAILURE);
eaa0b4a @pbrisbin use getopt for safer, better options handling
authored
67 }
726c83c @pbrisbin use options vs the insource #defines
authored
68 }
69 }
70
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
71 static struct message_t *parse_message(char *msg) {
5288da4 @pbrisbin stolen from falconindy: newer signal syntax, use pointer for message par...
authored
72 struct message_t *message;
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
73 char *tmp, *tok;
61eba64 @pbrisbin whitespace changes
authored
74 int field = 0;
96cf7df @pbrisbin support /s in message
authored
75
eab4b6c @pbrisbin deref so allocated size is size of message_t, not pointer
authored
76 message = malloc(sizeof *message);
96cf7df @pbrisbin support /s in message
authored
77
9f913a7 @pbrisbin fully support v1
authored
78 /* v1: device_id / notification_id / event_type / event_contents */
79 /* v2: "v2" / device_id / notification_id / event_type / data / event_contents */
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
80 for (tok = strsep(&msg, SEP); ++field <= 5; tok = strsep(&msg, SEP)) {
5dd270a @pbrisbin for loop shouldn't break on empty field(s)
authored
81 switch (field) {
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
82 case 1:
9f913a7 @pbrisbin fully support v1
authored
83 if (tok && STREQ(tok, "v2"))
84 message->version = 2;
85 else {
86 /* rebuild a v1 msg to start parsing at field 2 */
87 message->version = 1;
88 tmp = strdup(msg);
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
89 strcat(tok, SEP);
9f913a7 @pbrisbin fully support v1
authored
90 strcat(tok, tmp);
91 msg = tok;
7399ee9 @pbrisbin use enum for message type
authored
92 }
5288da4 @pbrisbin stolen from falconindy: newer signal syntax, use pointer for message par...
authored
93 break;
61eba64 @pbrisbin whitespace changes
authored
94
9f913a7 @pbrisbin fully support v1
authored
95 case 2: STRDUP(message->device_id, tok); break;
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
96 case 3: STRDUP(message->notification_id, tok); break;
9f913a7 @pbrisbin fully support v1
authored
97
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
98 case 4:
9f913a7 @pbrisbin fully support v1
authored
99 if (STREQ(tok, "RING")) message->event_type = Ring;
100 else if (STREQ(tok, "SMS")) message->event_type = SMS;
101 else if (STREQ(tok, "MMS")) message->event_type = MMS;
102 else if (STREQ(tok, "BATTERY")) message->event_type = Battery;
103 else if (STREQ(tok, "PING")) message->event_type = Ping;
104 else message->event_type = Unknown;
105
106 if (message->version == 1) {
107 /* for v1, grab everything else and return */
108 STRDUP(message->event_contents, msg);
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
109 message->data = "";
9f913a7 @pbrisbin fully support v1
authored
110 return message;
61eba64 @pbrisbin whitespace changes
authored
111 }
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
112 break;
44c9772 @pbrisbin parse_message and handle_message
authored
113
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
114 case 5:
115 STRDUP(message->data, tok);
9f913a7 @pbrisbin fully support v1
authored
116 STRDUP(message->event_contents, msg);
5288da4 @pbrisbin stolen from falconindy: newer signal syntax, use pointer for message par...
authored
117 return message;
118 }
61eba64 @pbrisbin whitespace changes
authored
119 }
44c9772 @pbrisbin parse_message and handle_message
authored
120
61eba64 @pbrisbin whitespace changes
authored
121 return message;
44c9772 @pbrisbin parse_message and handle_message
authored
122 }
123
dc234ae @pbrisbin Use libnotify directly
authored
124 static void handle_message(char *msg) {
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
125 printf("message received: %s\n", msg);
126
127 struct message_t *message;
dc234ae @pbrisbin Use libnotify directly
authored
128 char *title;
d07994f @pbrisbin Add icons to notifications
authored
129 char *icon;
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
130
131 message = parse_message(msg);
61eba64 @pbrisbin whitespace changes
authored
132
7fbafbe @pbrisbin Add remaining msg parts, simplify parsing
authored
133 switch (message->event_type) {
7399ee9 @pbrisbin use enum for message type
authored
134 case Ring:
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
135 asprintf(&title, "Call: %s", message->data);
d07994f @pbrisbin Add icons to notifications
authored
136 icon = "call-start";
7399ee9 @pbrisbin use enum for message type
authored
137 break;
138 case SMS:
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
139 asprintf(&title, "SMS: %s", message->data);
d07994f @pbrisbin Add icons to notifications
authored
140 icon = "stock_mail-unread";
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
141 break;
7399ee9 @pbrisbin use enum for message type
authored
142 case MMS:
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
143 asprintf(&title, "MMS: %s", message->data);
d07994f @pbrisbin Add icons to notifications
authored
144 icon = "stock_mail-unread";
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
145 break;
7399ee9 @pbrisbin use enum for message type
authored
146 case Battery:
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
147 asprintf(&title, "Battery: %s", message->data);
d07994f @pbrisbin Add icons to notifications
authored
148 icon = "battery-good"; // TODO: various levels and charge-state
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
149 break;
150 case Ping:
151 title = "Ping";
d07994f @pbrisbin Add icons to notifications
authored
152 icon = "emblem-important";
7399ee9 @pbrisbin use enum for message type
authored
153 break;
154
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
155 default:
156 return;
61eba64 @pbrisbin whitespace changes
authored
157 }
158
c7daf65 @pbrisbin Init notifysend on each message, garbage collect
authored
159 notify_init("android-receiver");
d07994f @pbrisbin Add icons to notifications
authored
160 NotifyNotification *n = notify_notification_new(title, message->event_contents, icon);
dc234ae @pbrisbin Use libnotify directly
authored
161 notify_notification_show(n, NULL);
c7daf65 @pbrisbin Init notifysend on each message, garbage collect
authored
162 g_object_unref(G_OBJECT(n));
163 notify_uninit();
bfae985 @pbrisbin initial commit
authored
164 }
165
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
166 int main(int argc, char *argv[]) {
8a1289a @pbrisbin define MAXBUF
authored
167 char buf[MAXBUF];
40502dc @pbrisbin refactor and double fork so we don't need to background the process
authored
168
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
169 struct sockaddr_in server, from;
40502dc @pbrisbin refactor and double fork so we don't need to background the process
authored
170
e8c9da7 @pbrisbin messy commit: refactor, simplify, macro, etc
authored
171 int sock, n;
172 unsigned int fromlen = sizeof from;
ba850b6 @pbrisbin Revert "refactor and double fork so we don't need to background the proc...
authored
173
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
174 parse_options(argc, argv);
40502dc @pbrisbin refactor and double fork so we don't need to background the process
authored
175
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
176 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
177 perror("error opening socket");
178 exit(EXIT_FAILURE);
179 }
bfae985 @pbrisbin initial commit
authored
180
e8c9da7 @pbrisbin messy commit: refactor, simplify, macro, etc
authored
181 memset(&server, '\0', sizeof server);
bfae985 @pbrisbin initial commit
authored
182
61eba64 @pbrisbin whitespace changes
authored
183 server.sin_family = AF_INET;
726c83c @pbrisbin use options vs the insource #defines
authored
184 server.sin_port = htons(portno);
e8c9da7 @pbrisbin messy commit: refactor, simplify, macro, etc
authored
185 server.sin_addr.s_addr = INADDR_ANY;
bfae985 @pbrisbin initial commit
authored
186
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
187 if (bind(sock, (struct sockaddr *)&server, sizeof server) < 0) {
188 perror("error binding to socket");
189 exit(EXIT_FAILURE);
190 }
bfae985 @pbrisbin initial commit
authored
191
1d4bf67 @pbrisbin formatting changes
authored
192 while (1) {
e89be16 @pbrisbin retry on EINTR
authored
193 while ((n = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&from, &fromlen)) < 0 && errno == EINTR)
194 ;
195
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
196 if (n < 0) {
197 perror("error receiving from socket");
198 exit(EXIT_FAILURE);
61eba64 @pbrisbin whitespace changes
authored
199 }
3944f7f @pbrisbin refactor, simplify, use notify-send
authored
200
201 handle_message(buf);
44c9772 @pbrisbin parse_message and handle_message
authored
202 }
bfae985 @pbrisbin initial commit
authored
203 }
Something went wrong with that request. Please try again.