]> git.mxchange.org Git - friendica-addons.git/blob - irc/irc.php
Merge pull request #1160 from nupplaphil/task/httprequest_optimiziations
[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\Core\Hook;
11 use Friendica\Core\Renderer;
12 use Friendica\DI;
13
14 function irc_install() {
15         Hook::register('app_menu', 'addon/irc/irc.php', 'irc_app_menu');
16         Hook::register('addon_settings', 'addon/irc/irc.php', 'irc_addon_settings');
17         Hook::register('addon_settings_post', 'addon/irc/irc.php', 'irc_addon_settings_post');
18 }
19
20 function irc_addon_settings(&$a,&$s) {
21         if(! local_user())
22                 return;
23
24     /* Add our stylesheet to the page so we can make our settings look nice */
25
26 //      DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/irc/irc.css' . '" media="all" />' . "\r\n";
27
28     /* setting popular channels, auto connect channels */
29         $sitechats = DI::pConfig()->get( local_user(), 'irc','sitechats'); /* popular channels */
30         $autochans = DI::pConfig()->get( local_user(), 'irc','autochans');  /* auto connect chans */
31
32         $t = Renderer::getMarkupTemplate( "settings.tpl", "addon/irc/" );
33         $s .= Renderer::replaceMacros($t, [
34                 '$header' => DI::l10n()->t('IRC Settings'),
35                 '$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.'),
36                 '$submit' => DI::l10n()->t('Save Settings'),
37                 '$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.')],
38                 '$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.') ]
39         ]);
40
41
42         return;
43
44 }
45
46 function irc_addon_settings_post(&$a, &$b) {
47         if(!local_user())
48                 return;
49
50         if(!empty($_POST['irc-submit'])) {
51                 if (isset($_POST['autochans'])) {
52                         DI::pConfig()->set(local_user(), 'irc', 'autochans', trim(($_POST['autochans'])));
53                 }
54                 if (isset($_POST['sitechats'])) {
55                         DI::pConfig()->set(local_user(), 'irc', 'sitechats', trim($_POST['sitechats']));
56                 }
57                 /* upid pop-up thing */
58         }
59 }
60
61 function irc_app_menu($a,&$b) {
62         $b['app_menu'][] = '<div class="app-title"><a href="irc">' . DI::l10n()->t('IRC Chatroom') . '</a></div>';
63 }
64
65
66 function irc_module() {
67         return;
68 }
69
70
71 function irc_content(&$a) {
72
73         $baseurl = DI::baseUrl()->get() . '/addon/irc';
74         $o = '';
75
76         /* set the list of popular channels */
77         if (local_user()) {
78             $sitechats = DI::pConfig()->get( local_user(), 'irc', 'sitechats');
79             if (!$sitechats)
80                 $sitechats = DI::config()->get('irc', 'sitechats');
81         } else {
82             $sitechats = DI::config()->get('irc','sitechats');
83         }
84         if($sitechats)
85                 $chats = explode(',',$sitechats);
86         else
87                 $chats = ['friendica','chat','chatback','hottub','ircbar','dateroom','debian'];
88
89
90         DI::page()['aside'] .= '<div class="widget"><h3>' . DI::l10n()->t('Popular Channels') . '</h3><ul>';
91         foreach($chats as $chat) {
92                 DI::page()['aside'] .= '<li><a href="' . DI::baseUrl()->get() . '/irc?channels=' . $chat . '" >' . '#' . $chat . '</a></li>';
93         }
94         DI::page()['aside'] .= '</ul></div>';
95
96         /* setting the channel(s) to auto connect */
97         if (local_user()) {
98             $autochans = DI::pConfig()->get(local_user(), 'irc', 'autochans');
99             if (!$autochans)
100                 $autochans = DI::config()->get('irc','autochans');
101         } else {
102             $autochans = DI::config()->get('irc','autochans');
103         }
104         if($autochans)
105                 $channels = $autochans;
106         else
107                 $channels = ($_GET['channels'] ?? '') ?: 'friendica';
108
109 /* add the chatroom frame and some html */
110   $o .= <<< EOT
111 <h2>IRC chat</h2>
112 <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>
113 <iframe src="//web.libera.chat?channels=$channels" style="width:100%; max-width:900px; height: 600px;"></iframe>
114 EOT;
115
116 return $o;
117
118 }
119
120 function irc_addon_admin_post (&$a) {
121         if(! is_site_admin())
122                 return;
123
124         if($_POST['irc-submit']) {
125                 DI::config()->set('irc','autochans',trim($_POST['autochans']));
126                 DI::config()->set('irc','sitechats',trim($_POST['sitechats']));
127         }
128 }
129 function irc_addon_admin (&$a, &$o) {
130         $sitechats = DI::config()->get('irc','sitechats'); /* popular channels */
131         $autochans = DI::config()->get('irc','autochans');  /* auto connect chans */
132         $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/irc/" );
133         $o = Renderer::replaceMacros($t, [
134                 '$submit' => DI::l10n()->t('Save Settings'),
135                 '$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.')],
136                 '$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.') ]
137         ]);
138 }