]> git.mxchange.org Git - friendica-addons.git/blob - irc/irc.php
[smileybutton] Add explicit conversion from float to int
[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(array &$data)
23 {
24         if (!DI::userSession()->getLocalUserId()) {
25                 return;
26         }
27
28         $sitechats = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'irc', 'sitechats'); /* popular channels */
29         $autochans = DI::pConfig()->get(DI::userSession()->getLocalUserId(), '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(array &$b)
46 {
47         if (!DI::userSession()->getLocalUserId()) {
48                 return;
49         }
50
51         if (!empty($_POST['irc-submit'])) {
52                 if (isset($_POST['autochans'])) {
53                         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'irc', 'autochans', trim(($_POST['autochans'])));
54                 }
55                 if (isset($_POST['sitechats'])) {
56                         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'irc', 'sitechats', trim($_POST['sitechats']));
57                 }
58                 /* upid pop-up thing */
59         }
60 }
61
62 function irc_app_menu(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  * This is a statement rather than an actual function definition. The simple
69  * existence of this method is checked to figure out if the addon offers a
70  * module.
71  */
72 function irc_module() {}
73
74 function irc_content()
75 {
76         $baseurl = DI::baseUrl() . '/addon/irc';
77         $o = '';
78
79         /* set the list of popular channels */
80         if (DI::userSession()->getLocalUserId()) {
81                 $sitechats = DI::pConfig()->get( DI::userSession()->getLocalUserId(), 'irc', 'sitechats');
82                 if (!$sitechats) {
83                         $sitechats = DI::config()->get('irc', 'sitechats');
84                 }
85         } else {
86                 $sitechats = DI::config()->get('irc','sitechats');
87         }
88
89         if ($sitechats) {
90                 $chats = explode(',',$sitechats);
91         } else {
92                 $chats = ['friendica','chat','chatback','hottub','ircbar','dateroom','debian'];
93         }
94
95
96         DI::page()['aside'] .= '<div class="widget"><h3>' . DI::l10n()->t('Popular Channels') . '</h3><ul>';
97         foreach ($chats as $chat) {
98                 DI::page()['aside'] .= '<li><a href="' . DI::baseUrl() . '/irc?channels=' . $chat . '" >' . '#' . $chat . '</a></li>';
99         }
100         DI::page()['aside'] .= '</ul></div>';
101
102         /* setting the channel(s) to auto connect */
103         if (DI::userSession()->getLocalUserId()) {
104             $autochans = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'irc', 'autochans');
105             if (!$autochans)
106                 $autochans = DI::config()->get('irc','autochans');
107         } else {
108             $autochans = DI::config()->get('irc','autochans');
109         }
110
111         if ($autochans) {
112                 $channels = $autochans;
113         } else {
114                 $channels = ($_GET['channels'] ?? '') ?: 'friendica';
115         }
116
117 /* add the chatroom frame and some html */
118   $o .= <<< EOT
119 <h2>IRC chat</h2>
120 <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>
121 <iframe src="//web.libera.chat?channels=$channels" style="width:100%; max-width:900px; height: 600px;"></iframe>
122 EOT;
123
124         return $o;
125 }
126
127 function irc_addon_admin_post ()
128 {
129         if (!DI::userSession()->isSiteAdmin()) {
130                 return;
131         }
132
133         if ($_POST['irc-submit']) {
134                 DI::config()->set('irc', 'autochans', trim($_POST['autochans']));
135                 DI::config()->set('irc', 'sitechats', trim($_POST['sitechats']));
136         }
137 }
138 function irc_addon_admin (string &$o) {
139         $sitechats = DI::config()->get('irc', 'sitechats'); /* popular channels */
140         $autochans = DI::config()->get('irc', 'autochans');  /* auto connect chans */
141         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/irc/' );
142         $o = Renderer::replaceMacros($t, [
143                 '$submit' => DI::l10n()->t('Save Settings'),
144                 '$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.')],
145                 '$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.') ]
146         ]);
147 }