aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: f0362345b3eebf9900957bb84ec9f5d28a61be2a (plain)
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
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <unistd.h>

#include "config.h"
#include <tinyosc.c>
#include <tinyosc.h>

#define buf_size 2048

/*
    chat-loop / main.c. sends text to vrchat using osc.
    Copyright (C) 2026 iceyrazor

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

static volatile sig_atomic_t keepRunning = true;

// handle Ctrl+C
static void sigintHandler(int x)
{
    keepRunning = false;
}

// execute command and put output into ret_buf buffer
int execlb(const char* cmd, char* buffer, size_t buffer_size)
{
    int link[2];
    pid_t pid;
    int status;

    if (pipe(link) == -1)
        exit(-5);

    if ((pid = fork()) == -1)
        exit(-5);

    if (pid == 0) {
        dup2(link[1], STDOUT_FILENO);
        close(link[0]);
        close(link[1]);
        execl("/bin/sh", "sh", "-c", cmd, (char*)0);
        _exit(127);
    } else {
        close(link[1]);
        int nbytes = read(link[0], buffer, buffer_size - 1);
        buffer[nbytes] = '\0';
        waitpid(pid,&status,0);
        close(link[0]);
        return status;
    }
}

int main()
{
    char wbuffer[2048] = {0}; // declare a 2Kb buffer to read packet data into
    int port = 9000;
    struct sockaddr_in servaddr;

    int socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (socketfd < 0) {
        perror("ERROR:");
        return -1;
    }

    servaddr.sin_family = AF_INET; // IPv4
    servaddr.sin_port = htons(port); // Server port
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Server IP

    // register the SIGINT handler (Ctrl+C)
    signal(SIGINT, sigintHandler);

    char dist[2048] = {0};
    int status=execlb("uname -r", dist, sizeof dist);
    if (status != 0){
        fprintf(stderr,"ERROR WITH execlb %d", status);
        exit(1);
    }
    dist[strcspn(dist,"\n")] = '\0';

    char comb_string[2048] = {0};
    char song_title[2048] = {0};

    size_t i = 0;
    while (keepRunning) {
        int song_status=execlb("src/title.sh", song_title, sizeof song_title);
        if (song_status != 0){
            fprintf(stderr,"ERROR WITH execlb %d", status);
            exit(1);
        }
        snprintf(comb_string, sizeof comb_string, "%s\nLinux %s\n%s\n%s", title, dist, arr[i], song_title);

        uint32_t len = tosc_writeMessage(wbuffer, sizeof(wbuffer), "/chatbox/input", "sTF", comb_string);
        sendto(socketfd, wbuffer, len, MSG_CONFIRM, (const struct sockaddr*)&servaddr, sizeof(servaddr));

        sleep(3);

        i = (i + 1) % (sizeof arr / sizeof arr[0]);
    }

    // remove till end if you dont care about sending a clear to chat box when stopping the program.

    usleep(900000); // if 2 messages are sent to quickly. this one doesnt send. so this seems like a good delay on my machine. comment out if you dont care
    uint32_t len = tosc_writeMessage(wbuffer, sizeof(wbuffer), "/chatbox/input", "sTF", "");
    sendto(socketfd, wbuffer, len, MSG_CONFIRM, (const struct sockaddr*)&servaddr, sizeof(servaddr));
    // end

    return 0;
}