/
main.cpp
147 lines (117 loc) · 3.59 KB
/
main.cpp
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
// ANSI C headers
#include <cstdlib>
// POSIX headers
#include <sys/types.h>
// C++ headers
#include <iostream>
using namespace std;
// Qt headers
#include <QCoreApplication>
#include <QHostAddress>
#include <QUdpSocket>
#include <QString>
#include <QFile>
// MythTV headers
#include "exitcodes.h"
#include "compat.h"
#ifndef VERBOSE
#define VB_IMPORTANT 0
#define VERBOSE(LEVEL, MSG) \
do { cout << QString(MSG).toLocal8Bit().constData() << endl; } while (0)
#endif
const QString kMessage =
"<mythmessage version=\"1\">\n"
" <text>%message_text%</text>\n"
"</mythmessage>";
static void printHelp(void)
{
cout << "\nUsage: mythmessage --message_text=<yourtext> [OPTION]\n";
cout << "A utility to broadcast messages to all running mythfrontends.\n\n";
cout << " --udpport=\"value\" : (optional) UDP port to sent to (--udpport=6948)\n";
cout << " --bcastaddr\"value\" : (optional) IP address to send to\n";
cout << " (--bcastaddr=127.0.0.1)\n";
cout << " --verbose : (optional) extra debug information\n";
cout << " --help : (optional) this text\n";
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCoreApplication::setApplicationName("mythmessage");
QHostAddress address = QHostAddress::Broadcast;
unsigned short port = 6948;
QString message = kMessage;
bool verbose = false;
if (a.argc() == 0)
{
printHelp();
return GENERIC_EXIT_OK;
}
for (int argpos = 1; argpos < a.argc(); ++argpos)
{
QString arg = a.argv()[argpos];
if (arg.startsWith("-h") || arg.startsWith("--help"))
{
printHelp();
return GENERIC_EXIT_OK;
}
}
for (int argpos = 1; argpos < a.argc(); ++argpos)
{
QString arg = a.argv()[argpos];
if (arg.startsWith("--help") || arg.startsWith("--template"))
continue;
if (arg.startsWith("--verbose"))
{
verbose = true;
continue;
}
if (arg.startsWith("--udpport"))
{
port = arg.section("=", 1).toUShort();
if (port == 0)
port = 6948;
continue;
}
if (arg.startsWith("--bcastaddr"))
{
QString newaddr = arg.section("=", 1);
if (!newaddr.isEmpty())
address.setAddress(newaddr);
continue;
}
QString name = arg.section("=", 0, 0);
name.replace("--", "");
QString value = QString::fromLocal8Bit(
arg.section("=", 1).toLocal8Bit());
if (verbose)
{
QByteArray tmp_name = name.toLocal8Bit();
QByteArray tmp_value = value.toLocal8Bit();
cerr << "name: " << tmp_name.constData()
<< " -- value: " << tmp_value.constData() << endl;
}
name.append("%");
name.prepend("%");
message.replace(name, value);
}
if (verbose)
{
QByteArray tmp_message = message.toLocal8Bit();
cout << "output:\n" << tmp_message.constData() << endl;
}
QUdpSocket *sock = new QUdpSocket();
QByteArray utf8 = message.toUtf8();
int size = utf8.length();
if (sock->writeDatagram(utf8.constData(), size, address, port) < 0)
{
VERBOSE(VB_IMPORTANT, "Failed to send UDP/XML packet");
}
else
{
cout << "Sent UDP/XML packet to IP "
<< address.toString().toLocal8Bit().constData()
<< " and port: " << port << endl;
}
sock->deleteLater();
return GENERIC_EXIT_OK;
}