]> git.mxchange.org Git - friendica-addons.git/blob - irc/irc.php
072ec4c5bd871089f02bf2d9dd26f4f11b028953
[friendica-addons.git] / irc / irc.php
1 <?php
2 /**
3 * Name: IRC Chat Addon
4 * Description: add an Internet Relay Chat chatroom on freenode
5 * Version: 1.1
6 * Author: tony baldwin <https://free-haven.org/profile/tony>
7 * Author: Tobias Diekershoff <https://f.diekershoff.de/u/tobias>
8 */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14
15 function irc_install()
16 {
17         Hook::register('app_menu', 'addon/irc/irc.php', 'irc_app_menu');
18         Hook::register('addon_settings', 'addon/irc/irc.php', 'irc_addon_settings');
19         Hook::register('addon_settings_post', 'addon/irc/irc.php', 'irc_addon_settings_post');
20 }
21
22 function irc_addon_settings(App &$a, array &$data)
23 {
24         if (!local_user()) {
25                 return;
26         }
27
28         $sitechats = DI::pConfig()->get(local_user(), 'irc', 'sitechats'); /* popular channels */
29         $autochans = DI::pConfig()->get(local_user(), 'irc', 'autochans');  /* auto connect chans */
30
31         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/irc/');
32         $html = Renderer::replaceMacros($t, [
33                 '$info'      => DI::l10n()->t('Here you can change the system wide settings for the channels to automatically join and access via the side bar. Note the changes you do here, only effect the channel selection if you are logged in.'),
34                 '$autochans' => ['autochans', DI::l10n()->t('Channel(s) to auto connect (comma separated)'), $autochans, DI::l10n()->t('List of channels that shall automatically connected to when the app is launched.')],
35                 '$sitechats' => ['sitechats', DI::l10n()->t('Popular Channels (comma separated)'), $sitechats, DI::l10n()->t('List of popular channels, will be displayed at the side and hotlinked for easy joining.')],
36         ]);
37
38         $data = [
39                 'addon' => 'irc',
40                 'title' => DI::l10n()->t('IRC Settings'),
41                 'html'  => $html,
42         ];
43 }
44
45 function irc_addon_settings_post(App $a, array &$b)
46 {
47         if (!local_user()) {
48                 return;
49         }
50
51         if (!empty($_POST['irc-submit'])) {
52                 if (isset($_POST['autochans'])) {
53                         DI::pConfig()->set(local_user(), 'irc', 'autochans', trim(($_POST['autochans'])));
54                 }
55                 if (isset($_POST['sitechats'])) {
56                         DI::pConfig()->set(local_user(), 'irc', 'sitechats', trim($_POST['sitechats']));
57                 }
58                 /* upid pop-up thing */
59         }
60 }
61
62 function irc_app_menu(App $a, array &$b)
63 {
64         $b['app_menu'][] = '<div class="app-title"><a href="irc">' . DI::l10n()->t('IRC Chatroom') . '</a></div>';
65 }
66
67
68 function irc_module() {
69         return;
70 }
71
72
73 function irc_content(App $a)
74 {
75         $baseurl = DI::baseUrl()->get() . '/addon/irc';
76         $o = '';
77
78         /* set the list of popular channels */
79         if (local_user()) {
80                 $sitechats = DI::pConfig()->get( local_user(), 'irc', 'sitechats');
81                 if (!$sitechats) {
82                         $sitechats = DI::config()->get('irc', 'sitechats');
83                 }
84         } else {
85                 $sitechats = DI::config()->get('irc','sitechats');
86         }
87
88         if ($sitechats) {
89                 $chats = explode(',',$sitechats);
90         } else {
91                 $chats = ['friendica','chat','chatback','hottub','ircbar','dateroom','debian'];
92         }
93
94
95         DI::page()['aside'] .= '<div class="widget"><h3>' . DI::l10n()->t('Popular Channels') . '</h3><ul>';
96         foreach ($chats as $chat) {
97                 DI::page()['aside'] .= '<li><a href="' . DI::baseUrl()->get() . '/irc?channels=' . $chat . '" >' . '#' . $chat . '</a></li>';
98         }
99         DI::page()['aside'] .= '</ul></div>';
100
101         /* setting the channel(s) to auto connect */
102         if (local_user()) {
103             $autochans = DI::pConfig()->get(local_user(), 'irc', 'autochans');
104             if (!$autochans)
105                 $autochans = DI::config()->get('irc','autochans');
106         } else {
107             $autochans = DI::config()->get('irc','autochans');
108         }
109
110         if ($autochans) {
111                 $channels = $autochans;
112         } else {
113                 $channels = ($_GET['channels'] ?? '') ?: 'friendica';
114         }
115
116 /* add the chatroom frame and some html */
117   $o .= <<< EOT
118 <h2>IRC chat</h2>
119 <p><a href="https://tldp.org/HOWTO/IRC/beginners.html" target="_blank" rel="noopener noreferrer">A beginner's guide to using IRC. [en]</a></p>
120 <iframe src="//web.libera.chat?channels=$channels" style="width:100%; max-width:900px; height: 600px;"></iframe>
121 EOT;
122
123         return $o;
124 }
125
126 function irc_addon_admin_post (App $a)
127 {
128         if (!$a->isSiteAdmin()) {
129                 return;
130         }
131
132         if ($_POST['irc-submit']) {
133                 DI::config()->set('irc', 'autochans', trim($_POST['autochans']));
134                 DI::config()->set('irc', 'sitechats', trim($_POST['sitechats']));
135         }
136 }
137 function irc_addon_admin (App $a, &$o) {
138         $sitechats = DI::config()->get('irc', 'sitechats'); /* popular channels */
139         $autochans = DI::config()->get('irc', 'autochans');  /* auto connect chans */
140         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/irc/' );
141         $o = Renderer::replaceMacros($t, [
142                 '$submit' => DI::l10n()->t('Save Settings'),
143                 '$autochans' => [ 'autochans', DI::l10n()->t('Channel(s) to auto connect (comma separated)'), $autochans, DI::l10n()->t('List of channels that shall automatically connected to when the app is launched.')],
144                 '$sitechats' => [ 'sitechats', DI::l10n()->t('Popular Channels (comma separated)'), $sitechats, DI::l10n()->t('List of popular channels, will be displayed at the side and hotlinked for easy joining.') ]
145         ]);
146 }