-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
95 lines (83 loc) · 2.42 KB
/
parser.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
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "sound.h"
#include "network.h"
#include "shared.h"
#include "history.h"
#include <pthread.h>
#include "lua.h"
static const char ping[] = "PING :tmi.twitch.tv";
static const char pong[] = "PONG :tmi.twitch.tv\r\n";
static const char default_line[] = ":tmi.twitch.tv";
static const char join[] = "JOIN";
static const char msg[] = "PRIVMSG";
extern pthread_mutex_t mutex;
extern void manager_plugin_receive_new_join (enum SERVERS server, const char *nick, const char *room);
extern void manager_plugin_receive_new_message (enum SERVERS server, const char *room, const char *nick, const char *message);
static void parser_twitch_buffer_for_message (char *room, char *nick, char *message, char **s) {
char *m = *s;
m++;
int count = 0;
while (*m != '!' && *m != '\n') {
nick[count++] = *m++;
}
nick[count] = 0;
if (*m == '\n') return;
while (*m != ' ' && *m != '\n') m++;
if (*m == '\n') return;
m++;
count = 0;
if (!strncmp (m, msg, sizeof (msg) - 1)) {
m += sizeof (msg);
while (*m != ' ' && *m != '\n') {
room[count++] = *m++;
}
room[count] = 0;
m += 2;
count = 0;
while (*m != '\n') {
message[count++] = *m++;
}
message[count] = 0;
pthread_mutex_lock (&mutex);
printf ("\033[1;33m[TWITCH]\033[00m\033[1;32m[%s]\033[00m\033[1;36m[%s]\033[00m: %s\n",
room,
nick,
message
);
sound_play ();
history_add (TWITCH_SERVER, room, nick, message);
manager_plugin_receive_new_message (TWITCH_SERVER, room, nick, message);
manager_lua_receive_new_message (TWITCH_SERVER, room, nick, message);
pthread_mutex_unlock (&mutex);
}
count = 0;
if (!strncmp (m, join, sizeof (join) - 1)) {
m += sizeof (join);
while (*m != ' ' && *m != '\n') {
room[count++] = *m++;
}
room[count] = 0;
pthread_mutex_lock (&mutex);
printf ("\033[1;33m[TWITCH]\033[00m\033[1;36m%s\033[00m joined to room \033[1;32m%s\033[00m\n",
nick,
room
);
sound_play ();
manager_plugin_receive_new_join (TWITCH_SERVER, nick, room);
pthread_mutex_unlock (&mutex);
}
}
void parser_twitch_rule (char *buffer) {
char *s = buffer;
char room[512];
char nick[64];
char message[MAX_BUFFER];
if (!strncmp (buffer, default_line, sizeof (default_line))) return;
if (!strncmp (buffer, ping, sizeof (ping) - 1)) {
network_send (TWITCH_SERVER, pong, sizeof (pong) - 1);
return;
}
parser_twitch_buffer_for_message (room, nick, message, &s);
}